summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 05:18:01 -0300
committerGabriel Schneider <[email protected]>2026-11-06 05:55:49 -0300
commitde0447471febe1253b42a32fe9a1d6cdccc9d5b9 (patch)
treecacff70a9b976f621832e7b9d41bf24ee3e5c5ed
parent3f4df1363d3051bf8c4665fdf4898d59ab365b27 (diff)
downloadpardes-de0447471febe1253b42a32fe9a1d6cdccc9d5b9.tar.gz
pardes-de0447471febe1253b42a32fe9a1d6cdccc9d5b9.zip
kinds #8, image.so: addons/image/image.c (144 lines) is the image kind against ABI 2 over stb_image as zstbi vendors it (every format it decoded, HDR out, at most 1280 pixels a side, 64 Mpixels refused before decoding), its record the file's bytes, its decode a job, one placement fitted, its prefix img; stb's memory is the pane's arena at open and the job's on a worker, so pa_out's alloc may be called as often as a job needs (an arena a job, freed after its job_done, the last allocation the answer: what the header found wanting); the build makes it with -Wall -Wextra -Werror and installs it to <prefix>/lib/pardes/addons/image.so (a bare build to ~/.local/lib/pardes/addons too); the compiled-in image kind is still there, so the start scan lists the library refused as a second image
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
-rw-r--r--addons/image/image.c144
-rw-r--r--build.zig17
-rw-r--r--src/host_io.zig11
-rw-r--r--src/kinds/pardes_addon.h4
4 files changed, 170 insertions, 6 deletions
diff --git a/addons/image/image.c b/addons/image/image.c
new file mode 100644
index 00000000..f7a228c8
--- /dev/null
+++ b/addons/image/image.c
@@ -0,0 +1,144 @@
+/* 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;
+}
+
+static int load(void *st, const uint8_t *buf, uint32_t n, uint32_t version) {
+ (void)version;
+ if (!st) return 1;
+ 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/build.zig b/build.zig
index d83b7139..132b3640 100644
--- a/build.zig
+++ b/build.zig
@@ -354,6 +354,13 @@ pub fn build(b: *std.Build) void {
const zstbi_dep = b.dependency("zstbi", .{ .target = target, .optimize = c_optimize });
const zstbi_mod = zstbi_dep.module("root");
+ // The image addon (addons/image/image.c over stb_image), installed where
+ // the start scan finds it (<prefix>/lib/pardes/addons) and handed by
+ // path to every test that opens a picture.
+ const image_addon = b.addLibrary(.{ .name = "image", .linkage = .dynamic, .root_module = b.createModule(.{ .target = target, .optimize = c_optimize, .link_libc = true }) });
+ image_addon.root_module.addIncludePath(b.path("src/kinds"));
+ image_addon.root_module.addIncludePath(zstbi_dep.path("libs/stbi"));
+ image_addon.root_module.addCSourceFile(.{ .file = b.path("addons/image/image.c"), .flags = &.{ "-std=c11", "-Wall", "-Wextra", "-Werror" } });
// mvzr backtracks with no bound on its work (its own test "do not make
// this test any longer": `a?` n times is 2^n steps), and pardes searches
@@ -1126,6 +1133,16 @@ pub fn build(b: *std.Build) void {
.root_module = root_mod,
});
installHome(b, exe, home_bin);
+ if (target.result.os.tag == .linux and !static) {
+ b.getInstallStep().dependOn(&b.addInstallFileWithDir(image_addon.getEmittedBin(), .lib, "pardes/addons/image.so").step);
+ if (home_bin) |dir| {
+ const copy = b.addSystemCommand(&.{ "install", "-Dm755" });
+ copy.addFileArg(image_addon.getEmittedBin());
+ copy.addArg(b.pathJoin(&.{ dir, "..", "lib", "pardes", "addons", "image.so" }));
+ copy.has_side_effects = true;
+ b.getInstallStep().dependOn(&copy.step);
+ }
+ }
if (gui_mod) |gm| {
var gui_name: []const u8 = "pardes-gui";
if (target.result.os.tag != .linux or target.result.cpu.arch != .x86_64)
diff --git a/src/host_io.zig b/src/host_io.zig
index 19cc920f..2320077e 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -84,6 +84,8 @@ pub const KindJob = struct {
gen: u32,
ops: *const pardes.Kind.Ops,
in: []u8,
+ /// What the job allocates (`pa_out`), freed with it; `out` the last.
+ arena: std.heap.ArenaAllocator,
out: []u8 = &.{},
id: u32 = 0,
@@ -96,7 +98,7 @@ pub const KindJob = struct {
if (j.*) |job| if (job.token == token) break j;
} else return null;
const j = slot.gpa.create(KindJob) catch return null;
- j.* = .{ .gpa = slot.gpa, .pane = pane, .serial = serial, .token = token, .gen = slot.gen, .ops = ops, .in = at.*.?.in };
+ j.* = .{ .gpa = slot.gpa, .pane = pane, .serial = serial, .token = token, .gen = slot.gen, .ops = ops, .in = at.*.?.in, .arena = .init(slot.gpa) };
at.* = null;
pardes.Addon.holdJob(ops, true);
return j;
@@ -106,9 +108,8 @@ pub const KindJob = struct {
const Out = struct {
fn alloc(out: [*c]pardes.Kind.c.pa_out, n: usize) callconv(.c) ?*anyopaque {
const job: *KindJob = @ptrCast(@alignCast(out.*.ctx));
- if (job.out.len > 0) return null;
- job.out = job.gpa.alloc(u8, n) catch return null;
- return job.out.ptr;
+ job.out = job.arena.allocator().alignedAlloc(u8, .@"16", n) catch &.{};
+ return if (job.out.len == n) job.out.ptr else null;
}
};
var out: pardes.Kind.c.pa_out = .{ .alloc = Out.alloc, .ctx = j };
@@ -125,7 +126,7 @@ pub const KindJob = struct {
pub fn drop(j: *KindJob) void {
pardes.Addon.holdJob(j.ops, false);
j.gpa.free(j.in);
- j.gpa.free(j.out);
+ j.arena.deinit();
j.gpa.destroy(j);
}
diff --git a/src/kinds/pardes_addon.h b/src/kinds/pardes_addon.h
index c6aa448b..6241c6c0 100644
--- a/src/kinds/pardes_addon.h
+++ b/src/kinds/pardes_addon.h
@@ -167,7 +167,9 @@ typedef struct pa_api {
uint64_t (*seed)(void);
} pa_api;
-/* A job's answer buffer: `alloc` it once, write it, return its length. */
+/* What a job allocates from, on its worker (pa_api is not there): as often
+ * as it needs, all of it freed after its job_done. The last it allocates
+ * is the answer, which the job writes and returns the length of. */
typedef struct pa_out {
void *(*alloc)(struct pa_out *out, size_t n);
void *ctx;