// SPDX-FileCopyrightText: © 2026 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! Animation: frames, the canvas, and the state machine between them. //! //! The GIFs here are assembled from named pieces rather than checked in as //! blobs, so that each test says which field it is about. The LZW payloads //! are the one part that cannot be written by hand and read back: they were //! generated by a small encoder and cross-checked against wuffs' own //! `artificial-gif` files, whose one-pixel payload is byte for byte the //! `one_px_1` below. //! //! The animations with an outside opinion about them --- real files, with //! wuffs' composited frames and durations beside them --- are in `corpus/`, //! which fetches them rather than vendoring them. These are the cases no //! corpus happens to contain. const std = @import("std"); const Io = std.Io; const mem = std.mem; const testing = std.testing; const Allocator = mem.Allocator; const z2d = @import("z2d"); const z2dimg = @import("z2dimg"); const Rgba8 = z2dimg.pixels.Rgba8; const transparent: Rgba8 = .{ .r = 0, .g = 0, .b = 0, .a = 0 }; const red: Rgba8 = .rgb(255, 0, 0); const green: Rgba8 = .rgb(0, 255, 0); const blue: Rgba8 = .rgb(0, 0, 255); const white: Rgba8 = .rgb(255, 255, 255); /// A 2x2 screen with a four-colour global palette: red, green, blue, white. const screen_2x2 = "GIF89a" ++ "\x02\x00\x02\x00" ++ "\x91" ++ "\x00\x00" ++ "\xff\x00\x00" ++ "\x00\xff\x00" ++ "\x00\x00\xff" ++ "\xff\xff\xff"; /// A 4x1 screen with the same palette. const screen_4x1 = "GIF89a" ++ "\x04\x00\x01\x00" ++ "\x91" ++ "\x00\x00" ++ "\xff\x00\x00" ++ "\x00\xff\x00" ++ "\x00\x00\xff" ++ "\xff\xff\xff"; // LZW payloads: the minimum code size, one sub-block, and the terminator. const four_px_0 = "\x02\x03\x04\x00\x05\x00"; const four_px_1 = "\x02\x03\x4c\x12\x05\x00"; const one_px_0 = "\x02\x02\x44\x01\x00"; const one_px_1 = "\x02\x02\x4c\x01\x00"; const one_px_2 = "\x02\x02\x54\x01\x00"; const one_px_3 = "\x02\x02\x5c\x01\x00"; const two_px_0 = "\x02\x02\x04\x0a\x00"; /// A graphic control extension: how long, what to do afterwards, and which /// index shows through. /// /// These return pointers to sized arrays rather than slices so that `++` /// knows their lengths and the whole file can be assembled at compile time, /// which is what lets a test read as the structure it is testing. fn control(comptime delay_cs: u16, comptime dispose: u8, comptime clear: ?u8) *const [8]u8 { const packed_fields: u8 = (dispose << 2) | (if (clear != null) @as(u8, 1) else 0); return "\x21\xf9\x04" ++ [_]u8{packed_fields} ++ [_]u8{ delay_cs & 0xff, delay_cs >> 8 } ++ [_]u8{clear orelse 0} ++ "\x00"; } /// An image descriptor and its data. fn image( comptime x: u16, comptime y: u16, comptime w: u16, comptime h: u16, comptime data: []const u8, ) *const [10 + data.len]u8 { return "\x2c" ++ [_]u8{ x & 0xff, x >> 8, y & 0xff, y >> 8 } ++ [_]u8{ w & 0xff, w >> 8, h & 0xff, h >> 8 } ++ "\x00" ++ data[0..data.len]; } /// The NETSCAPE loop-count extension, whose value excludes the first play. fn netscape(comptime repeats: u16) *const [19]u8 { return "\x21\xff\x0bNETSCAPE2.0\x03\x01" ++ [_]u8{ repeats & 0xff, repeats >> 8 } ++ "\x00"; } const trailer = "\x3b"; fn at(sfc: *const z2d.Surface, x: i32, y: i32) Rgba8 { return Rgba8.fromZ2d(sfc.getPixel(x, y).?); } test "a still image is an animation of one frame" { // Anything can be played, so a caller after "the frames of this file" // need not first ask what the file is. A BMP answers once and stops. const gpa = testing.allocator; var sfc: z2d.Surface = try .init(.image_surface_rgb, gpa, 3, 2); defer sfc.deinit(gpa); sfc.putPixel(0, 0, .{ .rgb = .{ .r = 10, .g = 20, .b = 30 } }); var list: std.ArrayList(u8) = .empty; defer list.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &list); defer list = w.toArrayList(); try z2dimg.encode(gpa, &sfc, &w.writer, .{ .bmp = .{} }); var r: Io.Reader = .fixed(w.written()); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const first = (try frames.next(gpa)).?; try testing.expectEqual(@as(u32, 0), first.index); try testing.expectEqual(@as(i32, 3), first.surface.getWidth()); try testing.expectEqual(@as(u32, 0), first.delay_ms); try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); const info = frames.info(); try testing.expectEqual(@as(u32, 1), info.loop_count); try testing.expectEqual(@as(?u32, 1), info.frames); } test "a frame is handed over un-composited, and the canvas composited" { // The whole point of returning both. Frame one is a single pixel in the // bottom right; on its own that is what the file stored, and on the // canvas it sits on top of frame zero. const gpa = testing.allocator; const bytes = screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ // all red image(1, 1, 1, 1, one_px_2) ++ // one blue pixel at (1,1) trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const first = (try frames.next(gpa)).?; try testing.expectEqual(@as(i32, 2), first.surface.getWidth()); try testing.expectEqual(@as(i32, 2), first.surface.getHeight()); for (0..2) |y| for (0..2) |x| { try testing.expectEqual(red, at(&first.surface, @intCast(x), @intCast(y))); }; const second = (try frames.next(gpa)).?; // Un-composited: one pixel, and nothing of the frame before it. try testing.expectEqual(@as(i32, 1), second.surface.getWidth()); try testing.expectEqual(@as(i32, 1), second.surface.getHeight()); try testing.expectEqual(@as(i32, 1), second.x); try testing.expectEqual(@as(i32, 1), second.y); try testing.expectEqual(blue, at(&second.surface, 0, 0)); // Composited: three red and the blue one drawn over it. const canvas = frames.canvasSurface().?; try testing.expectEqual(red, at(canvas, 0, 0)); try testing.expectEqual(red, at(canvas, 1, 0)); try testing.expectEqual(red, at(canvas, 0, 1)); try testing.expectEqual(blue, at(canvas, 1, 1)); } test "disposal decides what the next frame is drawn onto" { const gpa = testing.allocator; // `keep`: the pixel stays. { const bytes = screen_2x2 ++ control(0, 1, null) ++ image(0, 0, 2, 2, four_px_0) ++ control(0, 1, null) ++ image(0, 0, 1, 1, one_px_2) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); _ = try frames.next(gpa); try testing.expectEqual(blue, at(frames.canvasSurface().?, 0, 0)); try testing.expectEqual(red, at(frames.canvasSurface().?, 1, 1)); } // `background`: the first frame's rectangle is cleared once it has been // shown, so the second frame lands on transparency rather than on red. { const bytes = screen_2x2 ++ control(0, 2, null) ++ image(0, 0, 1, 1, one_px_0) ++ control(0, 1, null) ++ image(1, 1, 1, 1, one_px_2) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); try testing.expectEqual(red, at(frames.canvasSurface().?, 0, 0)); _ = try frames.next(gpa); // Cleared to transparent. wuffs' default, whose opt-in quirk for // honouring the declared background colour describes what it departs // from as "always being transparent black"; Pillow is the one that // fills with the palette entry instead. try testing.expectEqual(transparent, at(frames.canvasSurface().?, 0, 0)); try testing.expectEqual(blue, at(frames.canvasSurface().?, 1, 1)); } // `previous`: what was under the frame comes back, which is not the same // as clearing it --- here there is a red pixel underneath to restore. { const bytes = screen_2x2 ++ control(0, 1, null) ++ image(0, 0, 2, 2, four_px_0) ++ // all red control(0, 3, null) ++ image(0, 0, 1, 1, one_px_2) ++ // blue, undone control(0, 1, null) ++ image(1, 0, 1, 1, one_px_3) ++ // white elsewhere trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); _ = try frames.next(gpa); try testing.expectEqual(blue, at(frames.canvasSurface().?, 0, 0)); _ = try frames.next(gpa); // Back to the red that was under it, not cleared and not left blue. // Pillow decodes this same file to exactly these pixels, which is // where the expectation came from rather than from this decoder. try testing.expectEqual(red, at(frames.canvasSurface().?, 0, 0)); try testing.expectEqual(white, at(frames.canvasSurface().?, 1, 0)); } } test "disposal is applied after the frame it belongs to, not before" { // The ordering that is easy to get backwards. A frame asking for // `background` must still be *visible* while it is the current frame; the // clearing happens on the way to the next one. A decoder that disposed // eagerly would show a blank canvas here. const gpa = testing.allocator; const bytes = screen_2x2 ++ control(0, 2, null) ++ image(0, 0, 2, 2, four_px_0) ++ control(0, 1, null) ++ image(0, 0, 1, 1, one_px_2) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); // Still there. This is the assertion the eager version fails. try testing.expectEqual(red, at(frames.canvasSurface().?, 1, 1)); _ = try frames.next(gpa); try testing.expectEqual(blue, at(frames.canvasSurface().?, 0, 0)); try testing.expectEqual(transparent, at(frames.canvasSurface().?, 1, 1)); } test "a GIF loop count excludes the first play, and an absent one means once" { const gpa = testing.allocator; const cases = [_]struct { []const u8, u32 }{ // No extension at all: played once. Reported as one, not as zero, // which would mean forever. .{ screen_2x2 ++ image(0, 0, 1, 1, one_px_0) ++ trailer, 1 }, // A declared zero does mean forever. .{ screen_2x2 ++ netscape(0) ++ image(0, 0, 1, 1, one_px_0) ++ trailer, 0 }, // And a declared two means three plays: the file counts the repeats // after the first. Checked against wuffs, whose NIA footer says three // for a GIF whose extension says two. .{ screen_2x2 ++ netscape(2) ++ image(0, 0, 1, 1, one_px_0) ++ trailer, 3 }, .{ screen_2x2 ++ netscape(1) ++ image(0, 0, 1, 1, one_px_0) ++ trailer, 2 }, }; for (cases) |case| { const bytes, const want = case; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); try testing.expectEqual(want, frames.info().loop_count); } } test "the delay is what the file said, in milliseconds" { const gpa = testing.allocator; const bytes = screen_2x2 ++ control(10, 1, null) ++ image(0, 0, 1, 1, one_px_0) ++ // 10 hundredths control(0, 1, null) ++ image(0, 0, 1, 1, one_px_1) ++ // and none at all trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); try testing.expectEqual(@as(u32, 100), (try frames.next(gpa)).?.delay_ms); // Zero is left as zero. Viewers substitute about a tenth of a second and // that is a viewer's policy about what looks right, not a fact about the // file. try testing.expectEqual(@as(u32, 0), (try frames.next(gpa)).?.delay_ms); } test "a transparent index shows the canvas through" { const gpa = testing.allocator; // Frame one covers the whole screen but its only colour is the index // declared transparent, so the red underneath survives all of it. const bytes = screen_2x2 ++ control(0, 1, null) ++ image(0, 0, 2, 2, four_px_0) ++ control(0, 1, 1) ++ image(0, 0, 2, 2, four_px_1) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); const second = (try frames.next(gpa)).?; // Un-composited, the frame really is transparent. try testing.expectEqual(transparent, at(&second.surface, 0, 0)); // Composited, it changed nothing. for (0..2) |y| for (0..2) |x| { try testing.expectEqual(red, at(frames.canvasSurface().?, @intCast(x), @intCast(y))); }; } test "compositing can be turned off, and then there is no canvas" { const gpa = testing.allocator; const bytes = screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ image(1, 1, 1, 1, one_px_2) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{ .composite = false }); defer frames.deinit(gpa); const first = (try frames.next(gpa)).?; try testing.expectEqual(red, at(&first.surface, 0, 0)); const second = (try frames.next(gpa)).?; try testing.expectEqual(blue, at(&second.surface, 0, 0)); // The frames are all there is. try testing.expectEqual(@as(?*const z2d.Surface, null), frames.canvasSurface()); } test "a frame is clipped to the screen, and says what it really asked for" { const gpa = testing.allocator; // A 4x1 frame on a 2x2 screen, starting one pixel in: three of its four // pixels are off the right edge, so one is kept. const bytes = screen_2x2 ++ image(1, 0, 4, 1, four_px_1) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const frame = (try frames.next(gpa)).?; // The surface holds what landed. try testing.expectEqual(@as(i32, 1), frame.surface.getWidth()); // And the declared rectangle says what the file actually asked for, // which is the only place that survives. try testing.expectEqual(@as(u32, 4), frame.width); try testing.expectEqual(@as(u32, 1), frame.height); try testing.expectEqual(green, at(frames.canvasSurface().?, 1, 0)); } test "too many frames, and too many pixels over all of them" { const gpa = testing.allocator; const many = screen_4x1 ++ image(0, 0, 1, 1, one_px_0) ++ image(0, 0, 1, 1, one_px_1) ++ image(0, 0, 1, 1, one_px_2) ++ image(0, 0, 1, 1, one_px_3) ++ trailer; // A frame costs ten bytes to declare and everything to decode, so the // count is where the leverage is. { var r: Io.Reader = .fixed(many); var frames = try z2dimg.decodeAnimation(gpa, &r, .{ .limits = .{ .max_frames = 2 } }); defer frames.deinit(gpa); _ = try frames.next(gpa); _ = try frames.next(gpa); try testing.expectError(error.ImageTooLarge, frames.next(gpa)); } // And the pixels summed over every frame, which is the budget a // still-image `max_pixels` was never asked to cover. { var r: Io.Reader = .fixed(many); var frames = try z2dimg.decodeAnimation(gpa, &r, .{ .limits = .{ .max_total_pixels = 3 } }); defer frames.deinit(gpa); _ = try frames.next(gpa); _ = try frames.next(gpa); _ = try frames.next(gpa); try testing.expectError(error.ImageTooLarge, frames.next(gpa)); } } test "a frame kept past the next one has to be copied" { const gpa = testing.allocator; const bytes = screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ image(0, 0, 1, 1, one_px_2) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const first = (try frames.next(gpa)).?; var kept = try first.toOwned(gpa); defer kept.deinit(gpa); _ = try frames.next(gpa); // the borrowed surface is gone by now try testing.expectEqual(red, at(&kept, 0, 0)); try testing.expectEqual(@as(i32, 2), kept.getWidth()); } test "an empty animation and a truncated one" { const gpa = testing.allocator; // A screen and a trailer, and nothing between them. { var r: Io.Reader = .fixed(screen_2x2 ++ trailer); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); } // Stopping before the trailer is truncation, and what arrived is kept. { var r: Io.Reader = .fixed(screen_2x2 ++ image(0, 0, 2, 2, four_px_0)); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = (try frames.next(gpa)).?; try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); } } test "the first frame of an animation is what the still decoder returns" { // The two entry points share their machinery, and this is the claim that // says so: a plain `decode` of an animated GIF is its canvas after one // frame, exactly. const gpa = testing.allocator; const bytes = screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ image(0, 0, 1, 1, one_px_2) ++ trailer; var r1: Io.Reader = .fixed(bytes); var still = try z2dimg.decode(gpa, &r1, .{}); defer still.deinit(gpa); var r2: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r2, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); for (0..2) |y| for (0..2) |x| { try testing.expectEqual( at(&still, @intCast(x), @intCast(y)), at(frames.canvasSurface().?, @intCast(x), @intCast(y)), ); }; } test "two pixels of a wider frame, to prove the row buffer is the clip" { const gpa = testing.allocator; var r: Io.Reader = .fixed(screen_4x1 ++ image(2, 0, 2, 1, two_px_0) ++ trailer); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const frame = (try frames.next(gpa)).?; try testing.expectEqual(@as(i32, 2), frame.surface.getWidth()); try testing.expectEqual(red, at(frames.canvasSurface().?, 2, 0)); try testing.expectEqual(red, at(frames.canvasSurface().?, 3, 0)); try testing.expectEqual(transparent, at(frames.canvasSurface().?, 0, 0)); } test "an animation can be played by a format named from outside" { // The counterpart of `decodeAs`: a caller who already knows what the // stream is, from a media type it trusts. const gpa = testing.allocator; const bytes = screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimationAs(gpa, .gif, &r, .{}); defer frames.deinit(gpa); const frame = (try frames.next(gpa)).?; try testing.expectEqual(red, at(&frame.surface, 0, 0)); } // -- APNG -------------------------------------------------------------------- // // Generated by a small Python builder and checked against Pillow before being // pasted here: Pillow reads `apng_two_frames` as two frames of 100ms and // 50ms with the pixels asserted below, which is where those expectations came // from rather than from this decoder. The builder is in the session notes; // these are small enough to read as the chunk sequences they are. /// Two frames: an `fcTL` before the `IDAT`, so the default image is frame /// zero, and one `fdAT` frame of a single blue pixel at (1,1). The second /// frame's delay denominator is zero, which means hundredths of a second. const apng_two_frames = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x08, 0x61, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0x6a, 0x84, 0xc2, 0xca, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xe8, 0x54, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x44, 0x0c, 0x10, 0x0a, 0x00, 0x1f, 0xee, 0x03, 0xfd, 0x8b, 0x5f, 0x14, 0xd4, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x5e, 0xe2, 0x2d, 0x66, 0x00, 0x00, 0x00, 0x10, 0x66, 0x64, 0x41, 0x54, 0x00, 0x00, 0x00, 0x02, 0x78, 0x9c, 0x63, 0x60, 0x60, 0xf8, 0x0f, 0x00, 0x01, 0x03, 0x01, 0x00, 0x68, 0xbf, 0xed, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; /// The same file with a gap in the sequence numbers. const apng_sequence_gap = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x08, 0x61, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xf3, 0x8d, 0x93, 0x70, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xe8, 0x54, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x44, 0x0c, 0x10, 0x0a, 0x00, 0x1f, 0xee, 0x03, 0xfd, 0x8b, 0x5f, 0x14, 0xd4, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xb7, 0xe9, 0xe3, 0x39, 0x00, 0x00, 0x00, 0x10, 0x66, 0x64, 0x41, 0x54, 0x00, 0x00, 0x00, 0x03, 0x78, 0x9c, 0x63, 0x60, 0x60, 0xf8, 0x0f, 0x00, 0x01, 0x03, 0x01, 0x00, 0xb5, 0x29, 0x34, 0x71, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; /// `acTL` declaring three frames in a file that has two. const apng_count_mismatch = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x08, 0x61, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x00, 0xce, 0xed, 0xba, 0xc0, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xe8, 0x54, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x44, 0x0c, 0x10, 0x0a, 0x00, 0x1f, 0xee, 0x03, 0xfd, 0x8b, 0x5f, 0x14, 0xd4, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xc1, 0x0c, 0xda, 0x04, 0x00, 0x00, 0x00, 0x10, 0x66, 0x64, 0x41, 0x54, 0x00, 0x00, 0x00, 0x02, 0x78, 0x9c, 0x63, 0x60, 0x60, 0xf8, 0x0f, 0x00, 0x01, 0x03, 0x01, 0x00, 0x68, 0xbf, 0xed, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; /// A frame two pixels wide starting one pixel in, on a canvas two wide. const apng_frame_outside = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x08, 0x61, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xf3, 0x8d, 0x93, 0x70, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xe8, 0x54, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x44, 0x0c, 0x10, 0x0a, 0x00, 0x1f, 0xee, 0x03, 0xfd, 0x8b, 0x5f, 0x14, 0xd4, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xa3, 0xd4, 0x83, 0x6d, 0x00, 0x00, 0x00, 0x11, 0x66, 0x64, 0x41, 0x54, 0x00, 0x00, 0x00, 0x02, 0x78, 0x9c, 0x63, 0x60, 0x60, 0xf8, 0x0f, 0x44, 0x00, 0x05, 0x02, 0x01, 0xff, 0xd2, 0x27, 0x38, 0xe6, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; /// A `dispose_op` of seven, where the format defines nought to two. const apng_bad_dispose = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x08, 0x61, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0xf3, 0x8d, 0x93, 0x70, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x07, 0x00, 0xa7, 0x15, 0x4a, 0xc7, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x44, 0x0c, 0x10, 0x0a, 0x00, 0x1f, 0xee, 0x03, 0xfd, 0x8b, 0x5f, 0x14, 0xd4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; /// Not an APNG at all. const png_still = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x0f, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0x60, 0xf8, 0xcf, 0x00, 0x42, 0x10, 0x0a, 0x00, 0x1b, 0xf2, 0x03, 0xfd, 0x6d, 0x54, 0xb8, 0xc7, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; test "an APNG whose default image is its first frame" { const gpa = testing.allocator; var r: Io.Reader = .fixed(&apng_two_frames); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); // An `fcTL` before the `IDAT` is the only thing that says the default // image is part of the animation, so this is the assertion that the // ordering is read at all. const first = (try frames.next(gpa)).?; try testing.expectEqual(@as(i32, 2), first.surface.getWidth()); try testing.expectEqual(@as(u32, 100), first.delay_ms); try testing.expectEqual(red, at(&first.surface, 0, 0)); const second = (try frames.next(gpa)).?; try testing.expectEqual(@as(i32, 1), second.surface.getWidth()); try testing.expectEqual(@as(i32, 1), second.x); try testing.expectEqual(@as(i32, 1), second.y); // A delay denominator of zero means hundredths of a second, which the // specification says outright --- so the obvious division is also a // division by zero on a value real files carry. Pillow reads this frame // as fifty milliseconds too. try testing.expectEqual(@as(u32, 50), second.delay_ms); try testing.expectEqual(blue, at(&second.surface, 0, 0)); const canvas = frames.canvasSurface().?; try testing.expectEqual(red, at(canvas, 0, 0)); try testing.expectEqual(blue, at(canvas, 1, 1)); try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); // APNG counts plays directly, so nothing is converted the way a GIF's // count must be. try testing.expectEqual(@as(u32, 3), frames.info().loop_count); try testing.expectEqual(@as(?u32, 2), frames.info().frames); } test "an ordinary PNG plays as one frame" { const gpa = testing.allocator; var r: Io.Reader = .fixed(&png_still); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const only = (try frames.next(gpa)).?; try testing.expectEqual(green, at(&only.surface, 0, 0)); try testing.expectEqual(@as(u32, 0), only.delay_ms); try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); try testing.expectEqual(@as(u32, 1), frames.info().loop_count); } test "APNG files that contradict themselves" { const gpa = testing.allocator; // A gap in the sequence numbers. They are the only thing tying a frame's // data to the header describing it, so a file that can renumber them is a // file that can make a decoder draw one frame's pixels into another // frame's rectangle. { var r: Io.Reader = .fixed(&apng_sequence_gap); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); // the default image is fine try testing.expectError(error.InvalidData, frames.next(gpa)); } // `acTL` declaring more frames than arrived. Noticed at the end, which is // the only place it can be. { var r: Io.Reader = .fixed(&apng_count_mismatch); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); _ = try frames.next(gpa); try testing.expectError(error.InvalidData, frames.next(gpa)); } // A frame hanging off the edge of the canvas. APNG forbids this outright, // unlike GIF, where files that break the same rule are common enough that // this library clips instead of refusing. { var r: Io.Reader = .fixed(&apng_frame_outside); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); try testing.expectError(error.InvalidData, frames.next(gpa)); } // A disposal value the format does not define. APNG has three of three // specified, so a fourth is a file that is wrong rather than a file that // is newer than this decoder --- which is why this is refused where GIF's // undefined disposal values are quietly read as "leave it". { var r: Io.Reader = .fixed(&apng_bad_dispose); try testing.expectError( error.InvalidData, z2dimg.decodeAnimation(gpa, &r, .{}), ); } } test "a still PNG decodes the same through both entry points" { // The claim that the animated reader did not change what a plain PNG // means: every PNG now goes through it, so this is the assertion that // doing so is invisible. const gpa = testing.allocator; var r1: Io.Reader = .fixed(&png_still); var still = try z2dimg.decode(gpa, &r1, .{}); defer still.deinit(gpa); var r2: Io.Reader = .fixed(&png_still); var frames = try z2dimg.decodeAnimation(gpa, &r2, .{}); defer frames.deinit(gpa); _ = try frames.next(gpa); for (0..2) |y| for (0..2) |x| { try testing.expectEqual( at(&still, @intCast(x), @intCast(y)), at(frames.canvasSurface().?, @intCast(x), @intCast(y)), ); }; } test "a PNG with no IEND is refused by both entry points" { // PNG has a checksum on every chunk and an `IEND` to say the file is all // there, so a file that stops before one is detectably truncated and the // still decoder refuses it. The animated reader refuses it too: two entry // points disagreeing about whether the same file is broken is worse than // either answer. // // GIF is the other way round, deliberately. It has neither a checksum nor // any way to tell a truncated file from a short one, so it keeps what it // read --- which is asserted a few tests above. const gpa = testing.allocator; const chopped = png_still[0 .. png_still.len - 12]; var r1: Io.Reader = .fixed(chopped); const still = z2dimg.decode(gpa, &r1, .{}); var r2: Io.Reader = .fixed(chopped); var frames = try z2dimg.decodeAnimation(gpa, &r2, .{}); defer frames.deinit(gpa); const played = frames.next(gpa); // Both refuse, and with the same answer --- which is the claim. Which // error it is matters less than the two agreeing, since a caller deciding // whether a file is worth keeping should not get a different verdict for // asking a different way. try testing.expectError(error.EndOfStream, still); try testing.expectError(error.EndOfStream, played); } test "what a probe can say about animation, and what it cannot" { // Three answers, and the third is the point: "this does not animate" and // "this format animates and the header does not say" are different // things, and a null would have hidden the difference. const gpa = testing.allocator; // A PNG settles it either way, because `acTL` is required to precede // `IDAT` and a probe reads that far. { var r: Io.Reader = .fixed(&apng_two_frames); const info = try z2dimg.probe(&r, .{}); switch (info.animation) { .declared => |a| { try testing.expectEqual(@as(u32, 3), a.loop_count); try testing.expectEqual(@as(?u32, 2), a.frames); }, else => return error.ExpectedADeclaredAnimation, } } { var r: Io.Reader = .fixed(&png_still); const info = try z2dimg.probe(&r, .{}); try testing.expectEqual(z2dimg.Info.Animation.none, info.animation); } // A GIF cannot settle it. There is nowhere in the header for a frame // count, and the loop count lives in an extension that may sit anywhere, // so the honest answer is that the header does not know --- and it is the // same answer for a GIF that turns out to be a single frame as for one // that turns out to be forty. for ([_][]const u8{ screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ trailer, screen_2x2 ++ netscape(2) ++ image(0, 0, 2, 2, four_px_0) ++ image(0, 0, 1, 1, one_px_2) ++ trailer, }) |bytes| { var r: Io.Reader = .fixed(bytes); const info = try z2dimg.probe(&r, .{}); try testing.expectEqual(z2dimg.Info.Animation.unknown, info.animation); } // And a format with no animated form says so outright. { var sfc: z2d.Surface = try .init(.image_surface_rgb, gpa, 2, 2); defer sfc.deinit(gpa); var list: std.ArrayList(u8) = .empty; defer list.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &list); defer list = w.toArrayList(); try z2dimg.encode(gpa, &sfc, &w.writer, .{ .bmp = .{} }); var r: Io.Reader = .fixed(w.written()); const info = try z2dimg.probe(&r, .{}); try testing.expectEqual(z2dimg.Info.Animation.none, info.animation); } } test "a probe's declared animation is the one the frames turn out to be" { // The invariant worth having: where the header does commit to a number, // playing the file has to produce that number. A probe a caller trusted // and a decode that disagreed with it would be worse than a probe that // said nothing. const gpa = testing.allocator; var r: Io.Reader = .fixed(&apng_two_frames); const info = try z2dimg.probe(&r, .{}); const declared = switch (info.animation) { .declared => |a| a, else => return error.ExpectedADeclaredAnimation, }; var r2: Io.Reader = .fixed(&apng_two_frames); var frames = try z2dimg.decodeAnimation(gpa, &r2, .{}); defer frames.deinit(gpa); var seen: u32 = 0; while (try frames.next(gpa)) |_| seen += 1; try testing.expectEqual(declared.frames.?, seen); try testing.expectEqual(declared.loop_count, frames.info().loop_count); } /// The same two frames with `acTL` renamed to a private ancillary chunk, /// so nothing in the file declares an animation. Found by the fuzzer, which /// mutated the chunk type of the seed above. const apng_no_actl = [_]u8{ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x08, 0x02, 0x00, 0x00, 0x00, 0xfd, 0xd4, 0x9a, 0x73, 0x00, 0x00, 0x00, 0x08, 0x7a, 0x62, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x03, 0xd6, 0x80, 0x54, 0xf8, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x0a, 0x00, 0x00, 0xe8, 0x54, 0xdc, 0x00, 0x00, 0x00, 0x00, 0x10, 0x49, 0x44, 0x41, 0x54, 0x78, 0x9c, 0x63, 0xf8, 0xcf, 0xc0, 0x00, 0x44, 0x0c, 0x10, 0x0a, 0x00, 0x1f, 0xee, 0x03, 0xfd, 0x8b, 0x5f, 0x14, 0xd4, 0x00, 0x00, 0x00, 0x1a, 0x66, 0x63, 0x54, 0x4c, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x05, 0x00, 0x00, 0x00, 0x00, 0x5e, 0xe2, 0x2d, 0x66, 0x00, 0x00, 0x00, 0x10, 0x66, 0x64, 0x41, 0x54, 0x00, 0x00, 0x00, 0x02, 0x78, 0x9c, 0x63, 0x60, 0x60, 0xf8, 0x0f, 0x00, 0x01, 0x03, 0x01, 0x00, 0x68, 0xbf, 0xed, 0xf4, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82, }; test "frame chunks with no acTL are not frames" { // `acTL` is what makes a PNG an APNG. Without one, `fcTL` and `fdAT` are // ancillary chunks this decoder does not recognise --- exactly what they // are to a reader that never heard of APNG --- and they are skipped. // // Honouring them anyway made `probe` and `decodeAnimation` contradict // each other on the same file: the probe saw no `acTL` and said it does // not animate, and the decode played two frames out of it. The fuzzer // found that within a minute of the two being asserted together. const gpa = testing.allocator; var r: Io.Reader = .fixed(&apng_no_actl); const info = try z2dimg.probe(&r, .{}); try testing.expectEqual(z2dimg.Info.Animation.none, info.animation); var r2: Io.Reader = .fixed(&apng_no_actl); var frames = try z2dimg.decodeAnimation(gpa, &r2, .{}); defer frames.deinit(gpa); // One frame: the `IDAT`, with none of the timing the ignored `fcTL` // described. const only = (try frames.next(gpa)).?; try testing.expectEqual(@as(i32, 2), only.surface.getWidth()); try testing.expectEqual(@as(u32, 0), only.delay_ms); try testing.expectEqual(red, at(&only.surface, 0, 0)); try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); } /// Every frame of an animation, copied out so they outlive the decoder. const Collected = struct { frames: []anim.Frame, info: z2dimg.anim.Info, fn deinit(self: *Collected, gpa: Allocator) void { for (self.frames) |*f| f.surface.deinit(gpa); gpa.free(self.frames); } }; const anim = z2dimg.anim; fn collect(gpa: Allocator, bytes: []const u8) !Collected { var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); var list: std.ArrayList(anim.Frame) = .empty; errdefer { for (list.items) |*f| f.surface.deinit(gpa); list.deinit(gpa); } while (try frames.next(gpa)) |f| { var copy = f; copy.surface = try f.toOwned(gpa); try list.append(gpa, copy); } const info = frames.info(); return .{ .frames = try list.toOwnedSlice(gpa), .info = info }; } fn expectSameFrames(a: Collected, b: Collected) !void { try testing.expectEqual(a.frames.len, b.frames.len); for (a.frames, b.frames) |x, y| { try testing.expectEqual(x.x, y.x); try testing.expectEqual(x.y, y.y); try testing.expectEqual(x.delay_ms, y.delay_ms); try testing.expectEqual(x.dispose, y.dispose); try testing.expectEqual(x.blend, y.blend); try testing.expectEqual(x.surface.getWidth(), y.surface.getWidth()); try testing.expectEqual(x.surface.getHeight(), y.surface.getHeight()); for (0..@intCast(x.surface.getHeight())) |j| for (0..@intCast(x.surface.getWidth())) |i| { try testing.expectEqual( at(&x.surface, @intCast(i), @intCast(j)), at(&y.surface, @intCast(i), @intCast(j)), ); }; } } test "an APNG written from decoded frames decodes to the same frames" { // The claim that makes "honour the frames as given" worth having: an // animation through this library and out again is the same animation, // sub-rectangles and timing and disposal included, rather than a stack of // full-canvas pictures that happens to look the same. const gpa = testing.allocator; var first = try collect(gpa, &apng_two_frames); defer first.deinit(gpa); try testing.expectEqual(@as(usize, 2), first.frames.len); // The second frame really is a patch, which is the part a lossy // round trip would lose. try testing.expectEqual(@as(i32, 1), first.frames[1].surface.getWidth()); var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &out); defer out = w.toArrayList(); var enc = try z2dimg.png.encodeAnimation(&w.writer, .{ .width = 2, .height = 2, .frames = @intCast(first.frames.len), .loop_count = first.info.loop_count, }); for (first.frames) |f| try enc.frame(gpa, f); try enc.finish(); const written = w.written(); try testing.expectEqual(@as(?z2dimg.Format, .png), z2dimg.detect(written)); var again = try collect(gpa, written); defer again.deinit(gpa); try expectSameFrames(first, again); try testing.expectEqual(first.info.loop_count, again.info.loop_count); try testing.expectEqual(@as(?u32, 2), again.info.frames); } test "a declared frame count that does not come true is refused" { // The number goes into `acTL`, which precedes `IDAT` and cannot be // revised --- so an encoder that let it be wrong would write a file this // library's own decoder refuses. const gpa = testing.allocator; var collected = try collect(gpa, &apng_two_frames); defer collected.deinit(gpa); var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &out); defer out = w.toArrayList(); var enc = try z2dimg.png.encodeAnimation(&w.writer, .{ .width = 2, .height = 2, .frames = 3, // one more than will be given }); for (collected.frames) |f| try enc.frame(gpa, f); try testing.expectError(error.Unsupported, enc.finish()); // And one frame too many is refused as it arrives, rather than at the end. var out2: std.ArrayList(u8) = .empty; defer out2.deinit(gpa); var w2: Io.Writer.Allocating = .fromArrayList(gpa, &out2); defer out2 = w2.toArrayList(); var enc2 = try z2dimg.png.encodeAnimation(&w2.writer, .{ .width = 2, .height = 2, .frames = 1, }); try enc2.frame(gpa, collected.frames[0]); try testing.expectError(error.Unsupported, enc2.frame(gpa, collected.frames[1])); } test "an animated GIF written from decoded frames decodes to the same frames" { const gpa = testing.allocator; const bytes = screen_2x2 ++ netscape(2) ++ control(10, 2, null) ++ image(0, 0, 2, 2, four_px_0) ++ control(0, 1, null) ++ image(1, 1, 1, 1, one_px_2) ++ trailer; var first = try collect(gpa, bytes); defer first.deinit(gpa); try testing.expectEqual(@as(usize, 2), first.frames.len); try testing.expectEqual(@as(u32, 3), first.info.loop_count); // The second frame is a one-pixel patch, which is what a lossless pipe // has to keep and a naive one would expand to the whole screen. try testing.expectEqual(@as(i32, 1), first.frames[1].surface.getWidth()); try testing.expectEqual(anim.Dispose.background, first.frames[0].dispose); var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &out); defer out = w.toArrayList(); var enc = try z2dimg.gif.encodeAnimation(&w.writer, .{ .width = 2, .height = 2, .loop_count = first.info.loop_count, }); defer enc.deinit(gpa); for (first.frames) |f| try enc.frame(gpa, f); try enc.finish(); const written = w.written(); try testing.expectEqual(@as(?z2dimg.Format, .gif), z2dimg.detect(written)); var again = try collect(gpa, written); defer again.deinit(gpa); try expectSameFrames(first, again); try testing.expectEqual(first.info.loop_count, again.info.loop_count); } test "a GIF loop count survives the trip out and back" { // GIF counts the repeats after the first play and cannot spell "once" at // all --- a written zero means forever --- so once is spelled by leaving // the extension out. The encoder and the decoder have to agree about that // in both directions or every non-looping animation comes back wrong. const gpa = testing.allocator; var source = try collect(gpa, screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ trailer); defer source.deinit(gpa); for ([_]u32{ 1, 0, 2, 3, 40 }) |plays| { var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &out); defer out = w.toArrayList(); var enc = try z2dimg.gif.encodeAnimation(&w.writer, .{ .width = 2, .height = 2, .loop_count = plays, }); defer enc.deinit(gpa); for (source.frames) |f| try enc.frame(gpa, f); try enc.finish(); var back = try collect(gpa, w.written()); defer back.deinit(gpa); try testing.expectEqual(plays, back.info.loop_count); } } test "a frame with colours the global table lacks carries its own" { // What lets an animation hold more than 256 colours without this having // to quantise, which it still does not do. const gpa = testing.allocator; var source = try collect(gpa, screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ // red image(0, 0, 2, 2, four_px_1) ++ // green: not in the first frame's table trailer); defer source.deinit(gpa); var out: std.ArrayList(u8) = .empty; defer out.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &out); defer out = w.toArrayList(); var enc = try z2dimg.gif.encodeAnimation(&w.writer, .{ .width = 2, .height = 2 }); defer enc.deinit(gpa); for (source.frames) |f| try enc.frame(gpa, f); try enc.finish(); var back = try collect(gpa, w.written()); defer back.deinit(gpa); try expectSameFrames(source, back); try testing.expectEqual(red, at(&back.frames[0].surface, 0, 0)); try testing.expectEqual(green, at(&back.frames[1].surface, 0, 0)); } test "a supplied global palette keeps later frames from carrying their own" { // GIF puts its global table in the header, before any frame, so a // streaming encoder has only the first frame to build one from --- and // every later frame using a colour that frame lacked must then carry a // table of its own. A caller holding all the frames does know better, and // this is how it says so. Measured on a real transcode, it was the // difference between a file twice the size of the original and one // fifteen bytes larger. const gpa = testing.allocator; var source = try collect(gpa, screen_2x2 ++ image(0, 0, 2, 2, four_px_0) ++ // red image(0, 0, 2, 2, four_px_1) ++ // green, which red's table lacks trailer); defer source.deinit(gpa); const both = [_]Rgba8{ red, green }; const without = try encodeFrames(gpa, source, null); defer gpa.free(without); const with = try encodeFrames(gpa, source, &both); defer gpa.free(with); // The shorter one is the one that did not repeat a table. try testing.expect(with.len < without.len); // And both say the same thing, which is the part that matters. var a = try collect(gpa, without); defer a.deinit(gpa); var b = try collect(gpa, with); defer b.deinit(gpa); try expectSameFrames(source, a); try expectSameFrames(source, b); } fn encodeFrames(gpa: Allocator, source: Collected, palette: ?[]const Rgba8) ![]u8 { var out: std.ArrayList(u8) = .empty; errdefer out.deinit(gpa); var w: Io.Writer.Allocating = .fromArrayList(gpa, &out); errdefer out = w.toArrayList(); var enc = try z2dimg.encodeAnimation(&w.writer, .{ .gif = .{ .width = 2, .height = 2, .palette = palette, } }); defer enc.deinit(gpa); for (source.frames) |f| try enc.frame(gpa, f); try enc.finish(); out = w.toArrayList(); return out.toOwnedSlice(gpa); } test "a frame whose data outlasts its pixels does not swallow the next frame" { // A frame is complete before its sub-block chain is, which is ordinary: // an encoder writes the end code and then pads to a sub-block boundary, // and this library's own encoder does it on any image whose compressed // form runs past 255 bytes. // // The decoder stops the decompressor the moment the frame is full --- it // has to, or rubbish after a complete image would refuse the file --- and // then has to step over what is left, because the *next* frame is read // from exactly there. Without that, a sub-block's length byte is read as // the next block's kind. // // It hid for a while behind a leniency: when the stream merely said it // had ended, the unconsumed byte was the chain terminator, and a zero is // what the block loop already tolerates as padding. Only a whole extra // sub-block broke it, which is what the fuzzer eventually built. const gpa = testing.allocator; const bytes = screen_2x2 ++ // Frame zero's four pixels, then a sub-block of rubbish nothing will // read, then the terminator. image(0, 0, 2, 2, "\x02\x03\x04\x00\x05" ++ "\x02\xaa\xbb" ++ "\x00") ++ image(0, 0, 1, 1, one_px_2) ++ trailer; var r: Io.Reader = .fixed(bytes); var frames = try z2dimg.decodeAnimation(gpa, &r, .{}); defer frames.deinit(gpa); const zero = (try frames.next(gpa)).?; try testing.expectEqual(red, at(&zero.surface, 0, 0)); // The second frame is still there, which is the whole claim. const one = (try frames.next(gpa)).?; try testing.expectEqual(@as(i32, 1), one.surface.getWidth()); try testing.expectEqual(blue, at(&one.surface, 0, 0)); try testing.expectEqual(@as(?z2dimg.anim.Frame, null), try frames.next(gpa)); }