Skip to content

Commit b7b51d0

Browse files
nicotrflynn89
authored andcommitted
LibGfx/AnimationWriter: Survive animations with two identical frames
Creating a completely empty bitmap fails. Just write a 1x1 bitmap if the bitmap ends up empty for now.
1 parent 5161792 commit b7b51d0

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

Tests/LibGfx/TestImageWriter.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,34 @@ TEST_CASE(test_webp_incremental_animation)
229229
EXPECT_EQ(frame1.duration, 200);
230230
expect_bitmaps_equal(*frame1.image, *rgb_bitmap_2);
231231
}
232+
233+
TEST_CASE(test_webp_incremental_animation_two_identical_frames)
234+
{
235+
auto rgb_bitmap = TRY_OR_FAIL(create_test_rgba_bitmap());
236+
rgb_bitmap = TRY_OR_FAIL(rgb_bitmap->cropped({ 0, 0, 40, 20 })); // Even-sized.
237+
238+
// 20 kiB is enough for two 47x33 frames.
239+
auto stream_buffer = TRY_OR_FAIL(ByteBuffer::create_uninitialized(20 * 1024));
240+
FixedMemoryStream stream { Bytes { stream_buffer } };
241+
242+
auto animation_writer = TRY_OR_FAIL(Gfx::WebPWriter::start_encoding_animation(stream, rgb_bitmap->size()));
243+
244+
TRY_OR_FAIL(animation_writer->add_frame(*rgb_bitmap, 100));
245+
TRY_OR_FAIL(animation_writer->add_frame_relative_to_last_frame(*rgb_bitmap, 200, *rgb_bitmap));
246+
247+
auto encoded_animation = ReadonlyBytes { stream_buffer.data(), stream.offset() };
248+
249+
auto decoded_animation_plugin = TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(encoded_animation));
250+
EXPECT(decoded_animation_plugin->is_animated());
251+
EXPECT_EQ(decoded_animation_plugin->frame_count(), 2u);
252+
EXPECT_EQ(decoded_animation_plugin->loop_count(), 0u);
253+
EXPECT_EQ(decoded_animation_plugin->size(), rgb_bitmap->size());
254+
255+
auto frame0 = TRY_OR_FAIL(decoded_animation_plugin->frame(0));
256+
EXPECT_EQ(frame0.duration, 100);
257+
expect_bitmaps_equal(*frame0.image, *rgb_bitmap);
258+
259+
auto frame1 = TRY_OR_FAIL(decoded_animation_plugin->frame(1));
260+
EXPECT_EQ(frame1.duration, 200);
261+
expect_bitmaps_equal(*frame1.image, *rgb_bitmap);
262+
}

Userland/Libraries/LibGfx/ImageFormats/AnimationWriter.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ ErrorOr<void> AnimationWriter::add_frame_relative_to_last_frame(Bitmap& frame, i
8080

8181
auto rect = rect_where_pixels_are_different(*last_frame, frame);
8282

83+
if (rect.is_empty()) {
84+
// The frame is identical to the last frame. Don't store an empty bitmap.
85+
// FIXME: We could delay writing the last frame until we know that the next frame is different,
86+
// and just keep increasing that frame's duration instead.
87+
rect = { 0, 0, 1, 1 };
88+
}
89+
8390
// FIXME: It would be nice to have a way to crop a bitmap without copying the data.
8491
auto differences = TRY(frame.cropped(rect));
8592

0 commit comments

Comments
 (0)