-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_tga.c
executable file
·258 lines (216 loc) · 8.49 KB
/
load_tga.c
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
/*
* Copyright (c) 2015-2019 Alaskan Emily, Transnat Games
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include "image.h"
#include "bufferfile/bufferfile.h"
#include "memset_pattern4.h"
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
/*
* Technically the largest possible TGA image would need 35 bits for the field
* itself. It would consist of a 0xFFFF by 0xFFFF field, encoded with RLE, but
* with each pixel encoded as not-RLE, and in runs of 1 only.
*
* I'm not really worried about that a case. Such a TGA is highly unlikely, as
* it would require either an entirely incompetent encoder (using RLE-encoding
* but refusing to actually encode runs) or a moderately competent encoder
* (it's easier to encode each non-RLE pixel as an individual run of 1 pixel)
* and a very entropic image. Or a malicious input :)
*
* We don't handle that. Actually, we refuse to open a TGA with a width or
* height of more than 0xFFFE, which precludes anything higher than 33-bits.
*
* We include all sizes as 32-bit values since the need for more bits is a
* seriously edge case, and we are probably being fed garbage data (or utterly
* poorly encoded real data, or malicious inputs) if we encounter a larger
* field.
*/
enum AImg_TGAFormat { Nothing, RGBA, Mapped, BlackWhite };
struct AImg_TGAHeader {
const char *id;
uint8_t id_length;
unsigned is_rle, has_map;
enum AImg_TGAFormat format;
const uint8_t *color_map;
uint16_t color_map_entries;
uint16_t x_origin, y_origin, w, h;
uint8_t bits_per_map_entry, bits_per_pixel;
uint8_t flipped_vertically;
};
/* return how many bytes read */
typedef uint8_t (*aimg_tga_pixel_reader)(uint32_t *to, const uint8_t *from);
static uint16_t aimg_lo_hi_short(const void *z_){
const uint8_t *z = z_;
uint16_t r = z[1];
r <<= 8;
r |= z[0];
return r;
}
/* All reader functions return how many bytes they have consumed. A value of zero indicates an error.
* The accumulator exists because Solaris Studio will not perform TCO if we just put the addition on
* the result of the next call.
*/
static uint8_t aimg_read_tga_black_and_white(uint32_t *to, const uint8_t *from){
to[0] = AImg_RGBAToRaw(*from, *from, *from, 0xFF);
return 1;
}
static uint8_t aimg_read_tga_15(uint32_t *to, const uint8_t *from){
const uint16_t concat = aimg_lo_hi_short(from);
to[0] = AImg_RGBAToRaw(
((concat >> 10) & 0x1F) << 3,
((concat >> 5) & 0x1F) << 3,
(concat & 0x1F) << 3,
0xFF);
return 2;
}
static uint8_t aimg_read_tga_16(uint32_t *to, const uint8_t *from){
const uint16_t concat = aimg_lo_hi_short(from);
to[0] = AImg_RGBAToRaw(
((concat >> 10) & 0x1F) << 3,
((concat >> 5) & 0x1F) << 3,
(concat & 0x1F) << 3,
((concat & 0x80) == 0x80) ? 0xFF : 0);
return 2;
}
static uint8_t aimg_read_tga_24(uint32_t *to, const uint8_t *from){
to[0] = AImg_RGBAToRaw(from[2], from[1], from[0], 0xFF);
return 3;
}
static uint8_t aimg_read_tga_32(uint32_t *to, const uint8_t *from){
to[0] = AImg_RGBAToRaw(from[2], from[1], from[0], from[3]);
return 4;
}
static uint32_t aimg_tga_read_raw(struct AImg_Image *to, uint16_t x, uint16_t y, const uint8_t *field, aimg_tga_pixel_reader pixel_reader, uint32_t accumulator){
if(x >= to->w)
return aimg_tga_read_raw(to, 0, y + 1, field, pixel_reader, accumulator);
else if(y >= to->h)
return accumulator;
else{
const uint32_t z = pixel_reader(AImg_Pixel(to, x, y), field);
return aimg_tga_read_raw(to, x + 1, y, field + z, pixel_reader, accumulator + 1);
}
}
static int aimg_tga_read_raw_inner(uint32_t *to, const uint8_t *field, unsigned pixels_remaining, aimg_tga_pixel_reader pixel_reader, unsigned accumulator){
if(pixels_remaining){
const int size = pixel_reader(to, field);
return aimg_tga_read_raw_inner(to + 1, field + size, pixels_remaining - 1, pixel_reader, accumulator + size);
}
else{
return accumulator;
}
}
static uint32_t aimg_tga_read_rle(struct AImg_Image *to, uint16_t x, uint16_t y, const uint8_t *field, aimg_tga_pixel_reader pixel_reader, uint32_t accumulator){
if(x >= to->w)
return aimg_tga_read_rle(to, x - to->w, y + 1, field, pixel_reader, accumulator);
else if(y >= to->h)
return accumulator;
else{
const uint8_t run_size = (field[0] & 0x7F) + 1,
rle_packet = field[0] & 0x80;
field ++;
if(!rle_packet){
const int size = aimg_tga_read_raw_inner(AImg_Pixel(to, x, y), field, run_size, pixel_reader, 0);
return aimg_tga_read_rle(to, x + run_size, y, field + size, pixel_reader, accumulator + 1 + size);
}
else{
uint32_t pattern;
const int size = pixel_reader(&pattern, field);
memset_pattern4(AImg_Pixel(to, x, y), &pattern, run_size << 2);
return aimg_tga_read_rle(to, x + run_size, y, field + size, pixel_reader, accumulator + 1 + size);
}
}
}
static int aimg_tga_header_from_buffer(struct AImg_TGAHeader *header, const uint8_t *buffer){
header->id = (char *)buffer + 0x12;
header->id_length = buffer[0x00];
header->has_map = buffer[0x01] != 0;
header->is_rle = (buffer[0x02] & 8) != 0;
header->format = buffer[0x02] & 3;
header->color_map = buffer + aimg_lo_hi_short(buffer + 0x03);
header->color_map_entries = aimg_lo_hi_short(buffer + 0x05);
header->bits_per_map_entry = buffer[0x07];
header->x_origin = aimg_lo_hi_short(buffer + 0x08);
header->y_origin = aimg_lo_hi_short(buffer + 0x0A);
header->w = aimg_lo_hi_short(buffer + 0x0C);
header->h = aimg_lo_hi_short(buffer + 0x0E);
header->bits_per_pixel = buffer[0x10];
header->flipped_vertically = !(buffer[0x11] & 0x20);
return 0;
}
unsigned AIMG_FASTCALL AImg_LoadTGA(struct AImg_Image *to, const char *path){
int size;
const void * data = NULL;
if(!to || !path)
return AIMG_LOADPNG_IS_NULL;
data = BufferFile(path, &size);
if(!data){
return AIMG_LOADPNG_NO_FILE;
}
else if(size < 0x12){
FreeBufferFile((void*)data, size);
return AIMG_LOADPNG_BADFILE;
}
{
const unsigned r = AImg_LoadTGAMem(to, data, size);
FreeBufferFile((void*)data, size);
return r;
}
}
unsigned AIMG_FASTCALL AImg_LoadTGAMem(struct AImg_Image *to, const void *data, unsigned size){
if(!to || !data || size <= 0x12)
return AIMG_LOADPNG_IS_NULL;
{
struct AImg_TGAHeader header;
aimg_tga_pixel_reader pixel_reader;
aimg_tga_header_from_buffer(&header, data);
switch(header.bits_per_pixel){
case 8:
pixel_reader = aimg_read_tga_black_and_white;
break;
case 15:
pixel_reader = aimg_read_tga_15;
break;
case 16:
pixel_reader = aimg_read_tga_16;
break;
case 24:
pixel_reader = aimg_read_tga_24;
break;
case 32:
pixel_reader = aimg_read_tga_32;
break;
default:
return AIMG_LOADPNG_NFORMAT;
}
AImg_CreateImage(to, header.w, header.h);
{
const uint8_t *field = ((const uint8_t *)header.id) + header.id_length;
if(header.is_rle){
aimg_tga_read_rle(to, 0, 0, field, pixel_reader, 0);
}
else{
aimg_tga_read_raw(to, 0, 0, field, pixel_reader, 0);
}
}
/* TGA is upside down. So we need to reverse it. */
if(header.flipped_vertically)
AImg_FlipImageVertically(to, to);
}
return AIMG_LOADPNG_SUCCESS;
}