Skip to content

Commit

Permalink
LibGfx: Support UncompressedBlackAndWhite in TGAImageDecoderPlugin
Browse files Browse the repository at this point in the history
Test image generated with `magick buggie-bottom-left-uncompressed.tga
-colorspace Gray -compress None buggie-black-and-white-uncompressed.tga`
  • Loading branch information
mkanilsson authored and nico committed Sep 18, 2024
1 parent da2c80e commit 76301a6
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 9 additions & 0 deletions Tests/LibGfx/TestImageDecoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -841,6 +841,15 @@ TEST_CASE(test_targa_top_left_compressed)
TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
}

TEST_CASE(test_targa_black_and_white_uncompressed)
{
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tga/buggie-black-and-white-uncompressed.tga"sv)));
EXPECT(Gfx::TGAImageDecoderPlugin::validate_before_create(file->bytes()));
auto plugin_decoder = TRY_OR_FAIL(Gfx::TGAImageDecoderPlugin::create(file->bytes()));

TRY_OR_FAIL(expect_single_frame(*plugin_decoder));
}

TEST_CASE(test_tiff_uncompressed)
{
auto file = TRY_OR_FAIL(Core::MappedFile::map(TEST_INPUT("tiff/uncompressed.tiff"sv)));
Expand Down
Binary file not shown.
15 changes: 11 additions & 4 deletions Userland/Libraries/LibGfx/ImageFormats/TGALoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,13 @@ ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TGAImageDecoderPlugin::create(Readonl

static ErrorOr<ARGB32> read_pixel_from_stream(Stream& stream, size_t bytes_size)
{
// NOTE: We support 24-bit color pixels and 32-bit color pixels
VERIFY(bytes_size == 3 || bytes_size == 4);
// NOTE: We support 8-bit, 24-bit and 32-bit color pixels
VERIFY(bytes_size == 1 || bytes_size == 3 || bytes_size == 4);

if (bytes_size == 1) {
auto raw = TRY(stream.read_value<u8>());
return Color(raw, raw, raw).value();
}
if (bytes_size == 3) {
Array<u8, 3> raw;
TRY(stream.read_until_filled(raw.span()));
Expand Down Expand Up @@ -159,6 +164,7 @@ ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index, Optiona

RefPtr<Gfx::Bitmap> bitmap;
switch (bits_per_pixel) {
case 8:
case 24:
bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { m_context->header.width, m_context->header.height }));
break;
Expand All @@ -169,7 +175,7 @@ ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index, Optiona

default:
// FIXME: Implement other TGA bit depths
return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle 24 and 32 bits per pixel");
return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle 8, 24 and 32 bits per pixel");
}

// FIXME: Try to understand the Image origin (instead of X and Y origin coordinates)
Expand All @@ -189,6 +195,7 @@ ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index, Optiona
auto bytes_per_pixel = bits_per_pixel / 8;

switch (data_type) {
case TGADataType::UncompressedBlackAndWhite:
case TGADataType::UncompressedRGB: {
for (int row = 0; row < height; ++row) {
for (int col = 0; col < width; ++col) {
Expand Down Expand Up @@ -232,7 +239,7 @@ ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index, Optiona
}
default:
// FIXME: Implement other TGA data types
return Error::from_string_literal("TGAImageDecoderPlugin: Can currently only handle the UncompressedRGB or CompressedRGB data type");
return Error::from_string_literal("TGAImageDecoderPlugin: Can currently only handle the UncompressedRGB, CompressedRGB or UncompressedBlackAndWhite data type");
}

m_context->bitmap = bitmap;
Expand Down

0 comments on commit 76301a6

Please sign in to comment.