12 rgb.c - implements reading and writing sgi image files, uses io layer.
16 io_glue *ig = io_new_fd( fd );
17 i_img *im = i_readrgb_wiol(ig, -1); // no limit on how much is read
19 io_glue *ig = io_new_fd( fd );
20 return_code = i_writergb_wiol(im, ig);
24 rgb.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.
38 unsigned short imagic;
39 unsigned char storagetype;
41 unsigned short dimensions;
42 unsigned short xsize, ysize, zsize;
43 unsigned int min, max;
45 unsigned int colormap;
52 typedef enum { NoInit, Raw, Rle } rle_state;
68 =item rgb_header_unpack(header, headbuf)
70 Unpacks the header structure into from buffer and stores
71 in the header structure.
73 header - header structure
74 headbuf - buffer to unpack from
82 rgb_header_unpack(rgb_header *header, unsigned char *headbuf) {
83 header->imagic = (headbuf[0]<<8) + headbuf[1];
84 header->storagetype = headbuf[2];
85 header->BPC = headbuf[3];
86 header->dimensions = (headbuf[4]<<8) + headbuf[5];
87 header->xsize = (headbuf[6]<<8) + headbuf[7];
88 header->ysize = (headbuf[8]<<8) + headbuf[9];
89 header->zsize = (headbuf[10]<<8) + headbuf[11];
90 header->min = (headbuf[12]<<24) + (headbuf[13]<<16)+(headbuf[14]<<8)+headbuf[15];
91 header->max = (headbuf[16]<<24) + (headbuf[17]<<16)+(headbuf[18]<<8)+headbuf[19];
92 memcpy(header->name,headbuf+20,80);
93 header->colormap = (headbuf[100]<<24) + (headbuf[101]<<16)+(headbuf[102]<<8)+headbuf[103];
96 #if 0 /* this is currently unused */
99 =item rgb_header_pack(header, headbuf)
101 Packs header structure into buffer for writing.
103 header - header structure
104 headbuf - buffer to pack into
111 rgb_header_pack(rgb_header *header, unsigned char headbuf[512]) {
113 header->imagic = (headbuf[0]<<8) + headbuf[1];
114 header->storagetype = headbuf[2];
115 header->BPC = headbuf[3];
116 header->dimensions = (headbuf[4]<<8) + headbuf[5];
117 header->xsize = (headbuf[6]<<8) + headbuf[7];
118 header->ysize = (headbuf[8]<<8) + headbuf[9];
119 header->zsize = (headbuf[10]<<8) + headbuf[11];
120 header->min = (headbuf[12]<<24) + (headbuf[13]<<16)+(headbuf[14]<<8)+headbuf[15];
121 header->max = (headbuf[16]<<24) + (headbuf[17]<<16)+(headbuf[18]<<8)+headbuf[19];
122 memcpy(header->name,headbuf+20,80);
123 header->colormap = (headbuf[100]<<24) + (headbuf[101]<<16)+(headbuf[102]<<8)+headbuf[103];
132 =item rgb_dest_write(s, buf, pixels)
134 Writes pixels from buf to destination s. Takes care of compressing if the
135 destination is compressed.
139 pixels - number of pixels to put write to destination
146 rgb_dest_write(rgb_dest *s, unsigned char *buf, size_t pixels) {
158 =item i_readrgb_wiol(ig, length)
160 Read in an image from the iolayer data source and return the image structure to it.
161 Returns NULL on error.
164 length - maximum length to read from data source, before closing it -1
171 i_readrgb_wiol(io_glue *ig, int length) {
174 int width, height, channels;
175 unsigned long maxlen;
180 unsigned char headbuf[512];
181 unsigned char *databuf;
182 unsigned long *starttab, *lengthtab;
183 i_color *linebuf = NULL;
186 mm_log((1,"i_readrgb(ig %p, length %d)\n", ig, length));
190 io_glue_commit_types(ig);
192 if (ig->readcb(ig, headbuf, 512) != 512) {
193 i_push_error(errno, "could not read SGI rgb header");
197 rgb_header_unpack(&header, headbuf);
200 mm_log((1,"imagic: %d\n", header.imagic));
201 mm_log((1,"storagetype: %d\n", header.storagetype));
202 mm_log((1,"BPC: %d\n", header.BPC));
203 mm_log((1,"dimensions: %d\n", header.dimensions));
204 mm_log((1,"xsize: %d\n", header.xsize));
205 mm_log((1,"ysize: %d\n", header.ysize));
206 mm_log((1,"zsize: %d\n", header.zsize));
207 mm_log((1,"min: %d\n", header.min));
208 mm_log((1,"max: %d\n", header.max));
209 mm_log((1,"name [skipped]\n"));
210 mm_log((1,"colormap: %d\n", header.colormap));
212 if (header.colormap != 0) {
213 i_push_error(0, "SGI rgb image has a non zero colormap entry - obsolete format");
217 if (header.storagetype != 0 && header.storagetype != 1) {
218 i_push_error(0, "SGI rgb image has has invalid storage field");
222 width = header.xsize;
223 height = header.ysize;
224 channels = header.zsize;
226 img = i_img_empty_ch(NULL, width, height, channels);
230 i_tags_add(&img->tags, "rgb_namestr", 0, header.name, 80, 0);
231 i_tags_add(&img->tags, "i_format", 0, "rgb", -1, 0);
233 switch (header.storagetype) {
234 case 0: /* uncompressed */
236 linebuf = i_mempool_alloc(&mp, width*sizeof(i_color));
237 databuf = i_mempool_alloc(&mp, width);
239 savemask = i_img_getmask(img);
241 for(c=0; c<channels; c++) {
242 i_img_setmask(img, 1<<c);
243 for(y=0; y<height; y++) {
246 if (ig->readcb(ig, databuf, width) != width) {
247 i_push_error(0, "SGI rgb: cannot read");
251 for(x=0; x<width; x++)
252 linebuf[x].channel[c] = databuf[x];
254 i_plin(img, 0, width, height-1-y, linebuf);
257 i_img_setmask(img, savemask);
259 case 1: /* RLE compressed */
261 databuf = i_mempool_alloc(&mp, height*channels*4);
262 starttab = i_mempool_alloc(&mp, height*channels*sizeof(unsigned long));
263 lengthtab = i_mempool_alloc(&mp, height*channels*sizeof(unsigned long));
264 linebuf = i_mempool_alloc(&mp, width*sizeof(i_color));
266 /* Read offset table */
267 if (ig->readcb(ig, databuf, height*channels*4) != height*channels*4) goto ErrorReturn;
268 for(i=0; i<height*channels; i++) starttab[i] =
270 (databuf[i*4+1]<<16) |
271 (databuf[i*4+2]<<8) |
275 /* Read length table */
276 if (ig->readcb(ig, databuf, height*channels*4) != height*channels*4) goto ErrorReturn;
277 for(i=0; i<height*channels; i++) lengthtab[i] =
279 (databuf[i*4+1]<<16)+
283 mm_log((3, "Offset/length table:\n"));
284 for(i=0; i<height*channels; i++)
285 mm_log((3, "%d: %d/%d\n", i, starttab[i], lengthtab[i]));
288 /* Find max spanlength if someone is making very badly formed RLE data */
290 for(y=0; y<height; y++) maxlen = (maxlen>lengthtab[y])?maxlen:lengthtab[y];
292 mm_log((1, "maxlen for an rle buffer: %d\n", maxlen));
294 databuf = i_mempool_alloc(&mp, maxlen);
296 for(y=0; y<height; y++) {
297 for(c=0; c<channels; c++) {
298 unsigned long iidx = 0, oidx = 0, span = 0;
299 unsigned char cval = 0;
302 int datalen = lengthtab[ci];
304 if (ig->seekcb(ig, starttab[ci], SEEK_SET) != starttab[ci]) {
305 i_push_error(0, "SGI rgb: cannot seek");
308 if (ig->readcb(ig, databuf, datalen) != datalen) {
309 i_push_error(0, "SGI rgb: cannot read");
314 mm_log((1, "Buffer length %d\n", datalen));
315 for(i=0; i<datalen; i++)
316 mm_log((1, "0x%x\n", databuf[i]));
319 while( iidx <= datalen && oidx < width ) {
321 span = databuf[iidx] & 0x7f;
322 rle = !(databuf[iidx++] & 0x80);
323 /* mm_log((1,"new span %d, rle %d\n", span, rle)); */
326 i_push_error(0, "SGI rgb: bad rle data");
329 cval = databuf[iidx++];
330 /* mm_log((1, "rle value %d\n", cval)); */
333 linebuf[oidx++].channel[c] = rle ? cval : databuf[iidx++];
336 mm_log((1,"iidx=%d/%d, oidx=%d/%d, linebuf[%d].channel[%d] %d\n", iidx-1, datalen, oidx-1, width, oidx-1, c, linebuf[oidx-1].channel[c]));
340 i_plin(img, 0, width, height-1-y, linebuf);
346 i_tags_add(&img->tags, "i_format", 0, "rgb", -1, 0);
348 i_mempool_destroy(&mp);
352 i_mempool_destroy(&mp);
353 if (img) i_img_destroy(img);
360 =item i_writergb_wiol(img, ig)
362 Writes an image in targa format. Returns 0 on error.
371 i_writergb_wiol(i_img *img, io_glue *ig, int wierdpack, int compress, char *idstring, size_t idlen) {
373 i_push_error(0, "writing SGI RGB files is not implemented");