Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/tga/image_loader_tga.cpp
10277 views
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/io/image.h"
36
37
Error 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) {
38
Error error;
39
40
Vector<uint8_t> pixels;
41
error = pixels.resize(p_pixel_size);
42
if (error != OK) {
43
return error;
44
}
45
46
uint8_t *pixels_w = pixels.ptrw();
47
48
size_t compressed_pos = 0;
49
size_t output_pos = 0;
50
size_t c = 0;
51
size_t count = 0;
52
53
while (output_pos < p_output_size) {
54
c = p_compressed_buffer[compressed_pos];
55
compressed_pos += 1;
56
count = (c & 0x7f) + 1;
57
58
if (output_pos + count * p_pixel_size > p_output_size) {
59
return ERR_PARSE_ERROR;
60
}
61
62
if (c & 0x80) {
63
if (compressed_pos + p_pixel_size > p_input_size) {
64
return ERR_PARSE_ERROR;
65
}
66
for (size_t i = 0; i < p_pixel_size; i++) {
67
pixels_w[i] = p_compressed_buffer[compressed_pos];
68
compressed_pos += 1;
69
}
70
for (size_t i = 0; i < count; i++) {
71
for (size_t j = 0; j < p_pixel_size; j++) {
72
p_uncompressed_buffer[output_pos + j] = pixels_w[j];
73
}
74
output_pos += p_pixel_size;
75
}
76
} else {
77
if (compressed_pos + count * p_pixel_size > p_input_size) {
78
return ERR_PARSE_ERROR;
79
}
80
count *= p_pixel_size;
81
for (size_t i = 0; i < count; i++) {
82
p_uncompressed_buffer[output_pos] = p_compressed_buffer[compressed_pos];
83
compressed_pos += 1;
84
output_pos += 1;
85
}
86
}
87
}
88
return OK;
89
}
90
91
Error 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) {
92
#define TGA_PUT_PIXEL(r, g, b, a) \
93
int image_data_ofs = ((y * width) + x); \
94
image_data_w[image_data_ofs * 4 + 0] = r; \
95
image_data_w[image_data_ofs * 4 + 1] = g; \
96
image_data_w[image_data_ofs * 4 + 2] = b; \
97
image_data_w[image_data_ofs * 4 + 3] = a;
98
99
uint32_t width = p_header.image_width;
100
uint32_t height = p_header.image_height;
101
tga_origin_e origin = static_cast<tga_origin_e>((p_header.image_descriptor & TGA_ORIGIN_MASK) >> TGA_ORIGIN_SHIFT);
102
uint8_t alpha_bits = p_header.image_descriptor & TGA_IMAGE_DESCRIPTOR_ALPHA_MASK;
103
uint32_t x_start;
104
int32_t x_step;
105
uint32_t x_end;
106
uint32_t y_start;
107
int32_t y_step;
108
uint32_t y_end;
109
110
if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_TOP_RIGHT) {
111
y_start = 0;
112
y_step = 1;
113
y_end = height;
114
} else {
115
y_start = height - 1;
116
y_step = -1;
117
y_end = -1;
118
}
119
120
if (origin == TGA_ORIGIN_TOP_LEFT || origin == TGA_ORIGIN_BOTTOM_LEFT) {
121
x_start = 0;
122
x_step = 1;
123
x_end = width;
124
} else {
125
x_start = width - 1;
126
x_step = -1;
127
x_end = -1;
128
}
129
130
Vector<uint8_t> image_data;
131
image_data.resize(width * height * sizeof(uint32_t));
132
uint8_t *image_data_w = image_data.ptrw();
133
134
size_t i = 0;
135
uint32_t x = x_start;
136
uint32_t y = y_start;
137
138
if (p_header.pixel_depth == 8) {
139
if (p_is_monochrome) {
140
while (y != y_end) {
141
while (x != x_end) {
142
if (i >= p_input_size) {
143
return ERR_PARSE_ERROR;
144
}
145
uint8_t shade = p_buffer[i];
146
147
TGA_PUT_PIXEL(shade, shade, shade, 0xff)
148
149
x += x_step;
150
i += 1;
151
}
152
x = x_start;
153
y += y_step;
154
}
155
} else {
156
while (y != y_end) {
157
while (x != x_end) {
158
if (i >= p_input_size) {
159
return ERR_PARSE_ERROR;
160
}
161
uint8_t index = p_buffer[i];
162
uint8_t r = 0x00;
163
uint8_t g = 0x00;
164
uint8_t b = 0x00;
165
uint8_t a = 0xff;
166
167
if (p_header.color_map_depth == 24) {
168
// Due to low-high byte order, the color table must be
169
// read in the same order as image data (little endian)
170
r = (p_palette[(index * 3) + 2]);
171
g = (p_palette[(index * 3) + 1]);
172
b = (p_palette[(index * 3) + 0]);
173
} else {
174
return ERR_INVALID_DATA;
175
}
176
177
TGA_PUT_PIXEL(r, g, b, a)
178
179
x += x_step;
180
i += 1;
181
}
182
x = x_start;
183
y += y_step;
184
}
185
}
186
} else if (p_header.pixel_depth == 16) {
187
while (y != y_end) {
188
while (x != x_end) {
189
if (i + 1 >= p_input_size) {
190
return ERR_PARSE_ERROR;
191
}
192
193
// Always stored as RGBA5551
194
uint8_t r = (p_buffer[i + 1] & 0x7c) << 1;
195
uint8_t g = ((p_buffer[i + 1] & 0x03) << 6) | ((p_buffer[i + 0] & 0xe0) >> 2);
196
uint8_t b = (p_buffer[i + 0] & 0x1f) << 3;
197
uint8_t a = (p_buffer[i + 1] & 0x80) ? 0xff : 0;
198
199
TGA_PUT_PIXEL(r, g, b, alpha_bits ? a : 0xff);
200
201
x += x_step;
202
i += 2;
203
}
204
x = x_start;
205
y += y_step;
206
}
207
} else if (p_header.pixel_depth == 24) {
208
while (y != y_end) {
209
while (x != x_end) {
210
if (i + 2 >= p_input_size) {
211
return ERR_PARSE_ERROR;
212
}
213
214
uint8_t r = p_buffer[i + 2];
215
uint8_t g = p_buffer[i + 1];
216
uint8_t b = p_buffer[i + 0];
217
218
TGA_PUT_PIXEL(r, g, b, 0xff)
219
220
x += x_step;
221
i += 3;
222
}
223
x = x_start;
224
y += y_step;
225
}
226
} else if (p_header.pixel_depth == 32) {
227
while (y != y_end) {
228
while (x != x_end) {
229
if (i + 3 >= p_input_size) {
230
return ERR_PARSE_ERROR;
231
}
232
233
uint8_t a = p_buffer[i + 3];
234
uint8_t r = p_buffer[i + 2];
235
uint8_t g = p_buffer[i + 1];
236
uint8_t b = p_buffer[i + 0];
237
238
TGA_PUT_PIXEL(r, g, b, a)
239
240
x += x_step;
241
i += 4;
242
}
243
x = x_start;
244
y += y_step;
245
}
246
}
247
248
p_image->initialize_data(width, height, false, Image::FORMAT_RGBA8, image_data);
249
250
return OK;
251
}
252
253
Error ImageLoaderTGA::load_image(Ref<Image> p_image, Ref<FileAccess> f, BitField<ImageFormatLoader::LoaderFlags> p_flags, float p_scale) {
254
Vector<uint8_t> src_image;
255
uint64_t src_image_len = f->get_length();
256
ERR_FAIL_COND_V(src_image_len == 0, ERR_FILE_CORRUPT);
257
ERR_FAIL_COND_V(src_image_len < (int64_t)sizeof(tga_header_s), ERR_FILE_CORRUPT);
258
src_image.resize(src_image_len);
259
260
Error err = OK;
261
262
tga_header_s tga_header;
263
tga_header.id_length = f->get_8();
264
tga_header.color_map_type = f->get_8();
265
tga_header.image_type = static_cast<tga_type_e>(f->get_8());
266
267
tga_header.first_color_entry = f->get_16();
268
tga_header.color_map_length = f->get_16();
269
tga_header.color_map_depth = f->get_8();
270
271
tga_header.x_origin = f->get_16();
272
tga_header.y_origin = f->get_16();
273
tga_header.image_width = f->get_16();
274
tga_header.image_height = f->get_16();
275
tga_header.pixel_depth = f->get_8();
276
tga_header.image_descriptor = f->get_8();
277
278
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);
279
bool has_color_map = (tga_header.image_type == TGA_TYPE_RLE_INDEXED || tga_header.image_type == TGA_TYPE_INDEXED);
280
bool is_monochrome = (tga_header.image_type == TGA_TYPE_RLE_MONOCHROME || tga_header.image_type == TGA_TYPE_MONOCHROME);
281
282
if (tga_header.image_type == TGA_TYPE_NO_DATA) {
283
err = FAILED;
284
}
285
286
uint64_t color_map_size;
287
if (has_color_map) {
288
if (tga_header.color_map_length > 256 || (tga_header.color_map_depth != 24) || tga_header.color_map_type != 1) {
289
err = FAILED;
290
}
291
color_map_size = tga_header.color_map_length * (tga_header.color_map_depth >> 3);
292
} else {
293
if (tga_header.color_map_type) {
294
err = FAILED;
295
}
296
color_map_size = 0;
297
}
298
299
if ((src_image_len - f->get_position()) < (tga_header.id_length + color_map_size)) {
300
err = FAILED; // TGA data appears to be truncated (fewer bytes than expected).
301
}
302
303
if (tga_header.image_width <= 0 || tga_header.image_height <= 0) {
304
err = FAILED;
305
}
306
307
if (!(tga_header.pixel_depth == 8 || tga_header.pixel_depth == 16 || tga_header.pixel_depth == 24 || tga_header.pixel_depth == 32)) {
308
err = FAILED;
309
}
310
311
if (err == OK) {
312
f->seek(f->get_position() + tga_header.id_length);
313
314
Vector<uint8_t> palette;
315
316
if (has_color_map) {
317
err = palette.resize(color_map_size);
318
if (err == OK) {
319
uint8_t *palette_w = palette.ptrw();
320
f->get_buffer(&palette_w[0], color_map_size);
321
} else {
322
return OK;
323
}
324
}
325
326
uint8_t *src_image_w = src_image.ptrw();
327
f->get_buffer(&src_image_w[0], src_image_len - f->get_position());
328
329
const uint8_t *src_image_r = src_image.ptr();
330
331
const size_t pixel_size = tga_header.pixel_depth >> 3;
332
size_t buffer_size = (tga_header.image_width * tga_header.image_height) * pixel_size;
333
334
Vector<uint8_t> uncompressed_buffer;
335
uncompressed_buffer.resize(buffer_size);
336
uint8_t *uncompressed_buffer_w = uncompressed_buffer.ptrw();
337
const uint8_t *uncompressed_buffer_r;
338
339
const uint8_t *buffer = nullptr;
340
341
if (is_encoded) {
342
err = decode_tga_rle(src_image_r, pixel_size, uncompressed_buffer_w, buffer_size, src_image_len);
343
344
if (err == OK) {
345
uncompressed_buffer_r = uncompressed_buffer.ptr();
346
buffer = uncompressed_buffer_r;
347
}
348
} else {
349
buffer = src_image_r;
350
buffer_size = src_image_len;
351
};
352
353
if (err == OK) {
354
const uint8_t *palette_r = palette.ptr();
355
err = convert_to_image(p_image, buffer, tga_header, palette_r, is_monochrome, buffer_size);
356
}
357
}
358
359
return err;
360
}
361
362
void ImageLoaderTGA::get_recognized_extensions(List<String> *p_extensions) const {
363
p_extensions->push_back("tga");
364
}
365
366
static Ref<Image> _tga_mem_loader_func(const uint8_t *p_tga, int p_size) {
367
Ref<FileAccessMemory> memfile;
368
memfile.instantiate();
369
Error open_memfile_error = memfile->open_custom(p_tga, p_size);
370
ERR_FAIL_COND_V_MSG(open_memfile_error, Ref<Image>(), "Could not create memfile for TGA image buffer.");
371
372
Ref<Image> img;
373
img.instantiate();
374
Error load_error = ImageLoaderTGA().load_image(img, memfile, false, 1.0f);
375
ERR_FAIL_COND_V_MSG(load_error, Ref<Image>(), "Failed to load TGA image.");
376
return img;
377
}
378
379
ImageLoaderTGA::ImageLoaderTGA() {
380
Image::_tga_mem_loader_func = _tga_mem_loader_func;
381
}
382
383