copy of docs from 2.4x for python modules that have been kept
[blender.git] / source / blender / imbuf / intern / iris.c
1 /**
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): none yet.
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  * iris.c
28  *
29  * $Id$
30  */
31
32 #include <string.h>
33 #include "BLI_blenlib.h"
34 #include "imbuf.h"
35 #include "imbuf_patch.h"
36 #include "IMB_imbuf_types.h"
37 #include "IMB_imbuf.h"
38 #include "IMB_allocimbuf.h"
39 #include "IMB_iris.h"
40
41 typedef struct {
42         unsigned short  imagic;         /* stuff saved on disk . . */
43         unsigned short  type;
44         unsigned short  dim;
45         unsigned short  xsize;
46         unsigned short  ysize;
47         unsigned short  zsize;
48         unsigned int    min;
49         unsigned int    max;
50         unsigned int    wastebytes;     
51         char            name[80];
52         unsigned int    colormap;
53
54         int             file;           /* stuff used in core only */
55         unsigned short  flags;
56         short           dorev;
57         short           x;
58         short           y;
59         short           z;
60         short           cnt;
61         unsigned short  *ptr;
62         unsigned short  *base;
63         unsigned short  *tmpbuf;
64         unsigned int    offset;
65         unsigned int    rleend;         /* for rle images */
66         unsigned int    *rowstart;      /* for rle images */
67         int             *rowsize;       /* for rle images */
68 } IMAGE;
69
70 #define RINTLUM (79)
71 #define GINTLUM (156)
72 #define BINTLUM (21)
73
74 #define ILUM(r,g,b)     ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
75
76 #define OFFSET_R        0       /* this is byte order dependent */
77 #define OFFSET_G        1
78 #define OFFSET_B        2
79 #define OFFSET_A        3
80
81 #define CHANOFFSET(z)   (3-(z)) /* this is byte order dependent */
82
83 #define TYPEMASK                0xff00
84 #define BPPMASK                 0x00ff
85 #define ITYPE_VERBATIM          0x0000
86 #define ITYPE_RLE               0x0100
87 #define ISRLE(type)             (((type) & 0xff00) == ITYPE_RLE)
88 #define ISVERBATIM(type)        (((type) & 0xff00) == ITYPE_VERBATIM)
89 #define BPP(type)               ((type) & BPPMASK)
90 #define RLE(bpp)                (ITYPE_RLE | (bpp))
91 #define VERBATIM(bpp)           (ITYPE_VERBATIM | (bpp))
92 #define IBUFSIZE(pixels)        ((pixels+(pixels>>6))<<2)
93 #define RLE_NOP                 0x00
94
95 /* funcs */
96 static void readheader(FILE *inf, IMAGE *image);
97 static int writeheader(FILE *outf, IMAGE *image);
98
99 static unsigned short getshort(FILE *inf);
100 static unsigned int getlong(FILE *inf);
101 static void putshort(FILE *outf, unsigned short val);
102 static int putlong(FILE *outf, unsigned int val);
103 static int writetab(FILE *outf, unsigned int *tab, int len);
104 static void readtab(FILE *inf, unsigned int *tab, int len);
105
106 static void expandrow(unsigned char *optr, unsigned char *iptr, int z);
107 static void expandrow2(float *optr, unsigned char *iptr, int z);
108 static void interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n);
109 static void interleaverow2(float *lptr, unsigned char *cptr, int z, int n);
110 static int compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt);
111 static void lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n);
112
113 /*
114  *      byte order independent read/write of shorts and ints.
115  *
116  */
117
118 static uchar * file_data;
119 static int file_offset;
120
121 static unsigned short getshort(FILE *inf)
122 {
123         unsigned char * buf;
124         
125         buf = file_data + file_offset;
126         file_offset += 2;
127         
128         return (buf[0]<<8)+(buf[1]<<0);
129 }
130
131 static unsigned int getlong(FILE *inf)
132 {
133         unsigned char * buf;
134         
135         buf = file_data + file_offset;
136         file_offset += 4;
137         
138         return (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+(buf[3]<<0);
139 }
140
141 static void putshort(FILE *outf, unsigned short val)
142 {
143         unsigned char buf[2];
144
145         buf[0] = (val>>8);
146         buf[1] = (val>>0);
147         fwrite(buf,2,1,outf);
148 }
149
150 static int putlong(FILE *outf, unsigned int val)
151 {
152         unsigned char buf[4];
153
154         buf[0] = (val>>24);
155         buf[1] = (val>>16);
156         buf[2] = (val>>8);
157         buf[3] = (val>>0);
158         return fwrite(buf,4,1,outf);
159 }
160
161 static void readheader(FILE *inf, IMAGE *image)
162 {
163         memset(image, 0, sizeof(IMAGE));
164         image->imagic = getshort(inf);
165         image->type = getshort(inf);
166         image->dim = getshort(inf);
167         image->xsize = getshort(inf);
168         image->ysize = getshort(inf);
169         image->zsize = getshort(inf);
170 }
171
172 static int writeheader(FILE *outf, IMAGE *image)
173 {
174         IMAGE t;
175
176         memset(&t, 0, sizeof(IMAGE));
177         fwrite(&t,sizeof(IMAGE),1,outf);
178         fseek(outf,0,SEEK_SET);
179         putshort(outf,image->imagic);
180         putshort(outf,image->type);
181         putshort(outf,image->dim);
182         putshort(outf,image->xsize);
183         putshort(outf,image->ysize);
184         putshort(outf,image->zsize);
185         putlong(outf,image->min);
186         putlong(outf,image->max);
187         putlong(outf,0);
188         return fwrite("no name",8,1,outf);
189 }
190
191 static int writetab(FILE *outf, unsigned int *tab, int len)
192 {
193         int r = 0;
194
195         while(len) {
196                 r = putlong(outf,*tab++);
197                 len -= 4;
198         }
199         return r;
200 }
201
202 static void readtab(FILE *inf, unsigned int *tab, int len)
203 {
204         while(len) {
205                 *tab++ = getlong(inf);
206                 len -= 4;
207         }
208 }
209
210 static void test_endian_zbuf(struct ImBuf *ibuf)
211 {
212         int len;
213         int *zval;
214         
215         if( BIG_LONG(1) == 1 ) return;
216         if(ibuf->zbuf==0) return;
217         
218         len= ibuf->x*ibuf->y;
219         zval= ibuf->zbuf;
220         
221         while(len--) {
222                 zval[0]= BIG_LONG(zval[0]);
223                 zval++;
224         }
225 }
226
227
228 /*
229  *      longimagedata - 
230  *              read in a B/W RGB or RGBA iris image file and return a 
231  *      pointer to an array of ints.
232  *
233  */
234
235 struct ImBuf *imb_loadiris(unsigned char *mem, int flags)
236 {
237         unsigned int *base, *lptr = NULL;
238         float *fbase, *fptr = NULL;
239         unsigned int *zbase, *zptr;
240         unsigned char *rledat;
241         unsigned int *starttab, *lengthtab;
242         FILE *inf = NULL;
243         IMAGE image;
244         int x, y, z, tablen;
245         int xsize, ysize, zsize;
246         int bpp, rle, cur, badorder;
247         ImBuf * ibuf;
248         
249         /*printf("new iris\n");*/
250         
251         file_data = mem;
252         file_offset = 0;
253         
254         readheader(inf, &image);
255         if(image.imagic != IMAGIC) {
256                 fprintf(stderr,"longimagedata: bad magic number in image file\n");
257                 return(0);
258         }
259         
260         rle = ISRLE(image.type);
261         bpp = BPP(image.type);
262         if(bpp != 1 && bpp != 2) {
263                 fprintf(stderr,"longimagedata: image must have 1 or 2 byte per pix chan\n");
264                 return(0);
265         }
266         
267         xsize = image.xsize;
268         ysize = image.ysize;
269         zsize = image.zsize;
270         
271         if (flags & IB_test) {
272                 ibuf = IMB_allocImBuf(image.xsize, image.ysize, 8 * image.zsize, 0, 0);
273                 if (ibuf) ibuf->ftype = IMAGIC;
274                 return(ibuf);
275         }
276         
277         if (rle) {
278                 
279                 tablen = ysize*zsize*sizeof(int);
280                 starttab = (unsigned int *)malloc(tablen);
281                 lengthtab = (unsigned int *)malloc(tablen);
282                 file_offset = 512;
283                 
284                 readtab(inf,starttab,tablen);
285                 readtab(inf,lengthtab,tablen);
286         
287                 /* check data order */
288                 cur = 0;
289                 badorder = 0;
290                 for (y = 0; y<ysize; y++) {
291                         for (z = 0; z<zsize; z++) {
292                                 if (starttab[y+z*ysize]<cur) {
293                                         badorder = 1;
294                                         break;
295                                 }
296                                 cur = starttab[y+z*ysize];
297                         }
298                         if(badorder)
299                                 break;
300                 }
301         
302                 if (bpp == 1) {
303                         
304                         ibuf = IMB_allocImBuf(xsize, ysize, 8 * zsize, IB_rect, 0);
305                         if (ibuf->depth > 32) ibuf->depth = 32;
306                         base = ibuf->rect;
307                         zbase = (unsigned int *)ibuf->zbuf;
308                         
309                         if (badorder) {
310                                 for(z=0; z<zsize; z++) {
311                                         lptr = base;
312                                         for(y=0; y<ysize; y++) {
313                                                 file_offset = starttab[y+z*ysize];
314                                                 
315                                                 rledat = file_data + file_offset;
316                                                 file_offset += lengthtab[y+z*ysize];
317                                                 
318                                                 expandrow((uchar *)lptr, rledat, 3-z);
319                                                 lptr += xsize;
320                                         }
321                                 }
322                         } else {
323                                 lptr = base;
324                                 zptr = zbase;
325                                 for(y=0; y<ysize; y++) {
326                                 
327                                         for(z=0; z<zsize; z++) {
328                                                 
329                                                 file_offset = starttab[y+z*ysize];
330
331                                                 rledat = file_data + file_offset;
332                                                 file_offset += lengthtab[y+z*ysize];
333                                                 
334                                                 if(z<4) expandrow((uchar *)lptr, rledat, 3-z);
335                                                 else if(z<8) expandrow((uchar *)zptr, rledat, 7-z);
336                                         }
337                                         lptr += xsize;
338                                         zptr += xsize;
339                                 }
340                         }
341                         
342
343                 } else {        /* bpp == 2 */
344                         
345                         ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect)|IB_rectfloat, 0);
346                         
347                         fbase = ibuf->rect_float;
348                         
349                         if (badorder) {
350                                 for(z=0; z<zsize; z++) {
351                                         fptr = fbase;
352                                         for(y=0; y<ysize; y++) {
353                                                 file_offset = starttab[y+z*ysize];
354                                                 
355                                                 rledat = file_data + file_offset;
356                                                 file_offset += lengthtab[y+z*ysize];
357                                                 
358                                                 expandrow2(fptr, rledat, 3-z);
359                                                 fptr += xsize * 4;
360                                         }
361                                 }
362                         } else {
363                                 fptr = fbase;
364
365                                 for(y=0; y<ysize; y++) {
366                                 
367                                         for(z=0; z<zsize; z++) {
368                                                 
369                                                 file_offset = starttab[y+z*ysize];
370
371                                                 rledat = file_data + file_offset;
372                                                 file_offset += lengthtab[y+z*ysize];
373                                                 
374                                                 expandrow2(fptr, rledat, 3-z);
375                                                 
376                                         }
377                                         fptr += xsize * 4;
378                                 }
379                         }
380                 }
381                 
382                 free(starttab);
383                 free(lengthtab);        
384
385         } else {
386                 if (bpp == 1) {
387                         
388                         ibuf = IMB_allocImBuf(xsize, ysize, 8 * zsize, IB_rect, 0);
389                         if (ibuf->depth > 32) ibuf->depth = 32;
390
391                         base = ibuf->rect;
392                         zbase = (unsigned int *)ibuf->zbuf;
393                         
394                         file_offset = 512;
395                         rledat = file_data + file_offset;
396                         
397                         for(z = 0; z < zsize; z++) {
398                                 
399                                 if(z<4) lptr = base;
400                                 else if(z<8) lptr= zbase;
401                                 
402                                 for(y = 0; y < ysize; y++) {
403
404                                         interleaverow((uchar *)lptr, rledat, 3-z, xsize);
405                                         rledat += xsize;
406                                         
407                                         lptr += xsize;
408                                 }
409                         }
410                         
411                 } else {        /* bpp == 2 */
412                         
413                         ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect)|IB_rectfloat, 0);
414
415                         fbase = ibuf->rect_float;
416
417                         file_offset = 512;
418                         rledat = file_data + file_offset;
419                         
420                         for(z = 0; z < zsize; z++) {
421                                 
422                                 fptr = fbase;
423                                 
424                                 for(y = 0; y < ysize; y++) {
425
426                                         interleaverow2(fptr, rledat, 3-z, xsize);
427                                         rledat += xsize * 2;
428                                         
429                                         fptr += xsize * 4;
430                                 }
431                         }
432                         
433                 }
434         }
435         
436         
437         if (bpp == 1) {
438                 uchar * rect;
439                 
440                 if (image.zsize == 1){
441                         rect = (uchar *) ibuf->rect;
442                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
443                                 rect[0] = 255;
444                                 rect[1] = rect[2] = rect[3];
445                                 rect += 4;
446                         }
447                 } else if (image.zsize == 2){
448                         /* grayscale with alpha */
449                         rect = (uchar *) ibuf->rect;
450                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
451                                 rect[0] = rect[2];
452                                 rect[1] = rect[2] = rect[3];
453                                 rect += 4;
454                         }
455                 } else if (image.zsize == 3){
456                         /* add alpha */
457                         rect = (uchar *) ibuf->rect;
458                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
459                                 rect[0] = 255;
460                                 rect += 4;
461                         }
462                 }
463                 
464         } else {        /* bpp == 2 */
465                 
466                 if (image.zsize == 1){
467                         fbase = ibuf->rect_float;
468                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
469                                 fbase[0] = 1;
470                                 fbase[1] = fbase[2] = fbase[3];
471                                 fbase += 4;
472                         }
473                 } else if (image.zsize == 2){
474                         /* grayscale with alpha */
475                         fbase = ibuf->rect_float;
476                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
477                                 fbase[0] = fbase[2];
478                                 fbase[1] = fbase[2] = fbase[3];
479                                 fbase += 4;
480                         }
481                 } else if (image.zsize == 3){
482                         /* add alpha */
483                         fbase = ibuf->rect_float;
484                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
485                                 fbase[0] = 1;
486                                 fbase += 4;
487                         }
488                 }
489                 
490                 if (flags & IB_rect) {
491                         IMB_rect_from_float(ibuf);
492                 }
493                 
494         }
495
496         ibuf->ftype = IMAGIC;
497         ibuf->profile = IB_PROFILE_SRGB;
498         if (flags & IB_ttob) IMB_flipy(ibuf);
499         
500         test_endian_zbuf(ibuf);
501         
502         if (ibuf) {
503                 if (ibuf->rect) 
504                         IMB_convert_rgba_to_abgr(ibuf);
505         }
506
507         return(ibuf);
508 }
509
510 /* static utility functions for longimagedata */
511
512 static void interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
513 {
514         lptr += z;
515         while(n--) {
516                 *lptr = *cptr++;
517                 lptr += 4;
518         }
519 }
520
521 static void interleaverow2(float *lptr, unsigned char *cptr, int z, int n)
522 {
523         lptr += z;
524         while(n--) {
525                 *lptr = ((cptr[0]<<8) | (cptr[1]<<0)) / (float)0xFFFF;          
526                 cptr += 2;
527                 lptr += 4;
528         }
529 }
530
531 static void expandrow2(float *optr, unsigned char *iptr, int z)
532 {
533         unsigned short pixel, count;
534         float pixel_f;
535
536         optr += z;
537         while(1) {
538                 pixel = (iptr[0]<<8) | (iptr[1]<<0);
539                 iptr += 2;
540                 
541                 if ( !(count = (pixel & 0x7f)) )
542                         return;
543                 if(pixel & 0x80) {
544                         while(count>=8) {
545                                 optr[0*4] = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
546                                 optr[1*4] = ((iptr[2]<<8) | (iptr[3]<<0))/(float)0xFFFF;
547                                 optr[2*4] = ((iptr[4]<<8) | (iptr[5]<<0))/(float)0xFFFF;
548                                 optr[3*4] = ((iptr[6]<<8) | (iptr[7]<<0))/(float)0xFFFF;
549                                 optr[4*4] = ((iptr[8]<<8) | (iptr[9]<<0))/(float)0xFFFF;
550                                 optr[5*4] = ((iptr[10]<<8) | (iptr[11]<<0))/(float)0xFFFF;
551                                 optr[6*4] = ((iptr[12]<<8) | (iptr[13]<<0))/(float)0xFFFF;
552                                 optr[7*4] = ((iptr[14]<<8) | (iptr[15]<<0))/(float)0xFFFF;
553                                 optr += 8*4;
554                                 iptr += 8*2;
555                                 count -= 8;
556                         }
557                         while(count--) {
558                                 *optr = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
559                                 iptr+=2;
560                                 optr+=4;
561                         }
562                 } else {
563                         pixel_f = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
564                         iptr += 2;
565
566                         while(count>=8) {
567                                 optr[0*4] = pixel_f;
568                                 optr[1*4] = pixel_f;
569                                 optr[2*4] = pixel_f;
570                                 optr[3*4] = pixel_f;
571                                 optr[4*4] = pixel_f;
572                                 optr[5*4] = pixel_f;
573                                 optr[6*4] = pixel_f;
574                                 optr[7*4] = pixel_f;
575                                 optr += 8*4;
576                                 count -= 8;
577                         }
578                         while(count--) {
579                                 *optr = pixel_f;
580                                 optr+=4;
581                         }
582                 }
583         }       
584 }
585
586 static void expandrow(unsigned char *optr, unsigned char *iptr, int z)
587 {
588         unsigned char pixel, count;
589
590         optr += z;
591         while(1) {
592                 pixel = *iptr++;
593                 if ( !(count = (pixel & 0x7f)) )
594                         return;
595                 if(pixel & 0x80) {
596                         while(count>=8) {
597                                 optr[0*4] = iptr[0];
598                                 optr[1*4] = iptr[1];
599                                 optr[2*4] = iptr[2];
600                                 optr[3*4] = iptr[3];
601                                 optr[4*4] = iptr[4];
602                                 optr[5*4] = iptr[5];
603                                 optr[6*4] = iptr[6];
604                                 optr[7*4] = iptr[7];
605                                 optr += 8*4;
606                                 iptr += 8;
607                                 count -= 8;
608                         }
609                         while(count--) {
610                                 *optr = *iptr++;
611                                 optr+=4;
612                         }
613                 } else {
614                         pixel = *iptr++;
615                         while(count>=8) {
616                                 optr[0*4] = pixel;
617                                 optr[1*4] = pixel;
618                                 optr[2*4] = pixel;
619                                 optr[3*4] = pixel;
620                                 optr[4*4] = pixel;
621                                 optr[5*4] = pixel;
622                                 optr[6*4] = pixel;
623                                 optr[7*4] = pixel;
624                                 optr += 8*4;
625                                 count -= 8;
626                         }
627                         while(count--) {
628                                 *optr = pixel;
629                                 optr+=4;
630                         }
631                 }
632         }
633 }
634
635 /*
636  *      output_iris -
637  *              copy an array of ints to an iris image file.  Each int
638  *      represents one pixel.  xsize and ysize specify the dimensions of
639  *      the pixel array.  zsize specifies what kind of image file to
640  *      write out.  if zsize is 1, the luminance of the pixels are
641  *      calculated, and a sinlge channel black and white image is saved.
642  *      If zsize is 3, an RGB image file is saved.  If zsize is 4, an
643  *      RGBA image file is saved.
644  *
645  *  Added: zbuf write
646  */
647
648 static int output_iris(unsigned int *lptr, int xsize, int ysize, int zsize, char *name, int *zptr)
649 {
650         FILE *outf;
651         IMAGE *image;
652         int tablen, y, z, pos, len = 0;
653         unsigned int *starttab, *lengthtab;
654         unsigned char *rlebuf;
655         unsigned int *lumbuf;
656         int rlebuflen, goodwrite;
657
658         goodwrite = 1;
659         outf = fopen(name, "wb");
660         if(!outf) return 0;
661
662         tablen = ysize*zsize*sizeof(int);
663
664         image = (IMAGE *)malloc(sizeof(IMAGE));
665         starttab = (unsigned int *)malloc(tablen);
666         lengthtab = (unsigned int *)malloc(tablen);
667         rlebuflen = 1.05*xsize+10;
668         rlebuf = (unsigned char *)malloc(rlebuflen);
669         lumbuf = (unsigned int *)malloc(xsize*sizeof(int));
670
671         memset(image, 0, sizeof(IMAGE));
672         image->imagic = IMAGIC;
673         image->type = RLE(1);
674         if(zsize>1)
675                 image->dim = 3;
676         else
677                 image->dim = 2;
678         image->xsize = xsize;
679         image->ysize = ysize;
680         image->zsize = zsize;
681         image->min = 0;
682         image->max = 255;
683         goodwrite *= writeheader(outf,image);
684         fseek(outf,512+2*tablen,SEEK_SET);
685         pos = 512+2*tablen;
686         
687         for (y = 0; y < ysize; y++) {
688                 for (z = 0; z < zsize; z++) {
689                         
690                         if (zsize == 1) {
691                                 lumrow((uchar *)lptr,(uchar *)lumbuf,xsize);
692                                 len = compressrow((uchar *)lumbuf,rlebuf,CHANOFFSET(z),xsize);
693                         }
694                         else {
695                                 if(z<4) {
696                                         len = compressrow((uchar *)lptr, rlebuf,CHANOFFSET(z),xsize);
697                                 }
698                                 else if(z<8 && zptr) {
699                                         len = compressrow((uchar *)zptr, rlebuf,CHANOFFSET(z-4),xsize);
700                                 }
701                         }
702                         if(len>rlebuflen) {
703                                 fprintf(stderr,"output_iris: rlebuf is too small - bad poop\n");
704                                 exit(1);
705                         }
706                         goodwrite *= fwrite(rlebuf, len, 1, outf);
707                         starttab[y+z*ysize] = pos;
708                         lengthtab[y+z*ysize] = len;
709                         pos += len;
710                 }
711                 lptr += xsize;
712                 if(zptr) zptr += xsize;
713         }
714
715         fseek(outf,512,SEEK_SET);
716         goodwrite *= writetab(outf,starttab,tablen);
717         goodwrite *= writetab(outf,lengthtab,tablen);
718         free(image);
719         free(starttab);
720         free(lengthtab);
721         free(rlebuf);
722         free(lumbuf);
723         fclose(outf);
724         if(goodwrite)
725                 return 1;
726         else {
727                 fprintf(stderr,"output_iris: not enough space for image!!\n");
728                 return 0;
729         }
730 }
731
732 /* static utility functions for output_iris */
733
734 static void lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
735 {
736         lumptr += CHANOFFSET(0);
737         while(n--) {
738                 *lumptr = ILUM(rgbptr[OFFSET_R],rgbptr[OFFSET_G],rgbptr[OFFSET_B]);
739                 lumptr += 4;
740                 rgbptr += 4;
741         }
742 }
743
744 static int compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
745 {
746         unsigned char *iptr, *ibufend, *sptr, *optr;
747         short todo, cc;
748         int count;
749
750         lbuf += z;
751         iptr = lbuf;
752         ibufend = iptr+cnt*4;
753         optr = rlebuf;
754
755         while(iptr<ibufend) {
756                 sptr = iptr;
757                 iptr += 8;
758                 while((iptr<ibufend)&& ((iptr[-8]!=iptr[-4])||(iptr[-4]!=iptr[0])))
759                         iptr+=4;
760                 iptr -= 8;
761                 count = (iptr-sptr)/4;
762                 while(count) {
763                         todo = count>126 ? 126:count;
764                         count -= todo;
765                         *optr++ = 0x80|todo;
766                         while(todo>8) {
767                                 optr[0] = sptr[0*4];
768                                 optr[1] = sptr[1*4];
769                                 optr[2] = sptr[2*4];
770                                 optr[3] = sptr[3*4];
771                                 optr[4] = sptr[4*4];
772                                 optr[5] = sptr[5*4];
773                                 optr[6] = sptr[6*4];
774                                 optr[7] = sptr[7*4];
775
776                                 optr += 8;
777                                 sptr += 8*4;
778                                 todo -= 8;
779                         }
780                         while(todo--) {
781                                 *optr++ = *sptr;
782                                 sptr += 4;
783                         }
784                 }
785                 sptr = iptr;
786                 cc = *iptr;
787                 iptr += 4;
788                 while( (iptr<ibufend) && (*iptr == cc) )
789                         iptr += 4;
790                 count = (iptr-sptr)/4;
791                 while(count) {
792                         todo = count>126 ? 126:count;
793                         count -= todo;
794                         *optr++ = todo;
795                         *optr++ = cc;
796                 }
797         }
798         *optr++ = 0;
799         return optr - (unsigned char *)rlebuf;
800 }
801
802 short imb_saveiris(struct ImBuf * ibuf, char *name, int flags)
803 {
804         short zsize;
805         int ret;
806
807         zsize = (ibuf->depth + 7) >> 3;
808         if (flags & IB_zbuf &&  ibuf->zbuf != 0) zsize = 8;
809         
810         IMB_convert_rgba_to_abgr(ibuf);
811         test_endian_zbuf(ibuf);
812
813         ret = output_iris(ibuf->rect, ibuf->x, ibuf->y, zsize, name, ibuf->zbuf);
814
815         /* restore! Quite clumsy, 2 times a switch... maybe better a malloc ? */
816         IMB_convert_rgba_to_abgr(ibuf);
817         test_endian_zbuf(ibuf);
818
819         return(ret);
820 }
821