-
Notifications
You must be signed in to change notification settings - Fork 1
/
tgafunc.c
630 lines (565 loc) · 21.4 KB
/
tgafunc.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Copyright (c) 2021 Caden Ji
//
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#include "tgafunc.h"
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct tga_info {
uint16_t width, height;
enum tga_pixel_format pixel_format;
};
static inline bool check_dimensions(int width, int height);
static inline int pixel_format_to_pixel_size(enum tga_pixel_format format);
static enum tga_error load_image(uint8_t **data_out, tga_info **info_out,
FILE *file);
static inline uint8_t *get_pixel(uint8_t *data, const tga_info *info, int x,
int y);
static enum tga_error save_image(const uint8_t *data, const tga_info *info,
FILE *file);
enum tga_error tga_create(uint8_t **data_out, tga_info **info_out, int width,
int height, enum tga_pixel_format format) {
if (check_dimensions(width, height)) {
return TGA_ERROR_INVALID_IMAGE_DIMENSIONS;
}
int pixel_size = pixel_format_to_pixel_size(format);
if (pixel_size == -1) {
return TGA_ERROR_UNSUPPORTED_PIXEL_FORMAT;
}
// Creates image data and info structure.
uint8_t *data = (uint8_t *)calloc((size_t)width * height, pixel_size);
if (data == NULL) {
return TGA_ERROR_OUT_OF_MEMORY;
}
tga_info *info = (tga_info *)malloc(sizeof(tga_info));
if (info == NULL) {
free(data);
return TGA_ERROR_OUT_OF_MEMORY;
}
info->width = width;
info->height = height;
info->pixel_format = format;
*data_out = data;
*info_out = info;
return TGA_NO_ERROR;
}
enum tga_error tga_load(uint8_t **data_out, tga_info **info_out,
const char *file_name) {
FILE *file = fopen(file_name, "rb");
if (file == NULL) {
return TGA_ERROR_FILE_CANNOT_READ;
}
enum tga_error error_code = load_image(data_out, info_out, file);
fclose(file);
return error_code;
}
enum tga_error tga_save(const uint8_t *data, int width, int height,
enum tga_pixel_format format, const char *file_name) {
if (check_dimensions(width, height)) {
return TGA_ERROR_INVALID_IMAGE_DIMENSIONS;
}
tga_info info = {width, height, format};
return tga_save_from_info(data, &info, file_name);
}
enum tga_error tga_save_from_info(const uint8_t *data, const tga_info *info,
const char *file_name) {
if (data == NULL || info == NULL) {
return TGA_ERROR_NO_DATA;
}
// Check if a file with the same name already exists.
FILE *file = fopen(file_name, "r");
if (file != NULL) {
fclose(file);
return TGA_ERROR_FILE_CANNOT_WRITE;
}
file = fopen(file_name, "wb");
if (file == NULL) {
return TGA_ERROR_FILE_CANNOT_WRITE;
}
enum tga_error error_code = save_image(data, info, file);
fclose(file);
if (error_code != TGA_NO_ERROR) {
remove(file_name);
}
return error_code;
}
int tga_get_image_width(const tga_info *info) { return info->width; }
int tga_get_image_height(const tga_info *info) { return info->height; }
enum tga_pixel_format tga_get_pixel_format(const tga_info *info) {
return info->pixel_format;
}
uint8_t tga_get_bytes_per_pixel(const tga_info *info) {
return pixel_format_to_pixel_size(info->pixel_format);
}
uint8_t *tga_get_pixel(uint8_t *data, const tga_info *info, int x, int y) {
return get_pixel(data, info, x, y);
}
void tga_free_data(void *data) { free(data); }
void tga_free_info(tga_info *info) { free(info); }
void tga_image_flip_h(uint8_t *data, const tga_info *info) {
if (data == NULL || info == NULL) {
return;
}
int pixel_size = pixel_format_to_pixel_size(info->pixel_format);
uint8_t temp[pixel_size];
int flip_num = info->width / 2;
for (int i = 0; i < flip_num; ++i) {
for (int j = 0; j < info->height; ++j) {
uint8_t *p1 = get_pixel(data, info, i, j);
uint8_t *p2 = get_pixel(data, info, info->width - 1 - i, j);
// Swap two pixels.
memcpy(temp, p1, pixel_size);
memcpy(p1, p2, pixel_size);
memcpy(p2, temp, pixel_size);
}
}
}
void tga_image_flip_v(uint8_t *data, const tga_info *info) {
if (data == NULL || info == NULL) {
return;
}
int pixel_size = pixel_format_to_pixel_size(info->pixel_format);
uint8_t temp[pixel_size];
int flip_num = info->height / 2;
for (int i = 0; i < flip_num; ++i) {
for (int j = 0; j < info->width; ++j) {
uint8_t *p1 = get_pixel(data, info, j, i);
uint8_t *p2 = get_pixel(data, info, j, info->height - 1 - i);
// Swap two pixels.
memcpy(temp, p1, pixel_size);
memcpy(p1, p2, pixel_size);
memcpy(p2, temp, pixel_size);
}
}
}
enum tga_image_type {
TGA_TYPE_NO_DATA = 0,
TGA_TYPE_COLOR_MAPPED = 1,
TGA_TYPE_TRUE_COLOR = 2,
TGA_TYPE_GRAYSCALE = 3,
TGA_TYPE_RLE_COLOR_MAPPED = 9,
TGA_TYPE_RLE_TRUE_COLOR = 10,
TGA_TYPE_RLE_GRAYSCALE = 11
};
struct tga_header {
uint8_t id_length;
uint8_t map_type;
uint8_t image_type;
// Color map specification.
uint16_t map_first_entry;
uint16_t map_length;
uint8_t map_entry_size;
// Image specification.
uint16_t image_x_origin;
uint16_t image_y_origin;
uint16_t image_width;
uint16_t image_height;
uint8_t pixel_depth;
uint8_t image_descriptor;
};
struct color_map {
uint16_t first_index;
uint16_t entry_count;
uint8_t bytes_per_entry;
uint8_t *pixels;
};
#define HEADER_SIZE 18
#define IS_SUPPORTED_IMAGE_TYPE(header) \
((header).image_type == TGA_TYPE_COLOR_MAPPED || \
(header).image_type == TGA_TYPE_TRUE_COLOR || \
(header).image_type == TGA_TYPE_GRAYSCALE || \
(header).image_type == TGA_TYPE_RLE_COLOR_MAPPED || \
(header).image_type == TGA_TYPE_RLE_TRUE_COLOR || \
(header).image_type == TGA_TYPE_RLE_GRAYSCALE)
#define IS_COLOR_MAPPED(header) \
((header).image_type == TGA_TYPE_COLOR_MAPPED || \
(header).image_type == TGA_TYPE_RLE_COLOR_MAPPED)
#define IS_TRUE_COLOR(header) \
((header).image_type == TGA_TYPE_TRUE_COLOR || \
(header).image_type == TGA_TYPE_RLE_TRUE_COLOR)
#define IS_GRAYSCALE(header) \
((header).image_type == TGA_TYPE_GRAYSCALE || \
(header).image_type == TGA_TYPE_RLE_GRAYSCALE)
#define IS_RLE(header) \
((header).image_type == TGA_TYPE_RLE_COLOR_MAPPED || \
(header).image_type == TGA_TYPE_RLE_TRUE_COLOR || \
(header).image_type == TGA_TYPE_RLE_GRAYSCALE)
// Convert bits to integer bytes. E.g. 8 bits to 1 byte, 9 bits to 2 bytes.
#define BITS_TO_BYTES(bit_count) (((bit_count)-1) / 8 + 1)
static bool has_read_file_error = false;
// Reads a 8-bit integer from the file stream.
static inline uint8_t read_uint8(FILE *file) {
uint8_t value;
if (fread(&value, 1, 1, file) != 1) {
has_read_file_error = true;
return 0;
}
return value;
}
// Gets a 16-bit little-endian integer from the file stream.
// This function should works on both big-endian and little-endian architecture
// systems.
static inline uint16_t read_uint16_le(FILE *file) {
uint8_t buffer[2];
if (fread(&buffer, 1, 2, file) != 2) {
has_read_file_error = true;
return 0;
}
return buffer[0] + (((uint16_t)buffer[1]) << 8);
}
// Checks if the picture size is correct.
// Returns true if invalid dimensisns, otherwise returns false.
static inline bool check_dimensions(int width, int height) {
return width <= 0 || width > TGA_MAX_IMAGE_DIMENSIONS || height <= 0 ||
height > TGA_MAX_IMAGE_DIMENSIONS;
}
// Gets the bytes per pixel by pixel format.
// Returns bytes per pxiel, otherwise returns -1 means the parameter `format` is
// invalid.
static inline int pixel_format_to_pixel_size(enum tga_pixel_format format) {
switch (format) {
case TGA_PIXEL_BW8:
return 1;
case TGA_PIXEL_BW16:
case TGA_PIXEL_RGB555:
return 2;
case TGA_PIXEL_RGB24:
return 3;
case TGA_PIXEL_ARGB32:
return 4;
default:
return -1;
}
}
// Gets the pixel format according to the header.
// Returns false means the header is not illegal, otherwise returns true.
static bool get_pixel_format(enum tga_pixel_format *format,
const struct tga_header *header) {
if (IS_COLOR_MAPPED(*header)) {
// If the supported pixel_depth is changed, remember to also change
// the pixel_to_map_index() function.
if (header->pixel_depth == 8) {
switch (header->map_entry_size) {
case 15:
case 16:
*format = TGA_PIXEL_RGB555;
return false;
case 24:
*format = TGA_PIXEL_RGB24;
return false;
case 32:
*format = TGA_PIXEL_ARGB32;
return false;
}
}
} else if (IS_TRUE_COLOR(*header)) {
switch (header->pixel_depth) {
case 16:
*format = TGA_PIXEL_RGB555;
return false;
case 24:
*format = TGA_PIXEL_RGB24;
return false;
case 32:
*format = TGA_PIXEL_ARGB32;
return false;
}
} else if (IS_GRAYSCALE(*header)) {
switch (header->pixel_depth) {
case 8:
*format = TGA_PIXEL_BW8;
return false;
case 16:
*format = TGA_PIXEL_BW16;
return false;
}
}
return true;
}
// Loads TGA header from file stream and returns the pixel format.
static enum tga_error load_header(struct tga_header *header,
enum tga_pixel_format *pixel_format,
FILE *file) {
has_read_file_error = false;
header->id_length = read_uint8(file);
header->map_type = read_uint8(file);
header->image_type = read_uint8(file);
header->map_first_entry = read_uint16_le(file);
header->map_length = read_uint16_le(file);
header->map_entry_size = read_uint8(file);
header->image_x_origin = read_uint16_le(file);
header->image_y_origin = read_uint16_le(file);
header->image_width = read_uint16_le(file);
header->image_height = read_uint16_le(file);
header->pixel_depth = read_uint8(file);
header->image_descriptor = read_uint8(file);
if (has_read_file_error) {
return TGA_ERROR_FILE_CANNOT_READ;
}
if (header->map_type > 1) {
return TGA_ERROR_UNSUPPORTED_COLOR_MAP_TYPE;
}
if (header->image_type == TGA_TYPE_NO_DATA) {
return TGA_ERROR_NO_DATA;
}
if (!IS_SUPPORTED_IMAGE_TYPE(*header)) {
return TGA_ERROR_UNSUPPORTED_IMAGE_TYPE;
}
if (header->image_width <= 0 || header->image_height <= 0) {
// No need to check if the image size exceeds TGA_MAX_IMAGE_DIMENSIONS.
return TGA_ERROR_INVALID_IMAGE_DIMENSIONS;
}
if (get_pixel_format(pixel_format, header)) {
return TGA_ERROR_UNSUPPORTED_PIXEL_FORMAT;
}
return TGA_NO_ERROR;
}
// Used for color mapped image decode.
static inline uint16_t pixel_to_map_index(uint8_t *pixel_ptr) {
// Because only 8-bit index is supported now, so implemented in this way.
return pixel_ptr[0];
}
// Gets the color of the specified index from the map.
// Returns false means no error, otherwise returns true.
static inline bool try_get_color_from_map(uint8_t *dest, uint16_t index,
const struct color_map *map) {
index -= map->first_index;
if (index < 0 && index >= map->entry_count) {
return true;
}
memcpy(dest, map->pixels + map->bytes_per_entry * index,
map->bytes_per_entry);
return false;
}
// Decode image data from file stream.
static enum tga_error decode_data(uint8_t *data, const tga_info *info,
uint8_t pixel_size, bool is_color_mapped,
const struct color_map *map, FILE *file) {
enum tga_error error_code = TGA_NO_ERROR;
size_t pixel_count = (size_t)info->width * info->height;
if (is_color_mapped) {
for (; pixel_count > 0; --pixel_count) {
if (fread(data, 1, pixel_size, file) != pixel_size) {
error_code = TGA_ERROR_FILE_CANNOT_READ;
break;
}
// In color mapped image, the pixel as the index value of the color
// map. The actual pixel value is found from the color map.
uint16_t index = pixel_to_map_index(data);
if (try_get_color_from_map(data, index, map)) {
error_code = TGA_ERROR_COLOR_MAP_INDEX_FAILED;
break;
}
data += map->bytes_per_entry;
}
} else {
size_t data_size = pixel_count * pixel_size;
if (fread(data, 1, data_size, file) != data_size) {
error_code = TGA_ERROR_FILE_CANNOT_READ;
}
}
return error_code;
}
// Decode image data with run-length encoding from file stream.
static enum tga_error decode_data_rle(uint8_t *data, const tga_info *info,
uint8_t pixel_size, bool is_color_mapped,
const struct color_map *map, FILE *file) {
enum tga_error error_code = TGA_NO_ERROR;
size_t pixel_count = (size_t)info->width * info->height;
bool is_run_length_packet = false;
uint8_t packet_count = 0;
uint8_t pixel_buffer[is_color_mapped ? map->bytes_per_entry : pixel_size];
// The actual pixel size of the image, In order not to be confused with the
// name of the parameter pixel_size, named data element.
uint8_t data_element_size = pixel_format_to_pixel_size(info->pixel_format);
for (; pixel_count > 0; --pixel_count) {
if (packet_count == 0) {
uint8_t repetition_count_field;
if (fread(&repetition_count_field, 1, 1, file) != 1) {
error_code = TGA_ERROR_FILE_CANNOT_READ;
break;
}
is_run_length_packet = repetition_count_field & 0x80;
packet_count = (repetition_count_field & 0x7F) + 1;
if (is_run_length_packet) {
if (fread(pixel_buffer, 1, pixel_size, file) != pixel_size) {
error_code = TGA_ERROR_FILE_CANNOT_READ;
break;
}
if (is_color_mapped) {
// In color mapped image, the pixel as the index value of
// the color map. The actual pixel value is found from the
// color map.
uint16_t index = pixel_to_map_index(pixel_buffer);
if (try_get_color_from_map(pixel_buffer, index, map)) {
error_code = TGA_ERROR_COLOR_MAP_INDEX_FAILED;
break;
}
}
}
}
if (is_run_length_packet) {
memcpy(data, pixel_buffer, data_element_size);
} else {
if (fread(data, 1, pixel_size, file) != pixel_size) {
error_code = TGA_ERROR_FILE_CANNOT_READ;
break;
}
if (is_color_mapped) {
// Again, in color mapped image, the pixel as the index value of
// the color map. The actual pixel value is found from the color
// map.
uint16_t index = pixel_to_map_index(data);
if (try_get_color_from_map(data, index, map)) {
error_code = TGA_ERROR_COLOR_MAP_INDEX_FAILED;
break;
}
}
}
--packet_count;
data += data_element_size;
}
return error_code;
}
static enum tga_error load_image(uint8_t **data_out, tga_info **info_out,
FILE *file) {
struct tga_header header;
enum tga_pixel_format pixel_format;
enum tga_error error_code;
error_code = load_header(&header, &pixel_format, file);
if (error_code != TGA_NO_ERROR) {
return error_code;
}
// No need to handle the content of the ID field, so skip directly.
if (fseek(file, header.id_length, SEEK_CUR)) {
return TGA_ERROR_FILE_CANNOT_READ;
}
bool is_color_mapped = IS_COLOR_MAPPED(header);
bool is_rle = IS_RLE(header);
// Handle color map field.
struct color_map color_map;
color_map.pixels = NULL;
size_t map_size = header.map_length * BITS_TO_BYTES(header.map_entry_size);
if (is_color_mapped) {
color_map.first_index = header.map_first_entry;
color_map.entry_count = header.map_length;
color_map.bytes_per_entry = BITS_TO_BYTES(header.map_entry_size);
color_map.pixels = (uint8_t *)malloc(map_size);
if (color_map.pixels == NULL) {
return TGA_ERROR_OUT_OF_MEMORY;
}
if (fread(color_map.pixels, 1, map_size, file) != map_size) {
free(color_map.pixels);
return TGA_ERROR_FILE_CANNOT_READ;
}
} else if (header.map_type == 1) {
// The image is not color mapped at this time, but contains a color map.
// So skips the color map data block directly.
if (fseek(file, map_size, SEEK_CUR)) {
return TGA_ERROR_FILE_CANNOT_READ;
}
}
uint8_t *data;
tga_info *info;
error_code = tga_create(&data, &info, header.image_width,
header.image_height, pixel_format);
if (error_code != TGA_NO_ERROR) {
free(color_map.pixels);
return error_code;
}
// Load image data.
uint8_t pixel_size = BITS_TO_BYTES(header.pixel_depth);
if (is_rle) {
error_code = decode_data_rle(data, info, pixel_size, is_color_mapped,
&color_map, file);
} else {
error_code = decode_data(data, info, pixel_size, is_color_mapped,
&color_map, file);
}
free(color_map.pixels);
if (error_code != TGA_NO_ERROR) {
tga_free_data(data);
tga_free_info(info);
return error_code;
}
// Flip the image if necessary, to keep the origin in upper left corner.
bool flip_h = header.image_descriptor & 0x10;
bool flip_v = !(header.image_descriptor & 0x20);
if (flip_h) {
tga_image_flip_h(data, info);
}
if (flip_v) {
tga_image_flip_v(data, info);
}
*data_out = data;
*info_out = info;
return TGA_NO_ERROR;
}
// Returns the pixel at coordinates (x,y) for reading or writing.
// If the pixel coordinates are out of bounds (larger than width/height
// or small than 0), they will be clamped.
static inline uint8_t *get_pixel(uint8_t *data, const tga_info *info, int x,
int y) {
if (x < 0) {
x = 0;
} else if (x >= info->width) {
x = info->width - 1;
}
if (y < 0) {
y = 0;
} else if (y >= info->height) {
y = info->height - 1;
}
int pixel_size = pixel_format_to_pixel_size(info->pixel_format);
return data + (y * info->width + x) * pixel_size;
}
static enum tga_error save_image(const uint8_t *data, const tga_info *info,
FILE *file) {
int pixel_size = pixel_format_to_pixel_size(info->pixel_format);
uint8_t header[HEADER_SIZE];
memset(header, 0, HEADER_SIZE);
if (info->pixel_format == TGA_PIXEL_BW8 ||
info->pixel_format == TGA_PIXEL_BW16) {
header[2] = (uint8_t)TGA_TYPE_GRAYSCALE;
} else {
header[2] = (uint8_t)TGA_TYPE_TRUE_COLOR;
}
header[12] = info->width & 0xFF;
header[13] = (info->width >> 8) & 0xFF;
header[14] = info->height & 0xFF;
header[15] = (info->height >> 8) & 0xFF;
header[16] = pixel_size * 8;
if (info->pixel_format == TGA_PIXEL_ARGB32) {
header[17] = 0x28;
} else {
header[17] = 0x20;
}
if (fwrite(header, 1, HEADER_SIZE, file) != HEADER_SIZE) {
return TGA_ERROR_FILE_CANNOT_WRITE;
}
size_t data_size = (size_t)info->width * info->height * pixel_size;
if (fwrite(data, 1, data_size, file) != data_size) {
return TGA_ERROR_FILE_CANNOT_WRITE;
}
return TGA_NO_ERROR;
}