12 tga.c - implements reading and writing targa files, uses io layer.
16 io_glue *ig = io_new_fd( fd );
17 i_img *im = i_readtga_wiol(ig, -1); // no limit on how much is read
19 io_glue *ig = io_new_fd( fd );
20 return_code = i_writetga_wiol(im, ig);
24 tga.c implements the basic functions to read and write portable targa
25 files. It uses the iolayer and needs either a seekable source or an
26 entire memory mapped buffer.
28 =head1 FUNCTION REFERENCE
30 Some of these functions are internal.
41 unsigned char idlength;
44 short int colourmaporigin;
45 short int colourmaplength;
56 typedef enum { NoInit, Raw, Rle } rle_state;
62 unsigned char cval[4];
78 =item bpp_to_bytes(bpp)
80 Convert bits per pixel into bytes per pixel
90 bpp_to_bytes(unsigned int bpp) {
108 =item bpp_to_channels(bpp)
110 Convert bits per pixel into channels in the image
119 bpp_to_channels(unsigned int bpp) {
138 * Packing functions - used for (un)packing
139 * datastructures into raw bytes.
144 =item color_unpack(buf, bytepp, val)
146 Unpacks bytes into colour structures, for 2 byte type the first byte
147 coming from the file will actually be GGGBBBBB, and the second will be
148 ARRRRRGG. "A" represents an attribute bit. The 3 byte entry contains
149 1 byte each of blue, green, and red. The 4 byte entry contains 1 byte
150 each of blue, green, red, and attribute.
152 buf - pointer to data
153 bytepp - bytes per pixel
154 val - pointer to color to store to
161 color_unpack(unsigned char *buf, int bytepp, i_color *val) {
164 val->gray.gray_color = buf[0];
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;
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;
181 val->rgba.b = buf[0];
182 val->rgba.g = buf[1];
183 val->rgba.r = buf[2];
184 val->rgba.a = buf[3];
194 Packs a colour into an array of bytes, for 2 byte type the first byte
195 will be GGGBBBBB, and the second will be ARRRRRGG. "A" represents an
196 attribute bit. The 3 byte entry contains 1 byte each of blue, green,
197 and red. The 4 byte entry contains 1 byte each of blue, green, red,
200 buf - destination buffer
201 bitspp - bits per pixel
209 color_pack(unsigned char *buf, int bitspp, i_color *val) {
212 buf[0] = val->gray.gray_color;
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);
220 buf[1] |= val->rgba.a & 0x80;
228 buf[0] = val->rgba.b;
229 buf[1] = val->rgba.g;
230 buf[2] = val->rgba.r;
231 buf[3] = val->rgba.a;
240 Helper function for rle compressor to find the next triple repeat of the
241 same pixel value in buffer.
244 length - number of pixel values in buffer
245 bytepp - number of bytes in a pixel value
252 find_repeat(unsigned char *buf, int length, int bytepp) {
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)
271 Helper function for rle compressor to find the length of a span where
272 the same pixel value is in the buffer.
275 length - number of pixel values in buffer
276 bytepp - number of bytes in a pixel value
283 find_span(unsigned char *buf, int length, int bytepp) {
286 if(memcmp(buf, buf+(i*bytepp), bytepp) != 0) return i;
294 =item tga_header_unpack(header, headbuf)
296 Unpacks the header structure into from buffer and stores
297 in the header structure.
299 header - header structure
300 headbuf - buffer to unpack from
307 tga_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];
325 tga_header_verify(unsigned char headbuf[18]) {
327 tga_header_unpack(&header, headbuf);
328 switch (header.datatypecode) {
330 printf("bad typecode!\n");
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 */
342 switch (header.colourmaptype) {
344 printf("bad colourmaptype!\n");
356 =item tga_header_pack(header, headbuf)
358 Packs header structure into buffer for writing.
360 header - header structure
361 headbuf - buffer to pack into
368 tga_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;
391 =item tga_source_read(s, buf, pixels)
393 Reads pixel number of pixels from source s into buffer buf. Takes
394 care of decompressing the stream if needed.
397 buf - destination buffer
398 pixels - number of pixels to put into buffer
405 tga_source_read(tga_source *s, unsigned char *buf, size_t pixels) {
407 if (!s->compressed) {
408 if (s->ig->readcb(s->ig, buf, pixels*s->bytepp) != pixels*s->bytepp) return 0;
414 if (s->len == 0) s->state = NoInit;
417 if (s->ig->readcb(s->ig, &s->hdr, 1) != 1) return 0;
419 s->len = (s->hdr &~(1<<7))+1;
420 s->state = (s->hdr & (1<<7)) ? Rle : Raw;
424 printf("%04d %s: %d\n", cnt++, s->state==Rle?"RLE":"RAW", s->len);
427 if (s->state == Rle && s->ig->readcb(s->ig, s->cval, s->bytepp) != s->bytepp) return 0;
431 ml = i_min(s->len, pixels-cp);
432 for(k=0; k<ml; k++) for(j=0; j<s->bytepp; j++)
433 buf[(cp+k)*s->bytepp+j] = s->cval[j];
438 ml = i_min(s->len, pixels-cp);
439 if (s->ig->readcb(s->ig, buf+cp*s->bytepp, ml*s->bytepp) != ml*s->bytepp) return 0;
452 =item tga_dest_write(s, buf, pixels)
454 Writes pixels from buf to destination s. Takes care of compressing if the
455 destination is compressed.
459 pixels - number of pixels to put write to destination
466 tga_dest_write(tga_dest *s, unsigned char *buf, size_t pixels) {
469 if (!s->compressed) {
470 if (s->ig->writecb(s->ig, buf, pixels*s->bytepp) != pixels*s->bytepp) return 0;
476 int nxtrip = find_repeat(buf+cp*s->bytepp, pixels-cp, s->bytepp);
477 tlen = (nxtrip == -1) ? pixels-cp : nxtrip;
479 unsigned char clen = (tlen>128) ? 128 : tlen;
481 if (s->ig->writecb(s->ig, &clen, 1) != 1) return 0;
483 if (s->ig->writecb(s->ig, buf+cp*s->bytepp, clen*s->bytepp) != clen*s->bytepp) return 0;
487 if (cp >= pixels) break;
488 tlen = find_span(buf+cp*s->bytepp, pixels-cp, s->bytepp);
489 if (tlen <3) continue;
491 unsigned char clen = (tlen>128) ? 128 : tlen;
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;
509 =item tga_palette_read(ig, img, bytepp, colourmaplength)
511 Reads the colormap from a tga file and stores in the paletted image
514 ig - iolayer data source
515 img - image structure
516 bytepp - bytes per pixel
517 colourmaplength - number of colours in colourmap
524 tga_palette_read(io_glue *ig, i_img *img, int bytepp, int colourmaplength) {
527 unsigned char *palbuf;
530 palbsize = colourmaplength*bytepp;
531 palbuf = mymalloc(palbsize);
533 if (ig->readcb(ig, palbuf, palbsize) != palbsize) {
534 i_push_error(errno, "could not read targa colourmap");
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);
549 =item tga_palette_write(ig, img, bitspp, colourmaplength)
551 Stores the colormap of an image in the destination ig.
553 ig - iolayer data source
554 img - image structure
555 bitspp - bits per pixel in colourmap
556 colourmaplength - number of colours in colourmap
563 tga_palette_write(io_glue *ig, i_img *img, int bitspp, int colourmaplength) {
565 int bytepp = bpp_to_bytes(bitspp);
566 size_t palbsize = i_colorcount(img)*bytepp;
567 unsigned char *palbuf = mymalloc(palbsize);
569 for(i=0; i<colourmaplength; i++) {
571 i_getcolors(img, i, &val, 1);
572 color_pack(palbuf+i*bytepp, bitspp, &val);
575 if (ig->writecb(ig, palbuf, palbsize) != palbsize) {
576 i_push_error(errno, "could not write targa colourmap");
586 =item i_readtga_wiol(ig, length)
588 Read in an image from the iolayer data source and return the image structure to it.
589 Returns NULL on error.
592 length - maximum length to read from data source, before closing it -1
599 i_readtga_wiol(io_glue *ig, int length) {
602 int width, height, channels;
604 char *idstring = NULL;
608 unsigned char headbuf[18];
609 unsigned char *databuf;
611 i_color *linebuf = NULL;
614 mm_log((1,"i_readtga(ig %p, length %d)\n", ig, length));
616 io_glue_commit_types(ig);
618 if (ig->readcb(ig, &headbuf, 18) != 18) {
619 i_push_error(errno, "could not read targa header");
623 tga_header_unpack(&header, headbuf);
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));
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");
646 width = header.width;
647 height = header.height;
651 switch (header.datatypecode) {
652 case 0: /* No data in image */
653 i_push_error(0, "Targa image contains no image data");
654 if (idstring) myfree(idstring);
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 */
661 if (header.bitsperpixel != 8) {
662 i_push_error(0, "Targa: mapped/grayscale image's bpp is not 8, unsupported.");
663 if (idstring) myfree(idstring);
668 case 2: /* Uncompressed, rgb images */
669 case 10: /* Compressed, rgb images */
670 if ((src.bytepp = bpp_to_bytes(header.bitsperpixel)))
672 i_push_error(0, "Targa: direct color image's bpp is not 15/16/24/32 - unsupported.");
673 if (idstring) myfree(idstring);
676 case 32: /* Compressed color-mapped, Huffman, Delta and runlength */
677 case 33: /* Compressed color-mapped, Huffman, Delta and runlength */
678 i_push_error(0, "Unsupported Targa (Huffman/delta/rle/quadtree) subformat is not supported");
679 if (idstring) myfree(idstring);
682 default: /* All others which we don't know which might be */
683 i_push_error(0, "Unknown targa format");
684 if (idstring) myfree(idstring);
692 src.compressed = !!(header.datatypecode & (1<<3));
694 /* Determine number of channels */
697 switch (header.datatypecode) {
698 case 2: /* Uncompressed, rgb images */
699 case 10: /* Compressed, rgb images */
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");
707 if (idstring) myfree(idstring);
710 case 3: /* Uncompressed, grayscale images */
711 case 11: /* Compressed, grayscale images */
718 i_img_pal_new(width, height, channels, 256) :
719 i_img_empty_ch(NULL, width, height, channels);
728 i_tags_add(&img->tags, "tga_idstring", 0, idstring, header.idlength, 0);
733 !tga_palette_read(ig,
735 bpp_to_bytes(header.colourmapdepth),
736 header.colourmaplength)
738 i_push_error(0, "Targa Image has none of 15/16/24/32 pixel layout");
739 if (idstring) myfree(idstring);
740 if (img) i_img_destroy(img);
744 /* Allocate buffers */
745 databuf = mymalloc(width*src.bytepp);
746 if (!mapped) linebuf = mymalloc(width*sizeof(i_color));
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");
752 if (img) i_img_destroy(img);
755 if (mapped && header.colourmaporigin) for(x=0; x<width; x++) databuf[x] -= header.colourmaporigin;
756 if (mapped) i_ppal(img, 0, width, header.imagedescriptor & (1<<5) ? y : height-1-y, databuf);
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);
763 if (linebuf) myfree(linebuf);
765 i_tags_add(&img->tags, "i_format", 0, "tga", -1, 0);
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);
774 =item i_writetga_wiol(img, ig)
776 Writes an image in targa format. Returns 0 on error.
785 i_writetga_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idstring, size_t idlen) {
788 unsigned char headbuf[18];
797 char *idstring = "testing";
801 idlen = strlen(idstring);
802 mapped = img->type == i_palette_type;
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));
806 mm_log((1, "virtual %d, paletted %d\n", img->virtual, mapped));
807 mm_log((1, "channels %d\n", img->channels));
811 switch (img->channels) {
815 mm_log((1,"wierdpack option ignored for 1 channel images\n"));
820 i_push_error(0, "Cannot store 2 channel image in targa format");
824 bitspp = wierdpack ? 15 : 24;
827 bitspp = wierdpack ? 16 : 32;
830 i_push_error(0, "Targa only handles 1,3 and 4 channel images.");
834 io_glue_commit_types(ig);
836 header.idlength = idlen;
837 header.colourmaptype = mapped ? 1 : 0;
838 header.datatypecode = mapped ? 1 : img->channels == 1 ? 3 : 2;
839 header.datatypecode += compress ? 8 : 0;
840 mm_log((1, "datatypecode %d\n", header.datatypecode));
841 header.colourmaporigin = 0;
842 header.colourmaplength = mapped ? i_colorcount(img) : 0;
843 header.colourmapdepth = mapped ? bitspp : 0;
846 header.width = img->xsize;
847 header.height = img->ysize;
848 header.bitsperpixel = mapped ? 8 : bitspp;
849 header.imagedescriptor = (1<<5); /* normal order instead of upside down */
851 tga_header_pack(&header, headbuf);
853 if (ig->writecb(ig, &headbuf, sizeof(headbuf)) != sizeof(headbuf)) {
854 i_push_error(errno, "could not write targa header");
859 if (ig->writecb(ig, idstring, idlen) != idlen) {
860 i_push_error(errno, "could not write targa idstring");
865 /* Make this into a constructor? */
866 dest.compressed = compress;
867 dest.bytepp = mapped ? 1 : bpp_to_bytes(bitspp);
870 mm_log((1, "dest.compressed = %d\n", dest.compressed));
871 mm_log((1, "dest.bytepp = %d\n", dest.bytepp));
873 if (img->type == i_palette_type) {
874 if (!tga_palette_write(ig, img, bitspp, i_colorcount(img))) return 0;
876 if (!img->virtual && !dest.compressed) {
877 if (ig->writecb(ig, img->idata, img->bytes) != img->bytes) {
878 i_push_error(errno, "could not write targa image data");
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);
886 tga_dest_write(&dest, vals, img->xsize);
890 } else { /* direct type */
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);
897 for(y=0; y<img->ysize; y++) {
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);
916 Arnar M. Hrafnkelsson <addi@umich.edu>