Procházet zdrojové kódy

LibGfx/TIFF: Add support for grayscale images

Images with a single sample per pixel should be interpreted as
grayscale.
Lucas CHOLLET před 1 rokem
rodič
revize
da134f6867

+ 12 - 0
Tests/LibGfx/TestImageDecoder.cpp

@@ -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)));

binární
Tests/LibGfx/test-inputs/tiff/grayscale.tiff


+ 10 - 1
Userland/Libraries/LibGfx/ImageFormats/TIFFLoader.cpp

@@ -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());