2022-08-17 19:58:42 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2022, Tom Needham <06needhamt@gmail.com>
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
#include <AK/MemoryStream.h>
|
2022-08-17 19:58:42 +01:00
|
|
|
#include <AK/Span.h>
|
|
|
|
|
#include <AK/StdLibExtraDetails.h>
|
|
|
|
|
#include <AK/String.h>
|
2023-07-22 00:55:50 +03:00
|
|
|
#include <AK/Traits.h>
|
2023-03-21 14:58:06 -04:00
|
|
|
#include <LibGfx/ImageFormats/TGALoader.h>
|
2022-08-17 19:58:42 +01:00
|
|
|
|
|
|
|
|
namespace Gfx {
|
|
|
|
|
|
|
|
|
|
enum TGADataType : u8 {
|
|
|
|
|
None = 0,
|
|
|
|
|
UncompressedColorMapped = 1,
|
|
|
|
|
UncompressedRGB = 2,
|
|
|
|
|
UncompressedBlackAndWhite = 3,
|
|
|
|
|
RunLengthEncodedColorMapped = 9,
|
|
|
|
|
RunLengthEncodedRGB = 10,
|
|
|
|
|
CompressedBlackAndWhite = 11,
|
|
|
|
|
CompressedColorMapped = 32,
|
|
|
|
|
CompressedColorMappedFourPass = 33
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
struct [[gnu::packed]] TGAHeader {
|
|
|
|
|
u8 id_length;
|
|
|
|
|
u8 color_map_type;
|
|
|
|
|
TGADataType data_type_code;
|
|
|
|
|
i16 color_map_origin;
|
|
|
|
|
i16 color_map_length;
|
|
|
|
|
u8 color_map_depth;
|
|
|
|
|
i16 x_origin;
|
|
|
|
|
i16 y_origin;
|
|
|
|
|
u16 width;
|
|
|
|
|
u16 height;
|
|
|
|
|
u8 bits_per_pixel;
|
|
|
|
|
u8 image_descriptor;
|
|
|
|
|
};
|
|
|
|
|
static_assert(sizeof(TGAHeader) == 18);
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
}
|
2022-08-17 19:58:42 +01:00
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
template<>
|
2023-11-08 20:29:12 +01:00
|
|
|
struct AK::Traits<Gfx::TGAHeader> : public DefaultTraits<Gfx::TGAHeader> {
|
2023-07-22 00:55:50 +03:00
|
|
|
static constexpr bool is_trivially_serializable() { return true; }
|
LibGfx: Add support for RLE compressed TGA images
RLE is an old technique being used for decades, as is known as
Run-Length-Encoding, which means that for repeating sequence of bytes,
we keep an indicator for the length of the sequence and only one sample
of it, to save storage space.
GIMP can generate lossless-compressed TGA images, with RLE compression
being used. It means that for a compressed image, the data is no longer
arranged in sequence of pixels, but a sequence of pixel packets.
There are two possible pixel packets:
- RLE packets, which are encoded with one byte for indicating the
run-length and another one pixel (3 bytes for TrueColor pixel), so
essentially in runtime, the TGA decoder will use the length to plot
the same pixel in multiple pixels of the output pixel bitmap.
- Raw packets, which are encoded with one byte as indicator for the
length of the whole pixel sequence and N-length pixel sequence
afterwards.
This is not used for any sort of compression by the TGA format, but
still needed to be supported for full compatibility with TGA images
that uses the RLE compression.
2023-01-07 17:01:41 +02:00
|
|
|
};
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
namespace Gfx {
|
2022-08-17 19:58:42 +01:00
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
struct TGALoadingContext {
|
2023-07-27 15:05:13 +01:00
|
|
|
TGALoadingContext(ReadonlyBytes bytes, FixedMemoryStream stream)
|
|
|
|
|
: bytes(bytes)
|
|
|
|
|
, stream(move(stream))
|
2022-08-17 19:58:42 +01:00
|
|
|
{
|
|
|
|
|
}
|
2023-07-27 15:05:13 +01:00
|
|
|
ReadonlyBytes bytes;
|
2023-07-22 00:55:50 +03:00
|
|
|
FixedMemoryStream stream;
|
2023-07-09 00:48:25 -04:00
|
|
|
TGAHeader header {};
|
2022-08-17 19:58:42 +01:00
|
|
|
RefPtr<Gfx::Bitmap> bitmap;
|
|
|
|
|
};
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
TGAImageDecoderPlugin::TGAImageDecoderPlugin(NonnullOwnPtr<TGALoadingContext> context)
|
|
|
|
|
: m_context(move(context))
|
2022-08-17 19:58:42 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TGAImageDecoderPlugin::~TGAImageDecoderPlugin() = default;
|
|
|
|
|
|
|
|
|
|
IntSize TGAImageDecoderPlugin::size()
|
|
|
|
|
{
|
|
|
|
|
return IntSize { m_context->header.width, m_context->header.height };
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
static ErrorOr<void> ensure_header_validity(TGAHeader const& header, size_t whole_image_stream_size)
|
2022-08-17 19:58:42 +01:00
|
|
|
{
|
2023-07-22 00:55:50 +03:00
|
|
|
auto bytes_remaining = whole_image_stream_size - sizeof(TGAHeader);
|
|
|
|
|
if ((header.bits_per_pixel % 8) != 0 || header.bits_per_pixel < 8 || header.bits_per_pixel > 32)
|
2023-07-09 00:57:20 -04:00
|
|
|
return Error::from_string_literal("Invalid bit depth");
|
2023-07-22 00:55:50 +03:00
|
|
|
if (header.data_type_code == TGADataType::UncompressedRGB && bytes_remaining < static_cast<u64>(header.width) * header.height * (header.bits_per_pixel / 8))
|
|
|
|
|
return Error::from_string_literal("Not enough data to read an image with the expected size");
|
|
|
|
|
return {};
|
|
|
|
|
}
|
2022-08-17 19:58:42 +01:00
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
ErrorOr<void> TGAImageDecoderPlugin::decode_tga_header()
|
|
|
|
|
{
|
|
|
|
|
m_context->header = TRY(m_context->stream.read_value<TGAHeader>());
|
2023-07-27 15:05:13 +01:00
|
|
|
TRY(ensure_header_validity(m_context->header, m_context->bytes.size()));
|
2023-07-09 00:57:20 -04:00
|
|
|
return {};
|
2022-08-17 19:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 10:13:14 +02:00
|
|
|
ErrorOr<bool> TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data)
|
|
|
|
|
{
|
2023-07-22 00:55:50 +03:00
|
|
|
FixedMemoryStream stream { data };
|
|
|
|
|
auto header = TRY(stream.read_value<Gfx::TGAHeader>());
|
|
|
|
|
return !ensure_header_validity(header, data.size()).is_error();
|
2023-01-20 10:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TGAImageDecoderPlugin::create(ReadonlyBytes data)
|
|
|
|
|
{
|
2023-07-22 00:55:50 +03:00
|
|
|
FixedMemoryStream stream { data };
|
2023-07-27 15:05:13 +01:00
|
|
|
auto context = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TGALoadingContext(data, move(stream))));
|
2023-07-22 00:55:50 +03:00
|
|
|
auto plugin = TRY(adopt_nonnull_own_or_enomem(new (nothrow) TGAImageDecoderPlugin(move(context))));
|
2023-07-09 01:05:29 -04:00
|
|
|
TRY(plugin->decode_tga_header());
|
|
|
|
|
return plugin;
|
2023-01-20 10:13:14 +02:00
|
|
|
}
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
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);
|
|
|
|
|
if (bytes_size == 3) {
|
|
|
|
|
Array<u8, 3> raw;
|
|
|
|
|
TRY(stream.read_until_filled(raw.span()));
|
|
|
|
|
return Color(raw[2], raw[1], raw[0]).value();
|
|
|
|
|
}
|
|
|
|
|
return stream.read_value<ARGB32>();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
struct TGAPixelPacketHeader {
|
|
|
|
|
bool raw { false };
|
|
|
|
|
u8 pixels_count { 0 };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
static ErrorOr<TGAPixelPacketHeader> read_pixel_packet_header(Stream& stream)
|
|
|
|
|
{
|
|
|
|
|
auto const pixel_packet_header = TRY(stream.read_value<u8>());
|
|
|
|
|
bool pixels_raw_in_packet = !(pixel_packet_header & 0x80);
|
|
|
|
|
u8 pixels_count_in_packet = (pixel_packet_header & 0x7f);
|
|
|
|
|
// NOTE: Run-length-encoded/Raw pixel packets cannot encode zero pixels,
|
|
|
|
|
// so value 0 stands for 1 pixel, 1 stands for 2, etc...
|
|
|
|
|
pixels_count_in_packet++;
|
|
|
|
|
VERIFY(pixels_count_in_packet > 0);
|
|
|
|
|
return TGAPixelPacketHeader { pixels_raw_in_packet, pixels_count_in_packet };
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-02 22:20:06 +01:00
|
|
|
ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index, Optional<IntSize>)
|
2022-08-17 19:58:42 +01:00
|
|
|
{
|
|
|
|
|
auto bits_per_pixel = m_context->header.bits_per_pixel;
|
|
|
|
|
auto color_map = m_context->header.color_map_type;
|
|
|
|
|
auto data_type = m_context->header.data_type_code;
|
|
|
|
|
auto width = m_context->header.width;
|
|
|
|
|
auto height = m_context->header.height;
|
2022-12-19 10:48:17 +02:00
|
|
|
auto x_origin = m_context->header.x_origin;
|
|
|
|
|
auto y_origin = m_context->header.y_origin;
|
2022-08-17 19:58:42 +01:00
|
|
|
|
|
|
|
|
if (index != 0)
|
|
|
|
|
return Error::from_string_literal("TGAImageDecoderPlugin: frame index must be 0");
|
|
|
|
|
|
|
|
|
|
if (color_map > 1)
|
|
|
|
|
return Error::from_string_literal("TGAImageDecoderPlugin: Invalid color map type");
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
if (m_context->bitmap)
|
2023-01-20 18:50:49 +02:00
|
|
|
return ImageFrameDescriptor { m_context->bitmap, 0 };
|
|
|
|
|
|
|
|
|
|
RefPtr<Gfx::Bitmap> bitmap;
|
2022-08-17 19:58:42 +01:00
|
|
|
switch (bits_per_pixel) {
|
|
|
|
|
case 24:
|
2023-01-20 20:06:05 +01:00
|
|
|
bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { m_context->header.width, m_context->header.height }));
|
2022-08-17 19:58:42 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case 32:
|
2023-01-20 20:06:05 +01:00
|
|
|
bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { m_context->header.width, m_context->header.height }));
|
2022-08-17 19:58:42 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
// FIXME: Implement other TGA bit depths
|
|
|
|
|
return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle 24 and 32 bits per pixel");
|
|
|
|
|
}
|
2022-12-19 10:48:17 +02:00
|
|
|
|
|
|
|
|
// FIXME: Try to understand the Image origin (instead of X and Y origin coordinates)
|
|
|
|
|
// based on the Image descriptor, Field 5.6, bits 4 and 5.
|
|
|
|
|
|
|
|
|
|
// NOTE: If Y origin is set to a negative number, just assume the generating software
|
|
|
|
|
// meant that we start with Y origin at the top height of the picture.
|
|
|
|
|
// At least this is the observed behavior when generating some pictures in GIMP.
|
|
|
|
|
if (y_origin < 0)
|
|
|
|
|
y_origin = height;
|
|
|
|
|
if (y_origin != 0 && y_origin != height)
|
|
|
|
|
return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle Y origin which is 0 or the entire height");
|
|
|
|
|
if (x_origin != 0 && x_origin != width)
|
|
|
|
|
return Error::from_string_literal("TGAImageDecoderPlugin: Can only handle X origin which is 0 or the entire width");
|
|
|
|
|
|
2023-07-22 00:55:50 +03:00
|
|
|
VERIFY((bits_per_pixel % 8) == 0);
|
|
|
|
|
auto bytes_per_pixel = bits_per_pixel / 8;
|
|
|
|
|
|
2022-08-17 19:58:42 +01:00
|
|
|
switch (data_type) {
|
|
|
|
|
case TGADataType::UncompressedRGB: {
|
|
|
|
|
for (int row = 0; row < height; ++row) {
|
|
|
|
|
for (int col = 0; col < width; ++col) {
|
2022-12-19 10:48:17 +02:00
|
|
|
auto actual_row = row;
|
|
|
|
|
if (y_origin < height)
|
|
|
|
|
actual_row = height - 1 - row;
|
|
|
|
|
auto actual_col = col;
|
|
|
|
|
if (x_origin > width)
|
|
|
|
|
actual_col = width - 1 - col;
|
2023-07-22 00:55:50 +03:00
|
|
|
bitmap->scanline(actual_row)[actual_col] = TRY(read_pixel_from_stream(m_context->stream, bytes_per_pixel));
|
2022-08-17 19:58:42 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-07-22 00:55:50 +03:00
|
|
|
|
LibGfx: Add support for RLE compressed TGA images
RLE is an old technique being used for decades, as is known as
Run-Length-Encoding, which means that for repeating sequence of bytes,
we keep an indicator for the length of the sequence and only one sample
of it, to save storage space.
GIMP can generate lossless-compressed TGA images, with RLE compression
being used. It means that for a compressed image, the data is no longer
arranged in sequence of pixels, but a sequence of pixel packets.
There are two possible pixel packets:
- RLE packets, which are encoded with one byte for indicating the
run-length and another one pixel (3 bytes for TrueColor pixel), so
essentially in runtime, the TGA decoder will use the length to plot
the same pixel in multiple pixels of the output pixel bitmap.
- Raw packets, which are encoded with one byte as indicator for the
length of the whole pixel sequence and N-length pixel sequence
afterwards.
This is not used for any sort of compression by the TGA format, but
still needed to be supported for full compatibility with TGA images
that uses the RLE compression.
2023-01-07 17:01:41 +02:00
|
|
|
case TGADataType::RunLengthEncodedRGB: {
|
|
|
|
|
size_t pixel_index = 0;
|
|
|
|
|
size_t pixel_count = height * width;
|
|
|
|
|
while (pixel_index < pixel_count) {
|
2023-07-22 00:55:50 +03:00
|
|
|
auto pixel_packet_header = TRY(read_pixel_packet_header(m_context->stream));
|
|
|
|
|
VERIFY(pixel_packet_header.pixels_count > 0);
|
|
|
|
|
|
|
|
|
|
auto pixel = TRY(read_pixel_from_stream(m_context->stream, bytes_per_pixel));
|
|
|
|
|
auto max_pixel_index = min(pixel_index + pixel_packet_header.pixels_count, pixel_count);
|
LibGfx: Add support for RLE compressed TGA images
RLE is an old technique being used for decades, as is known as
Run-Length-Encoding, which means that for repeating sequence of bytes,
we keep an indicator for the length of the sequence and only one sample
of it, to save storage space.
GIMP can generate lossless-compressed TGA images, with RLE compression
being used. It means that for a compressed image, the data is no longer
arranged in sequence of pixels, but a sequence of pixel packets.
There are two possible pixel packets:
- RLE packets, which are encoded with one byte for indicating the
run-length and another one pixel (3 bytes for TrueColor pixel), so
essentially in runtime, the TGA decoder will use the length to plot
the same pixel in multiple pixels of the output pixel bitmap.
- Raw packets, which are encoded with one byte as indicator for the
length of the whole pixel sequence and N-length pixel sequence
afterwards.
This is not used for any sort of compression by the TGA format, but
still needed to be supported for full compatibility with TGA images
that uses the RLE compression.
2023-01-07 17:01:41 +02:00
|
|
|
for (size_t current_pixel_index = pixel_index; current_pixel_index < max_pixel_index; ++current_pixel_index) {
|
|
|
|
|
int row = current_pixel_index / width;
|
|
|
|
|
int col = current_pixel_index % width;
|
|
|
|
|
auto actual_row = row;
|
|
|
|
|
if (y_origin < height)
|
|
|
|
|
actual_row = height - 1 - row;
|
|
|
|
|
auto actual_col = col;
|
|
|
|
|
if (x_origin > width)
|
|
|
|
|
actual_col = width - 1 - col;
|
2023-07-22 00:55:50 +03:00
|
|
|
bitmap->scanline(actual_row)[actual_col] = pixel;
|
|
|
|
|
if (pixel_packet_header.raw && (current_pixel_index + 1) < max_pixel_index)
|
|
|
|
|
pixel = TRY(read_pixel_from_stream(m_context->stream, bytes_per_pixel));
|
LibGfx: Add support for RLE compressed TGA images
RLE is an old technique being used for decades, as is known as
Run-Length-Encoding, which means that for repeating sequence of bytes,
we keep an indicator for the length of the sequence and only one sample
of it, to save storage space.
GIMP can generate lossless-compressed TGA images, with RLE compression
being used. It means that for a compressed image, the data is no longer
arranged in sequence of pixels, but a sequence of pixel packets.
There are two possible pixel packets:
- RLE packets, which are encoded with one byte for indicating the
run-length and another one pixel (3 bytes for TrueColor pixel), so
essentially in runtime, the TGA decoder will use the length to plot
the same pixel in multiple pixels of the output pixel bitmap.
- Raw packets, which are encoded with one byte as indicator for the
length of the whole pixel sequence and N-length pixel sequence
afterwards.
This is not used for any sort of compression by the TGA format, but
still needed to be supported for full compatibility with TGA images
that uses the RLE compression.
2023-01-07 17:01:41 +02:00
|
|
|
}
|
2023-07-22 00:55:50 +03:00
|
|
|
pixel_index += pixel_packet_header.pixels_count;
|
LibGfx: Add support for RLE compressed TGA images
RLE is an old technique being used for decades, as is known as
Run-Length-Encoding, which means that for repeating sequence of bytes,
we keep an indicator for the length of the sequence and only one sample
of it, to save storage space.
GIMP can generate lossless-compressed TGA images, with RLE compression
being used. It means that for a compressed image, the data is no longer
arranged in sequence of pixels, but a sequence of pixel packets.
There are two possible pixel packets:
- RLE packets, which are encoded with one byte for indicating the
run-length and another one pixel (3 bytes for TrueColor pixel), so
essentially in runtime, the TGA decoder will use the length to plot
the same pixel in multiple pixels of the output pixel bitmap.
- Raw packets, which are encoded with one byte as indicator for the
length of the whole pixel sequence and N-length pixel sequence
afterwards.
This is not used for any sort of compression by the TGA format, but
still needed to be supported for full compatibility with TGA images
that uses the RLE compression.
2023-01-07 17:01:41 +02:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-08-17 19:58:42 +01:00
|
|
|
default:
|
|
|
|
|
// FIXME: Implement other TGA data types
|
LibGfx: Add support for RLE compressed TGA images
RLE is an old technique being used for decades, as is known as
Run-Length-Encoding, which means that for repeating sequence of bytes,
we keep an indicator for the length of the sequence and only one sample
of it, to save storage space.
GIMP can generate lossless-compressed TGA images, with RLE compression
being used. It means that for a compressed image, the data is no longer
arranged in sequence of pixels, but a sequence of pixel packets.
There are two possible pixel packets:
- RLE packets, which are encoded with one byte for indicating the
run-length and another one pixel (3 bytes for TrueColor pixel), so
essentially in runtime, the TGA decoder will use the length to plot
the same pixel in multiple pixels of the output pixel bitmap.
- Raw packets, which are encoded with one byte as indicator for the
length of the whole pixel sequence and N-length pixel sequence
afterwards.
This is not used for any sort of compression by the TGA format, but
still needed to be supported for full compatibility with TGA images
that uses the RLE compression.
2023-01-07 17:01:41 +02:00
|
|
|
return Error::from_string_literal("TGAImageDecoderPlugin: Can currently only handle the UncompressedRGB or CompressedRGB data type");
|
2022-08-17 19:58:42 +01:00
|
|
|
}
|
|
|
|
|
|
2023-01-20 18:50:49 +02:00
|
|
|
m_context->bitmap = bitmap;
|
2022-08-17 19:58:42 +01:00
|
|
|
return ImageFrameDescriptor { m_context->bitmap, 0 };
|
|
|
|
|
}
|
2023-01-26 07:23:59 -05:00
|
|
|
|
2022-08-17 19:58:42 +01:00
|
|
|
}
|