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