//! image.zig: pictures as a pardes addon (.png .jpg .gif .bmp .ppm .pgm .tga). //! 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"); 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): 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 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); 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 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 size(w, h); } /// stb's width and height as a size: a top-down BMP's height is negative in /// its info (stb's loader takes the length of it); none, nothing to show. fn size(w: c_int, h: c_int) ?[2]u32 { if (w <= 0 or h == 0) return null; return .{ @intCast(w), @abs(h) }; } /// 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, 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 draw; 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, _: ?[]const u8, bytes: ?[]const u8) void { const b = bytes orelse return; 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. pub fn prefix(_: *const Image, w: *std.Io.Writer) !void { try w.writeAll("img"); } pub fn paint(im: *const Image, w: addon.Window, out: addon.Paint) void { 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; 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 = size(w, h) orelse return null; 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; // The pixels are the load's: copied by its size, never info's alone. const loaded = size(w, h) orelse return null; if (loaded[0] != wh[0] or loaded[1] != wh[1]) 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; } /// 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; 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 }; } }; pub const pardes_addon = addon.ops(Image); pub const panic = addon.panic; comptime { addon.exportLib(&pardes_addon); } 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); } test "a top-down BMP (its height negative in its header) opens at its size, as any picture" { var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); defer arena.deinit(); // 2x2, 24 bits, biHeight -2: rows top first, each 6 bytes and 2 of padding. const bmp = "BM" ++ "\x46\x00\x00\x00" ++ "\x00\x00\x00\x00" ++ "\x36\x00\x00\x00" ++ "\x28\x00\x00\x00" ++ "\x02\x00\x00\x00" ++ "\xfe\xff\xff\xff" ++ "\x01\x00" ++ "\x18\x00" ++ "\x00\x00\x00\x00" ++ "\x10\x00\x00\x00" ++ "\x13\x0b\x00\x00" ++ "\x13\x0b\x00\x00" ++ "\x00\x00\x00\x00" ++ "\x00\x00\x00\x00" ++ "\x00\x00\xff\x00\xff\x00\x00\x00" ++ "\xff\x00\x00\xff\xff\xff\x00\x00"; try std.testing.expectEqual([2]u32{ 2, 2 }, info(bmp).?); const got = Image.jobRun(bmp, arena.allocator()).?; try std.testing.expectEqual(8 + 2 * 2 * 4, got.len); try std.testing.expectEqual(2, std.mem.readInt(u32, got[4..8], .little)); // Its first pixel is the top row's first: red (BGR 00 00 ff). try std.testing.expectEqualSlices(u8, &.{ 0xff, 0, 0, 0xff }, got[8..12]); }