From 1497cf19f852a5b38bc3e2f3641a81a077ba8a32 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Tue, 10 Nov 2026 11:30:01 -0300 Subject: zstbi: upstream's main, the fork pinned before being gone: Pivok7/zstbi (zig-gamedev/zstbi PR #16, the Zig 0.17 port the manifest waited on) is deleted and a fresh checkout could not fetch it, while upstream has since ported itself (6b9bea1f); its init takes no Io and can fail, and its resize can fail, so TerminalImages.start takes the allocator alone (a start refused leaves PNGs refused, as before one) and a resize that fails is an undecodable PNG; host_io.startWorkers and tape.replayMain lose the Io they only handed on; build.zig's patch of zstbi.c applies unchanged (the file is the same); test: kitty: a PNG longer than 1280 pixels on a side is kept at 1280 (the one path through zstbi, which no case walked: a 1300-pixel one-bit PNG built in the test, its row stored); kitty-test 45 of 45; the changelog says so --- test/kitty.zig | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 55 insertions(+), 1 deletion(-) (limited to 'test/kitty.zig') diff --git a/test/kitty.zig b/test/kitty.zig index ad70f453..ac407e1a 100644 --- a/test/kitty.zig +++ b/test/kitty.zig @@ -66,7 +66,7 @@ const Rig = struct { fn init(shells: u8) !Rig { // f=100 decodes through wuffs, its resize through stb_image_resize, // which the shells start at launch. - pardes.TerminalImages.start(std.testing.io, gpa); + pardes.TerminalImages.start(gpa); // ghostty warns of every refused command; the refusals are the point. std.testing.log_level = .err; // A core boots one shell or the classic three. @@ -211,6 +211,60 @@ test "kitty: quiet levels: q=1 drops the OK, q=2 the errors too" { } } +/// A one-row, one-bit grayscale PNG `width` pixels wide, its row stored +/// uncompressed: a few hundred bytes however wide. +fn widePng(arena: std.mem.Allocator, width: u32) ![]const u8 { + var out: std.ArrayList(u8) = .empty; + try out.appendSlice(arena, "\x89PNG\r\n\x1a\n"); + const chunk = struct { + fn add(list: *std.ArrayList(u8), a: std.mem.Allocator, kind: *const [4]u8, data: []const u8) !void { + var word: [4]u8 = undefined; + std.mem.writeInt(u32, &word, @intCast(data.len), .big); + try list.appendSlice(a, &word); + const from = list.items.len; + try list.appendSlice(a, kind); + try list.appendSlice(a, data); + std.mem.writeInt(u32, &word, std.hash.Crc32.hash(list.items[from..]), .big); + try list.appendSlice(a, &word); + } + }.add; + var ihdr: [13]u8 = .{ 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0 }; + std.mem.writeInt(u32, ihdr[0..4], width, .big); + try chunk(&out, arena, "IHDR", &ihdr); + // The filter byte and the row, in one stored deflate block. + const row = try arena.alloc(u8, 1 + (width + 7) / 8); + @memset(row, 0); + var idat: std.ArrayList(u8) = .empty; + try idat.appendSlice(arena, &.{ 0x78, 0x01, 0x01 }); + var len: [2]u8 = undefined; + std.mem.writeInt(u16, &len, @intCast(row.len), .little); + try idat.appendSlice(arena, &len); + std.mem.writeInt(u16, &len, ~@as(u16, @intCast(row.len)), .little); + try idat.appendSlice(arena, &len); + try idat.appendSlice(arena, row); + var sum: [4]u8 = undefined; + std.mem.writeInt(u32, &sum, std.hash.Adler32.hash(row), .big); + try idat.appendSlice(arena, &sum); + try chunk(&out, arena, "IDAT", idat.items); + try chunk(&out, arena, "IEND", ""); + return out.items; +} + +test "kitty: a PNG longer than 1280 pixels on a side is kept at 1280" { + var r = try Rig.init(1); + defer r.deinit(); + const arena = r.frame.allocator(); + const png = try widePng(arena, 1300); + const payload = try arena.alloc(u8, std.base64.standard.Encoder.calcSize(png.len)); + _ = std.base64.standard.Encoder.encode(payload, png); + r.feed(0, try std.fmt.allocPrint(arena, "\x1b_Ga=T,i=1,f=100,c=2,r=1;{s}\x1b\\", .{payload})); + try std.testing.expectEqualStrings(reply("i=1", "OK"), try r.take(0)); + // Decoded by wuffs at 1300, brought down by stb_image_resize (zstbi). + const img = r.storage(0).imageById(1) orelse return error.NotStored; + try std.testing.expectEqual(@as(u32, 1280), img.width); + try std.testing.expectEqual(@as(u32, 1), img.height); +} + test "kitty: RGB, RGBA, PNG and zlib-compressed RGB all draw the same pixels" { const Case = struct { control: []const u8, payload: []const u8 }; const cases = [_]Case{ -- cgit v1.3