LibGfx/TIFF: Add support for grayscale images

Images with a single sample per pixel should be interpreted as
grayscale.
This commit is contained in:
Lucas CHOLLET 2023-12-01 19:28:06 -05:00 committed by Sam Atkins
parent 923d9e834f
commit da134f6867
3 changed files with 22 additions and 1 deletions

View file

@ -408,6 +408,18 @@ TEST_CASE(test_tiff_packed_bits)
EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color::NamedColor::Red);
}
TEST_CASE(test_tiff_grayscale)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("tiff/grayscale.tiff"sv)));
EXPECT(Gfx::TIFFImageDecoderPlugin::sniff(file->bytes()));
auto plugin_decoder = MUST(Gfx::TIFFImageDecoderPlugin::create(file->bytes()));
auto frame = expect_single_frame_of_size(*plugin_decoder, { 400, 300 });
EXPECT_EQ(frame.image->get_pixel(0, 0), Gfx::Color::NamedColor::White);
EXPECT_EQ(frame.image->get_pixel(60, 75), Gfx::Color(130, 130, 130));
}
TEST_CASE(test_webp_simple_lossy)
{
auto file = MUST(Core::MappedFile::map(TEST_INPUT("webp/simple-vp8.webp"sv)));

Binary file not shown.

View file

@ -96,7 +96,16 @@ private:
Optional<Color> last_color {};
for (u32 column = 0; column < *m_metadata.image_width(); ++column) {
auto color = Color { TRY(decoded_strip->template read_value<u8>()), TRY(decoded_strip->template read_value<u8>()), TRY(decoded_strip->template read_value<u8>()) };
Color color {};
if (m_metadata.samples_per_pixel().value_or(3) == 3) {
color = Color { TRY(decoded_strip->template read_value<u8>()), TRY(decoded_strip->template read_value<u8>()), TRY(decoded_strip->template read_value<u8>()) };
} else if (*m_metadata.samples_per_pixel() == 1) {
auto luminosity = TRY(decoded_strip->template read_value<u8>());
color = Color { luminosity, luminosity, luminosity };
} else {
return Error::from_string_literal("Unsupported number of sample per pixel");
}
if (m_metadata.predictor() == Predictor::HorizontalDifferencing && last_color.has_value()) {
color.set_red(last_color->red() + color.red());