1/**************************************************************************/
2/* image_loader_tga.cpp */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#include "image_loader_tga.h"
32
33#include "core/error/error_macros.h"
34#include "core/io/file_access_memory.h"
35#include "core/os/os.h"
36#include "core/string/print_string.h"
37
38Error ImageLoaderTGA::decode_tga_rle(const uint8_t *p_compressed_buffer, size_t p_pixel_size, uint8_t *p_uncompressed_buffer, size_t p_output_size, size_t p_input_size) {
39 Error error;
40
41 Vector<uint8_t> pixels;
42 error = pixels.resize(p_pixel_size);
43 if (error != OK) {
44 return error;
45 }
46
47 uint8_t *pixels_w = pixels.ptrw();
48
49 size_t compressed_pos = 0;
50 size_t output_pos = 0;
51 size_t c = 0;
52 size_t count = 0;
53
54 while (output_pos < p_output_size) {
55 c = p_compressed_buffer[compressed_pos];
56 compressed_pos += 1;
57 count = (c & 0x7f) + 1;
58
59 if (output_pos + count * p_pixel_size > p_output_size) {
60 return ERR_PARSE_ERROR;
61 }
62
63 if (c & 0x80) {
64 if (compressed_pos + p_pixel_size > p_input_size) {
65 return ERR_PARSE_ERROR;
66 }
67 for (size_t i = 0; i < p_pixel_size; i++) {
68 pixels_w[i] = p_compressed_buffer[compressed_pos];
69 compressed_pos += 1;
70 }
71 for (size_t i = 0; i < count; i++) {
72 for (size_t j = 0; j < p_pixel_size; j++) {
73 p_uncompressed_buffer[output_pos + j] = pixels_w[j];
74 }
75 output_pos += p_pixel_size;
76 }
77 } else {
78 if (compressed_pos + count * p_pixel_size > p_input_size) {
79 return ERR_PARSE_ERROR;
80 }
81 count *= p_pixel_size;
82 for (size_t i = 0; i < count; i++) {
83 p_uncompressed_buffer[output_pos] = p_compressed_buffer[compressed_pos];
84 compressed_pos += 1;
85 output_pos += 1;
86 }
87 }
88 }
89 return OK;
90}
91
92Error ImageLoaderTGA::convert_to_image(Ref<Image> p_image, const uint8_t *p_buffer, const tga_header_s &p_header, const uint8_t *p_palette, const bool p_is_monochrome, size_t p_input_size) {
93#define TGA_PUT_PIXEL(r, g, b, a) \
94 int image_data_ofs = ((y * width) + x); \
95 image_data_w[image_data_ofs * 4 + 0] = r; \
96 image_data_w[image_data_ofs * 4 + 1] = g; \
97 image_data_w[image_data_ofs * 4 + 2] = b; \
98 image_data_w[image_data_ofs * 4 + 3] = a;
99
100 uint32_t width = p_header.image_width;
101 uint32_t height = p_header.image_height;
102 tga_origin_e origin = static_cast<tga_origin_e>((p_header.image_descriptor & TGA_ORIGIN_MASK) >> TGA_ORIGIN_SHIFT);
103 uint8_t alpha_bits = p_header.image_descriptor & TGA_IMAGE_DESCRIPTOR_ALPHA_MASK;
104 uint32_t x_start;
105 int32_t x_step;
106 uint32_t x_end;
107 uint32_t y_start;
108 int32_t y_step;
109 uint32_t y_end;
110
111 if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_TOP_RIGHT) {
112 y_start = 0;
113 y_step = 1;
114 y_end = height;
115 } else {
116 y_start = height - 1;
117 y_step = -1;
118 y_end = -1;
119 }
120
121 if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_BOTTOM_LEFT) {
122 x_start = 0;
123 x_step = 1;
124 x_end = width;
125 } else {
126 x_start = width - 1;
127 x_step = -1;
128 x_end = -1;
129 }
130
131 Vector<uint8_t> image_data;
132 image_data.resize(width * height * sizeof(uint32_t));
133 uint8_t *image_data_w = image_data.ptrw();
134
135 size_t i = 0;
136 uint32_t x = x_start;
137 uint32_t y = y_start;
138
139 if (p_header.pixel_depth == 8) {
140 if (p_is_monochrome) {
141 while (y != y_end) {
142 while (x != x_end) {
143 if (i >= p_input_size) {
144 return ERR_PARSE_ERROR;
145 }
146 uint8_t shade = p_buffer[i];
147
148 TGA_PUT_PIXEL(shade, shade, shade, 0xff)
149
150 x += x_step;
151 i += 1;
152 }
153 x = x_start;
154 y += y_step;
155 }
156 } else {
157 while (y != y_end) {
158 while (x != x_end) {
159 if (i >= p_input_size) {
160 return ERR_PARSE_ERROR;
161 }
162 uint8_t index = p_buffer[i];
163 uint8_t r = 0x00;
164 uint8_t g = 0x00;
165 uint8_t b = 0x00;
166 uint8_t a = 0xff;
167
168 if (p_header.color_map_depth == 24) {
169 // Due to low-high byte order, the color table must be
170 // read in the same order as image data (little endian)
171 r = (p_palette[(index * 3) + 2]);
172 g = (p_palette[(index * 3) + 1]);
173 b = (p_palette[(index * 3) + 0]);
174 } else {
175 return ERR_INVALID_DATA;
176 }
177
178 TGA_PUT_PIXEL(r, g, b, a)
179
180 x += x_step;
181 i += 1;
182 }
183 x = x_start;
184 y += y_step;
185 }
186 }
187 } else if (p_header.pixel_depth == 16) {
188 while (y != y_end) {
189 while (x != x_end) {
190 if (i + 1 >= p_input_size) {
191 return ERR_PARSE_ERROR;
192 }
193
194 // Always stored as RGBA5551
195 uint8_t r = (p_buffer[i + 1] & 0x7c) << 1;
196 uint8_t g = ((p_buffer[i + 1] & 0x03) << 6) | ((p_buffer[i + 0] & 0xe0) >> 2);
197 uint8_t b = (p_buffer[i + 0] & 0x1f) << 3;
198 uint8_t a = (p_buffer[i + 1] & 0x80) ? 0xff : 0;
199
200 TGA_PUT_PIXEL(r, g, b, alpha_bits ? a : 0xff);
201
202 x += x_step;
203 i += 2;
204 }
205 x = x_start;
206 y += y_step;
207 }
208 } else if (p_header.pixel_depth == 24) {
209 while (y != y_end) {
210 while (x != x_end) {
211 if (i + 2 >= p_input_size) {
212 return ERR_PARSE_ERROR;
213 }
214
215 uint8_t r = p_buffer[i + 2];
216 uint8_t g = p_buffer[i + 1];
217 uint8_t b = p_buffer[i + 0];
218
219 TGA_PUT_PIXEL(r, g, b, 0xff)
220
221 x += x_step;
222 i += 3;
223 }
224 x = x_start;
225 y += y_step;
226 }
227 } else if (p_header.pixel_depth == 32) {
228 while (y != y_end) {
229 while (x != x_end) {
230 if (i + 3 >= p_input_size) {
231 return ERR_PARSE_ERROR;
232 }
233
234 uint8_t a = p_buffer[i + 3];
235 uint8_t r = p_buffer[i + 2];
236 uint8_t g = p_buffer[i + 1];
237 uint8_t b = p_buffer[i + 0];
238
239 TGA_PUT_PIXEL(r, g, b, a)
240
241 x += x_step;
242 i += 4;
243 }
244 x = x_start;
245 y += y_step;
246 }
247 }
248
249 p_image->initialize_data(width, height, false, Image::FORMAT_RGBA8, image_data);
250
251 return OK;
252}
253
254Error ImageLoaderTGA::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
255 Vector<uint8_t> src_image;
256 uint64_t src_image_len = f->get_length();
257 ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
258 ERR_FAIL_COND_V(src_image_len < (int64_t)sizeof(tga_header_s), ERR_FILE_CORRUPT);
259 src_image.resize(src_image_len);
260
261 Error err = OK;
262
263 tga_header_s tga_header;
264 tga_header.id_length = f->get_8();
265 tga_header.color_map_type = f->get_8();
266 tga_header.image_type = static_cast<tga_type_e>(f->get_8());
267
268 tga_header.first_color_entry = f->get_16();
269 tga_header.color_map_length = f->get_16();
270 tga_header.color_map_depth = f->get_8();
271
272 tga_header.x_origin = f->get_16();
273 tga_header.y_origin = f->get_16();
274 tga_header.image_width = f->get_16();
275 tga_header.image_height = f->get_16();
276 tga_header.pixel_depth = f->get_8();
277 tga_header.image_descriptor = f->get_8();
278
279 bool is_encoded = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_RLE_RGB || tga_header.image_type == TGA_TYPE_RLE_MONOCHROME);
280 bool has_color_map = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_INDEXED);
281 bool is_monochrome = (tga_header.image_type == TGA_TYPE_RLE_MONOCHROME || tga_header.image_type == TGA_TYPE_MONOCHROME);
282
283 if (tga_header.image_type == TGA_TYPE_NO_DATA) {
284 err = FAILED;
285 }
286
287 uint64_t color_map_size;
288 if (has_color_map) {
289 if (tga_header.color_map_length > 256 || (tga_header.color_map_depth != 24) || tga_header.color_map_type != 1) {
290 err = FAILED;
291 }
292 color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3);
293 } else {
294 if (tga_header.color_map_type) {
295 err = FAILED;
296 }
297 color_map_size = 0;
298 }
299
300 if ((src_image_len - f->get_position()) < (tga_header.id_length + color_map_size)) {
301 err = FAILED; // TGA data appears to be truncated (fewer bytes than expected).
302 }
303
304 if (tga_header.image_width <= 0 || tga_header.image_height <= 0) {
305 err = FAILED;
306 }
307
308 if (!(tga_header.pixel_depth == 8 || tga_header.pixel_depth == 16 || tga_header.pixel_depth == 24 || tga_header.pixel_depth == 32)) {
309 err = FAILED;
310 }
311
312 if (err == OK) {
313 f->seek(f->get_position() + tga_header.id_length);
314
315 Vector<uint8_t> palette;
316
317 if (has_color_map) {
318 err = palette.resize(color_map_size);
319 if (err == OK) {
320 uint8_t *palette_w = palette.ptrw();
321 f->get_buffer(&palette_w[0], color_map_size);
322 } else {
323 return OK;
324 }
325 }
326
327 uint8_t *src_image_w = src_image.ptrw();
328 f->get_buffer(&src_image_w[0], src_image_len - f->get_position());
329
330 const uint8_t *src_image_r = src_image.ptr();
331
332 const size_t pixel_size = tga_header.pixel_depth >> 3;
333 size_t buffer_size = (tga_header.image_width * tga_header.image_height) * pixel_size;
334
335 Vector<uint8_t> uncompressed_buffer;
336 uncompressed_buffer.resize(buffer_size);
337 uint8_t *uncompressed_buffer_w = uncompressed_buffer.ptrw();
338 const uint8_t *uncompressed_buffer_r;
339
340 const uint8_t *buffer = nullptr;
341
342 if (is_encoded) {
343 err = decode_tga_rle(src_image_r, pixel_size, uncompressed_buffer_w, buffer_size, src_image_len);
344
345 if (err == OK) {
346 uncompressed_buffer_r = uncompressed_buffer.ptr();
347 buffer = uncompressed_buffer_r;
348 }
349 } else {
350 buffer = src_image_r;
351 buffer_size = src_image_len;
352 };
353
354 if (err == OK) {
355 const uint8_t *palette_r = palette.ptr();
356 err = convert_to_image(p_image, buffer, tga_header, palette_r, is_monochrome, buffer_size);
357 }
358 }
359
360 return err;
361}
362
363void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const {
364 p_extensions->push_back("tga");
365}
366
367static Ref<Image> _tga_mem_loader_func(const uint8_t *p_tga, int p_size) {
368 Ref<FileAccessMemory> memfile;
369 memfile.instantiate();
370 Error open_memfile_error = memfile->open_custom(p_tga, p_size);
371 ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for TGA image buffer.");
372
373 Ref<Image> img;
374 img.instantiate();
375 Error load_error = ImageLoaderTGA().load_image(img, memfile, false, 1.0f);
376 ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load TGA image.");
377 return img;
378}
379
380ImageLoaderTGA::ImageLoaderTGA() {
381 Image::_tga_mem_loader_func = _tga_mem_loader_func;
382}
383