From dad4843ee49b5b558d89cb61e15acbc82e0cf246 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sat, 4 May 2024 21:53:35 -0400 Subject: [PATCH] TestImageWriter: Add a JPEG roundtrip test --- Tests/LibGfx/TestImageWriter.cpp | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/Tests/LibGfx/TestImageWriter.cpp b/Tests/LibGfx/TestImageWriter.cpp index 7b0cb4f86d1..01f793ce37f 100644 --- a/Tests/LibGfx/TestImageWriter.cpp +++ b/Tests/LibGfx/TestImageWriter.cpp @@ -8,6 +8,8 @@ #include #include #include +#include +#include #include #include #include @@ -36,7 +38,7 @@ static ErrorOr> expect_single_frame_of_size(Gfx::Imag } template -static ErrorOr test_roundtrip(Gfx::Bitmap const& bitmap) +static ErrorOr> get_roundtrip_bitmap(Gfx::Bitmap const& bitmap) { ByteBuffer encoded_data; if constexpr (requires(AllocatingMemoryStream stream) { Writer::encode(stream, bitmap); }) { @@ -48,7 +50,13 @@ static ErrorOr test_roundtrip(Gfx::Bitmap const& bitmap) } auto plugin = TRY(Loader::create(encoded_data)); - auto decoded = TRY(expect_single_frame_of_size(*plugin, bitmap.size())); + return expect_single_frame_of_size(*plugin, bitmap.size()); +} + +template +static ErrorOr test_roundtrip(Gfx::Bitmap const& bitmap) +{ + auto decoded = TRY((get_roundtrip_bitmap(bitmap))); for (int y = 0; y < bitmap.height(); ++y) for (int x = 0; x < bitmap.width(); ++x) @@ -89,6 +97,12 @@ TEST_CASE(test_bmp) TRY_OR_FAIL((test_roundtrip(TRY_OR_FAIL(create_test_rgba_bitmap())))); } +TEST_CASE(test_jpeg) +{ + // JPEG is lossy, so the roundtripped bitmap won't match the original bitmap. But it should still have the same size. + (void)TRY_OR_FAIL((get_roundtrip_bitmap(TRY_OR_FAIL(create_test_rgb_bitmap())))); +} + TEST_CASE(test_png) { TRY_OR_FAIL((test_roundtrip(TRY_OR_FAIL(create_test_rgb_bitmap()))));