- convert t/t104ppm.t to Test::More
[imager.git] / tga.c
CommitLineData
1ec86afa 1#include "image.h"
1ec86afa
AMH
2#include "log.h"
3#include "iolayer.h"
4
5#include <stdlib.h>
6#include <errno.h>
7
8
9/*
10=head1 NAME
11
12tga.c - implements reading and writing targa files, uses io layer.
13
14=head1 SYNOPSIS
15
16 io_glue *ig = io_new_fd( fd );
17 i_img *im = i_readtga_wiol(ig, -1); // no limit on how much is read
18 // or
19 io_glue *ig = io_new_fd( fd );
20 return_code = i_writetga_wiol(im, ig);
21
22=head1 DESCRIPTION
23
24tga.c implements the basic functions to read and write portable targa
25files. It uses the iolayer and needs either a seekable source or an
26entire memory mapped buffer.
27
28=head1 FUNCTION REFERENCE
29
30Some of these functions are internal.
31
b8c2033e 32=over
1ec86afa
AMH
33
34=cut
35*/
36
37
1ec86afa
AMH
38
39
40typedef struct {
b96be931 41 unsigned char idlength;
1ec86afa
AMH
42 char colourmaptype;
43 char datatypecode;
44 short int colourmaporigin;
45 short int colourmaplength;
46 char colourmapdepth;
47 short int x_origin;
48 short int y_origin;
49 short width;
50 short height;
51 char bitsperpixel;
52 char imagedescriptor;
53} tga_header;
54
55
76ff75b8
AMH
56typedef enum { NoInit, Raw, Rle } rle_state;
57
920aa4a5
AMH
58typedef struct {
59 int compressed;
60 int bytepp;
76ff75b8 61 rle_state state;
920aa4a5
AMH
62 unsigned char cval[4];
63 int len;
64 unsigned char hdr;
65 io_glue *ig;
66} tga_source;
67
68
76ff75b8
AMH
69typedef struct {
70 int compressed;
71 int bytepp;
72 io_glue *ig;
73} tga_dest;
74
75
76
febba01f
AMH
77/*
78=item bpp_to_bytes(bpp)
79
80Convert bits per pixel into bytes per pixel
81
82 bpp - bits per pixel
83
84=cut
85*/
86
87
920aa4a5
AMH
88static
89int
90bpp_to_bytes(unsigned int bpp) {
91 switch (bpp) {
92 case 8:
93 return 1;
94 case 15:
95 case 16:
96 return 2;
97 case 24:
98 return 3;
99 case 32:
100 return 4;
101 }
102 return 0;
103}
104
febba01f
AMH
105
106
107/*
108=item bpp_to_channels(bpp)
109
110Convert bits per pixel into channels in the image
111
112 bpp - bits per pixel
113
114=cut
115*/
116
920aa4a5
AMH
117static
118int
119bpp_to_channels(unsigned int bpp) {
120 switch (bpp) {
121 case 8:
122 return 1;
123 case 15:
124 return 3;
125 case 16:
126 return 4;
127 case 24:
128 return 3;
129 case 32:
130 return 4;
131 }
132 return 0;
133}
134
135
136
76ff75b8
AMH
137/*
138 * Packing functions - used for (un)packing
139 * datastructures into raw bytes.
febba01f
AMH
140 */
141
142
143/*
144=item color_unpack(buf, bytepp, val)
76ff75b8 145
febba01f
AMH
146Unpacks bytes into colour structures, for 2 byte type the first byte
147coming from the file will actually be GGGBBBBB, and the second will be
148ARRRRRGG. "A" represents an attribute bit. The 3 byte entry contains
1491 byte each of blue, green, and red. The 4 byte entry contains 1 byte
150each of blue, green, red, and attribute.
76ff75b8 151
febba01f
AMH
152 buf - pointer to data
153 bytepp - bytes per pixel
154 val - pointer to color to store to
920aa4a5 155
febba01f 156=cut
920aa4a5
AMH
157*/
158
159static
160void
161color_unpack(unsigned char *buf, int bytepp, i_color *val) {
162 switch (bytepp) {
163 case 1:
164 val->gray.gray_color = buf[0];
165 break;
166 case 2:
167 val->rgba.r = (buf[1] & 0x7c) << 1;
168 val->rgba.g = ((buf[1] & 0x03) << 6) | ((buf[0] & 0xe0) >> 2);
169 val->rgba.b = (buf[0] & 0x1f) << 3;
76ff75b8
AMH
170 val->rgba.a = (buf[1] & 0x80) ? 255 : 0;
171 val->rgba.r |= val->rgba.r >> 5;
172 val->rgba.g |= val->rgba.g >> 5;
173 val->rgba.b |= val->rgba.b >> 5;
920aa4a5
AMH
174 break;
175 case 3:
176 val->rgb.b = buf[0];
177 val->rgb.g = buf[1];
178 val->rgb.r = buf[2];
179 break;
180 case 4:
181 val->rgba.b = buf[0];
182 val->rgba.g = buf[1];
183 val->rgba.r = buf[2];
184 val->rgba.a = buf[3];
185 break;
920aa4a5
AMH
186 }
187}
188
76ff75b8
AMH
189
190
febba01f
AMH
191/*
192=item color_pack
76ff75b8 193
febba01f
AMH
194Packs a colour into an array of bytes, for 2 byte type the first byte
195will be GGGBBBBB, and the second will be ARRRRRGG. "A" represents an
76ff75b8
AMH
196attribute bit. The 3 byte entry contains 1 byte each of blue, green,
197and red. The 4 byte entry contains 1 byte each of blue, green, red,
198and attribute.
febba01f
AMH
199
200 buf - destination buffer
201 bitspp - bits per pixel
202 val - color to pack
203
204=cut
920aa4a5
AMH
205*/
206
76ff75b8
AMH
207static
208void
209color_pack(unsigned char *buf, int bitspp, i_color *val) {
210 switch (bitspp) {
211 case 8:
212 buf[0] = val->gray.gray_color;
213 break;
214 case 15:
215 buf[0] = (val->rgba.b >> 3);
216 buf[0] |= (val->rgba.g & 0x38) << 2;
217 buf[1] = (val->rgba.r & 0xf8)>> 1;
218 buf[1] |= (val->rgba.g >> 6);
219 case 16:
220 buf[1] |= val->rgba.a & 0x80;
221 break;
222 case 24:
223 buf[0] = val->rgb.b;
224 buf[1] = val->rgb.g;
225 buf[2] = val->rgb.r;
226 break;
227 case 32:
228 buf[0] = val->rgba.b;
229 buf[1] = val->rgba.g;
230 buf[2] = val->rgba.r;
231 buf[3] = val->rgba.a;
232 break;
76ff75b8 233 }
76ff75b8
AMH
234}
235
236
febba01f
AMH
237/*
238=item find_repeat
239
240Helper function for rle compressor to find the next triple repeat of the
241same pixel value in buffer.
242
243 buf - buffer
244 length - number of pixel values in buffer
245 bytepp - number of bytes in a pixel value
76ff75b8 246
febba01f
AMH
247=cut
248*/
76ff75b8
AMH
249
250static
251int
252find_repeat(unsigned char *buf, int length, int bytepp) {
253 int i = 0;
254
255 while(i<length-1) {
256 if(memcmp(buf+i*bytepp, buf+(i+1)*bytepp, bytepp) == 0) {
257 if (i == length-2) return -1;
258 if (memcmp(buf+(i+1)*bytepp, buf+(i+2)*bytepp,bytepp) == 0)
259 return i;
260 else i++;
261 }
262 i++;
263 }
264 return -1;
265}
266
267
febba01f
AMH
268/*
269=item find_span
270
271Helper function for rle compressor to find the length of a span where
272the same pixel value is in the buffer.
273
274 buf - buffer
275 length - number of pixel values in buffer
276 bytepp - number of bytes in a pixel value
277
278=cut
279*/
280
76ff75b8
AMH
281static
282int
283find_span(unsigned char *buf, int length, int bytepp) {
284 int i = 0;
285 while(i<length) {
286 if(memcmp(buf, buf+(i*bytepp), bytepp) != 0) return i;
287 i++;
288 }
289 return length;
290}
291
292
febba01f
AMH
293/*
294=item tga_header_unpack(header, headbuf)
295
296Unpacks the header structure into from buffer and stores
297in the header structure.
298
299 header - header structure
300 headbuf - buffer to unpack from
301
302=cut
303*/
304
305static
306void
307tga_header_unpack(tga_header *header, unsigned char headbuf[18]) {
308 header->idlength = headbuf[0];
309 header->colourmaptype = headbuf[1];
310 header->datatypecode = headbuf[2];
311 header->colourmaporigin = (headbuf[4] << 8) + headbuf[3];
312 header->colourmaplength = (headbuf[6] << 8) + headbuf[5];
313 header->colourmapdepth = headbuf[7];
314 header->x_origin = (headbuf[9] << 8) + headbuf[8];
315 header->y_origin = (headbuf[11] << 8) + headbuf[10];
316 header->width = (headbuf[13] << 8) + headbuf[12];
317 header->height = (headbuf[15] << 8) + headbuf[14];
318 header->bitsperpixel = headbuf[16];
319 header->imagedescriptor = headbuf[17];
320}
321
322
63e0dc02 323/* this function should never produce diagnostics to stdout, maybe to the logfile */
84e51293
AMH
324int
325tga_header_verify(unsigned char headbuf[18]) {
326 tga_header header;
327 tga_header_unpack(&header, headbuf);
328 switch (header.datatypecode) {
329 default:
63e0dc02 330 /*printf("bad typecode!\n");*/
84e51293
AMH
331 return 0;
332 case 0:
333 case 1: /* Uncompressed, color-mapped images */
334 case 2: /* Uncompressed, rgb images */
335 case 3: /* Uncompressed, grayscale images */
336 case 9: /* Compressed, color-mapped images */
337 case 10: /* Compressed, rgb images */
338 case 11: /* Compressed, grayscale images */
221cbd78
AMH
339 break;
340 }
84e51293
AMH
341
342 switch (header.colourmaptype) {
343 default:
63e0dc02 344 /*printf("bad colourmaptype!\n");*/
84e51293
AMH
345 return 0;
346 case 0:
347 case 1:
221cbd78
AMH
348 break;
349 }
84e51293
AMH
350
351 return 1;
352}
353
354
febba01f
AMH
355/*
356=item tga_header_pack(header, headbuf)
357
358Packs header structure into buffer for writing.
76ff75b8 359
febba01f
AMH
360 header - header structure
361 headbuf - buffer to pack into
76ff75b8 362
febba01f
AMH
363=cut
364*/
365
366static
367void
368tga_header_pack(tga_header *header, unsigned char headbuf[18]) {
369 headbuf[0] = header->idlength;
370 headbuf[1] = header->colourmaptype;
371 headbuf[2] = header->datatypecode;
372 headbuf[3] = header->colourmaporigin & 0xff;
373 headbuf[4] = header->colourmaporigin >> 8;
374 headbuf[5] = header->colourmaplength & 0xff;
375 headbuf[6] = header->colourmaplength >> 8;
376 headbuf[7] = header->colourmapdepth;
377 headbuf[8] = header->x_origin & 0xff;
378 headbuf[9] = header->x_origin >> 8;
379 headbuf[10] = header->y_origin & 0xff;
380 headbuf[11] = header->y_origin >> 8;
381 headbuf[12] = header->width & 0xff;
382 headbuf[13] = header->width >> 8;
383 headbuf[14] = header->height & 0xff;
384 headbuf[15] = header->height >> 8;
385 headbuf[16] = header->bitsperpixel;
386 headbuf[17] = header->imagedescriptor;
387}
388
389
390/*
391=item tga_source_read(s, buf, pixels)
392
393Reads pixel number of pixels from source s into buffer buf. Takes
394care of decompressing the stream if needed.
395
396 s - data source
397 buf - destination buffer
398 pixels - number of pixels to put into buffer
399
400=cut
401*/
76ff75b8 402
920aa4a5
AMH
403static
404int
405tga_source_read(tga_source *s, unsigned char *buf, size_t pixels) {
406 int cp = 0, j, k;
407 if (!s->compressed) {
408 if (s->ig->readcb(s->ig, buf, pixels*s->bytepp) != pixels*s->bytepp) return 0;
409 return 1;
410 }
411
412 while(cp < pixels) {
413 int ml;
414 if (s->len == 0) s->state = NoInit;
415 switch (s->state) {
416 case NoInit:
417 if (s->ig->readcb(s->ig, &s->hdr, 1) != 1) return 0;
418
419 s->len = (s->hdr &~(1<<7))+1;
420 s->state = (s->hdr & (1<<7)) ? Rle : Raw;
9a88a5e6 421 {
84e51293 422/*
9a88a5e6
AMH
423 static cnt = 0;
424 printf("%04d %s: %d\n", cnt++, s->state==Rle?"RLE":"RAW", s->len);
84e51293
AMH
425 */
426 }
920aa4a5
AMH
427 if (s->state == Rle && s->ig->readcb(s->ig, s->cval, s->bytepp) != s->bytepp) return 0;
428
429 break;
430 case Rle:
b33c08f8 431 ml = i_min(s->len, pixels-cp);
920aa4a5
AMH
432 for(k=0; k<ml; k++) for(j=0; j<s->bytepp; j++)
433 buf[(cp+k)*s->bytepp+j] = s->cval[j];
920aa4a5
AMH
434 cp += ml;
435 s->len -= ml;
436 break;
437 case Raw:
b33c08f8 438 ml = i_min(s->len, pixels-cp);
920aa4a5
AMH
439 if (s->ig->readcb(s->ig, buf+cp*s->bytepp, ml*s->bytepp) != ml*s->bytepp) return 0;
440 cp += ml;
441 s->len -= ml;
442 break;
443 }
444 }
445 return 1;
446}
447
76ff75b8
AMH
448
449
76ff75b8 450
febba01f
AMH
451/*
452=item tga_dest_write(s, buf, pixels)
453
454Writes pixels from buf to destination s. Takes care of compressing if the
455destination is compressed.
456
457 s - data destination
458 buf - source buffer
459 pixels - number of pixels to put write to destination
460
461=cut
76ff75b8
AMH
462*/
463
464static
465int
466tga_dest_write(tga_dest *s, unsigned char *buf, size_t pixels) {
63e0dc02 467 int cp = 0;
76ff75b8
AMH
468
469 if (!s->compressed) {
470 if (s->ig->writecb(s->ig, buf, pixels*s->bytepp) != pixels*s->bytepp) return 0;
471 return 1;
472 }
473
474 while(cp < pixels) {
475 int tlen;
476 int nxtrip = find_repeat(buf+cp*s->bytepp, pixels-cp, s->bytepp);
477 tlen = (nxtrip == -1) ? pixels-cp : nxtrip;
478 while(tlen) {
9a88a5e6 479 unsigned char clen = (tlen>128) ? 128 : tlen;
76ff75b8
AMH
480 clen--;
481 if (s->ig->writecb(s->ig, &clen, 1) != 1) return 0;
482 clen++;
483 if (s->ig->writecb(s->ig, buf+cp*s->bytepp, clen*s->bytepp) != clen*s->bytepp) return 0;
484 tlen -= clen;
485 cp += clen;
486 }
487 if (cp >= pixels) break;
488 tlen = find_span(buf+cp*s->bytepp, pixels-cp, s->bytepp);
07d70837 489 if (tlen <3) continue;
76ff75b8 490 while (tlen) {
9a88a5e6 491 unsigned char clen = (tlen>128) ? 128 : tlen;
76ff75b8
AMH
492 clen = (clen - 1) | 0x80;
493 if (s->ig->writecb(s->ig, &clen, 1) != 1) return 0;
494 clen = (clen & ~0x80) + 1;
495 if (s->ig->writecb(s->ig, buf+cp*s->bytepp, s->bytepp) != s->bytepp) return 0;
496 tlen -= clen;
497 cp += clen;
498 }
499 }
500 return 1;
501}
502
503
504
505
506
507
febba01f
AMH
508/*
509=item tga_palette_read(ig, img, bytepp, colourmaplength)
510
511Reads the colormap from a tga file and stores in the paletted image
512structure.
76ff75b8 513
febba01f
AMH
514 ig - iolayer data source
515 img - image structure
516 bytepp - bytes per pixel
517 colourmaplength - number of colours in colourmap
518
519=cut
520*/
76ff75b8 521
920aa4a5
AMH
522static
523int
524tga_palette_read(io_glue *ig, i_img *img, int bytepp, int colourmaplength) {
525 int i;
526 size_t palbsize;
527 unsigned char *palbuf;
528 i_color val;
529
530 palbsize = colourmaplength*bytepp;
531 palbuf = mymalloc(palbsize);
532
533 if (ig->readcb(ig, palbuf, palbsize) != palbsize) {
534 i_push_error(errno, "could not read targa colourmap");
535 return 0;
536 }
537
538 /* populate the palette of the new image */
539 for(i=0; i<colourmaplength; i++) {
540 color_unpack(palbuf+i*bytepp, bytepp, &val);
541 i_addcolors(img, &val, 1);
542 }
543 myfree(palbuf);
544 return 1;
545}
546
547
febba01f
AMH
548/*
549=item tga_palette_write(ig, img, bitspp, colourmaplength)
550
551Stores the colormap of an image in the destination ig.
552
553 ig - iolayer data source
554 img - image structure
555 bitspp - bits per pixel in colourmap
556 colourmaplength - number of colours in colourmap
557
558=cut
559*/
920aa4a5 560
76ff75b8
AMH
561static
562int
563tga_palette_write(io_glue *ig, i_img *img, int bitspp, int colourmaplength) {
564 int i;
565 int bytepp = bpp_to_bytes(bitspp);
566 size_t palbsize = i_colorcount(img)*bytepp;
567 unsigned char *palbuf = mymalloc(palbsize);
568
569 for(i=0; i<colourmaplength; i++) {
570 i_color val;
571 i_getcolors(img, i, &val, 1);
572 color_pack(palbuf+i*bytepp, bitspp, &val);
573 }
574
575 if (ig->writecb(ig, palbuf, palbsize) != palbsize) {
576 i_push_error(errno, "could not write targa colourmap");
577 return 0;
578 }
579 myfree(palbuf);
580 return 1;
581}
582
920aa4a5 583
1ec86afa
AMH
584
585/*
586=item i_readtga_wiol(ig, length)
587
febba01f
AMH
588Read in an image from the iolayer data source and return the image structure to it.
589Returns NULL on error.
1ec86afa
AMH
590
591 ig - io_glue object
592 length - maximum length to read from data source, before closing it -1
593 signifies no limit.
594
595=cut
596*/
597
598i_img *
599i_readtga_wiol(io_glue *ig, int length) {
07d70837 600 i_img* img = NULL;
a659442a 601 int x, y;
1ec86afa 602 int width, height, channels;
920aa4a5 603 int mapped;
07d70837 604 char *idstring = NULL;
1ec86afa 605
920aa4a5 606 tga_source src;
1ec86afa 607 tga_header header;
1ec86afa 608 unsigned char headbuf[18];
7c58edfc 609 unsigned char *databuf;
920aa4a5
AMH
610
611 i_color *linebuf = NULL;
1ec86afa
AMH
612 i_clear_error();
613
614 mm_log((1,"i_readtga(ig %p, length %d)\n", ig, length));
615
616 io_glue_commit_types(ig);
617
618 if (ig->readcb(ig, &headbuf, 18) != 18) {
619 i_push_error(errno, "could not read targa header");
620 return NULL;
621 }
622
76ff75b8 623 tga_header_unpack(&header, headbuf);
1ec86afa
AMH
624
625 mm_log((1,"Id length: %d\n",header.idlength));
626 mm_log((1,"Colour map type: %d\n",header.colourmaptype));
627 mm_log((1,"Image type: %d\n",header.datatypecode));
628 mm_log((1,"Colour map offset: %d\n",header.colourmaporigin));
629 mm_log((1,"Colour map length: %d\n",header.colourmaplength));
630 mm_log((1,"Colour map depth: %d\n",header.colourmapdepth));
631 mm_log((1,"X origin: %d\n",header.x_origin));
632 mm_log((1,"Y origin: %d\n",header.y_origin));
633 mm_log((1,"Width: %d\n",header.width));
634 mm_log((1,"Height: %d\n",header.height));
635 mm_log((1,"Bits per pixel: %d\n",header.bitsperpixel));
636 mm_log((1,"Descriptor: %d\n",header.imagedescriptor));
637
1ec86afa
AMH
638 if (header.idlength) {
639 idstring = mymalloc(header.idlength+1);
640 if (ig->readcb(ig, idstring, header.idlength) != header.idlength) {
641 i_push_error(errno, "short read on targa idstring");
642 return NULL;
643 }
1ec86afa
AMH
644 }
645
646 width = header.width;
647 height = header.height;
648
649 /* Set tags here */
920aa4a5 650
1ec86afa 651 switch (header.datatypecode) {
1ec86afa
AMH
652 case 0: /* No data in image */
653 i_push_error(0, "Targa image contains no image data");
07d70837 654 if (idstring) myfree(idstring);
1ec86afa
AMH
655 return NULL;
656 break;
920aa4a5
AMH
657 case 1: /* Uncompressed, color-mapped images */
658 case 9: /* Compressed, color-mapped images */
659 case 3: /* Uncompressed, grayscale images */
660 case 11: /* Compressed, grayscale images */
1ec86afa 661 if (header.bitsperpixel != 8) {
920aa4a5 662 i_push_error(0, "Targa: mapped/grayscale image's bpp is not 8, unsupported.");
07d70837 663 if (idstring) myfree(idstring);
1ec86afa
AMH
664 return NULL;
665 }
920aa4a5 666 src.bytepp = 1;
1ec86afa 667 break;
920aa4a5
AMH
668 case 2: /* Uncompressed, rgb images */
669 case 10: /* Compressed, rgb images */
670 if ((src.bytepp = bpp_to_bytes(header.bitsperpixel)))
671 break;
672 i_push_error(0, "Targa: direct color image's bpp is not 15/16/24/32 - unsupported.");
07d70837 673 if (idstring) myfree(idstring);
1ec86afa
AMH
674 return NULL;
675 break;
676 case 32: /* Compressed color-mapped, Huffman, Delta and runlength */
1ec86afa 677 case 33: /* Compressed color-mapped, Huffman, Delta and runlength */
920aa4a5 678 i_push_error(0, "Unsupported Targa (Huffman/delta/rle/quadtree) subformat is not supported");
07d70837 679 if (idstring) myfree(idstring);
1ec86afa
AMH
680 return NULL;
681 break;
682 default: /* All others which we don't know which might be */
683 i_push_error(0, "Unknown targa format");
07d70837 684 if (idstring) myfree(idstring);
1ec86afa
AMH
685 return NULL;
686 break;
687 }
920aa4a5
AMH
688
689 src.state = NoInit;
690 src.len = 0;
691 src.ig = ig;
692 src.compressed = !!(header.datatypecode & (1<<3));
693
694 /* Determine number of channels */
695
696 mapped = 1;
697 switch (header.datatypecode) {
920aa4a5
AMH
698 case 2: /* Uncompressed, rgb images */
699 case 10: /* Compressed, rgb images */
700 mapped = 0;
701 case 1: /* Uncompressed, color-mapped images */
702 case 9: /* Compressed, color-mapped images */
703 if ((channels = bpp_to_channels(mapped ?
704 header.colourmapdepth :
705 header.bitsperpixel))) break;
706 i_push_error(0, "Targa Image has none of 15/16/24/32 pixel layout");
07d70837 707 if (idstring) myfree(idstring);
920aa4a5
AMH
708 return NULL;
709 break;
710 case 3: /* Uncompressed, grayscale images */
711 case 11: /* Compressed, grayscale images */
712 mapped = 0;
713 channels = 1;
714 break;
715 }
716
717 img = mapped ?
718 i_img_pal_new(width, height, channels, 256) :
719 i_img_empty_ch(NULL, width, height, channels);
fe055ff6
TC
720
721 if (!img) {
722 if (idstring)
723 myfree(idstring);
724 return NULL;
725 }
920aa4a5 726
07d70837
AMH
727 if (idstring) {
728 i_tags_add(&img->tags, "tga_idstring", 0, idstring, header.idlength, 0);
4dfa5522 729 myfree(idstring);
07d70837
AMH
730 }
731
920aa4a5
AMH
732 if (mapped &&
733 !tga_palette_read(ig,
734 img,
735 bpp_to_bytes(header.colourmapdepth),
736 header.colourmaplength)
737 ) {
738 i_push_error(0, "Targa Image has none of 15/16/24/32 pixel layout");
07d70837
AMH
739 if (idstring) myfree(idstring);
740 if (img) i_img_destroy(img);
920aa4a5
AMH
741 return NULL;
742 }
743
744 /* Allocate buffers */
745 databuf = mymalloc(width*src.bytepp);
746 if (!mapped) linebuf = mymalloc(width*sizeof(i_color));
747
748 for(y=0; y<height; y++) {
749 if (!tga_source_read(&src, databuf, width)) {
750 i_push_error(errno, "read for targa data failed");
751 myfree(databuf);
07d70837 752 if (img) i_img_destroy(img);
920aa4a5
AMH
753 return NULL;
754 }
7456c26c 755 if (mapped && header.colourmaporigin) for(x=0; x<width; x++) databuf[x] -= header.colourmaporigin;
920aa4a5
AMH
756 if (mapped) i_ppal(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, databuf);
757 else {
758 for(x=0; x<width; x++) color_unpack(databuf+x*src.bytepp, src.bytepp, linebuf+x);
759 i_plin(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, linebuf);
760 }
761 }
762 myfree(databuf);
763 if (linebuf) myfree(linebuf);
07d70837 764
fe055ff6 765 i_tags_add(&img->tags, "i_format", 0, "tga", -1, 0);
07d70837
AMH
766 i_tags_addn(&img->tags, "tga_bitspp", 0, mapped?header.colourmapdepth:header.bitsperpixel);
767 if (src.compressed) i_tags_addn(&img->tags, "compressed", 0, 1);
920aa4a5 768 return img;
1ec86afa
AMH
769}
770
771
772
febba01f
AMH
773/*
774=item i_writetga_wiol(img, ig)
1ec86afa 775
febba01f 776Writes an image in targa format. Returns 0 on error.
1ec86afa 777
febba01f
AMH
778 img - image to store
779 ig - io_glue object
1ec86afa 780
febba01f
AMH
781=cut
782*/
1ec86afa
AMH
783
784undef_int
febba01f 785i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idstring, size_t idlen) {
7456c26c 786 tga_header header;
76ff75b8 787 tga_dest dest;
7456c26c 788 unsigned char headbuf[18];
76ff75b8
AMH
789 unsigned int bitspp;
790
76ff75b8 791 int mapped;
1ec86afa 792
76ff75b8 793 /* parameters */
febba01f
AMH
794
795 /*
796 int compress = 1;
797 char *idstring = "testing";
798 int wierdpack = 0;
799 */
1ec86afa 800
76ff75b8
AMH
801 idlen = strlen(idstring);
802 mapped = img->type == i_palette_type;
1ec86afa 803
febba01f
AMH
804 mm_log((1,"i_writetga_wiol(img %p, ig %p, idstring %p, idlen %d, wierdpack %d, compress %d)\n",
805 img, ig, idstring, idlen, wierdpack, compress));
7456c26c
AMH
806 mm_log((1, "virtual %d, paletted %d\n", img->virtual, mapped));
807 mm_log((1, "channels %d\n", img->channels));
76ff75b8
AMH
808
809 i_clear_error();
810
811 switch (img->channels) {
812 case 1:
813 bitspp = 8;
814 if (wierdpack) {
815 mm_log((1,"wierdpack option ignored for 1 channel images\n"));
816 wierdpack=0;
817 }
818 break;
819 case 2:
820 i_push_error(0, "Cannot store 2 channel image in targa format");
821 return 0;
822 break;
823 case 3:
824 bitspp = wierdpack ? 15 : 24;
825 break;
826 case 4:
827 bitspp = wierdpack ? 16 : 32;
828 break;
829 default:
830 i_push_error(0, "Targa only handles 1,3 and 4 channel images.");
831 return 0;
832 }
7456c26c 833
76ff75b8
AMH
834 io_glue_commit_types(ig);
835
7456c26c
AMH
836 header.idlength = idlen;
837 header.colourmaptype = mapped ? 1 : 0;
838 header.datatypecode = mapped ? 1 : img->channels == 1 ? 3 : 2;
7456c26c 839 header.datatypecode += compress ? 8 : 0;
76ff75b8 840 mm_log((1, "datatypecode %d\n", header.datatypecode));
7456c26c
AMH
841 header.colourmaporigin = 0;
842 header.colourmaplength = mapped ? i_colorcount(img) : 0;
76ff75b8 843 header.colourmapdepth = mapped ? bitspp : 0;
7456c26c
AMH
844 header.x_origin = 0;
845 header.y_origin = 0;
846 header.width = img->xsize;
847 header.height = img->ysize;
76ff75b8 848 header.bitsperpixel = mapped ? 8 : bitspp;
7456c26c
AMH
849 header.imagedescriptor = (1<<5); /* normal order instead of upside down */
850
76ff75b8 851 tga_header_pack(&header, headbuf);
7456c26c
AMH
852
853 if (ig->writecb(ig, &headbuf, sizeof(headbuf)) != sizeof(headbuf)) {
854 i_push_error(errno, "could not write targa header");
855 return 0;
7c58edfc
AMH
856 }
857
7456c26c
AMH
858 if (idlen) {
859 if (ig->writecb(ig, idstring, idlen) != idlen) {
860 i_push_error(errno, "could not write targa idstring");
861 return 0;
1ec86afa
AMH
862 }
863 }
7456c26c 864
76ff75b8
AMH
865 /* Make this into a constructor? */
866 dest.compressed = compress;
867 dest.bytepp = mapped ? 1 : bpp_to_bytes(bitspp);
868 dest.ig = ig;
c95846c2 869
76ff75b8
AMH
870 mm_log((1, "dest.compressed = %d\n", dest.compressed));
871 mm_log((1, "dest.bytepp = %d\n", dest.bytepp));
c95846c2 872
76ff75b8 873 if (img->type == i_palette_type) {
76ff75b8
AMH
874 if (!tga_palette_write(ig, img, bitspp, i_colorcount(img))) return 0;
875
876 if (!img->virtual && !dest.compressed) {
7456c26c
AMH
877 if (ig->writecb(ig, img->idata, img->bytes) != img->bytes) {
878 i_push_error(errno, "could not write targa image data");
879 return 0;
1ec86afa 880 }
7456c26c
AMH
881 } else {
882 int y;
883 i_palidx *vals = mymalloc(sizeof(i_palidx)*img->xsize);
884 for(y=0; y<img->ysize; y++) {
885 i_gpal(img, 0, img->xsize, y, vals);
76ff75b8 886 tga_dest_write(&dest, vals, img->xsize);
1ec86afa 887 }
7456c26c 888 myfree(vals);
1ec86afa 889 }
76ff75b8
AMH
890 } else { /* direct type */
891 int x, y;
892 int bytepp = wierdpack ? 2 : bpp_to_bytes(bitspp);
893 int lsize = bytepp * img->xsize;
894 i_color *vals = mymalloc(img->xsize*sizeof(i_color));
895 unsigned char *buf = mymalloc(lsize);
896
7456c26c 897 for(y=0; y<img->ysize; y++) {
76ff75b8
AMH
898 i_glin(img, 0, img->xsize, y, vals);
899 for(x=0; x<img->xsize; x++) color_pack(buf+x*bytepp, bitspp, vals+x);
900 tga_dest_write(&dest, buf, img->xsize);
1ec86afa 901 }
76ff75b8
AMH
902 myfree(buf);
903 myfree(vals);
1ec86afa 904 }
10461f9a
TC
905
906 ig->closecb(ig);
907
7456c26c 908 return 1;
1ec86afa 909}
b8c2033e
AMH
910
911/*
912=back
913
914=head1 AUTHOR
915
916Arnar M. Hrafnkelsson <addi@umich.edu>
917
918=head1 SEE ALSO
919
920Imager(3)
921
922=cut
923*/