diff options
| -rw-r--r-- | addons/image/image.zig | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/addons/image/image.zig b/addons/image/image.zig index 48135458..8e2cb32a 100644 --- a/addons/image/image.zig +++ b/addons/image/image.zig @@ -33,7 +33,14 @@ fn info(bytes: []const u8) ?[2]u32 { defer job = null; var w: c_int, var h: c_int = .{ 0, 0 }; if (bytes.len == 0 or bytes.len > std.math.maxInt(c_int) or pa_image_info(bytes.ptr, @intCast(bytes.len), &w, &h) == 0) return null; - return .{ @intCast(w), @intCast(h) }; + return size(w, h); +} + +/// stb's width and height as a size: a top-down BMP's height is negative in +/// its info (stb's loader takes the length of it); none, nothing to show. +fn size(w: c_int, h: c_int) ?[2]u32 { + if (w <= 0 or h == 0) return null; + return .{ @intCast(w), @abs(h) }; } /// Each pane's pixels in this process, by its block's address: a draw's @@ -83,9 +90,12 @@ pub const Image = extern struct { defer job = null; var w: c_int, var h: c_int = .{ 0, 0 }; if (in.len == 0 or in.len > std.math.maxInt(c_int) or pa_image_info(in.ptr, @intCast(in.len), &w, &h) == 0) return null; - const wh: [2]u32 = .{ @intCast(w), @intCast(h) }; + const wh = size(w, h) orelse return null; if (@as(u64, wh[0]) * wh[1] > max_pixels) return null; var px = pa_image_load(in.ptr, @intCast(in.len), &w, &h) orelse return null; + // The pixels are the load's: copied by its size, never info's alone. + const loaded = size(w, h) orelse return null; + if (loaded[0] != wh[0] or loaded[1] != wh[1]) return null; const longest: u64 = @max(wh[0], wh[1]); var nw, var nh = wh; if (longest > max_dim) { |
