diff options
Diffstat (limited to 'src/image.zig')
| -rw-r--r-- | src/image.zig | 45 |
1 files changed, 40 insertions, 5 deletions
diff --git a/src/image.zig b/src/image.zig index 5797ce9e..19dbb9e4 100644 --- a/src/image.zig +++ b/src/image.zig @@ -1,5 +1,6 @@ //! Image decoding, pixel placement, and glyph approximation. const std = @import("std"); +const builtin = @import("builtin"); const zstbi = @import("zstbi"); /// The 16-colour ANSI table below is the only thing this file ever wanted from /// the emulator, and a build without one still renders images — see @@ -767,7 +768,9 @@ pub fn readable(bytes: []const u8) bool { /// 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 } { +const Decoded = struct { rgba: []u8, w: usize, h: usize }; + +pub fn decode(gpa: std.mem.Allocator, bytes: []const u8) ?Decoded { 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; @@ -812,8 +815,14 @@ pub const State = struct { raw: []u8 = &.{}, 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, + 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 (state.answer) |a| gpa.free(a.rgba); gpa.free(state.path); if (state.rgba.len > 0) gpa.free(state.rgba); if (state.raw.len > 0) gpa.free(state.raw); @@ -941,15 +950,41 @@ pub fn tagPrefix(arena: std.mem.Allocator, state: *const State, glyphs: bool) 