summaryrefslogtreecommitdiff
path: root/addons
diff options
context:
space:
mode:
Diffstat (limited to 'addons')
-rw-r--r--addons/image/image.zig64
1 files changed, 46 insertions, 18 deletions
diff --git a/addons/image/image.zig b/addons/image/image.zig
index 09b17ca7..f2303433 100644
--- a/addons/image/image.zig
+++ b/addons/image/image.zig
@@ -1,7 +1,9 @@
//! image.zig: pictures as a pardes addon (.png .jpg .gif .bmp .ppm .pgm .tga).
-//! A pane decodes its file's bytes 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). It has no record: a Restore reads the file.
+//! A pane's state is its file's size (PA_PLAIN: a client runs it as the
+//! session does); each process that shows it decodes the file's bytes on a
+//! draw (api.draw) with stb_image (stb.c), at most max_dim pixels a side,
+//! and keeps the RGBA for its paint alone. The core shows it fitted to the
+//! pane (or as glyph art, its Petscii word).
const std = @import("std");
const addon = @import("addon");
@@ -12,10 +14,10 @@ extern fn pa_image_info(bytes: [*]const u8, n: c_int, w: *c_int, h: *c_int) c_in
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.
+/// stb's memory (stb.c): an arena of the call's (a draw's, or `info`'s).
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;
+ return ((job orelse return null).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);
@@ -25,24 +27,44 @@ export fn pa_image_remem(p: ?*anyopaque, old: usize, n: usize) ?*anyopaque {
/// Its width and height, if stb can read it.
fn info(bytes: []const u8) ?[2]u32 {
+ var arena: std.heap.ArenaAllocator = .init(addon.heap);
+ defer arena.deinit();
+ job = arena.allocator();
+ defer job = null;
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 {
+/// Each pane's pixels in this process, by its block's address: a draw's
+/// answer, which paint alone reads.
+const Picture = struct { of: ?*const Image = null, w: u32 = 0, h: u32 = 0, rgba: []u8 = &.{} };
+var pictures: [64]Picture = @splat(.{});
+
+fn pictureOf(im: *const Image) ?*Picture {
+ for (&pictures) |*p| if (p.of == im) return p;
+ return null;
+}
+
+pub const Image = extern struct {
+ /// The file's size, as it says (the block: the same in every process).
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", .pictures = true };
+ pub const head: addon.Head = .{ .name = "image", .version = 1, .exts = ".png .jpg .jpeg .gif .bmp .ppm .pgm .tga", .pictures = true, .plain = true };
- /// Decoded on a job; one it cannot read (corrupt, truncated, not what
+ /// Decoded on a draw; one it cannot read (corrupt, truncated, not what
/// its name says) is refused, rather than shown as a blank pane.
- pub fn open(_: *Image, _: ?[]const u8, bytes: ?[]const u8) void {
+ pub fn open(im: *Image, _: ?[]const u8, bytes: ?[]const u8) void {
const b = bytes orelse return;
- if (info(b) == null) return addon.say("not an image pardes can read", .err);
- _ = addon.job(b);
+ im.w, im.h = info(b) orelse return addon.say("not an image pardes can read", .err);
+ _ = addon.draw(b);
+ }
+
+ pub fn close(im: *Image) void {
+ const p = pictureOf(im) orelse return;
+ addon.heap.free(p.rgba);
+ p.* = .{};
}
/// The core follows it with its art words and the path.
@@ -51,17 +73,18 @@ pub const Image = struct {
}
pub fn paint(im: *const Image, w: addon.Window, out: addon.Paint) void {
- 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 } });
+ const p = pictureOf(im) orelse return;
+ out.place(.{ .rgba = p.rgba.ptr, .w = p.w, .h = p.h, .src = .{ .w = p.w, .h = p.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 };
+ if (in.len == 0 or in.len > std.math.maxInt(c_int) or pa_image_info(in.ptr, @intCast(in.len), &w, &h) == 0) return null;
+ const wh: [2]u32 = .{ @intCast(w), @intCast(h) };
+ if (@as(u64, wh[0]) * wh[1] > max_pixels) return null;
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;
@@ -79,13 +102,18 @@ pub const Image = struct {
return a;
}
+ /// A draw's answer: this process's pixels, the block untouched.
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 };
+ const p = pictureOf(im) orelse for (&pictures) |*free| {
+ if (free.of == null) break free;
+ } else return;
+ const rgba = addon.heap.dupe(u8, out[8..]) catch return;
+ addon.heap.free(p.rgba);
+ p.* = .{ .of = im, .w = w, .h = h, .rgba = rgba };
}
};