summaryrefslogtreecommitdiff
path: root/src/TerminalImages.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 05:19:21 -0300
committerGabriel Schneider <[email protected]>2026-11-06 05:55:49 -0300
commitc8553032ef84aec74cbb5d07d07f934eecf3d4a9 (patch)
treee47d3dd72b83e0b95d53271df9f5932dab389959 /src/TerminalImages.zig
parentde0447471febe1253b42a32fe9a1d6cdccc9d5b9 (diff)
downloadpardes-c8553032ef84aec74cbb5d07d07f934eecf3d4a9.tar.gz
pardes-c8553032ef84aec74cbb5d07d07f934eecf3d4a9.zip
kinds #9, the core's image code gone: src/image.zig (558 lines) and the static table go, so no kind is compiled in (Registry.init, Kind.image, compiledIn, `static` in /addons and "is compiled in" with them) and a test of the core's side of a kind's pane opens Kind.test_kind (a picture of one pixel, no library); a placement's fit (NativeGeometry, containGeometry) moves to surface.zig and a PDF's fit modes to pdf_view.zig; a terminal's kitty PNG decodes in TerminalImages.zig through wuffs as before, its downscale through stb_image_resize, the one part of zstbi the core keeps (stb_image patched out of it, the wuffs_images option, tag_image and image_exts gone); the image cases load the built image.so by path: test/panes.zig's and fs_namespace's through Addon.perform, T1 through Addon load (a new behaviour case: PNG, JPEG and PPM through 9P, the art words, a refused corrupt PNG, a dump's placeholder and its picture back, which replaces the decode unit test and ctl.zig's corrupt-image case), the snapshot, process and image harnesses by linking it into each session's data directory; images.snap unchanged
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/TerminalImages.zig')
-rw-r--r--src/TerminalImages.zig55
1 files changed, 45 insertions, 10 deletions
diff --git a/src/TerminalImages.zig b/src/TerminalImages.zig
index 0aee6b52..c352c2a4 100644
--- a/src/TerminalImages.zig
+++ b/src/TerminalImages.zig
@@ -10,11 +10,12 @@
const std = @import("std");
const ghostty_vt = @import("ghostty-vt");
const pardes = @import("pardes.zig");
-const image = @import("image.zig");
+const zstbi = @import("zstbi");
+const wuffs = @import("wuffs");
const TerminalImages = @This();
const gfx = ghostty_vt.kitty.graphics;
-const PixelRect = image.PixelRect;
+const PixelRect = @import("surface.zig").RectPx;
/// The bytes ghostty-vt may hold for one screen's images (it evicts the
/// oldest past this): a 4K picture in RGBA, with room for a second.
@@ -59,25 +60,25 @@ const PlaceholderRuns = struct {
continue;
}
var run: ?gfx.unicode.IncompletePlacement = null;
- var start: usize = 0;
+ var first: usize = 0;
const cells = row.cells(.all);
while (self.x < cells.len) : (self.x += 1) {
const cell = &cells[self.x];
if (cell.codepoint() != gfx.unicode.placeholder) {
- if (run) |done| return complete(row, start, done);
+ if (run) |done| return complete(row, first, done);
continue;
}
const style = row.style(cell);
const curr: gfx.unicode.IncompletePlacement = .init(&style, row.grapheme(cell) orelse &.{});
if (run) |*prev| {
- if (!prev.append(&curr)) return complete(row, start, prev.*);
+ if (!prev.append(&curr)) return complete(row, first, prev.*);
} else {
run = curr.start();
- start = self.x;
+ first = self.x;
}
}
self.advance();
- if (run) |done| return complete(row, start, done);
+ if (run) |done| return complete(row, first, done);
}
return null;
}
@@ -109,14 +110,48 @@ pub fn deinit(ti: *TerminalImages, gpa: std.mem.Allocator) void {
}
/// Accept PNG (`f=100`) as well as raw pixels: ghostty-vt built as a
-/// library decodes none itself. stb_image does it, as for image panes.
+/// library decodes none itself. wuffs does it (ghostty's, linked for it).
pub fn installPngDecoder() void {
ghostty_vt.sys.decode_png = &decodePng;
}
+/// The longest side a PNG is kept at: a pane is at most a screenful of
+/// cells, and a multi-thousand-pixel picture only costs its memory.
+const max_dim: u32 = 1280;
+
+/// stb_image_resize has its allocator (`start`): before, a PNG is refused.
+var started = false;
+
+/// Once at startup and exit, with what the resize allocates from.
+pub fn start(io: std.Io, gpa: std.mem.Allocator) void {
+ zstbi.init(io, gpa);
+ started = true;
+}
+pub fn stop() void {
+ started = false;
+ zstbi.deinit();
+}
+
fn decodePng(gpa: std.mem.Allocator, bytes: []const u8) ghostty_vt.sys.DecodeError!ghostty_vt.sys.Image {
- const decoded = image.decode(gpa, bytes) orelse return error.InvalidData;
- return .{ .width = @intCast(decoded.w), .height = @intCast(decoded.h), .data = decoded.rgba };
+ if (!started) return error.InvalidData;
+ // Bounded before decoding: a few KB of PNG can name gigapixels. The
+ // signature, then IHDR first: its length, type, width and height.
+ if (bytes.len < 24 or !std.mem.startsWith(u8, bytes, "\x89PNG\r\n\x1a\n") or !std.mem.eql(u8, bytes[12..16], "IHDR")) return error.InvalidData;
+ const w = std.mem.readInt(u32, bytes[16..20], .big);
+ const h = std.mem.readInt(u32, bytes[20..24], .big);
+ if (@as(u64, w) * h > pardes.memory.limits.image_decode_pixels) return error.InvalidData;
+ const got = wuffs.png.decode(gpa, bytes) catch return error.InvalidData;
+ if (got.data.len != @as(usize, got.width) * got.height * 4) {
+ gpa.free(got.data);
+ return error.InvalidData;
+ }
+ const longest = @max(got.width, got.height);
+ if (longest <= max_dim) return .{ .width = got.width, .height = got.height, .data = got.data };
+ defer gpa.free(got.data);
+ const img: zstbi.Image = .{ .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 };
+ var scaled = img.resize(@intCast(@max(1, @as(u64, got.width) * max_dim / longest)), @intCast(@max(1, @as(u64, got.height) * max_dim / longest)));
+ defer scaled.deinit();
+ return .{ .width = scaled.width, .height = scaled.height, .data = gpa.dupe(u8, scaled.data) catch return error.InvalidData };
}
/// Draw the active screen's images. `rows_of[v]` is the body row the