diff options
Diffstat (limited to 'src/TerminalImages.zig')
| -rw-r--r-- | src/TerminalImages.zig | 55 |
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 |
