summaryrefslogtreecommitdiff
path: root/src/image.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/image.zig')
-rw-r--r--src/image.zig22
1 files changed, 16 insertions, 6 deletions
diff --git a/src/image.zig b/src/image.zig
index 8f193674..9163730d 100644
--- a/src/image.zig
+++ b/src/image.zig
@@ -755,6 +755,13 @@ 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;
+/// stb_image's header reader; a freestanding build has no stb_image (its
+/// decoder is the shim in src/web/zstbi.zig), so no header reads as one.
+fn stbiInfo(bytes: []const u8, w: *c_int, h: *c_int, c: *c_int) bool {
+ if (comptime !pardes.hosted) return false;
+ return stbi_info_from_memory(bytes.ptr, @intCast(bytes.len), w, h, c) == 1;
+}
+
/// 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
@@ -787,7 +794,7 @@ fn headerSize(bytes: []const u8) ?Size {
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 (!stbiInfo(bytes, &iw, &ih, &ic)) return null;
return .{ .w = @intCast(@max(0, iw)), .h = @intCast(@max(0, ih)) };
}
@@ -828,7 +835,7 @@ pub fn readable(bytes: []const u8) bool {
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;
+ return stbiInfo(bytes, &w, &h, &c);
}
/// decode + downscale to RGBA, gpa-owned. Returns null on any failure — the
@@ -929,6 +936,9 @@ const GridKey = struct {
ascii: bool = true,
};
+/// A host's thread; a freestanding build has none to have.
+const Decoder = if (pardes.hosted) std.Thread else void;
+
pub const State = struct {
/// What the replay digest leaves out (src/walk.zig): the picture as
/// decoded from its bytes, off the loop as the render first wants it,
@@ -947,12 +957,12 @@ pub const State = struct {
grid: GlyphArt.Grid = .{ .cells = &.{}, .cols = 0, .rows = 0 },
grid_key: GridKey = .{},
/// The decode under way off the loop (ensureDecoded), and its answer.
- decoder: ?std.Thread = null,
+ decoder: ?Decoder = null,
decoded: std.atomic.Value(bool) = .init(false),
answer: ?Decoded = null,
pub fn deinit(state: *State, gpa: std.mem.Allocator) void {
- if (state.decoder) |t| t.join();
+ if (comptime pardes.hosted) if (state.decoder) |t| t.join();
if (state.answer) |a| gpa.free(a.rgba);
gpa.free(state.path);
if (state.rgba.len > 0) gpa.free(state.rgba);
@@ -1026,11 +1036,11 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) ![
/// docs' screenshot: all of it once on the frame before).
/// ponytail: a thread a picture; a pool if many open at once.
fn ensureDecoded(p: *pardes.Pardes, state: *State) void {
- if (state.decoder) |t| {
+ if (comptime pardes.hosted) if (state.decoder) |t| {
if (!state.decoded.load(.acquire)) return;
t.join();
state.decoder = null;
- }
+ };
if (state.answer) |decoded| {
state.answer = null;
state.rgba = decoded.rgba;