From 00240cb0b339c35492ec73ebb9c997992cf80ff7 Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Mon, 31 Jul 2023 16:56:12 -0400 Subject: [PATCH] LibGfx/JPEGXL: Fix property 8 The first implementation of this property was just plain wrong. Looks like this property isn't used a lot as I found the issue by reviewing the code and not because of a specific image. The test image is a 32x32 mosaic of alternating black and yellow pixels, it was generated using this code: Bitdepth 8 RCT 1 Width 32 Height 32 if W-WW-NW+NWW > -300 - Set -1000 - Set 900 --- Tests/LibGfx/TestImageDecoder.cpp | 20 ++++++++++++++++++ .../test-inputs/jxl/modular_property_8.jxl | Bin 0 -> 26 bytes .../LibGfx/ImageFormats/JPEGXLLoader.cpp | 15 +++++++------ 3 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 Tests/LibGfx/test-inputs/jxl/modular_property_8.jxl diff --git a/Tests/LibGfx/TestImageDecoder.cpp b/Tests/LibGfx/TestImageDecoder.cpp index e2e52751b6..f8706c05f2 100644 --- a/Tests/LibGfx/TestImageDecoder.cpp +++ b/Tests/LibGfx/TestImageDecoder.cpp @@ -574,3 +574,23 @@ TEST_CASE(test_jxl_modular_simple_tree_upsample2_10bits) auto frame = MUST(plugin_decoder->frame(0)); EXPECT_EQ(frame.image->get_pixel(42, 57), Gfx::Color::from_string("#4c0072"sv)); } + +TEST_CASE(test_jxl_modular_property_8) +{ + auto file = MUST(Core::MappedFile::map(TEST_INPUT("jxl/modular_property_8.jxl"sv))); + EXPECT(Gfx::JPEGXLImageDecoderPlugin::sniff(file->bytes())); + auto plugin_decoder = MUST(Gfx::JPEGXLImageDecoderPlugin::create(file->bytes())); + + expect_single_frame_of_size(*plugin_decoder, { 32, 32 }); + + auto frame = MUST(plugin_decoder->frame(0)); + for (u8 i = 0; i < 32; ++i) { + for (u8 j = 0; j < 32; ++j) { + auto const color = frame.image->get_pixel(i, j); + if ((i + j) % 2 == 0) + EXPECT_EQ(color, Gfx::Color::Black); + else + EXPECT_EQ(color, Gfx::Color::Yellow); + } + } +} diff --git a/Tests/LibGfx/test-inputs/jxl/modular_property_8.jxl b/Tests/LibGfx/test-inputs/jxl/modular_property_8.jxl new file mode 100644 index 0000000000000000000000000000000000000000..8c3e6f69836cdbd7f96827f63685583420f734af GIT binary patch literal 26 icmey*> get_properties(Vector const& channels, u16 TRY(properties.try_append(W)); // x > 0 ? W - /* (the value of property 9 at position (x - 1, y)) */ : W - i32 x_1 = x - 1; - i32 const W_x_1 = x_1 > 0 ? channels[i].get(x_1 - 1, y) : (x_1 >= 0 && y > 0 ? channels[i].get(x_1, y - 1) : 0); - i32 const N_x_1 = x_1 >= 0 && y > 0 ? channels[i].get(x_1, y - 1) : W_x_1; - i32 const NW_x_1 = x_1 > 0 && y > 0 ? channels[i].get(x_1 - 1, y - 1) : W_x_1; - - TRY(properties.try_append(W_x_1 + N_x_1 - NW_x_1)); + if (x > 0) { + auto const x_1 = x - 1; + i32 const W_x_1 = x_1 > 0 ? channels[i].get(x_1 - 1, y) : (y > 0 ? channels[i].get(x_1, y - 1) : 0); + i32 const N_x_1 = y > 0 ? channels[i].get(x_1, y - 1) : W_x_1; + i32 const NW_x_1 = x_1 > 0 && y > 0 ? channels[i].get(x_1 - 1, y - 1) : W_x_1; + TRY(properties.try_append(W - (W_x_1 + N_x_1 - NW_x_1))); + } else { + TRY(properties.try_append(W)); + } TRY(properties.try_append(W + N - NW)); TRY(properties.try_append(W - NW));