summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/TGALoader.cpp
blob: 067299454aea1182d9ba767bf56df5f9baeca3f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
/*
 * Copyright (c) 2022, Tom Needham <06needhamt@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/Span.h>
#include <AK/StdLibExtraDetails.h>
#include <AK/String.h>
#include <LibGfx/TGALoader.h>

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

union [[gnu::packed]] TGAPixel {
    struct TGAColor {
        u8 blue;
        u8 green;
        u8 red;
        u8 alpha;
    } components;

    u32 data;
};

struct TGAPixelPacket {
    bool raw;
    u8 pixels_count;
};

static_assert(AssertSize<TGAPixel, 4>());

class TGAReader {
public:
    TGAReader(ReadonlyBytes data)
        : m_data(move(data))
    {
    }

    TGAReader(ReadonlyBytes data, size_t index)
        : m_data(move(data))
        , m_index(index)
    {
    }

    ALWAYS_INLINE u8 read_u8()
    {
        u8 value = m_data[m_index];
        m_index++;
        return value;
    }

    ALWAYS_INLINE i8 read_i8()
    {
        return static_cast<i8>(read_u8());
    }

    ALWAYS_INLINE u16 read_u16()
    {
        return read_u8() | read_u8() << 8;
    }

    ALWAYS_INLINE i16 read_i16()
    {
        return read_i8() | read_i8() << 8;
    }

    ALWAYS_INLINE u32 read_u32()
    {
        return read_u16() | read_u16() << 16;
    }

    ALWAYS_INLINE i32 read_i32()
    {
        return read_i16() | read_i16() << 16;
    }

    ALWAYS_INLINE TGAPixelPacket read_packet_type()
    {
        auto pixel_packet_type = read_u8();
        auto pixel_packet = TGAPixelPacket();
        pixel_packet.raw = !(pixel_packet_type & 0x80);
        pixel_packet.pixels_count = (pixel_packet_type & 0x7f);

        // NOTE: Run-length-encoded/Raw pixel packets cannot encode zero pixels,
        // so value 0 stands for 1 pixel, 1 stands for 2, etc...
        pixel_packet.pixels_count++;
        return pixel_packet;
    }

    ALWAYS_INLINE TGAPixel read_pixel(u8 bits_per_pixel)
    {
        auto pixel = TGAPixel();

        switch (bits_per_pixel) {
        case 24:
            pixel.components.blue = read_u8();
            pixel.components.green = read_u8();
            pixel.components.red = read_u8();
            pixel.components.alpha = 0xFF;
            return pixel;

        case 32:
            pixel.components.blue = read_u8();
            pixel.components.green = read_u8();
            pixel.components.red = read_u8();
            pixel.components.alpha = read_u8();
            return pixel;

        default:
            VERIFY_NOT_REACHED();
        }
    }

    size_t index() const
    {
        return m_index;
    }

    ReadonlyBytes data() const
    {
        return m_data;
    }

private:
    ReadonlyBytes m_data;
    size_t m_index { 0 };
};

struct TGALoadingContext {
    TGAHeader header;
    OwnPtr<TGAReader> reader = { nullptr };
    RefPtr<Gfx::Bitmap> bitmap;
};

TGAImageDecoderPlugin::TGAImageDecoderPlugin(u8 const* file_data, size_t file_size)
{
    m_context = make<TGALoadingContext>();
    m_context->reader = make<TGAReader>(ReadonlyBytes { file_data, file_size });
}

TGAImageDecoderPlugin::~TGAImageDecoderPlugin() = default;

IntSize TGAImageDecoderPlugin::size()
{
    return IntSize { m_context->header.width, m_context->header.height };
}

void TGAImageDecoderPlugin::set_volatile()
{
    if (m_context->bitmap)
        m_context->bitmap->set_volatile();
}

bool TGAImageDecoderPlugin::set_nonvolatile(bool& was_purged)
{
    if (!m_context->bitmap)
        return false;
    return m_context->bitmap->set_nonvolatile(was_purged);
}

bool TGAImageDecoderPlugin::decode_tga_header()
{
    auto& reader = m_context->reader;
    if (reader->data().size() < sizeof(TGAHeader))
        return false;

    m_context->header = TGAHeader();
    m_context->header.id_length = reader->read_u8();
    m_context->header.color_map_type = reader->read_u8();
    m_context->header.data_type_code = static_cast<TGADataType>(reader->read_u8());
    m_context->header.color_map_origin = reader->read_i16();
    m_context->header.color_map_length = reader->read_i16();
    m_context->header.color_map_depth = reader->read_u8();
    m_context->header.x_origin = reader->read_i16();
    m_context->header.y_origin = reader->read_i16();
    m_context->header.width = reader->read_u16();
    m_context->header.height = reader->read_u16();
    m_context->header.bits_per_pixel = reader->read_u8();
    m_context->header.image_descriptor = reader->read_u8();

    auto bytes_remaining = reader->data().size() - reader->index();

    // FIXME: Check for multiplication overflow!
    if (m_context->header.data_type_code == TGADataType::UncompressedRGB && bytes_remaining < static_cast<size_t>(m_context->header.width * m_context->header.height * (m_context->header.bits_per_pixel / 8)))
        return false;

    if (m_context->header.bits_per_pixel < 8 || m_context->header.bits_per_pixel > 32)
        return false;

    return true;
}

bool TGAImageDecoderPlugin::initialize()
{
    return decode_tga_header();
}

ErrorOr<bool> TGAImageDecoderPlugin::validate_before_create(ReadonlyBytes data)
{
    if (data.size() < sizeof(TGAHeader))
        return false;
    TGAHeader const& header = *reinterpret_cast<TGAHeader const*>(data.data());
    // FIXME: Check for multiplication overflow!
    if (header.data_type_code == TGADataType::UncompressedRGB && data.size() < static_cast<size_t>(header.width * header.height * (header.bits_per_pixel / 8)))
        return false;
    if (header.bits_per_pixel < 8 || header.bits_per_pixel > 32)
        return false;
    return true;
}

ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> TGAImageDecoderPlugin::create(ReadonlyBytes data)
{
    return adopt_nonnull_own_or_enomem(new (nothrow) TGAImageDecoderPlugin(data.data(), data.size()));
}

bool TGAImageDecoderPlugin::is_animated()
{
    return false;
}

size_t TGAImageDecoderPlugin::loop_count()
{
    return 0;
}

size_t TGAImageDecoderPlugin::frame_count()
{
    return 1;
}

ErrorOr<ImageFrameDescriptor> TGAImageDecoderPlugin::frame(size_t index)
{
    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;
    auto x_origin = m_context->header.x_origin;
    auto y_origin = m_context->header.y_origin;

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

    if (m_context->bitmap) {
        return ImageFrameDescriptor { m_context->bitmap, 0 };
    } else {
        // NOTE: Just to be on the safe side, if m_context->bitmap is nullptr, then
        // just re-construct the reader object. This will ensure that if the bitmap
        // was set as volatile and therefore it is gone, we can always re-generate it
        // with a new call to this method!
        VERIFY(m_context->reader);
        m_context->reader = make<TGAReader>(m_context->reader->data(), sizeof(TGAHeader));
    }

    RefPtr<Gfx::Bitmap> bitmap;
    switch (bits_per_pixel) {
    case 24:
        bitmap = TRY(Bitmap::create(BitmapFormat::BGRx8888, { m_context->header.width, m_context->header.height }));
        break;

    case 32:
        bitmap = TRY(Bitmap::create(BitmapFormat::BGRA8888, { m_context->header.width, m_context->header.height }));
        break;

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

    // 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");

    switch (data_type) {
    case TGADataType::UncompressedRGB: {
        for (int row = 0; row < height; ++row) {
            for (int col = 0; col < width; ++col) {
                auto pixel = m_context->reader->read_pixel(bits_per_pixel);
                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;
                bitmap->scanline(actual_row)[actual_col] = pixel.data;
            }
        }
        break;
    }
    case TGADataType::RunLengthEncodedRGB: {
        size_t pixel_index = 0;
        size_t pixel_count = height * width;
        while (pixel_index < pixel_count) {
            auto packet_type = m_context->reader->read_packet_type();
            VERIFY(packet_type.pixels_count > 0);
            TGAPixel pixel = m_context->reader->read_pixel(bits_per_pixel);
            auto max_pixel_index = min(pixel_index + packet_type.pixels_count, pixel_count);
            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;
                bitmap->scanline(actual_row)[actual_col] = pixel.data;
                if (packet_type.raw && (current_pixel_index + 1) < max_pixel_index)
                    pixel = m_context->reader->read_pixel(bits_per_pixel);
            }
            pixel_index += packet_type.pixels_count;
        }
        break;
    }
    default:
        // FIXME: Implement other TGA data types
        return Error::from_string_literal("TGAImageDecoderPlugin: Can currently only handle the UncompressedRGB or CompressedRGB data type");
    }

    m_context->bitmap = bitmap;
    return ImageFrameDescriptor { m_context->bitmap, 0 };
}

ErrorOr<Optional<ReadonlyBytes>> TGAImageDecoderPlugin::icc_data()
{
    return OptionalNone {};
}

}