//! Image decoding, pixel placement, and the image pane. const std = @import("std"); 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 /// screenful of cells, multi-thousand-pixel photos waste decode/transmit time const MAX_DIM: u32 = 1280; pub const PaletteMode = pardes.Placement.Art.Palette; /// How a native pixel attachment is placed in its pane body. `contain` is the /// historical image-pane policy: shrink only when necessary and keep the /// image at the top left. The axis-specific modes may enlarge, letterbox the /// unconstrained axis, or crop it when it overflows. pub const NativeFit = if (pdf_enabled) enum { contain, width, height } else enum { contain }; pub const PixelRect = struct { x: u32 = 0, y: u32 = 0, w: u32 = 0, h: u32 = 0, }; pub const PixelPoint = struct { x: u32, y: u32 }; /// Source pixels and their destination inside a pane body's pixel rectangle. /// Both rectangles are half-open. Rounding is always toward the inside, so a /// backend can draw this result without a backend-specific overflow clip. pub const NativeGeometry = struct { src: PixelRect, dst: PixelRect, /// Map a destination pixel back into the retained source crop. This is the /// same integer transform the texture rectangle describes; points in a /// letterbox return null. pub fn sourcePoint(self: NativeGeometry, x: u32, y: u32) ?PixelPoint { if (self.src.w == 0 or self.src.h == 0 or self.dst.w == 0 or self.dst.h == 0) return null; if (x < self.dst.x or y < self.dst.y) return null; const dx = x - self.dst.x; const dy = y - self.dst.y; if (dx >= self.dst.w or dy >= self.dst.h) return null; return .{ .x = self.src.x + @as(u32, @intCast(@as(u64, dx) * self.src.w / self.dst.w)), .y = self.src.y + @as(u32, @intCast(@as(u64, dy) * self.src.h / self.dst.h)), }; } }; fn panOffset(overflow: u32, pan: u16) u32 { if (overflow == 0 or pan == 0) return 0; if (pan == std.math.maxInt(u16)) return overflow; // Round, rather than truncate, so the midpoint is the visual midpoint. return @intCast((@as(u64, overflow) * pan + std.math.maxInt(u16) / 2) / std.math.maxInt(u16)); } fn fitWidth(src_w: u32, src_h: u32, body_w: u32, body_h: u32, pan_y: u16) ?NativeGeometry { const scaled_h = @as(u64, src_h) * body_w; if (scaled_h <= @as(u64, body_h) * src_w) { const full_h: u32 = @intCast(@max(1, scaled_h / src_w)); return .{ .src = .{ .w = src_w, .h = src_h }, .dst = .{ .y = (body_h - full_h) / 2, .w = body_w, .h = full_h }, }; } // Retain the largest whole source-pixel crop which cannot extend beyond // the body at this scale. The sub-pixel remainder becomes at most a small // centered letterbox instead of leaking into an adjacent cell in Kitty. const crop_h_wide = @as(u64, body_h) * src_w / body_w; if (crop_h_wide == 0) return null; const crop_h: u32 = @min(src_h, @as(u32, @intCast(crop_h_wide))); const dst_h: u32 = @min(body_h, @max(1, @as(u32, @intCast(@as(u64, crop_h) * body_w / src_w)))); return .{ .src = .{ .y = panOffset(src_h - crop_h, pan_y), .w = src_w, .h = crop_h, }, .dst = .{ .y = (body_h - dst_h) / 2, .w = body_w, .h = dst_h }, }; } fn fitHeight(src_w: u32, src_h: u32, body_w: u32, body_h: u32, pan_x: u16) ?NativeGeometry { const scaled_w = @as(u64, src_w) * body_h; if (scaled_w <= @as(u64, body_w) * src_h) { const full_w: u32 = @intCast(@max(1, scaled_w / src_h)); return .{ .src = .{ .w = src_w, .h = src_h }, .dst = .{ .x = (body_w - full_w) / 2, .w = full_w, .h = body_h }, }; } const crop_w_wide = @as(u64, body_w) * src_h / body_h; if (crop_w_wide == 0) return null; const crop_w: u32 = @min(src_w, @as(u32, @intCast(crop_w_wide))); const dst_w: u32 = @min(body_w, @max(1, @as(u32, @intCast(@as(u64, crop_w) * body_h / src_h)))); return .{ .src = .{ .x = panOffset(src_w - crop_w, pan_x), .w = crop_w, .h = src_h, }, .dst = .{ .x = (body_w - dst_w) / 2, .w = dst_w, .h = body_h }, }; } /// Compute native image placement without floating point. Source dimensions /// are limited to u32 because SDL's texture API and Kitty's crop parameters /// cannot describe anything larger. Kitty applies the stricter u16 check at /// its protocol boundary. pub fn nativeGeometry( iw: usize, ih: usize, body_pixel_w: u32, body_pixel_h: u32, fit: NativeFit, pan_x: u16, pan_y: u16, ) ?NativeGeometry { if (comptime !pdf_enabled) { return containGeometry(iw, ih, body_pixel_w, body_pixel_h); } if (iw == 0 or ih == 0 or body_pixel_w == 0 or body_pixel_h == 0 or iw > std.math.maxInt(u32) or ih > std.math.maxInt(u32)) return null; const src_w: u32 = @intCast(iw); const src_h: u32 = @intCast(ih); return switch (fit) { .width => fitWidth(src_w, src_h, body_pixel_w, body_pixel_h, pan_y), .height => fitHeight(src_w, src_h, body_pixel_w, body_pixel_h, pan_x), // Pan is not contain's: it keeps the top-left placement. .contain => containGeometry(iw, ih, body_pixel_w, body_pixel_h), }; } /// The ordinary image-pane policy: shrunk only when it must be, at the top /// left. The scale ratios are compared without floating point: the tighter /// bound is exact, the other rounded inward. pub fn containGeometry(iw: usize, ih: usize, max_w: u32, max_h: u32) ?NativeGeometry { if (iw == 0 or ih == 0 or max_w == 0 or max_h == 0 or iw > std.math.maxInt(u32) or ih > std.math.maxInt(u32)) return null; const src_w: u32 = @intCast(iw); const src_h: u32 = @intCast(ih); if (src_w <= max_w and src_h <= max_h) return .{ .src = .{ .w = src_w, .h = src_h }, .dst = .{ .w = src_w, .h = src_h }, }; const dst: PixelRect = if (@as(u64, max_w) * src_h <= @as(u64, max_h) * src_w) .{ .w = max_w, .h = @intCast(@max(1, @as(u64, src_h) * max_w / src_w)), } else .{ .w = @intCast(@max(1, @as(u64, src_w) * max_h / src_h)), .h = max_h, }; return .{ .src = .{ .w = src_w, .h = src_h }, .dst = dst }; } test "native image contain keeps aspect, bounds, top-left, and small-image size" { try std.testing.expectEqual(PixelRect{ .w = 40, .h = 20 }, containGeometry(400, 200, 40, 40).?.dst); try std.testing.expectEqual(PixelRect{ .w = 20, .h = 40 }, containGeometry(200, 400, 40, 40).?.dst); try std.testing.expectEqual(PixelRect{ .w = 17, .h = 9 }, containGeometry(17, 9, 40, 40).?.dst); try std.testing.expect(containGeometry(17, 9, 0, 40) == null); const odd = containGeometry(403, 211, 101, 47).?; try std.testing.expectEqual(PixelRect{ .w = 89, .h = 47 }, odd.dst); try std.testing.expectEqual(PixelRect{ .w = 403, .h = 211 }, odd.src); } test "native fit width handles portrait crop, pan extrema, and landscape letterbox" { if (comptime !pdf_enabled) return; const top = nativeGeometry(600, 1000, 800, 600, .width, 0, 0).?; try std.testing.expectEqual(PixelRect{ .w = 600, .h = 450 }, top.src); try std.testing.expectEqual(PixelRect{ .w = 800, .h = 600 }, top.dst); const middle = nativeGeometry(600, 1000, 800, 600, .width, 0, 32768).?; try std.testing.expectEqual(@as(u32, 275), middle.src.y); const bottom = nativeGeometry(600, 1000, 800, 600, .width, 65535, 65535).?; try std.testing.expectEqual(@as(u32, 550), bottom.src.y); const landscape = nativeGeometry(1000, 600, 800, 600, .width, 0, 65535).?; try std.testing.expectEqual(PixelRect{ .w = 1000, .h = 600 }, landscape.src); try std.testing.expectEqual(PixelRect{ .y = 60, .w = 800, .h = 480 }, landscape.dst); } test "native fit height handles portrait letterbox and landscape crop" { if (comptime !pdf_enabled) return; const portrait = nativeGeometry(600, 1000, 800, 600, .height, 65535, 0).?; try std.testing.expectEqual(PixelRect{ .w = 600, .h = 1000 }, portrait.src); try std.testing.expectEqual(PixelRect{ .x = 220, .w = 360, .h = 600 }, portrait.dst); const left = nativeGeometry(1000, 600, 800, 600, .height, 0, 0).?; try std.testing.expectEqual(PixelRect{ .w = 800, .h = 600 }, left.src); try std.testing.expectEqual(PixelRect{ .w = 800, .h = 600 }, left.dst); const right = nativeGeometry(1000, 600, 800, 600, .height, 65535, 0).?; try std.testing.expectEqual(@as(u32, 200), right.src.x); } test "native fit odd ratios round inward and map points through letterboxes and crops" { if (comptime !pdf_enabled) return; const width = nativeGeometry(403, 211, 101, 47, .width, 0, 32768).?; try std.testing.expectEqual(PixelRect{ .y = 12, .w = 403, .h = 187 }, width.src); try std.testing.expectEqual(PixelRect{ .w = 101, .h = 46 }, width.dst); const mapped = width.sourcePoint(100, 45).?; try std.testing.expect(mapped.x < width.src.x + width.src.w); try std.testing.expect(mapped.y < width.src.y + width.src.h); try std.testing.expect(width.sourcePoint(101, 46) == null); const height = nativeGeometry(403, 211, 101, 47, .height, 32768, 0).?; try std.testing.expectEqual(PixelRect{ .w = 403, .h = 211 }, height.src); try std.testing.expectEqual(PixelRect{ .x = 6, .w = 89, .h = 47 }, height.dst); try std.testing.expect(height.sourcePoint(5, 20) == null); try std.testing.expect(height.sourcePoint(6, 20) != null); } test "native geometry rejects dimensions which its backend contract cannot represent" { try std.testing.expect(containGeometry(0, 10, 20, 20) == null); if (@bitSizeOf(usize) > 32) try std.testing.expect(containGeometry(@as(usize, std.math.maxInt(u32)) + 1, 10, 20, 20) == null); if (comptime pdf_enabled) { try std.testing.expect(nativeGeometry(10, 10, 0, 20, .width, 0, 0) == null); if (@bitSizeOf(usize) > 32) try std.testing.expect(nativeGeometry(@as(usize, std.math.maxInt(u32)) + 1, 10, 20, 20, .height, 0, 0) == null); } } test "native geometry is total at extreme accepted aspect ratios" { if (comptime !pdf_enabled) return; const largest = std.math.maxInt(u32); try std.testing.expect(nativeGeometry(1, largest, largest, 1, .width, 0, 65535) == null); try std.testing.expect(nativeGeometry(largest, 1, 1, largest, .height, 65535, 0) == null); const wide = nativeGeometry(largest, 1, largest, 1, .width, 0, 0).?; try std.testing.expectEqual(PixelRect{ .w = largest, .h = 1 }, wide.dst); const tall = nativeGeometry(1, largest, 1, largest, .height, 0, 0).?; try std.testing.expectEqual(PixelRect{ .w = 1, .h = largest }, tall.dst); } /// 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; extern fn stbi_info_from_memory(buffer: [*]const u8, len: c_int, x: *c_int, y: *c_int, comp: *c_int) c_int; /// stb_image's header reader. fn stbiInfo(bytes: []const u8, w: *c_int, h: *c_int, c: *c_int) bool { return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), w, h, c) == 1; } /// PNG and JPEG are ghostty's decoders (wuffs, which ghostty-vt links for /// kitty graphics) wherever ghostty-vt is in the build; stb_image is then /// compiled without them, and without the inflate only its PNG used /// (build.zig, docs/shared-libs.md). Other formats, and builds without /// ghostty-vt, are stb_image's. const wuffs_images = @hasDecl(@import("pardes_config"), "wuffs_images") and @import("pardes_config").wuffs_images; const wuffs = if (wuffs_images) @import("wuffs") else struct {}; const Format = enum { png, jpeg, other }; fn format(bytes: []const u8) Format { if (std.mem.startsWith(u8, bytes, "\x89PNG\r\n\x1a\n")) return .png; if (bytes.len >= 3 and bytes[0] == 0xff and bytes[1] == 0xd8 and bytes[2] == 0xff) return .jpeg; return .other; } const Size = struct { w: usize, h: usize }; /// An image's size from its header alone, before anything is decoded. fn headerSize(bytes: []const u8) ?Size { if (comptime wuffs_images) switch (format(bytes)) { .png => { // The signature, then IHDR first: length, type, width, height. if (bytes.len < 24 or !std.mem.eql(u8, bytes[12..16], "IHDR")) return null; return .{ .w = std.mem.readInt(u32, bytes[16..20], .big), .h = std.mem.readInt(u32, bytes[20..24], .big) }; }, .jpeg => return jpegSize(bytes), .other => {}, }; var iw: c_int = 0; var ih: c_int = 0; var ic: c_int = 0; if (!stbiInfo(bytes, &iw, &ih, &ic)) return null; return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) }; } /// A JPEG's size, from its first start-of-frame segment (any SOFn but the /// DHT, JPG and DAC markers that share their range). fn jpegSize(bytes: []const u8) ?Size { var at: usize = 2; while (at + 4 <= bytes.len) { if (bytes[at] != 0xff) return null; const marker = bytes[at + 1]; // Fill bytes, and the markers that stand alone. if (marker == 0xff) { at += 1; continue; } if (marker == 0x01 or (marker >= 0xd0 and marker <= 0xd7)) { at += 2; continue; } const len = std.mem.readInt(u16, bytes[at + 2 ..][0..2], .big); if (len < 2) return null; const sof = marker >= 0xc0 and marker <= 0xcf and marker != 0xc4 and marker != 0xc8 and marker != 0xcc; if (sof) { if (at + 9 > bytes.len) return null; return .{ .w = std.mem.readInt(u16, bytes[at + 7 ..][0..2], .big), .h = std.mem.readInt(u16, bytes[at + 5 ..][0..2], .big) }; } if (marker == 0xda) return null; // scan data before any frame at += 2 + len; } return null; } /// Whether `bytes` are an image `decode` can read, from its header alone. pub fn readable(bytes: []const u8) bool { if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return false; var w: c_int = 0; var h: c_int = 0; var c: c_int = 0; if (!started) return true; // ponytail: no stb_image yet (a unit test), so no judging if (comptime wuffs_images) if (format(bytes) != .other) return headerSize(bytes) != null; return stbiInfo(bytes, &w, &h, &c); } /// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the /// pane then simply shows a blank body. const Decoded = struct { rgba: []u8, w: usize, h: usize }; pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded { // stb_image allocates through what `start` gave it: before that, a call // jumps through a null pointer (a terminal's PNG in a core no shell set up). if (!started) return null; if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int)) return null; // Bounded before decoding: a few KB of PNG can name gigapixels. const size = headerSize(bytes) orelse return null; const pixels = std.math.mul(usize, size.w, size.h) catch return null; if (pixels > @import("memory.zig").limits.image_decode_pixels) return null; if (comptime wuffs_images) switch (format(bytes)) { inline .png, .jpeg => |f| { const got = @field(wuffs, @tagName(f)).decode(gpa, bytes) catch return null; defer gpa.free(got.data); if (got.data.len != @as(usize, got.width) * got.height * 4) return null; return finish(gpa, .{ .data = got.data, .width = got.width, .height = got.height, .num_components = 4, .bytes_per_component = 1, .bytes_per_row = got.width * 4, .is_hdr = false, }); }, .other => {}, }; var img = zstbi.Image.loadFromMemory(bytes, 4) catch return null; // 4 = RGBA defer img.deinit(); return finish(gpa, img); } /// The decoded RGBA, scaled to MAX_DIM at most, in a gpa copy. fn finish(gpa: std.mem.Allocator, img: zstbi.Image) ?Decoded { var scaled: ?zstbi.Image = null; defer if (scaled) |*s| s.deinit(); const longest = @max(img.width, img.height); const src: *const zstbi.Image = if (longest > MAX_DIM) blk: { const nw = @max(1, img.width * MAX_DIM / longest); const nh = @max(1, img.height * MAX_DIM / longest); scaled = img.resize(nw, nh); break :blk &scaled.?; } else &img; const rgba = gpa.dupe(u8, src.data) catch return null; return .{ .rgba = rgba, .w = src.width, .h = src.height }; } test "PNG and JPEG decode through wuffs, sized from their headers first; other formats through stb_image" { const gpa = std.testing.allocator; start(std.testing.io, gpa); defer stop(); const dec = std.base64.standard.Decoder; const Fixture = struct { b64: []const u8, w: usize, h: usize, rgb: [3]u8, tolerance: u8 }; const fixtures = [_]Fixture{ // 4x2, red left, blue right (a hand-built PNG). .{ .b64 = "iVBORw0KGgoAAAANSUhEUgAAAAQAAAACCAIAAADwyuo0AAAAE0lEQVR4nGP4z8AARGDiPwMyBwBnsgf5Q2OaZwAAAABJRU5ErkJggg==", .w = 4, .h = 2, .rgb = .{ 255, 0, 0 }, .tolerance = 0 }, // 16x8 of (200,30,30), baseline and progressive (Pillow, quality 95). .{ .b64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wAARCAAIABADASIAAhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQAAAF9AQIDAAQRBRIhMUEGE1FhByJxFDKBkaEII0KxwRVS0fAkM2JyggkKFhcYGRolJicoKSo0NTY3ODk6Q0RFRkdISUpTVFVWV1hZWmNkZWZnaGlqc3R1dnd4eXqDhIWGh4iJipKTlJWWl5iZmqKjpKWmp6ipqrKztLW2t7i5usLDxMXGx8jJytLT1NXW19jZ2uHi4+Tl5ufo6erx8vP09fb3+Pn6/8QAHwEAAwEBAQEBAQEBAQAAAAAAAAECAwQFBgcICQoL/8QAtREAAgECBAQDBAcFBAQAAQJ3AAECAxEEBSExBhJBUQdhcRMiMoEIFEKRobHBCSMzUvAVYnLRChYkNOEl8RcYGRomJygpKjU2Nzg5OkNERUZHSElKU1RVVldYWVpjZGVmZ2hpanN0dXZ3eHl6goOEhYaHiImKkpOUlZaXmJmaoqOkpaanqKmqsrO0tba3uLm6wsPExcbHyMnK0tPU1dbX2Nna4uPk5ebn6Onq8vP09fb3+Pn6/9oADAMBAAIRAxEAPwD5Dooor8LP9VD/2Q==", .w = 16, .h = 8, .rgb = .{ 200, 30, 30 }, .tolerance = 6 }, .{ .b64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAMEBgUGBgYFBgYGBwkIBgcJBwYGCAsICQoKCgoKBggLDAsKDAkKCgr/2wBDAQICAgICAgUDAwUKBwYHCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgr/wgARCAAIABADASIAAhEBAxEB/8QAFQABAQAAAAAAAAAAAAAAAAAAAAf/xAAVAQEBAAAAAAAAAAAAAAAAAAAGCP/aAAwDAQACEAMQAAABkIC1V//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAQUCP//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Bf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Bf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEABj8CP//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8hP//aAAwDAQACAAMAAAAQA//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQMBAT8Qf//EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAIAQIBAT8Qf//EABQQAQAAAAAAAAAAAAAAAAAAABD/2gAIAQEAAT8QP//Z", .w = 16, .h = 8, .rgb = .{ 200, 30, 30 }, .tolerance = 6 }, }; for (fixtures) |f| { const bytes = try gpa.alloc(u8, try dec.calcSizeForSlice(f.b64)); defer gpa.free(bytes); try dec.decode(bytes, f.b64); try std.testing.expect(readable(bytes)); const size = headerSize(bytes).?; try std.testing.expectEqual(Size{ .w = f.w, .h = f.h }, size); const got = decode(gpa, bytes) orelse return error.NotDecoded; defer gpa.free(got.rgba); try std.testing.expectEqual(f.w, got.w); try std.testing.expectEqual(f.h, got.h); for (got.rgba[0..3], f.rgb) |have, want| try std.testing.expect(@max(have, want) - @min(have, want) <= f.tolerance); try std.testing.expectEqual(@as(u8, 255), got.rgba[3]); } // A PPM still goes through stb_image. const ppm = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7"; try std.testing.expectEqual(Format.other, format(ppm)); const got = decode(gpa, ppm) orelse return error.NotDecoded; defer gpa.free(got.rgba); try std.testing.expectEqualSlices(u8, &.{ 0x0d, 0x4d, 0xe7, 255 }, got.rgba[0..4]); // Truncated headers are no image, never a read past the end. try std.testing.expect(headerSize("\x89PNG\r\n\x1a\n\x00\x00") == null); try std.testing.expect(headerSize("\xff\xd8\xff\xe0\x00\x10JFIF") == null); try std.testing.expect(decode(gpa, "\xff\xd8\xff\xe0\x00\x10JFIF") == null); } // ---- the image kind (kinds/pardes_addon.h), compiled in ---- /// 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, }); const exts = blk: { var all: []const u8 = ""; for (config.image_exts, 0..) |e, i| all = all ++ (if (i == 0) "" else " ") ++ e; break :blk all ++ ""; }; 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, }; 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]; } 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; } keep(st, raw); } 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)); } 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.*; // `img`, which the core follows with its art words and the path. @memcpy(o.prefix[0..config.tag_image.len], config.tag_image); o.prefix_len = config.tag_image.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, .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; }