From 3ca6dfba488ede1e55bb0ca4e159ff462373508e Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 31 May 2024 21:07:05 -0400 Subject: [PATCH] Tests: Add test for color indexing for single-channel images The color indexing transform shouldn't make single-channel images larger (by needlessly writing a palette). If there <= 16 colors in the single channel, it should make the image smaller. --- Tests/LibGfx/TestImageWriter.cpp | 33 ++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/Tests/LibGfx/TestImageWriter.cpp b/Tests/LibGfx/TestImageWriter.cpp index be10b09a510..c99d5833aae 100644 --- a/Tests/LibGfx/TestImageWriter.cpp +++ b/Tests/LibGfx/TestImageWriter.cpp @@ -216,6 +216,39 @@ TEST_CASE(test_webp_color_indexing_transform) } } +TEST_CASE(test_webp_color_indexing_transform_single_channel) +{ + Array colors; + for (size_t i = 0; i < colors.size(); ++i) { + colors[i].set_red(0); + colors[i].set_green(255 - i); + colors[i].set_blue(128); + colors[i].set_alpha(255); + } + for (int bits_per_pixel : { 1, 2, 4, 8 }) { + int number_of_colors = 1 << bits_per_pixel; + + auto bitmap = TRY_OR_FAIL(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, { 47, 33 })); + for (int y = 0; y < bitmap->height(); ++y) + for (int x = 0; x < bitmap->width(); ++x) + bitmap->set_pixel(x, y, colors[(x * bitmap->width() + y) % number_of_colors]); + + auto encoded_data = TRY_OR_FAIL(encode_bitmap(bitmap)); + auto decoded_bitmap = TRY_OR_FAIL(expect_single_frame_of_size(*TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(encoded_data)), bitmap->size())); + expect_bitmaps_equal(*decoded_bitmap, *bitmap); + + Gfx::WebPEncoderOptions options; + options.vp8l_options.allowed_transforms = 0; + auto encoded_data_without_color_indexing = TRY_OR_FAIL(encode_bitmap(bitmap, options)); + if (bits_per_pixel == 8) + EXPECT(encoded_data.size() <= encoded_data_without_color_indexing.size()); + else + EXPECT(encoded_data.size() < encoded_data_without_color_indexing.size()); + auto decoded_bitmap_without_color_indexing = TRY_OR_FAIL(expect_single_frame_of_size(*TRY_OR_FAIL(Gfx::WebPImageDecoderPlugin::create(encoded_data)), bitmap->size())); + expect_bitmaps_equal(*decoded_bitmap_without_color_indexing, *decoded_bitmap); + } +} + TEST_CASE(test_webp_icc) { auto sRGB_icc_profile = MUST(Gfx::ICC::sRGB());