Skip to content

Commit

Permalink
Tests: Add tests for jpeg2000 palette handling difference in PDF files
Browse files Browse the repository at this point in the history
I created indexed-small.jp2 with this script:
https://github.com/nico/hack/blob/d4772bae/make_palettized_jpeg2000.py

I created jpeg2000-indexed-small.pdf with a local script that
creates a PDF file embedding a jpeg2000 file, and then manually changed
the indexed color space in the PDF to use 0x7f bytes for all bytes
in the palette that were 0xff. Since PDF renderers are supposed to
use the palette that's in the PDF file, not the one that's in the
JPEG2000 data, this means that the image shows up darker in the
PDF than when viewing the file standalone. (It'd also be possible
to change the palette colors to something completely different.
I just went with "make it slightly darker" since that seems less
confusing.)
  • Loading branch information
nico committed Mar 3, 2025
1 parent 2207e87 commit 5b0a43b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 0 deletions.
16 changes: 16 additions & 0 deletions Tests/LibGfx/TestImageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,22 @@ TEST_CASE(test_jpeg2000_decode_indexed)
}
}

TEST_CASE(test_jpeg2000_decode_indexed_small_raw)
{
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("jpeg2000/indexed-small.jp2"sv)));
EXPECT(Gfx::JPEG2000ImageDecoderPlugin::sniff(file->bytes()));
auto plugin_decoder = TRY_OR_FAIL(Gfx::JPEG2000ImageDecoderPlugin::create(file->bytes()));

auto frame = TRY_OR_FAIL(plugin_decoder->frame(0)).image;
EXPECT_EQ(frame->size(), Gfx::IntSize(3, 2));
EXPECT_EQ(frame->scanline(0)[0], Gfx::Color(255, 0, 0).value());
EXPECT_EQ(frame->scanline(0)[1], Gfx::Color(0, 255, 0).value());
EXPECT_EQ(frame->scanline(0)[2], Gfx::Color(0, 0, 255).value());
EXPECT_EQ(frame->scanline(1)[0], Gfx::Color(0, 255, 255).value());
EXPECT_EQ(frame->scanline(1)[1], Gfx::Color(255, 0, 255).value());
EXPECT_EQ(frame->scanline(1)[2], Gfx::Color(255, 255, 0).value());
}

TEST_CASE(test_jpeg2000_decode_unsupported)
{
Array test_inputs = {
Expand Down
Binary file added Tests/LibGfx/test-inputs/jpeg2000/indexed-small.jp2
Binary file not shown.
1 change: 1 addition & 0 deletions Tests/LibPDF/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ set(TEST_FILES
encoding.pdf
encryption_nocopy.pdf
jbig2-globals.pdf
jpeg2000-indexed-small.pdf
linearized.pdf
non-linearized.pdf
offset.pdf
Expand Down
26 changes: 26 additions & 0 deletions Tests/LibPDF/TestPDF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,29 @@ TEST_CASE(render)
// MyCalGray
EXPECT_EQ(bitmap->get_pixel(270, 370 - 320), Gfx::Color::NamedColor::Black);
}

TEST_CASE(render_jpeg2000_indexed)
{
#if !defined(AK_OS_SERENITY)
// Get from Build/lagom/bin/TestPDF to Build/lagom/Root/res.
auto source_root = LexicalPath(MUST(Core::System::current_executable_path())).parent().parent().string();
Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(MUST(String::formatted("{}/Root/res", source_root))));
#endif

auto file = MUST(Core::MappedFile::map("jpeg2000-indexed-small.pdf"sv));
auto document = MUST(PDF::Document::create(file->bytes()));
MUST(document->initialize());
EXPECT_EQ(document->get_page_count(), 1U);

auto page = MUST(document->get_page(0));
auto page_size = Gfx::IntSize { 3, 2 };
auto bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, page_size));
MUST(PDF::Renderer::render(document, page, bitmap, Color::White, PDF::RenderingPreferences {}));

EXPECT_EQ(bitmap->scanline(0)[0], Gfx::Color(127, 0, 0).value());
EXPECT_EQ(bitmap->scanline(0)[1], Gfx::Color(0, 127, 0).value());
EXPECT_EQ(bitmap->scanline(0)[2], Gfx::Color(0, 0, 127).value());
EXPECT_EQ(bitmap->scanline(1)[0], Gfx::Color(0, 127, 127).value());
EXPECT_EQ(bitmap->scanline(1)[1], Gfx::Color(127, 0, 127).value());
EXPECT_EQ(bitmap->scanline(1)[2], Gfx::Color(127, 127, 0).value());
}
Binary file added Tests/LibPDF/jpeg2000-indexed-small.pdf
Binary file not shown.

0 comments on commit 5b0a43b

Please sign in to comment.