13 tga.c - implements reading and writing targa files, uses io layer.
17 io_glue *ig = io_new_fd( fd );
18 i_img *im = i_readtga_wiol(ig, -1); // no limit on how much is read
20 io_glue *ig = io_new_fd( fd );
21 return_code = i_writetga_wiol(im, ig);
25 tga.c implements the basic functions to read and write portable targa
26 files. It uses the iolayer and needs either a seekable source or an
27 entire memory mapped buffer.
29 =head1 FUNCTION REFERENCE
31 Some of these functions are internal.
47 short int colourmaporigin;
48 short int colourmaplength;
61 =item i_readtga_wiol(ig, length)
63 Retrieve an image and stores in the iolayer object. Returns NULL on fatal error.
66 length - maximum length to read from data source, before closing it -1
73 i_readtga_wiol(io_glue *ig, int length) {
77 int width, height, channels;
84 unsigned char *palbuf;
85 unsigned char headbuf[18];
86 unsigned char *linebuf;
90 mm_log((1,"i_readtga(ig %p, length %d)\n", ig, length));
92 io_glue_commit_types(ig);
94 if (ig->readcb(ig, &headbuf, 18) != 18) {
95 i_push_error(errno, "could not read targa header");
99 header.idlength = headbuf[0];
100 header.colourmaptype = headbuf[1];
101 header.datatypecode = headbuf[2];
102 header.colourmaporigin = headbuf[4] << 8 + headbuf[3];
103 header.colourmaplength = headbuf[6] << 8 + headbuf[5];
104 header.colourmapdepth = headbuf[7];
105 header.x_origin = headbuf[9] << 8 + headbuf[8];
106 header.y_origin = headbuf[11] << 8 + headbuf[10];
107 header.width = (headbuf[13] << 8) + headbuf[12];
108 header.height = (headbuf[15] << 8) + headbuf[14];
109 header.bitsperpixel = headbuf[16];
110 header.imagedescriptor = headbuf[17];
112 mm_log((1,"Id length: %d\n",header.idlength));
113 mm_log((1,"Colour map type: %d\n",header.colourmaptype));
114 mm_log((1,"Image type: %d\n",header.datatypecode));
115 mm_log((1,"Colour map offset: %d\n",header.colourmaporigin));
116 mm_log((1,"Colour map length: %d\n",header.colourmaplength));
117 mm_log((1,"Colour map depth: %d\n",header.colourmapdepth));
118 mm_log((1,"X origin: %d\n",header.x_origin));
119 mm_log((1,"Y origin: %d\n",header.y_origin));
120 mm_log((1,"Width: %d\n",header.width));
121 mm_log((1,"Height: %d\n",header.height));
122 mm_log((1,"Bits per pixel: %d\n",header.bitsperpixel));
123 mm_log((1,"Descriptor: %d\n",header.imagedescriptor));
126 if (header.idlength) {
127 idstring = mymalloc(header.idlength+1);
128 if (ig->readcb(ig, idstring, header.idlength) != header.idlength) {
129 i_push_error(errno, "short read on targa idstring");
132 myfree(idstring); /* Move this later, once this is stored in a tag */
135 width = header.width;
136 height = header.height;
140 /* Only RGB for now, eventually support all formats */
141 switch (header.datatypecode) {
143 case 0: /* No data in image */
144 i_push_error(0, "Targa image contains no image data");
148 case 1: /* Uncompressed, color-mapped images */
149 if (header.imagedescriptor != 0) {
150 i_push_error(0, "Targa: Imagedescriptor not 0, not supported internal format");
154 if (header.bitsperpixel != 8) {
155 i_push_error(0, "Targa: bpp is not 8, unsupported.");
159 mm_log((1, "Uncompressed color-mapped image\n"));
160 if (header.colourmapdepth != 24) {
161 i_push_error(0, "Colourmap is not 24 bit");
166 img = i_img_pal_new(width, height, channels, 256);
168 /* Read in the palette */
170 palbsize = header.colourmaplength*channels;
171 palbuf = mymalloc(palbsize);
173 if (ig->readcb(ig, palbuf, palbsize) != palbsize) {
174 i_push_error(errno, "could not read targa colourmap");
178 /* populate the palette of the new image */
179 for(i=0; i<header.colourmaplength; i++) {
181 col.rgba.r = palbuf[i*channels+2];
182 col.rgba.g = palbuf[i*channels+1];
183 col.rgba.b = palbuf[i*channels+0];
184 i_addcolors(img, &col, 1);
189 /* read image data in */
190 linebuf = mymalloc(width);
191 for(y=height-1;y>=0;y--) {
192 if (ig->readcb(ig, linebuf, width) != width) {
193 i_push_error(errno, "read for targa data failed");
197 i_ppal(img, 0, width, y, linebuf);
203 case 2: /* Uncompressed, RGB images */
204 mm_log((1, "Uncompressed RGB image\n"));
208 case 3: /* Uncompressed, black and white images */
209 i_push_error(0, "Targa black and white subformat is not supported");
212 case 9: /* Runlength encoded, color-mapped images */
213 i_push_error(0, "Targa runlength coded colormapped subformat is not supported");
216 case 10: /* Runlength encoded, RGB images */
220 case 11: /* Compressed, black and white images */
221 i_push_error(0, "Targa compressed black and white subformat is not supported");
224 case 32: /* Compressed color-mapped, Huffman, Delta and runlength */
225 i_push_error(0, "Targa Huffman/delta/rle subformat is not supported");
228 case 33: /* Compressed color-mapped, Huffman, Delta and runlength */
229 i_push_error(0, "Targa Huffman/delta/rle/quadtree subformat is not supported");
232 default: /* All others which we don't know which might be */
233 i_push_error(0, "Unknown targa format");
249 i_writetga_wiol(i_img *im, io_glue *ig) {
254 mm_log((1,"i_writeppm(im %p, ig %p)\n", im, ig));
257 /* Add code to get the filename info from the iolayer */
258 /* Also add code to check for mmapped code */
260 io_glue_commit_types(ig);
262 if (im->channels == 3) {
263 sprintf(header,"P6\n#CREATOR: Imager\n%d %d\n255\n",im->xsize,im->ysize);
264 if (ig->writecb(ig,header,strlen(header))<0) {
265 i_push_error(errno, "could not write ppm header");
266 mm_log((1,"i_writeppm: unable to write ppm header.\n"));
270 if (!im->virtual && im->bits == i_8_bits && im->type == i_direct_type) {
271 rc = ig->writecb(ig,im->idata,im->bytes);
274 unsigned char *data = mymalloc(3 * im->xsize);
279 static int rgb_chan[3] = { 0, 1, 2 };
282 while (y < im->ysize && rc >= 0) {
283 i_gsamp(im, 0, im->xsize, y, data, rgb_chan, 3);
284 rc = ig->writecb(ig, data, im->xsize * 3);
289 i_push_error(0, "Out of memory");
294 i_push_error(errno, "could not write ppm data");
295 mm_log((1,"i_writeppm: unable to write ppm data.\n"));
299 else if (im->channels == 1) {
300 sprintf(header, "P5\n#CREATOR: Imager\n%d %d\n255\n",
301 im->xsize, im->ysize);
302 if (ig->writecb(ig,header, strlen(header)) < 0) {
303 i_push_error(errno, "could not write pgm header");
304 mm_log((1,"i_writeppm: unable to write pgm header.\n"));
308 if (!im->virtual && im->bits == i_8_bits && im->type == i_direct_type) {
309 rc=ig->writecb(ig,im->idata,im->bytes);
312 unsigned char *data = mymalloc(im->xsize);
320 while (y < im->ysize && rc >= 0) {
321 i_gsamp(im, 0, im->xsize, y, data, &chan, 1);
322 rc = ig->writecb(ig, data, im->xsize);
327 i_push_error(0, "Out of memory");
332 i_push_error(errno, "could not write pgm data");
333 mm_log((1,"i_writeppm: unable to write pgm data.\n"));
338 i_push_error(0, "can only save 1 or 3 channel images to pnm");
339 mm_log((1,"i_writeppm: ppm/pgm is 1 or 3 channel only (current image is %d)\n",im->channels));