diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 00:02:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 01:21:34 -0300 |
| commit | 423440f25b4b23b56162008bba35218d438767fe (patch) | |
| tree | a7b06a88c7a55308e7e906ff49fa264a1fd147cf /src/image.zig | |
| parent | 94ca47f225632fefc8f688c0c0822d25d6a5db1e (diff) | |
| download | pardes-423440f25b4b23b56162008bba35218d438767fe.tar.gz pardes-423440f25b4b23b56162008bba35218d438767fe.zip | |
kinds #2, a pane of a kind is one slot and image is a kind compiled in: src/kinds/pardes_addon.h is the kinds' C ABI (the cell, the window a kind paints, placements and the tag prefix it hands back, keys, the mouse, pa_api of six functions over the pane's arena, words, the head and pa_ops of paint and sixteen optional functions), translated once by the build into the core's records, never mirrored; src/Kind.zig is the core's side: the kinds this process has (a registry with the checks a load makes: the head's size and abi major, a paint, a name, words, leaders and extensions none else has), a pane's Slot (the kind's state and arena, its path, the art bytes Petscii, Palette and Ascii set, its tick deadline and jobs) and the addon arm at the sites: Look and the initial file by extension, draw (cells into the grid, placements keyed by serial and the kind's key), the tag prefix from a paint over no cells, nextWake and the step (ticks, jobs' answers landing on the loop), close, Recent, paneDir, /index's kind word; a dump writes a slot by hand (kind name and version, path, where, art, the kind's own record) and loads it through the kind's open and load, or as a placeholder that keeps the record when the kind is not here; image.zig's pane is now a static pa_ops (open refuses what it cannot read, the decode a job whose answer lands at a step instead of a thread polled by draw, one placement fitted by the frontend, its record the file's bytes), Pane.image is Pane.addon, and the three art words act on the slot; no golden changes (images.snap and tagbottomimage.snap pass, the tag prefix and the placement key are the same bytes); tests: the image dump test, the Petscii/Palette/Ascii ctl tests, the image tag-prefix test and fs_namespace's decode test rewritten on the slot (the decode now lands at the step after the open), plus a registry test; gates: core-test 960/960 on both, unit-test 1487/1488 tui and 1531/1532 gui, behaviour-test 244/244, pane-test 171/172, snap all ok, process-test 0 of 37 failed on both
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/image.zig')
| -rw-r--r-- | src/image.zig | 238 |
1 files changed, 122 insertions, 116 deletions
diff --git a/src/image.zig b/src/image.zig index 215bfc25..80261d91 100644 --- a/src/image.zig +++ b/src/image.zig @@ -4,6 +4,7 @@ const builtin = @import("builtin"); const zstbi = @import("zstbi"); const pdf_enabled = @import("pardes_config").mupdf; const pardes = @import("pardes.zig"); +const Kind = @import("Kind.zig"); const config = @import("config.zig"); const filesystem = @import("fs.zig"); /// cap the longest side before keeping/transmitting: a pane is at most a @@ -245,13 +246,18 @@ test "native geometry is total at extreme accepted aspect ratios" { /// call once at startup / exit (stb_image's allocator shim) pub fn start(io: std.Io, gpa: std.mem.Allocator) void { zstbi.init(io, gpa); + decode_gpa = gpa; started = true; } pub fn stop() void { started = false; + decode_gpa = null; zstbi.deinit(); } +/// What a decode allocates from: the image region `start` was given. +var decode_gpa: ?std.mem.Allocator = null; + /// stb_image has its allocator (`start`): before, nothing of it may run. var started = false; @@ -427,132 +433,132 @@ test "PNG and JPEG decode through wuffs, sized from their headers first; other f try std.testing.expect(decode(gpa, "\xff\xd8\xff\xe0\x00\x10JFIF") == null); } -// ---- the image pane ---- +// ---- the image kind (kinds/pardes_addon.h), compiled in ---- -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. - pub const replica = .{ .derived = .{ "tried", "rgba", "iw", "ih", "decoder", "decoded", "answer" } }; - path: []u8, - /// Petscii, Palette and Ascii: how a frontend draws the picture as - /// glyph art (`Placement.Art`), which the core never does. - glyph_art: bool = false, - pmode: PaletteMode = .commodore, - ascii: bool = true, - tried: bool = false, - rgba: []u8 = &.{}, - iw: usize = 0, - ih: usize = 0, - /// Dump-loaded bytes, decoded lazily by the same path as a disk image. - raw: []u8 = &.{}, - /// The decode under way off the loop (ensureDecoded), and its answer. - decoder: ?std.Thread = null, - decoded: std.atomic.Value(bool) = .init(false), - answer: ?Decoded = null, +/// A picture: its file's bytes decoded on a job, shown as one placement +/// over the body that a frontend fits (or draws as glyph art). +pub const kind: Kind.Ops = std.mem.zeroInit(Kind.Ops, .{ + .head = std.mem.zeroInit(Kind.c.pa_head, .{ + .size = @sizeOf(Kind.c.pa_head), + .abi = Kind.abi, + .name = "image".* ++ @as([11]u8, @splat(0)), + .version = 1, + .exts = exts, + }), + .open = open, + .paint = paint, + .job_run = jobRun, + .job_done = jobDone, + .dump = dump, + .load = load, +}); - pub fn deinit(state: *State, gpa: std.mem.Allocator) void { - 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); - if (state.raw.len > 0) gpa.free(state.raw); - state.* = undefined; - } +const exts = blk: { + var all: []const u8 = ""; + for (config.image_exts, 0..) |e, i| all = all ++ (if (i == 0) "" else " ") ++ e; + break :blk all ++ ""; }; -/// Construct an image pane from a path and optionally transferred dump bytes. -/// `raw` must be image_gpa-owned and ownership transfers only on success. -/// Without them the file is read here, not at the first draw: a draw -/// must not go out into the host (`pardes.turn`), and the path may be a -/// mount this editor serves. A file that cannot be read draws blank. -pub fn create(p: *pardes.Pardes, id: usize, path: []const u8, raw: []u8) !*pardes.Pane { - var bytes = raw; - var read_here = false; - if (bytes.len == 0) { - // The slot stays ours across the read: another request may - // make a pane meanwhile. - p.reserved_slots[id] = true; - defer p.reserved_slots[id] = false; - if (filesystem.read(p, path)) |from_disk| { - defer p.gpa.free(from_disk); - bytes = try p.image_gpa.dupe(u8, from_disk); - read_here = true; - } else |_| {} - } - errdefer if (read_here) p.image_gpa.free(bytes); - const path_copy = try p.image_gpa.dupe(u8, path); - errdefer p.image_gpa.free(path_copy); - const pane = try p.newDocPane(id); - pane.image = .{ .path = path_copy, .raw = bytes }; - return pane; -} +const State = struct { + path: []const u8, + /// The file's bytes, as a dump keeps them. + raw: []const u8 = &.{}, + rgba: []const u8 = &.{}, + w: u32 = 0, + h: u32 = 0, +}; -/// Image renderer choices are pane-local, not global Config values. Keep them -/// queryable where they apply: the live tag beside the image's path. -/// `glyphs`: whether the picture is drawn as glyph art, which a terminal -/// with no graphics does whatever `glyph_art` says. -pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![]u8 { - return std.fmt.allocPrint( - arena, - "{s} petscii:{s} palette:{s} ascii:{s} {s}", - .{ - config.tag_image, - if (glyphs) "on" else "off", - @tagName(state.pmode), - if (state.ascii) "on" else "off", - state.path, - }, - ); +fn alloc(comptime T: type, n: usize) ?[]T { + const mem = Kind.api.alloc.?(n * @sizeOf(T)) orelse return null; + return @as([*]T, @ptrCast(@alignCast(mem)))[0..n]; } -/// The picture decoded off the loop: the first draw starts a thread and -/// draws the pane without it (its tag and frame at once); the frames after -/// it look (Pardes.nextWake asks for them) and the picture lands on the -/// first one after the decode (80 ms in ReleaseSafe, 290 in Debug, for the -/// 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 (!state.decoded.load(.acquire)) return; - t.join(); - state.decoder = null; - } - if (state.answer) |decoded| { - state.answer = null; - state.rgba = decoded.rgba; - state.iw = decoded.w; - state.ih = decoded.h; - } - if (state.tried) return; - state.tried = true; - if (state.raw.len == 0) return; // nothing was readable at open - const Work = struct { - fn run(gpa: std.mem.Allocator, st: *State) void { - st.answer = decode(gpa, st.raw); - st.decoded.store(true, .release); +fn open(path: [*c]const u8, bytes: [*c]const u8, len: u64) callconv(.c) ?*anyopaque { + const st = &(alloc(State, 1) orelse return null)[0]; + const p = if (path == null) "" else std.mem.span(path); + const kept = alloc(u8, p.len) orelse return null; + @memcpy(kept, p); + st.* = .{ .path = kept }; + if (bytes != null) { + const raw = bytes[0..@intCast(len)]; + // One it cannot read (corrupt, truncated, not what its name says) + // is refused, rather than shown as a blank pane. + if (!readable(raw)) { + const why = "not an image pardes can read"; + Kind.api.say.?(why, why.len, 1); + return st; } - }; - if (comptime !builtin.is_test) { - if (std.Thread.spawn(.{}, Work.run, .{ p.image_gpa, state })) |t| { - state.decoder = t; - return; - } else |_| {} + keep(st, raw); } - Work.run(p.image_gpa, state); - ensureDecoded(p, state); + return st; +} + +/// The file's bytes kept, and decoded on a job. +fn keep(st: *State, raw: []const u8) void { + const copy = alloc(u8, raw.len) orelse return; + @memcpy(copy, raw); + st.raw = copy; + if (raw.len > 0) _ = Kind.api.job.?(copy.ptr, @intCast(copy.len)); } -/// The picture over the body's cells, left for the frontend to fit (or -/// to draw as glyph art). -pub fn draw(p: *pardes.Pardes, state: *State, pane_id: u8, serial: u32, x: u16, y: u16, cols: u16, rows: u16) void { - ensureDecoded(p, state); - if (state.rgba.len == 0 or cols == 0 or rows == 0) return; - _ = p.appendPlacement(.{ - .pane = pane_id, - .raster = .of(state.rgba, state.iw, state.ih), - .src = .{ .w = @intCast(state.iw), .h = @intCast(state.ih) }, - .dst = .{ .x = x, .y = y, .w = cols, .h = rows }, - .key = pardes.Placement.keyOf(serial, 0, 0), - .art = .{ .glyph = state.glyph_art, .palette = state.pmode, .ascii = state.ascii }, +fn paint(ctx: ?*anyopaque, win: [*c]const Kind.c.pa_window, out: [*c]Kind.c.pa_paint) callconv(.c) void { + const st: *const State = @ptrCast(@alignCast(ctx orelse return)); + const w = win.*; + const o = &out.*; + const prefix = std.fmt.bufPrint(&o.prefix, "{s} petscii:{s} palette:{s} ascii:{s} {s}", .{ + config.tag_image, + if (w.glyphs != 0) "on" else "off", + if (w.palette == 0) "commodore" else "terminal", + if (w.ascii != 0) "on" else "off", + st.path, + }) catch &o.prefix; + o.prefix_len = @intCast(prefix.len); + if (st.rgba.len == 0 or o.cap == 0) return; + o.placements[0] = std.mem.zeroInit(Kind.c.pa_placement, .{ + .rgba = st.rgba.ptr, + .w = st.w, + .h = st.h, + .stride = st.w * 4, + .src = .{ .x = 0, .y = 0, .w = st.w, .h = st.h }, + .dst = .{ .x = 0, .y = 0, .w = w.cols, .h = w.rows }, }); + o.n = 1; +} + +/// Off the loop (80 ms in ReleaseSafe, 290 in Debug, for the docs' +/// screenshot): width and height, then the RGBA. +fn jobRun(in: [*c]const u8, n: u32, out: [*c]Kind.c.pa_out) callconv(.c) u32 { + const got = decode(decode_gpa orelse return 0, in[0..n]) orelse return 0; + defer decode_gpa.?.free(got.rgba); + const len = 8 + got.rgba.len; + const buf: [*]u8 = @ptrCast(out.*.alloc.?(out, len) orelse return 0); + std.mem.writeInt(u32, buf[0..4], @intCast(got.w), .little); + std.mem.writeInt(u32, buf[4..8], @intCast(got.h), .little); + @memcpy(buf[8..len], got.rgba); + return @intCast(len); +} + +fn jobDone(ctx: ?*anyopaque, _: u32, out: [*c]const u8, n: u32) callconv(.c) void { + const st: *State = @ptrCast(@alignCast(ctx orelse return)); + if (n < 8) return; + const w = std.mem.readInt(u32, out[0..4], .little); + const h = std.mem.readInt(u32, out[4..8], .little); + if (@as(u64, w) * h * 4 != n - 8) return; + const rgba = alloc(u8, n - 8) orelse return; + @memcpy(rgba, out[8..n]); + st.rgba = rgba; + st.w = w; + st.h = h; +} + +fn dump(ctx: ?*anyopaque, buf: [*c]u8, n: u32) callconv(.c) u32 { + const st: *const State = @ptrCast(@alignCast(ctx orelse return 0)); + if (st.raw.len <= n) @memcpy(buf[0..st.raw.len], st.raw); + return @intCast(st.raw.len); +} + +fn load(ctx: ?*anyopaque, buf: [*c]const u8, n: u32, _: u32) callconv(.c) c_int { + const st: *State = @ptrCast(@alignCast(ctx orelse return 1)); + keep(st, buf[0..n]); + return 0; } |
