diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-06 13:28:30 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-06 14:24:59 -0300 |
| commit | 0f81de017ca7b52e951bb70f6f5f3e8c88b425e6 (patch) | |
| tree | fde88db0ed0dd68fbeb7bf7aaa49d3cd1f08b1c1 /addons/image | |
| parent | b7fdfb18ffd199d49a254c1c799bc3415ad45ec2 (diff) | |
| download | pardes-0f81de017ca7b52e951bb70f6f5f3e8c88b425e6.tar.gz pardes-0f81de017ca7b52e951bb70f6f5f3e8c88b425e6.zip | |
kinds #25, addons in Zig: src/kinds/addon.zig, over the translated header and nothing of the core, makes a kind's pa_ops from a struct of the functions it has (its head from name, version, extensions and words; a public function of no kind's name a compile error), the pane's arena and a job's pa_out as std Allocators, typed keys, a Window to put text in, a Paint for the prefix and placements, tick's flags and a Deadline, dump and load as an extern struct's bytes in one line each, and body and ctl over a Writer; snake.zig and image.zig (stb_image in stb.c, static, beside any zstbi) replace snake.c and image.c with the same goldens, harness and perf cell, and addon-test runs their own zig tests
Co-Authored-By: Claude Fable 5.1 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'addons/image')
| -rw-r--r-- | addons/image/image.c | 146 | ||||
| -rw-r--r-- | addons/image/image.zig | 124 | ||||
| -rw-r--r-- | addons/image/stb.c | 27 |
3 files changed, 151 insertions, 146 deletions
diff --git a/addons/image/image.c b/addons/image/image.c deleted file mode 100644 index 1aa34428..00000000 --- a/addons/image/image.c +++ /dev/null @@ -1,146 +0,0 @@ -/* image.c: pictures as a pardes addon (.png .jpg .gif .bmp .ppm .pgm .tga). - * A pane keeps its file's bytes (its record in a dump) and decodes them on - * a job with stb_image, at most MAX_DIM pixels a side; the core shows the - * RGBA fitted to the pane (or as glyph art, its Petscii word). */ -#include <limits.h> -#include "pardes_addon.h" - -#define MAX_DIM 1280u /* the longest side kept */ -#define MAX_PIXELS (64ull * 1024 * 1024) /* refused before decoding */ - -static const pa_api *api; - -/* stb's memory: the job's on a worker (freed after its job_done), else - * the pane's arena (a header read at open). Both are freed whole. */ -static _Thread_local pa_out *job; -static void *mem(size_t n) { return job ? job->alloc(job, n) : api->alloc(n); } -static void *remem(void *p, size_t old, size_t n) { - void *q = mem(n); - if (q && p) memcpy(q, p, old < n ? old : n); - return q; -} -#define STBI_MALLOC(n) mem(n) -#define STBI_REALLOC_SIZED(p, old, n) remem(p, old, n) -#define STBI_FREE(p) ((void)(p)) -#define STBIR_MALLOC(n, c) ((void)(c), mem(n)) -#define STBIR_FREE(p, c) ((void)(p), (void)(c)) -#define STBI_NO_STDIO -#define STBI_NO_HDR -#define STB_IMAGE_IMPLEMENTATION -#define STB_IMAGE_RESIZE_IMPLEMENTATION -#define STB_IMAGE_STATIC -#define STB_IMAGE_RESIZE_STATIC -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-function" -#include "stb_image.h" -#include "stb_image_resize.h" -#pragma GCC diagnostic pop - -typedef struct image { - const uint8_t *raw; /* the file's bytes */ - uint32_t len, w, h; - const uint8_t *rgba; /* w x h, once its job is done */ -} image; - -static int init(const pa_api *a) { api = a; return PA_OK; } - -/* The bytes kept, and decoded on a job. */ -static void keep(image *im, const uint8_t *bytes, uint32_t len) { - uint8_t *copy = api->alloc(len); - if (!copy) return; - memcpy(copy, bytes, len); - im->raw = copy, im->len = len; - if (len > 0) api->job(copy, len); -} - -static void *open(const char *path, const uint8_t *bytes, uint64_t len) { - (void)path; - image *im = api->alloc(sizeof *im); - if (!im || !bytes) return im; - int w, h, c; - /* One it cannot read (corrupt, truncated, not what its name says) is - * refused, rather than shown as a blank pane. */ - if (len == 0 || len > INT_MAX || !stbi_info_from_memory(bytes, (int)len, &w, &h, &c)) { - static const char why[] = "not an image pardes can read"; - api->say(why, sizeof why - 1, 1); - } else keep(im, bytes, (uint32_t)len); - return im; -} - -static void paint(void *st, const pa_window *win, pa_paint *out) { - const image *im = st; - memcpy(out->prefix, "img", 3); /* the core follows it with its art words and the path */ - out->prefix_len = 3; - if (!im || !im->rgba) return; - out->placements[0] = (pa_placement){ .rgba = im->rgba, .w = im->w, .h = im->h, - .src = { 0, 0, im->w, im->h }, .dst = { 0, 0, win->cols, win->rows } }; - out->n = 1; -} - -static void put32(uint8_t *at, uint32_t v) { - for (int i = 0; i < 4; i++) at[i] = (uint8_t)(v >> (8 * i)); -} - -/* On a worker: width and height (little endian), then the RGBA. */ -static uint32_t decode(const uint8_t *in, uint32_t n) { - int w, h, c; - if (n == 0 || n > INT_MAX || !stbi_info_from_memory(in, (int)n, &w, &h, &c) || (uint64_t)w * (uint64_t)h > MAX_PIXELS) return 0; - uint8_t *px = stbi_load_from_memory(in, (int)n, &w, &h, &c, 4); - if (!px) return 0; - uint32_t longest = (uint32_t)(w > h ? w : h), nw = (uint32_t)w, nh = (uint32_t)h; - if (longest > MAX_DIM) { - nw = (uint32_t)((uint64_t)w * MAX_DIM / longest), nh = (uint32_t)((uint64_t)h * MAX_DIM / longest); - nw += !nw, nh += !nh; - uint8_t *small = mem((size_t)nw * nh * 4); - if (!small || !stbir_resize_uint8(px, w, h, 0, small, (int)nw, (int)nh, 0, 4)) return 0; - px = small; - } - size_t len = 8 + (size_t)nw * nh * 4; - uint8_t *a = job->alloc(job, len); /* the last: the answer */ - if (!a) return 0; - put32(a, nw), put32(a + 4, nh); - memcpy(a + 8, px, len - 8); - return (uint32_t)len; -} - -static uint32_t job_run(const uint8_t *in, uint32_t n, pa_out *out) { - job = out; - uint32_t got = decode(in, n); - job = 0; - return got; -} - -static void job_done(void *st, uint32_t token, const uint8_t *out, uint32_t n) { - image *im = st; - (void)token; - if (!im || n < 8) return; - uint32_t w = out[0] | out[1] << 8 | out[2] << 16 | (uint32_t)out[3] << 24; - uint32_t h = out[4] | out[5] << 8 | out[6] << 16 | (uint32_t)out[7] << 24; - if ((uint64_t)w * h * 4 != n - 8) return; - uint8_t *rgba = api->alloc(n - 8); - if (!rgba) return; - memcpy(rgba, out + 8, n - 8); - im->rgba = rgba, im->w = w, im->h = h; -} - -/* The record is the file's bytes. */ -static uint32_t dump(void *st, uint8_t *buf, uint32_t n) { - const image *im = st; - if (!im) return 0; - if (im->len <= n) memcpy(buf, im->raw, im->len); - return im->len; -} - -/* On a state with no picture (a Restore's), the record's; one open made - * from the file as it is now (it changed on disk) keeps that one. */ -static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) { - (void)version; - if (!st) return 1; - if (!((image *)st)->raw) keep(st, buf, n); - return PA_OK; -} - -const pa_ops pardes_addon = { - .head = PA_ADDON("image", 1, ".png .jpg .jpeg .gif .bmp .ppm .pgm .tga", 0, 0), - .init = init, .open = open, .paint = paint, .job_run = job_run, .job_done = job_done, .dump = dump, .load = load, -}; diff --git a/addons/image/image.zig b/addons/image/image.zig new file mode 100644 index 00000000..824d708f --- /dev/null +++ b/addons/image/image.zig @@ -0,0 +1,124 @@ +//! image.zig: pictures as a pardes addon (.png .jpg .gif .bmp .ppm .pgm .tga). +//! A pane keeps its file's bytes (its record in a dump) and decodes them on +//! a job with stb_image (stb.c), at most max_dim pixels a side; the core +//! shows the RGBA fitted to the pane (or as glyph art, its Petscii word). +const std = @import("std"); +const addon = @import("addon"); + +const max_dim = 1280; // the longest side kept +const max_pixels = 64 << 20; // refused before decoding + +extern fn pa_image_info(bytes: [*]const u8, n: c_int, w: *c_int, h: *c_int) c_int; +extern fn pa_image_load(bytes: [*]const u8, n: c_int, w: *c_int, h: *c_int) ?[*]const u8; +extern fn pa_image_resize(in: [*]const u8, w: c_int, h: c_int, out: [*]u8, nw: c_int, nh: c_int) c_int; + +/// stb's memory (stb.c): the job's on a worker, else the pane's arena. +threadlocal var job: ?std.mem.Allocator = null; +export fn pa_image_mem(n: usize) ?*anyopaque { + return ((job orelse addon.arena).alignedAlloc(u8, .@"16", n) catch return null).ptr; +} +export fn pa_image_remem(p: ?*anyopaque, old: usize, n: usize) ?*anyopaque { + const q: [*]u8 = @ptrCast(pa_image_mem(n) orelse return null); + if (p) |from| @memcpy(q[0..@min(old, n)], @as([*]const u8, @ptrCast(from))); + return q; +} + +/// Its width and height, if stb can read it. +fn info(bytes: []const u8) ?[2]u32 { + var w: c_int, var h: c_int = .{ 0, 0 }; + if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int) or pa_image_info(bytes.ptr, @intCast(bytes.len), &w, &h) == 0) return null; + return .{ @intCast(w), @intCast(h) }; +} + +pub const Image = struct { + raw: []const u8 = &.{}, // the file's bytes + w: u32 = 0, + h: u32 = 0, + rgba: ?[]const u8 = null, // w x h, once its job is done + + pub const head: addon.Head = .{ .name = "image", .version = 1, .exts = ".png .jpg .jpeg .gif .bmp .ppm .pgm .tga" }; + + /// The bytes kept, and decoded on a job. + fn keep(im: *Image, bytes: []const u8) void { + im.raw = addon.arena.dupe(u8, bytes) catch return; + if (bytes.len > 0) _ = addon.job(im.raw); + } + + /// One it cannot read (corrupt, truncated, not what its name says) is + /// refused, rather than shown as a blank pane. + pub fn open(im: *Image, bytes: ?[]const u8) void { + const b = bytes orelse return; + if (info(b) == null) return addon.say("not an image pardes can read", .err); + im.keep(b); + } + + pub fn paint(im: *const Image, w: addon.Window, out: addon.Paint) void { + out.prefix("img", .{}); // the core follows it with its art words and the path + const rgba = im.rgba orelse return; + out.place(.{ .rgba = rgba.ptr, .w = im.w, .h = im.h, .src = .{ .w = im.w, .h = im.h }, .dst = .{ .w = w.cols, .h = w.rows } }); + } + + /// On a worker: width and height (little endian), then the RGBA. + pub fn jobRun(in: []const u8, gpa: std.mem.Allocator) ?[]u8 { + job = gpa; + defer job = null; + const wh = info(in) orelse return null; + if (@as(u64, wh[0]) * wh[1] > max_pixels) return null; + var w: c_int, var h: c_int = .{ 0, 0 }; + var px = pa_image_load(in.ptr, @intCast(in.len), &w, &h) orelse return null; + const longest: u64 = @max(wh[0], wh[1]); + var nw, var nh = wh; + if (longest > max_dim) { + nw = @max(1, @as(u32, @intCast(@as(u64, wh[0]) * max_dim / longest))); + nh = @max(1, @as(u32, @intCast(@as(u64, wh[1]) * max_dim / longest))); + const small = gpa.alloc(u8, @as(usize, nw) * nh * 4) catch return null; + if (pa_image_resize(px, w, h, small.ptr, @intCast(nw), @intCast(nh)) == 0) return null; + px = small.ptr; + } + const a = gpa.alloc(u8, 8 + @as(usize, nw) * nh * 4) catch return null; // the last: the answer + std.mem.writeInt(u32, a[0..4], nw, .little); + std.mem.writeInt(u32, a[4..8], nh, .little); + @memcpy(a[8..], px[0 .. a.len - 8]); + return a; + } + + pub fn jobDone(im: *Image, _: u32, out: []const u8) void { + if (out.len < 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 != out.len - 8) return; + im.rgba = addon.arena.dupe(u8, out[8..]) catch return; + im.w, im.h = .{ w, h }; + } + + /// The record is the file's bytes. + pub fn dump(im: *const Image, buf: []u8) usize { + if (im.raw.len <= buf.len) @memcpy(buf[0..im.raw.len], im.raw); + return im.raw.len; + } + + /// On a state with no picture (a Restore's), the record's; one open + /// made from the file as it is now (it changed on disk) keeps that one. + pub fn load(im: *Image, bytes: []const u8, _: u32) bool { + if (im.raw.len == 0) im.keep(bytes); + return true; + } +}; + +export const pardes_addon = addon.ops(Image); + +test "a job decodes a picture to its size and pixels, and one past max_dim is scaled to fit" { + var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); + defer arena.deinit(); + const png = "\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\x0cIDATx\x9cc\xf8\xcf\xc0\x00\x00\x03\x01\x01\x00\xc9\xfe\x92\xef\x00\x00\x00\x00IEND\xaeB`\x82"; + const got = Image.jobRun(png, arena.allocator()).?; + try std.testing.expectEqualSlices(u8, &.{ 1, 0, 0, 0, 1, 0, 0, 0, 0xff, 0, 0, 0xff }, got); + // A PPM 2600x1: 1280x1 after. + var ppm: std.ArrayList(u8) = .empty; + try ppm.print(arena.allocator(), "P6\n2600 1\n255\n", .{}); + try ppm.appendNTimes(arena.allocator(), 0x80, 2600 * 3); + const wide = Image.jobRun(ppm.items, arena.allocator()).?; + try std.testing.expectEqual(1280, std.mem.readInt(u32, wide[0..4], .little)); + try std.testing.expectEqual(8 + 1280 * 4, wide.len); + try std.testing.expect(Image.jobRun("not a picture", arena.allocator()) == null); +} diff --git a/addons/image/stb.c b/addons/image/stb.c new file mode 100644 index 00000000..5fba51bc --- /dev/null +++ b/addons/image/stb.c @@ -0,0 +1,27 @@ +/* stb_image and its resize for image.zig, static here (a static pardes + * links a zstbi beside them), their memory image.zig's: the job's on a + * worker, else the pane's arena; both are freed whole. */ +#include <stddef.h> +#include <string.h> +void *pa_image_mem(size_t n); +void *pa_image_remem(void *p, size_t old, size_t n); +#define STBI_MALLOC(n) pa_image_mem(n) +#define STBI_REALLOC_SIZED(p, old, n) pa_image_remem(p, old, n) +#define STBI_FREE(p) ((void)(p)) +#define STBIR_MALLOC(n, c) ((void)(c), pa_image_mem(n)) +#define STBIR_FREE(p, c) ((void)(p), (void)(c)) +#define STBI_NO_STDIO +#define STBI_NO_HDR +#define STB_IMAGE_IMPLEMENTATION +#define STB_IMAGE_RESIZE_IMPLEMENTATION +#define STB_IMAGE_STATIC +#define STB_IMAGE_RESIZE_STATIC +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-function" +#include "stb_image.h" +#include "stb_image_resize.h" +#pragma GCC diagnostic pop + +int pa_image_info(const unsigned char *b, int n, int *w, int *h) { int c; return stbi_info_from_memory(b, n, w, h, &c); } +unsigned char *pa_image_load(const unsigned char *b, int n, int *w, int *h) { int c; return stbi_load_from_memory(b, n, w, h, &c, 4); } +int pa_image_resize(const unsigned char *in, int w, int h, unsigned char *out, int nw, int nh) { return stbir_resize_uint8(in, w, h, 0, out, nw, nh, 0, 4); } |
