1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
|
//! Image panes. Decoding is zstbi (C stb_image) to RGBA, downscaled; the kept
//! pixels render as PETSCII glyph art directly into the Surface — or, when the
//! host supports native images (and PETSCII isn't toggled), ride the Surface as
//! a pixel attachment the shell transmits/places (TTY: Kitty; SDL: GPU texture).
const std = @import("std");
const zstbi = @import("zstbi");
const ghostty_vt = @import("ghostty-vt");
pub const petscii = @import("petscii.zig");
/// the terminal's own 16 ANSI colors — what the `terminal` palette mode
/// scores against; cells then paint as indexed colors so the real terminal
/// resolves the RGB
pub fn ansiPalette() [16][3]u8 {
var p: [16][3]u8 = undefined;
for (0..16) |i| {
const c = ghostty_vt.color.default[i];
p[i] = .{ c.r, c.g, c.b };
}
return p;
}
/// 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 = enum { commodore, terminal };
/// Fit an image inside a pixel rectangle without changing its aspect ratio or
/// enlarging it. Native SDL uses this for the same "contain" policy the kitty
/// placement uses. Keeping the arithmetic here makes the backend geometry
/// testable without a GPU or an image-capable terminal.
pub const Fit = struct { w: u32, h: u32 };
pub fn contain(iw: usize, ih: usize, max_w: u32, max_h: u32) Fit {
if (iw == 0 or ih == 0 or max_w == 0 or max_h == 0) return .{ .w = 0, .h = 0 };
const src_w: u64 = @intCast(iw);
const src_h: u64 = @intCast(ih);
const bound_w: u64 = max_w;
const bound_h: u64 = max_h;
if (src_w <= bound_w and src_h <= bound_h)
return .{ .w = @intCast(src_w), .h = @intCast(src_h) };
// Compare the two scale ratios without floating point. Whichever bound is
// tighter determines one exact dimension; the other is rounded down so it
// can never leak a pixel outside the pane body.
if (bound_w * src_h <= bound_h * src_w) {
return .{
.w = max_w,
.h = @intCast(@max(1, src_h * bound_w / src_w)),
};
}
return .{
.w = @intCast(@max(1, src_w * bound_h / src_h)),
.h = max_h,
};
}
test "native image contain keeps aspect, bounds, and small-image size" {
try std.testing.expectEqual(Fit{ .w = 40, .h = 20 }, contain(400, 200, 40, 40));
try std.testing.expectEqual(Fit{ .w = 20, .h = 40 }, contain(200, 400, 40, 40));
try std.testing.expectEqual(Fit{ .w = 17, .h = 9 }, contain(17, 9, 40, 40));
try std.testing.expectEqual(Fit{ .w = 0, .h = 0 }, contain(17, 9, 0, 40));
// Awkward ratios stay inside both bounds rather than rounding one pixel
// over them.
const odd = contain(403, 211, 101, 47);
try std.testing.expect(odd.w <= 101 and odd.h <= 47);
}
/// 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);
}
pub fn stop() void {
zstbi.deinit();
}
/// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the
/// pane then simply shows a blank body.
pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?struct { rgba: []u8, w: usize, h: usize } {
if (bytes.len == 0) return null;
var img = zstbi.Image.loadFromMemory(bytes, 4) catch return null; // 4 = RGBA
defer img.deinit();
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 };
}
|