summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-28 21:23:53 -0300
committerGabriel Schneider <[email protected]>2026-10-28 21:46:53 -0300
commit3ad8a8117435124d5df8c1d70de62c32dd97e113 (patch)
tree1a62696a04308a64f5122a17a8f0fabeae393677 /src
parentb250eb6135475699a945324c005890e8e1b4dd0b (diff)
downloadpardes-3ad8a8117435124d5df8c1d70de62c32dd97e113.tar.gz
pardes-3ad8a8117435124d5df8c1d70de62c32dd97e113.zip
one copy of each codec: PNG and JPEG through wuffs (ghostty's, already linked by ghostty-vt), stb_image without them, its writer and its inflate, SDL without its private stb_image; a symbol-table guard; docs/shared-libs.md
ReleaseFast before, both binaries carried stb_image's PNG, JPEG and inflate beside wuffs's (whose function tables keep its decoders linked whatever pardes calls), stb_image_write and its deflate (unused), and the GUI a second stb_image of SDL's (SDL_stb.c, for SDL_LoadPNG and camera MJPEG). Now: - image.zig decodes PNG and JPEG with wuffs (`wuffs_images`, wherever ghostty-vt is in the build), sized from the IHDR or the first SOF before decoding, as stb's info call sized them; other formats and the builds without ghostty-vt keep stb_image. - build.zig compiles zstbi with -DSTBI_NO_PNG -DSTBI_NO_JPEG and patches its stb_image_write include out (one count-checked anchor; a no-op stbi_flip_vertically_on_write stays for zstbi.deinit), and SDL with SDL_DISABLE_STB. Text: tty -74,488 bytes, gui -154,392. `zig build shared-libs-check` (in unit-test, ELF) reads the linked symbol table and fails on a family the build chose against: stb's PNG/JPEG/inflate beside wuffs, a second stb_image, stb_image_write's deflate, a second zlib or FreeType. Checked against the old binaries: it names every one. docs/shared-libs.md: the inventory (implementation, users, copies and bytes before and after, allocator hook) and where one copy was not possible: zlib and wuffs both inflate (FreeType's and MuPDF's zlib API vs ghostty's kitty o=z), MuPDF's libjpeg beside wuffs (filter-dct.c is libjpeg's API), MuPDF's unreached fitz image loaders, base64 in Zig std/simdutf/MuPDF, per-library Unicode code, std.debug's flate. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/CHANGELOG.md8
-rw-r--r--src/image.zig135
2 files changed, 138 insertions, 5 deletions
diff --git a/src/CHANGELOG.md b/src/CHANGELOG.md
index 7aab597d..4466ad26 100644
--- a/src/CHANGELOG.md
+++ b/src/CHANGELOG.md
@@ -2,6 +2,14 @@
## Unreleased
+- One PNG and one JPEG decoder in a binary: images decode through wuffs,
+ the decoders ghostty uses (ghostty-vt already links them), and stb_image
+ keeps only the other formats. stb's own inflate and image writer go, as
+ does SDL's private stb_image, which pardes never called: 73 KB less code
+ in the tty binary, 151 KB in the GUI. `zig build shared-libs-check` (run
+ with unit-test) fails if a second copy of one of them comes back;
+ docs/shared-libs.md lists what is shared and where one copy was not
+ possible.
- Kitty graphics in a terminal pane take every medium kitty and ghostty
do: a file (`t=f`), a temporary file (`t=t`) and shared memory (`t=s`)
beside direct data, so `kitten icat` no longer falls back to base64
diff --git a/src/image.zig b/src/image.zig
index 5ceadd76..e334965a 100644
--- a/src/image.zig
+++ b/src/image.zig
@@ -756,6 +756,71 @@ 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;
+/// 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 (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) 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;
@@ -763,6 +828,7 @@ pub fn readable(bytes: []const u8) bool {
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 stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &w, &h, &c) == 1;
}
@@ -776,13 +842,33 @@ pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded {
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.
- var iw: c_int = 0;
- var ih: c_int = 0;
- var ic: c_int = 0;
- if (stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), &iw, &ih, &ic) != 1) return null;
- if (@as(usize, @intCast(@max(0, iw))) * @as(usize, @intCast(@max(0, ih))) > @import("memory.zig").limits.image_decode_pixels) return null;
+ 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);
@@ -796,6 +882,45 @@ pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded {
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 pane ----
const GridKey = struct {