Debian Codegraph @

addons/image/tga.c

raw
1 /* ______ ___ ___
2 * /\ _ \ /\_ \ /\_ \
3 * \ \ \L\ \\//\ \ \//\ \ __ __ _ __ ___
4 * \ \ __ \ \ \ \ \ \ \ /'__`\ /'_ `\/\`'__\/ __`\
5 * \ \ \/\ \ \_\ \_ \_\ \_/\ __//\ \L\ \ \ \//\ \L\ \
6 * \ \_\ \_\/\____\/\____\ \____\ \____ \ \_\\ \____/
7 * \/_/\/_/\/____/\/____/\/____/\/___L\ \/_/ \/___/
8 * /\____/
9 * \_/__/
10 *
11 * TGA reader.
12 *
13 * By Tim Gunn.
14 *
15 * RLE support added by Michal Mertl and Salvador Eduardo Tropea.
16 *
17 * Palette reading improved by Peter Wang.
18 *
19 * Big-endian support added by Eric Botcazou.
20 *
21 * See readme.txt for copyright information.
22 */
23
24
25 #include "allegro5/allegro.h"
26 #include "allegro5/allegro_image.h"
27 #include "allegro5/internal/aintern_image.h"
28 #include "allegro5/internal/aintern_pixels.h"
29
30 #include "iio.h"
31
32 ALLEGRO_DEBUG_CHANNEL("image")
33
34
35 /* raw_tga_read8:
36 * Helper for reading 256-color raw data from TGA files.
37 * Returns pointer past the end.
38 */
39 static INLINE unsigned char *raw_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f)
40 {
41 return b + al_fread(f, b, w);
42 }
43
44
45
46 /* rle_tga_read8:
47 * Helper for reading 256-color RLE data from TGA files.
48 * Returns pointer past the end or NULL for error.
49 */
50 static unsigned char *rle_tga_read8(unsigned char *b, int w, ALLEGRO_FILE *f)
51 {
52 int value, count, c = 0;
53
54 do {
55 count = al_fgetc(f);
56 if (count & 0x80) {
57 /* run-length packet */
58 count = (count & 0x7F) + 1;
59 c += count;
60 if (c > w) {
61 /* Stepped past the end of the line, error */
62 return NULL;
63 }
64 value = al_fgetc(f);
65 while (count--)
66 *b++ = value;
67 }
68 else {
69 /* raw packet */
70 count++;
71 c += count;
72 if (c > w) {
73 /* Stepped past the end of the line, error */
74 return NULL;
75 }
76 b = raw_tga_read8(b, count, f);
77 }
78 } while (c < w);
79 return b;
80 }
81
82
83
84 /* single_tga_read32:
85 * Helper for reading a single 32-bit data from TGA files.
86 */
87 static INLINE int32_t single_tga_read32(ALLEGRO_FILE *f)
88 {
89 return al_fread32le(f);
90 }
91
92
93
94 /* raw_tga_read32:
95 * Helper for reading 32-bit raw data from TGA files.
96 * Returns pointer past the end.
97 */
98 static unsigned int *raw_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f)
99 {
100 while (w--)
101 *b++ = single_tga_read32(f);
102
103 return b;
104 }
105
106
107
108 /* rle_tga_read32:
109 * Helper for reading 32-bit RLE data from TGA files.
110 * Returns pointer past the end or NULL for error.
111 */
112 static unsigned int *rle_tga_read32(unsigned int *b, int w, ALLEGRO_FILE *f)
113 {
114 int color, count, c = 0;
115
116 do {
117 count = al_fgetc(f);
118 if (count & 0x80) {
119 /* run-length packet */
120 count = (count & 0x7F) + 1;
121 c += count;
122 if (c > w) {
123 /* Stepped past the end of the line, error */
124 return NULL;
125 }
126 color = single_tga_read32(f);
127 while (count--)
128 *b++ = color;
129 }
130 else {
131 /* raw packet */
132 count++;
133 c += count;
134 if (c > w) {
135 /* Stepped past the end of the line, error */
136 return NULL;
137 }
138 b = raw_tga_read32(b, count, f);
139 }
140 } while (c < w);
141 return b;
142 }
143
144
145 /* single_tga_read24:
146 * Helper for reading a single 24-bit data from TGA files.
147 */
148 static INLINE void single_tga_read24(ALLEGRO_FILE *f, unsigned char color[3])
149 {
150 al_fread(f, color, 3);
151 }
152
153
154
155 /* raw_tga_read24:
156 * Helper for reading 24-bit raw data from TGA files.
157 * Returns pointer past the end.
158 */
159 static unsigned char *raw_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f)
160 {
161 while (w--) {
162 single_tga_read24(f, b);
163 b += 3;
164 }
165
166 return b;
167 }
168
169
170
171 /* rle_tga_read24:
172 * Helper for reading 24-bit RLE data from TGA files.
173 * Returns pointer past the end or NULL for error.
174 */
175 static unsigned char *rle_tga_read24(unsigned char *b, int w, ALLEGRO_FILE *f)
176 {
177 int count, c = 0;
178 unsigned char color[3];
179
180 do {
181 count = al_fgetc(f);
182 if (count & 0x80) {
183 /* run-length packet */
184 count = (count & 0x7F) + 1;
185 c += count;
186 if (c > w) {
187 /* Stepped past the end of the line, error */
188 return NULL;
189 }
190 single_tga_read24(f, color);
191 while (count--) {
192 b[0] = color[0];
193 b[1] = color[1];
194 b[2] = color[2];
195 b += 3;
196 }
197 }
198 else {
199 /* raw packet */
200 count++;
201 c += count;
202 if (c > w) {
203 /* Stepped past the end of the line, error */
204 return NULL;
205 }
206 b = raw_tga_read24(b, count, f);
207 }
208 } while (c < w);
209 return b;
210 }
211
212
213
214 /* single_tga_read16:
215 * Helper for reading a single 16-bit data from TGA files.
216 */
217 static INLINE int single_tga_read16(ALLEGRO_FILE *f)
218 {
219 return al_fread16le(f);
220 }
221
222
223
224 /* raw_tga_read16:
225 * Helper for reading 16-bit raw data from TGA files.
226 * Returns pointer past the end.
227 */
228 static unsigned short *raw_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f)
229 {
230 while (w--)
231 *b++ = single_tga_read16(f);
232
233 return b;
234 }
235
236
237
238 /* rle_tga_read16:
239 * Helper for reading 16-bit RLE data from TGA files.
240 * Returns pointer past the end or NULL for error.
241 */
242 static unsigned short *rle_tga_read16(unsigned short *b, int w, ALLEGRO_FILE *f)
243 {
244 int color, count, c = 0;
245
246 do {
247 count = al_fgetc(f);
248 if (count & 0x80) {
249 /* run-length packet */
250 count = (count & 0x7F) + 1;
251 c += count;
252 if (c > w) {
253 /* Stepped past the end of the line, error */
254 return NULL;
255 }
256 color = single_tga_read16(f);
257 while (count--)
258 *b++ = color;
259 }
260 else {
261 /* raw packet */
262 count++;
263 c += count;
264 if (c > w) {
265 /* Stepped past the end of the line, error */
266 return NULL;
267 }
268 b = raw_tga_read16(b, count, f);
269 }
270 } while (c < w);
271 return b;
272 }
273
274 typedef unsigned char palette_entry[3];
275
276 /* Like load_tga, but starts loading from the current place in the ALLEGRO_FILE
277 * specified. If successful the offset into the file will be left just after
278 * the image data. If unsuccessful the offset into the file is unspecified,
279 * i.e. you must either reset the offset to some known place or close the
280 * packfile. The packfile is not closed by this function.
281 */
282 ALLEGRO_BITMAP *_al_load_tga_f(ALLEGRO_FILE *f, int flags)
283 {
284 unsigned char image_id[256];
285 palette_entry image_palette[256];
286 unsigned char id_length, palette_type, image_type, palette_entry_size;
287 unsigned char bpp, descriptor_bits;
288 short unsigned int palette_colors;
289 short unsigned int palette_start;
290 short unsigned int image_width, image_height;
291 bool left_to_right;
292 bool top_to_bottom;
293 unsigned int c, i;
294 int y;
295 int compressed;
296 ALLEGRO_BITMAP *bmp;
297 ALLEGRO_LOCKED_REGION *lr;
298 unsigned char *buf;
299 bool premul = !(flags & ALLEGRO_NO_PREMULTIPLIED_ALPHA);
300 ASSERT(f);
301
302 id_length = al_fgetc(f);
303 palette_type = al_fgetc(f);
304 image_type = al_fgetc(f);
305 palette_start = al_fread16le(f); /* first_color */
306 palette_colors = al_fread16le(f);
307 palette_entry_size = al_fgetc(f);
308 al_fread16le(f); /* left */
309 al_fread16le(f); /* top */
310 image_width = al_fread16le(f);
311 image_height = al_fread16le(f);
312 bpp = al_fgetc(f);
313 descriptor_bits = al_fgetc(f);
314
315 left_to_right = !(descriptor_bits & (1 << 4));
316 top_to_bottom = (descriptor_bits & (1 << 5));
317
318 al_fread(f, image_id, id_length);
319
320 /* Image type:
321 * 0 = no image data
322 * 1 = uncompressed color mapped
323 * 2 = uncompressed true color
324 * 3 = grayscale
325 * 9 = RLE color mapped
326 * 10 = RLE true color
327 * 11 = RLE grayscale
328 */
329 compressed = (image_type & 8);
330 image_type &= 7;
331
332 if ((image_type < 1) || (image_type > 3)) {
333 ALLEGRO_ERROR("Invalid image type %d.\n", image_type);
334 return NULL;
335 }
336
337 switch (image_type) {
338
339 case 1:
340 /* paletted image */
341 /* Only support 8 bit palettes (up to 256 entries) though in principle the file format could have more(?)*/
342 if ((palette_type != 1) || (bpp != 8) || (palette_start + palette_colors) > 256) {
343 ALLEGRO_ERROR("Invalid image/palette/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp);
344 return NULL;
345 }
346
347 break;
348
349 case 2:
350 /* truecolor image */
351 if ((palette_type == 0) && ((bpp == 15) || (bpp == 16))) {
352 bpp = 15;
353 }
354 else if ((palette_type == 0) && ((bpp == 24) || (bpp == 32))) {
355 }
356 else {
357 ALLEGRO_ERROR("Invalid image/palette/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp);
358 return NULL;
359 }
360 break;
361
362 case 3:
363 /* grayscale image */
364 if ((palette_type != 0) || (bpp != 8)) {
365 ALLEGRO_ERROR("Invalid image/palette/bpp combination %d/%d/%d.\n", image_type, palette_type, bpp);
366 return NULL;
367 }
368
369 for (i=0; i<256; i++) {
370 image_palette[i][0] = i;
371 image_palette[i][1] = i;
372 image_palette[i][2] = i;
373 }
374 break;
375
376 default:
377 ALLEGRO_ERROR("Invalid image type %d.\n", image_type);
378 return NULL;
379 }
380
381 if (palette_type == 0) {
382 /* No-op */
383 } else if (palette_type == 1) {
384 for (i = 0; i < palette_colors; i++) {
385 palette_entry *entry = image_palette + palette_start + i;
386 switch (palette_entry_size) {
387 case 16:
388 c = al_fread16le(f);
389 (*entry)[0] = (c & 0x1F) << 3;
390 (*entry)[1] = ((c >> 5) & 0x1F) << 3;
391 (*entry)[2] = ((c >> 10) & 0x1F) << 3;
392 break;
393 case 24:
394 (*entry)[0] = al_fgetc(f);
395 (*entry)[1] = al_fgetc(f);
396 (*entry)[2] = al_fgetc(f);
397 break;
398 case 32:
399 (*entry)[0] = al_fgetc(f);
400 (*entry)[1] = al_fgetc(f);
401 (*entry)[2] = al_fgetc(f);
402 al_fgetc(f); /* Ignore 4th byte (alpha) */
403 break;
404 }
405 }
406 }
407 else {
408 ALLEGRO_ERROR("Invalid palette type %d.\n", palette_type);
409 return NULL;
410 }
411
412
413 bmp = al_create_bitmap(image_width, image_height);
414 if (!bmp) {
415 ALLEGRO_ERROR("Failed to create bitmap.\n");
416 return NULL;
417 }
418
419 al_set_errno(0);
420
421 lr = al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ABGR_8888_LE, ALLEGRO_LOCK_WRITEONLY);
422 if (!lr) {
423 ALLEGRO_ERROR("Failed to lock bitmap.\n");
424 al_destroy_bitmap(bmp);
425 return NULL;
426 }
427
428 /* bpp + 1 accounts for 15 bpp. */
429 buf = al_malloc(image_width * ((bpp + 1) / 8));
430 if (!buf) {
431 al_unlock_bitmap(bmp);
432 al_destroy_bitmap(bmp);
433 ALLEGRO_ERROR("Failed to allocate enough memory.\n");
434 return NULL;
435 }
436
437 for (y = 0; y < image_height; y++) {
438 int true_y = (top_to_bottom) ? y : (image_height - 1 - y);
439
440 switch (image_type) {
441
442 case 1:
443 case 3: {
444 unsigned char *ptr;
445 if (compressed)
446 ptr = rle_tga_read8(buf, image_width, f);
447 else
448 ptr = raw_tga_read8(buf, image_width, f);
449 if (!ptr) {
450 al_free(buf);
451 al_unlock_bitmap(bmp);
452 al_destroy_bitmap(bmp);
453 ALLEGRO_ERROR("Invalid image data.\n");
454 return NULL;
455 }
456 }
457 for (i = 0; i < image_width; i++) {
458 int true_x = (left_to_right) ? i : (image_width - 1 - i);
459 int pix = buf[i];
460
461 unsigned char *dest = (unsigned char *)lr->data +
462 lr->pitch*true_y + true_x*4;
463 if (pix < palette_start || pix >= (palette_start + palette_colors)) {
464 al_free(buf);
465 al_unlock_bitmap(bmp);
466 al_destroy_bitmap(bmp);
467 ALLEGRO_ERROR("Invalid image data.\n");
468 return NULL;
469 }
470 palette_entry* entry = image_palette + pix;
471 dest[0] = (*entry)[2];
472 dest[1] = (*entry)[1];
473 dest[2] = (*entry)[0];
474 dest[3] = 255;
475 }
476
477 break;
478
479 case 2:
480 if (bpp == 32) {
481 unsigned int *ptr;
482 if (compressed)
483 ptr = rle_tga_read32((unsigned int *)buf, image_width, f);
484 else
485 ptr = raw_tga_read32((unsigned int *)buf, image_width, f);
486 if (!ptr) {
487 al_free(buf);
488 al_unlock_bitmap(bmp);
489 al_destroy_bitmap(bmp);
490 ALLEGRO_ERROR("Invalid image data.\n");
491 return NULL;
492 }
493 for (i = 0; i < image_width; i++) {
494 int true_x = (left_to_right) ? i : (image_width - 1 - i);
495 unsigned char *dest = (unsigned char *)lr->data +
496 lr->pitch*true_y + true_x*4;
497
498 #ifdef ALLEGRO_BIG_ENDIAN
499 int a = buf[i * 4 + 0];
500 int r = buf[i * 4 + 1];
501 int g = buf[i * 4 + 2];
502 int b = buf[i * 4 + 3];
503 #else
504 int b = buf[i * 4 + 0];
505 int g = buf[i * 4 + 1];
506 int r = buf[i * 4 + 2];
507 int a = buf[i * 4 + 3];
508 #endif
509 if (premul) {
510 r = r * a / 255;
511 g = g * a / 255;
512 b = b * a / 255;
513 }
514
515 dest[0] = r;
516 dest[1] = g;
517 dest[2] = b;
518 dest[3] = a;
519 }
520 }
521 else if (bpp == 24) {
522 unsigned char *ptr;
523 if (compressed)
524 ptr = rle_tga_read24(buf, image_width, f);
525 else
526 ptr = raw_tga_read24(buf, image_width, f);
527 if (!ptr) {
528 al_free(buf);
529 al_unlock_bitmap(bmp);
530 al_destroy_bitmap(bmp);
531 ALLEGRO_ERROR("Invalid image data.\n");
532 return NULL;
533 }
534 for (i = 0; i < image_width; i++) {
535 int true_x = (left_to_right) ? i : (image_width - 1 - i);
536 int b = buf[i * 3 + 0];
537 int g = buf[i * 3 + 1];
538 int r = buf[i * 3 + 2];
539
540 unsigned char *dest = (unsigned char *)lr->data +
541 lr->pitch*true_y + true_x*4;
542 dest[0] = r;
543 dest[1] = g;
544 dest[2] = b;
545 dest[3] = 255;
546 }
547 }
548 else {
549 unsigned short *ptr;
550 if (compressed)
551 ptr = rle_tga_read16((unsigned short *)buf, image_width, f);
552 else
553 ptr = raw_tga_read16((unsigned short *)buf, image_width, f);
554 if (!ptr) {
555 al_free(buf);
556 al_unlock_bitmap(bmp);
557 al_destroy_bitmap(bmp);
558 ALLEGRO_ERROR("Invalid image data.\n");
559 return NULL;
560 }
561 for (i = 0; i < image_width; i++) {
562 int true_x = (left_to_right) ? i : (image_width - 1 - i);
563 int pix = *((unsigned short *)(buf + i * 2));
564 /* TODO - do something with the 1-bit A value (alpha?) */
565 int r = _al_rgb_scale_5[(pix >> 10) & 0x1F];
566 int g = _al_rgb_scale_5[(pix >> 5) & 0x1F];
567 int b = _al_rgb_scale_5[(pix & 0x1F)];
568
569 unsigned char *dest = (unsigned char *)lr->data +
570 lr->pitch*true_y + true_x*4;
571 dest[0] = r;
572 dest[1] = g;
573 dest[2] = b;
574 dest[3] = 255;
575 }
576 }
577 break;
578 }
579 }
580
581 al_free(buf);
582 al_unlock_bitmap(bmp);
583
584 if (al_get_errno()) {
585 ALLEGRO_ERROR("Error detected: %d.\n", al_get_errno());
586 al_destroy_bitmap(bmp);
587 return NULL;
588 }
589
590 return bmp;
591 }
592
593
594
595 /* Like save_tga but writes into the ALLEGRO_FILE given instead of a new file.
596 * The packfile is not closed after writing is completed. On success the
597 * offset into the file is left after the TGA file just written. On failure
598 * the offset is left at the end of whatever incomplete data was written.
599 */
600 bool _al_save_tga_f(ALLEGRO_FILE *f, ALLEGRO_BITMAP *bmp)
601 {
602 int x, y;
603 int w, h;
604 ASSERT(f);
605 ASSERT(bmp);
606
607 al_set_errno(0);
608
609 w = al_get_bitmap_width(bmp);
610 h = al_get_bitmap_height(bmp);
611
612 al_fputc(f, 0); /* id length (no id saved) */
613 al_fputc(f, 0); /* palette type */
614 al_fputc(f, 2); /* image type */
615 al_fwrite16le(f, 0); /* first colour */
616 al_fwrite16le(f, 0); /* number of colours */
617 al_fputc(f, 0); /* palette entry size */
618 al_fwrite16le(f, 0); /* left */
619 al_fwrite16le(f, 0); /* top */
620 al_fwrite16le(f, w); /* width */
621 al_fwrite16le(f, h); /* height */
622 al_fputc(f, 32); /* bits per pixel */
623 al_fputc(f, 8); /* descriptor (bottom to top, 8-bit alpha) */
624
625 al_lock_bitmap(bmp, ALLEGRO_PIXEL_FORMAT_ANY, ALLEGRO_LOCK_READONLY);
626
627 for (y = h - 1; y >= 0; y--) {
628 for (x = 0; x < w; x++) {
629 ALLEGRO_COLOR c = al_get_pixel(bmp, x, y);
630 unsigned char r, g, b, a;
631 al_unmap_rgba(c, &r, &g, &b, &a);
632 al_fputc(f, b);
633 al_fputc(f, g);
634 al_fputc(f, r);
635 al_fputc(f, a);
636 }
637 }
638
639 al_unlock_bitmap(bmp);
640
641 return al_get_errno() ? false : true;
642 }
643
644
645 ALLEGRO_BITMAP *_al_load_tga(const char *filename, int flags)
646 {
647 ALLEGRO_FILE *f;
648 ALLEGRO_BITMAP *bmp;
649 ASSERT(filename);
650
651 f = al_fopen(filename, "rb");
652 if (!f) {
653 ALLEGRO_ERROR("Unable to open %s for reading.\n", filename);
654 return NULL;
655 }
656
657 bmp = _al_load_tga_f(f, flags);
658
659 al_fclose(f);
660
661 return bmp;
662 }
663
664
665
666 bool _al_save_tga(const char *filename, ALLEGRO_BITMAP *bmp)
667 {
668 ALLEGRO_FILE *f;
669 bool retsave;
670 bool retclose;
671 ASSERT(filename);
672
673 f = al_fopen(filename, "wb");
674 if (!f) {
675 ALLEGRO_ERROR("Unable to open %s for writing.\n", filename);
676 return false;
677 }
678
679 retsave = _al_save_tga_f(f, bmp);
680
681 retclose = al_fclose(f);
682
683 return retsave && retclose;
684 }
685
686
687 bool _al_identify_tga(ALLEGRO_FILE *f)
688 {
689 uint8_t x[4];
690 al_fgetc(f); // skip id length
691 al_fread(f, x, 4);
692
693 if (x[0] > 1) // TGA colormap must be 0 or 1
694 return false;
695 if ((x[1] & 0xf7) == 0) // type must be 1, 2, 3, 9, 10 or 11
696 return false;
697 if (x[2] != 0 || x[3] != 0) // color map must start at 0
698 return false;
699 return true;
700 }
701
702
703 /* vim: set sts=3 sw=3 et: */