summaryrefslogtreecommitdiff
path: root/addons/image
diff options
context:
space:
mode:
Diffstat (limited to 'addons/image')
-rw-r--r--addons/image/image.zig30
1 files changed, 28 insertions, 2 deletions
diff --git a/addons/image/image.zig b/addons/image/image.zig
index f2303433..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) {
@@ -137,3 +147,19 @@ test "a job decodes a picture to its size and pixels, and one past max_dim is sc
try std.testing.expectEqual(8 + 1280 * 4, wide.len);
try std.testing.expect(Image.jobRun("not a picture", arena.allocator()) == null);
}
+
+test "a top-down BMP (its height negative in its header) opens at its size, as any picture" {
+ var arena: std.heap.ArenaAllocator = .init(std.testing.allocator);
+ defer arena.deinit();
+ // 2x2, 24 bits, biHeight -2: rows top first, each 6 bytes and 2 of padding.
+ const bmp = "BM" ++ "\x46\x00\x00\x00" ++ "\x00\x00\x00\x00" ++ "\x36\x00\x00\x00" ++
+ "\x28\x00\x00\x00" ++ "\x02\x00\x00\x00" ++ "\xfe\xff\xff\xff" ++ "\x01\x00" ++ "\x18\x00" ++
+ "\x00\x00\x00\x00" ++ "\x10\x00\x00\x00" ++ "\x13\x0b\x00\x00" ++ "\x13\x0b\x00\x00" ++ "\x00\x00\x00\x00" ++ "\x00\x00\x00\x00" ++
+ "\x00\x00\xff\x00\xff\x00\x00\x00" ++ "\xff\x00\x00\xff\xff\xff\x00\x00";
+ try std.testing.expectEqual([2]u32{ 2, 2 }, info(bmp).?);
+ const got = Image.jobRun(bmp, arena.allocator()).?;
+ try std.testing.expectEqual(8 + 2 * 2 * 4, got.len);
+ try std.testing.expectEqual(2, std.mem.readInt(u32, got[4..8], .little));
+ // Its first pixel is the top row's first: red (BGR 00 00 ff).
+ try std.testing.expectEqualSlices(u8, &.{ 0xff, 0, 0, 0xff }, got[8..12]);
+}