summaryrefslogtreecommitdiff
path: root/src/image.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/image.zig')
-rw-r--r--src/image.zig89
1 files changed, 20 insertions, 69 deletions
diff --git a/src/image.zig b/src/image.zig
index e334965a..9163730d 100644
--- a/src/image.zig
+++ b/src/image.zig
@@ -11,7 +11,6 @@ const pdf_enabled = @import("pardes_config").mupdf;
const pardes = @import("pardes.zig");
const config = @import("config.zig");
const filesystem = @import("fs.zig");
-const dump = @import("dump.zig");
// Glyph matching adapted from caioluders/petsciinator.
pub const GlyphArt = struct {
pub const commodore = [16][3]u8{
@@ -756,6 +755,13 @@ var started = false;
extern fn stbi_info_from_memory(buffer: [*]const u8, len: c_int, x: *c_int, y: *c_int, comp: *c_int) c_int;
+/// stb_image's header reader; a freestanding build has no stb_image (its
+/// decoder is the shim in src/web/zstbi.zig), so no header reads as one.
+fn stbiInfo(bytes: []const u8, w: *c_int, h: *c_int, c: *c_int) bool {
+ if (comptime !pardes.hosted) return false;
+ return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), w, h, c) == 1;
+}
+
/// PNG and JPEG are ghostty's decoders (wuffs, which ghostty-vt links for
/// kitty graphics) wherever ghostty-vt is in the build; stb_image is then
/// compiled without them, and without the inflate only its PNG used
@@ -788,7 +794,7 @@ fn headerSize(bytes: []const u8) ?Size {
var iw: c_int = 0;
var ih: c_int = 0;
var ic: c_int = 0;
- if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null;
+ if (!stbiInfo(bytes, &iw, &ih, &ic)) return null;
return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) };
}
@@ -829,7 +835,7 @@ pub fn readable(bytes: []const u8) bool {
var c: c_int = 0;
if (!started) return true; // ponytail: no stb_image yet (a unit test), so no judging
if (comptime wuffs_images) if (format(bytes) != .other) return headerSize(bytes) != null;
- return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &w, &h, &c) == 1;
+ return stbiInfo(bytes, &w, &h, &c);
}
/// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the
@@ -930,7 +936,14 @@ const GridKey = struct {
ascii: bool = true,
};
+/// A host's thread; a freestanding build has none to have.
+const Decoder = if (pardes.hosted) std.Thread else void;
+
pub const State = struct {
+ /// What the replay digest leaves out (src/walk.zig): the picture as
+ /// decoded from its bytes, off the loop as the render first wants it,
+ /// and the glyph art drawn from that.
+ pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "grid", "grid_key", "decoder", "decoded", "answer" } };
path: []u8,
glyph_art: bool = false,
pmode: PaletteMode = .commodore,
@@ -944,12 +957,12 @@ pub const State = struct {
grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 },
grid_key: GridKey = .{},
/// The decode under way off the loop (ensureDecoded), and its answer.
- decoder: ?std.Thread = null,
+ decoder: ?Decoder = null,
decoded: std.atomic.Value(bool) = .init(false),
answer: ?Decoded = null,
pub fn deinit(state: *State, gpa: std.mem.Allocator) void {
- if (state.decoder) |t| t.join();
+ if (comptime pardes.hosted) if (state.decoder) |t| t.join();
if (state.answer) |a| gpa.free(a.rgba);
gpa.free(state.path);
if (state.rgba.len > 0) gpa.free(state.rgba);
@@ -986,68 +999,6 @@ pub fn create(p: *pardes.Pardes, id: usize, path: []const u8, raw: []u8) !*parde
return pane;
}
-/// Serialize the binary image record used by images and, for dump-schema
-/// compatibility, PDFs. Common pane metadata is supplied by the core.
-pub fn dumpPane(
- p: *pardes.Pardes,
- arena: std.mem.Allocator,
- pane: *const pardes.Pane,
- tag: []const u8,
- body: []const u8,
- scroll: usize,
- path: []const u8,
- raw: []const u8,
-) !dump.Pane {
- // A picture or PDF on disk is read from there at Restore, as a clean
- // file's text is: the dump keeps only the bytes of one that is not
- // (megabytes of a PDF per dump, and a stale copy, otherwise).
- _ = p;
- const on_disk = if (comptime !pardes.hosted) false else if (filesystem.localPath(path)) |local| @import("exec.zig").kindOf(local) == .file else false;
- const bytes: []const u8 = if (on_disk) &.{} else raw;
- return .{
- .kind = .image,
- .tag = tag,
- .body = body,
- .scroll = scroll,
- .cols = pane.cols,
- .rows = pane.rows,
- .vweight = pane.vweight,
- .collapsed = pane.collapsed,
- .image = .{
- .path = path,
- .bytes_b64 = if (bytes.len > 0) try dump.encodeBytes(arena, bytes) else "",
- .petscii = if (pane.image) |state| state.glyph_art else false,
- .palette = if (pane.image) |state| switch (state.pmode) {
- .commodore => .commodore,
- .terminal => .terminal,
- } else .commodore,
- .ascii = if (pane.image) |state| state.ascii else true,
- .pdf_fit = if (comptime pardes.pdf_enabled) (if (pane.pdf) |pv| @tagName(pv.fit) else "") else "",
- .pdf_tint = if (comptime pardes.pdf_enabled) (if (pane.pdf) |pv| @tagName(pv.tint) else "") else "",
- },
- };
-}
-
-pub fn restore(p: *pardes.Pardes, id: usize, src: dump.Pane) !*pardes.Pane {
- const saved = src.image.?;
- var raw: []u8 = if (saved.bytes_b64.len > 0)
- try dump.decodeBytes(p.image_gpa, saved.bytes_b64)
- else
- &.{};
- errdefer if (raw.len > 0) p.image_gpa.free(raw);
- const pane = try create(p, id, saved.path, raw);
- raw = &.{};
- pane.image.?.glyph_art = saved.petscii;
- pane.image.?.pmode = switch (saved.palette) {
- .commodore => .commodore,
- .terminal => .terminal,
- };
- pane.image.?.ascii = saved.ascii;
- pane.cols = @max(1, src.cols);
- pane.rows = @max(1, src.rows);
- return pane;
-}
-
pub fn toggleGlyphArt(state: *State) void {
state.glyph_art = !state.glyph_art;
}
@@ -1085,11 +1036,11 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![
/// docs' screenshot: all of it once on the frame before).
/// ponytail: a thread a picture; a pool if many open at once.
fn ensureDecoded(p: *pardes.Pardes, state: *State) void {
- if (state.decoder) |t| {
+ if (comptime pardes.hosted) if (state.decoder) |t| {
if (!state.decoded.load(.acquire)) return;
t.join();
state.decoder = null;
- }
+ };
if (state.answer) |decoded| {
state.answer = null;
state.rgba = decoded.rgba;