From f2efb97578ce8073e063d4c4a486f53e6817f492 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 7 Apr 2023 18:40:28 -0400 Subject: [PATCH] Tests: Add webp lossless test with color index and < 16 colors For the test files, I opened Base/res/icons/catdog/alert.png in Adobe Photoshop 2023, used Image->Mode->Index Color...-> Palette: Local (Perceptive) to reduce the number of colors to 13, 8, and 3 with transparency, and 2 without transparency, then converted it back to Image->Mode->RGB Color (else it can't be saved as webp), then File->Save a Copy... to save a WebP (mode lossless) for every palette size. --- Tests/LibGfx/TestImageDecoder.cpp | 40 ++++++++++++++++++ Tests/LibGfx/test-inputs/catdog-alert-13.webp | Bin 0 -> 400 bytes Tests/LibGfx/test-inputs/catdog-alert-2.webp | Bin 0 -> 270 bytes Tests/LibGfx/test-inputs/catdog-alert-3.webp | Bin 0 -> 298 bytes Tests/LibGfx/test-inputs/catdog-alert-8.webp | Bin 0 -> 350 bytes 5 files changed, 40 insertions(+) create mode 100644 Tests/LibGfx/test-inputs/catdog-alert-13.webp create mode 100644 Tests/LibGfx/test-inputs/catdog-alert-2.webp create mode 100644 Tests/LibGfx/test-inputs/catdog-alert-3.webp create mode 100644 Tests/LibGfx/test-inputs/catdog-alert-8.webp diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp index 74d395fb0a..c8e2ced9a3 100644 --- a/Tests/LibGfx/TestImageDecoder.cpp +++ b/Tests/LibGfx/TestImageDecoder.cpp @@ -380,6 +380,46 @@ TEST_CASE(test_webp_simple_lossless_color_index_transform) EXPECT_EQ(frame.image->get_pixel(100, 100), Gfx::Color(0x73, 0x37, 0x23, 0xff)); } +TEST_CASE(test_webp_simple_lossless_color_index_transform_pixel_bundling) +{ + struct TestCase { + StringView file_name; + Gfx::Color line_color; + Gfx::Color background_color; + }; + + // The number after the dash is the number of colors in each file's color index bitmap. + // catdog-alert-2 tests the 1-bit-per-pixel case, + // catdog-alert-3 tests the 2-bit-per-pixel case, + // catdog-alert-8 and catdog-alert-13 both test the 4-bits-per-pixel case. + TestCase test_cases[] = { + { "catdog-alert-2.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0xf3, 0xe6, 0xd8, 0xff) }, + { "catdog-alert-3.webp"sv, Gfx::Color(0x35, 0x12, 0x0a, 0xff), Gfx::Color(0, 0, 0, 0) }, + { "catdog-alert-8.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) }, + { "catdog-alert-13.webp"sv, Gfx::Color(0, 0, 0, 255), Gfx::Color(0, 0, 0, 0) }, + }; + + for (auto test_case : test_cases) { + auto file = MUST(Core::MappedFile::map(MUST(String::formatted("{}{}", TEST_INPUT(""), test_case.file_name)))); + EXPECT(Gfx::WebPImageDecoderPlugin::sniff(file->bytes())); + auto plugin_decoder = MUST(Gfx::WebPImageDecoderPlugin::create(file->bytes())); + EXPECT(plugin_decoder->initialize()); + + EXPECT_EQ(plugin_decoder->frame_count(), 1u); + EXPECT_EQ(plugin_decoder->size(), Gfx::IntSize(32, 32)); + + auto frame = MUST(plugin_decoder->frame(0)); + EXPECT_EQ(frame.image->size(), Gfx::IntSize(32, 32)); + + EXPECT_EQ(frame.image->get_pixel(4, 0), test_case.background_color); + EXPECT_EQ(frame.image->get_pixel(5, 0), test_case.line_color); + + EXPECT_EQ(frame.image->get_pixel(9, 5), test_case.background_color); + EXPECT_EQ(frame.image->get_pixel(10, 5), test_case.line_color); + EXPECT_EQ(frame.image->get_pixel(11, 5), test_case.background_color); + } +} + TEST_CASE(test_webp_extended_lossless_animated) { auto file = MUST(Core::MappedFile::map(TEST_INPUT("extended-lossless-animated.webp"sv))); diff --git a/Tests/LibGfx/test-inputs/catdog-alert-13.webp b/Tests/LibGfx/test-inputs/catdog-alert-13.webp new file mode 100644 index 0000000000000000000000000000000000000000..3c3254d3e2b4cb190881f7d32d789d4345e5b53c GIT binary patch literal 400 zcmWIYbaU%qWMBw)bqWXzu!!JdU|;mZvOc*A#c|HBV z{KVcR zYED4>mf%Zwa#cdUG3~hcZNKlm`)v2;^Ze(^Um#fWU3ht%w15oTgDnloFVAVkE)_h& zk+IrEMq$x^VFo6Fh79gCskRbM9BaH24hcBB?r%zDaN{U>B^0nET#M&hpzX3pW-ZP~ ztXl4!-lQWR;6G{S?(5fBJ*VwjzV#!sT+shRQ~7uEWpXs%n|J59v)ugSk3UwJI4u6g zdSAdP-0*vi+{KrdqdcdbuZ_KZ{l3BG>!Zc@x5eaC;|2`sSo6=92^II(^a! zj6OGuBwOlqrkI>Kf7F1h*C%<;xt3)dDqaT`v)%c0U=6duCWkb(JIx2|#n>)euwQ-u zI3%ZOvg{#~?50!v2Tz%apUbb`XmL%COFSZ5{vo8RoH7 ze0a)s&Uxp>j0X!GH{V%sgCXOH(GJ56O9UH^-Im?u@YD7u3;RFasWv}!RQaF$@%ocL zaqZroyQMiPCo8-*Zird$oM1&)#lJq5C3apJ=q?q}!@0gyt z^XmhSm$Dynt-mV45D@I>>BqppVBzHH%lwuB1Oym77#J8B85m(SNRCAVB*nnPz`(#{ Z{{hSf>tS&Qvv~uOQo|Vq7+4v>TmZT7WM%*W literal 0 HcmV?d00001 diff --git a/Tests/LibGfx/test-inputs/catdog-alert-8.webp b/Tests/LibGfx/test-inputs/catdog-alert-8.webp new file mode 100644 index 0000000000000000000000000000000000000000..2d355dc0b89d562da454adce3eee538b43ba82a0 GIT binary patch literal 350 zcmWIYbaM-1WMBw)bqWXzu!!JdU|&(4(t&5b^YQ27J-A&iqf7JjMms6uc~1$Q2Q*;z)&PI>Hp4sXFGT&D0@!2 zeWl`pv)8TdFBp9q46+-PHvGA-#_;dBoE2}!i~lSQ3@i#+jiJ{L3OFU`HgY=mbnUxi zz;Jl(vc3kP*i6G6Dy2TByMNmz@-AF)KOj9T=US8IvyMvRI)$1({XIu+bf16c9N537 z{z*rT)P<#+#b7S<^pE>V-zhejpcJ%aPU|_It^7LhX%K!ob3?2*&42%qnFd8Jsq5+a(;9+23 bV6y)JW`p&xID^@|0ZFOhi~