Blender V2.27

iris.c

00001 
00032 #include <string.h>
00033 
00034 #include "BLI_blenlib.h"
00035 #include "MEM_guardedalloc.h"
00036 
00037 #include "imbuf.h"
00038 #include "IMB_imbuf_types.h"
00039 #include "IMB_imbuf.h"
00040 #include "IMB_allocimbuf.h"
00041 #include "IMB_filetype.h"
00042 
00043 typedef struct {
00044         unsigned short  imagic;         /* stuff saved on disk . . */
00045         unsigned short  type;
00046         unsigned short  dim;
00047         unsigned short  xsize;
00048         unsigned short  ysize;
00049         unsigned short  zsize;
00050         unsigned int    min;
00051         unsigned int    max;
00052         unsigned int    wastebytes;     
00053         char            name[80];
00054         unsigned int    colormap;
00055 
00056         int             file;           /* stuff used in core only */
00057         unsigned short  flags;
00058         short           dorev;
00059         short           x;
00060         short           y;
00061         short           z;
00062         short           cnt;
00063         unsigned short  *ptr;
00064         unsigned short  *base;
00065         unsigned short  *tmpbuf;
00066         unsigned int    offset;
00067         unsigned int    rleend;         /* for rle images */
00068         unsigned int    *rowstart;      /* for rle images */
00069         int             *rowsize;       /* for rle images */
00070 } IMAGE;
00071 
00072 #define RINTLUM (79)
00073 #define GINTLUM (156)
00074 #define BINTLUM (21)
00075 
00076 #define ILUM(r,g,b)     ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
00077 
00078 #define OFFSET_R        0       /* this is byte order dependent */
00079 #define OFFSET_G        1
00080 #define OFFSET_B        2
00081 #define OFFSET_A        3
00082 
00083 #define CHANOFFSET(z)   (3-(z)) /* this is byte order dependent */
00084 
00085 #define TYPEMASK                0xff00
00086 #define BPPMASK                 0x00ff
00087 #define ITYPE_VERBATIM          0x0000
00088 #define ITYPE_RLE               0x0100
00089 #define ISRLE(type)             (((type) & 0xff00) == ITYPE_RLE)
00090 #define ISVERBATIM(type)        (((type) & 0xff00) == ITYPE_VERBATIM)
00091 #define BPP(type)               ((type) & BPPMASK)
00092 #define RLE(bpp)                (ITYPE_RLE | (bpp))
00093 #define VERBATIM(bpp)           (ITYPE_VERBATIM | (bpp))
00094 #define IBUFSIZE(pixels)        ((pixels+(pixels>>6))<<2)
00095 #define RLE_NOP                 0x00
00096 
00097 /* funcs */
00098 static void readheader(FILE *inf, IMAGE *image);
00099 static int writeheader(FILE *outf, IMAGE *image);
00100 
00101 static unsigned short getshort(FILE *inf);
00102 static unsigned int getlong(FILE *inf);
00103 static void putshort(FILE *outf, unsigned short val);
00104 static int putlong(FILE *outf, unsigned int val);
00105 static int writetab(FILE *outf, unsigned int *tab, int len);
00106 static void readtab(FILE *inf, unsigned int *tab, int len);
00107 
00108 static void expandrow(unsigned char *optr, unsigned char *iptr, int z);
00109 static void expandrow2(float *optr, unsigned char *iptr, int z);
00110 static void interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n);
00111 static void interleaverow2(float *lptr, unsigned char *cptr, int z, int n);
00112 static int compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt);
00113 static void lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n);
00114 
00115 /*
00116  *      byte order independent read/write of shorts and ints.
00117  *
00118  */
00119 
00120 static uchar * file_data;
00121 static int file_offset;
00122 
00123 static unsigned short getshort(FILE *inf)
00124 {
00125         unsigned char * buf;
00126         (void)inf; /* unused */
00127 
00128         buf = file_data + file_offset;
00129         file_offset += 2;
00130         
00131         return (buf[0]<<8)+(buf[1]<<0);
00132 }
00133 
00134 static unsigned int getlong(FILE *inf)
00135 {
00136         unsigned char * buf;
00137         (void)inf; /* unused */
00138         
00139         buf = file_data + file_offset;
00140         file_offset += 4;
00141         
00142         return (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+(buf[3]<<0);
00143 }
00144 
00145 static void putshort(FILE *outf, unsigned short val)
00146 {
00147         unsigned char buf[2];
00148 
00149         buf[0] = (val>>8);
00150         buf[1] = (val>>0);
00151         fwrite(buf,2,1,outf);
00152 }
00153 
00154 static int putlong(FILE *outf, unsigned int val)
00155 {
00156         unsigned char buf[4];
00157 
00158         buf[0] = (val>>24);
00159         buf[1] = (val>>16);
00160         buf[2] = (val>>8);
00161         buf[3] = (val>>0);
00162         return fwrite(buf,4,1,outf);
00163 }
00164 
00165 static void readheader(FILE *inf, IMAGE *image)
00166 {
00167         memset(image, 0, sizeof(IMAGE));
00168         image->imagic = getshort(inf);
00169         image->type = getshort(inf);
00170         image->dim = getshort(inf);
00171         image->xsize = getshort(inf);
00172         image->ysize = getshort(inf);
00173         image->zsize = getshort(inf);
00174 }
00175 
00176 static int writeheader(FILE *outf, IMAGE *image)
00177 {
00178         IMAGE t= {0};
00179 
00180         fwrite(&t,sizeof(IMAGE),1,outf);
00181         fseek(outf,0,SEEK_SET);
00182         putshort(outf,image->imagic);
00183         putshort(outf,image->type);
00184         putshort(outf,image->dim);
00185         putshort(outf,image->xsize);
00186         putshort(outf,image->ysize);
00187         putshort(outf,image->zsize);
00188         putlong(outf,image->min);
00189         putlong(outf,image->max);
00190         putlong(outf,0);
00191         return fwrite("no name",8,1,outf);
00192 }
00193 
00194 static int writetab(FILE *outf, unsigned int *tab, int len)
00195 {
00196         int r = 0;
00197 
00198         while(len) {
00199                 r = putlong(outf,*tab++);
00200                 len -= 4;
00201         }
00202         return r;
00203 }
00204 
00205 static void readtab(FILE *inf, unsigned int *tab, int len)
00206 {
00207         while(len) {
00208                 *tab++ = getlong(inf);
00209                 len -= 4;
00210         }
00211 }
00212 
00213 static void test_endian_zbuf(struct ImBuf *ibuf)
00214 {
00215         int len;
00216         int *zval;
00217         
00218         if( BIG_LONG(1) == 1 ) return;
00219         if(ibuf->zbuf==0) return;
00220         
00221         len= ibuf->x*ibuf->y;
00222         zval= ibuf->zbuf;
00223         
00224         while(len--) {
00225                 zval[0]= BIG_LONG(zval[0]);
00226                 zval++;
00227         }
00228 }
00229 
00230 /* from misc_util: flip the bytes from x  */
00231 #define GS(x) (((unsigned char *)(x))[0] << 8 | ((unsigned char *)(x))[1])
00232 
00233 /* this one is only def-ed once, strangely... */
00234 #define GSS(x) (((uchar *)(x))[1] << 8 | ((uchar *)(x))[0])
00235 
00236 int imb_is_a_iris(unsigned char *mem)
00237 {
00238         return ((GS(mem) == IMAGIC) || (GSS(mem) == IMAGIC));
00239 }
00240 
00241 /*
00242  *      longimagedata - 
00243  *              read in a B/W RGB or RGBA iris image file and return a 
00244  *      pointer to an array of ints.
00245  *
00246  */
00247 
00248 struct ImBuf *imb_loadiris(unsigned char *mem, size_t size, int flags)
00249 {
00250         unsigned int *base, *lptr = NULL;
00251         float *fbase, *fptr = NULL;
00252         unsigned int *zbase, *zptr;
00253         unsigned char *rledat;
00254         unsigned int *starttab, *lengthtab;
00255         FILE *inf = NULL;
00256         IMAGE image;
00257         int x, y, z, tablen;
00258         int xsize, ysize, zsize;
00259         int bpp, rle, cur, badorder;
00260         ImBuf * ibuf;
00261 
00262         (void)size; /* unused */
00263         
00264         if(!imb_is_a_iris(mem)) return NULL;
00265 
00266         /*printf("new iris\n");*/
00267         
00268         file_data = mem;
00269         file_offset = 0;
00270         
00271         readheader(inf, &image);
00272         if(image.imagic != IMAGIC) {
00273                 fprintf(stderr,"longimagedata: bad magic number in image file\n");
00274                 return(0);
00275         }
00276         
00277         rle = ISRLE(image.type);
00278         bpp = BPP(image.type);
00279         if(bpp != 1 && bpp != 2) {
00280                 fprintf(stderr,"longimagedata: image must have 1 or 2 byte per pix chan\n");
00281                 return(0);
00282         }
00283         
00284         xsize = image.xsize;
00285         ysize = image.ysize;
00286         zsize = image.zsize;
00287         
00288         if (flags & IB_test) {
00289                 ibuf = IMB_allocImBuf(image.xsize, image.ysize, 8 * image.zsize, 0);
00290                 if (ibuf) ibuf->ftype = IMAGIC;
00291                 return(ibuf);
00292         }
00293         
00294         if (rle) {
00295                 
00296                 tablen = ysize*zsize*sizeof(int);
00297                 starttab = (unsigned int *)MEM_mallocN(tablen, "iris starttab");
00298                 lengthtab = (unsigned int *)MEM_mallocN(tablen, "iris endtab");
00299                 file_offset = 512;
00300                 
00301                 readtab(inf,starttab,tablen);
00302                 readtab(inf,lengthtab,tablen);
00303         
00304                 /* check data order */
00305                 cur = 0;
00306                 badorder = 0;
00307                 for (y = 0; y<ysize; y++) {
00308                         for (z = 0; z<zsize; z++) {
00309                                 if (starttab[y+z*ysize]<cur) {
00310                                         badorder = 1;
00311                                         break;
00312                                 }
00313                                 cur = starttab[y+z*ysize];
00314                         }
00315                         if(badorder)
00316                                 break;
00317                 }
00318         
00319                 if (bpp == 1) {
00320                         
00321                         ibuf = IMB_allocImBuf(xsize, ysize, 8 * zsize, IB_rect);
00322                         if (ibuf->depth > 32) ibuf->depth = 32;
00323                         base = ibuf->rect;
00324                         zbase = (unsigned int *)ibuf->zbuf;
00325                         
00326                         if (badorder) {
00327                                 for(z=0; z<zsize; z++) {
00328                                         lptr = base;
00329                                         for(y=0; y<ysize; y++) {
00330                                                 file_offset = starttab[y+z*ysize];
00331                                                 
00332                                                 rledat = file_data + file_offset;
00333                                                 file_offset += lengthtab[y+z*ysize];
00334                                                 
00335                                                 expandrow((uchar *)lptr, rledat, 3-z);
00336                                                 lptr += xsize;
00337                                         }
00338                                 }
00339                         } else {
00340                                 lptr = base;
00341                                 zptr = zbase;
00342                                 for(y=0; y<ysize; y++) {
00343                                 
00344                                         for(z=0; z<zsize; z++) {
00345                                                 
00346                                                 file_offset = starttab[y+z*ysize];
00347 
00348                                                 rledat = file_data + file_offset;
00349                                                 file_offset += lengthtab[y+z*ysize];
00350                                                 
00351                                                 if(z<4) expandrow((uchar *)lptr, rledat, 3-z);
00352                                                 else if(z<8) expandrow((uchar *)zptr, rledat, 7-z);
00353                                         }
00354                                         lptr += xsize;
00355                                         zptr += xsize;
00356                                 }
00357                         }
00358                         
00359 
00360                 } else {        /* bpp == 2 */
00361                         
00362                         ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect)|IB_rectfloat);
00363                         
00364                         fbase = ibuf->rect_float;
00365                         
00366                         if (badorder) {
00367                                 for(z=0; z<zsize; z++) {
00368                                         fptr = fbase;
00369                                         for(y=0; y<ysize; y++) {
00370                                                 file_offset = starttab[y+z*ysize];
00371                                                 
00372                                                 rledat = file_data + file_offset;
00373                                                 file_offset += lengthtab[y+z*ysize];
00374                                                 
00375                                                 expandrow2(fptr, rledat, 3-z);
00376                                                 fptr += xsize * 4;
00377                                         }
00378                                 }
00379                         } else {
00380                                 fptr = fbase;
00381 
00382                                 for(y=0; y<ysize; y++) {
00383                                 
00384                                         for(z=0; z<zsize; z++) {
00385                                                 
00386                                                 file_offset = starttab[y+z*ysize];
00387 
00388                                                 rledat = file_data + file_offset;
00389                                                 file_offset += lengthtab[y+z*ysize];
00390                                                 
00391                                                 expandrow2(fptr, rledat, 3-z);
00392                                                 
00393                                         }
00394                                         fptr += xsize * 4;
00395                                 }
00396                         }
00397                 }
00398                 
00399                 MEM_freeN(starttab);
00400                 MEM_freeN(lengthtab);   
00401 
00402         } else {
00403                 if (bpp == 1) {
00404                         
00405                         ibuf = IMB_allocImBuf(xsize, ysize, 8 * zsize, IB_rect);
00406                         if (ibuf->depth > 32) ibuf->depth = 32;
00407 
00408                         base = ibuf->rect;
00409                         zbase = (unsigned int *)ibuf->zbuf;
00410                         
00411                         file_offset = 512;
00412                         rledat = file_data + file_offset;
00413                         
00414                         for(z = 0; z < zsize; z++) {
00415                                 
00416                                 if(z<4) lptr = base;
00417                                 else if(z<8) lptr= zbase;
00418                                 
00419                                 for(y = 0; y < ysize; y++) {
00420 
00421                                         interleaverow((uchar *)lptr, rledat, 3-z, xsize);
00422                                         rledat += xsize;
00423                                         
00424                                         lptr += xsize;
00425                                 }
00426                         }
00427                         
00428                 } else {        /* bpp == 2 */
00429                         
00430                         ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect)|IB_rectfloat);
00431 
00432                         fbase = ibuf->rect_float;
00433 
00434                         file_offset = 512;
00435                         rledat = file_data + file_offset;
00436                         
00437                         for(z = 0; z < zsize; z++) {
00438                                 
00439                                 fptr = fbase;
00440                                 
00441                                 for(y = 0; y < ysize; y++) {
00442 
00443                                         interleaverow2(fptr, rledat, 3-z, xsize);
00444                                         rledat += xsize * 2;
00445                                         
00446                                         fptr += xsize * 4;
00447                                 }
00448                         }
00449                         
00450                 }
00451         }
00452         
00453         
00454         if (bpp == 1) {
00455                 uchar * rect;
00456                 
00457                 if (image.zsize == 1){
00458                         rect = (uchar *) ibuf->rect;
00459                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
00460                                 rect[0] = 255;
00461                                 rect[1] = rect[2] = rect[3];
00462                                 rect += 4;
00463                         }
00464                 } else if (image.zsize == 2){
00465                         /* grayscale with alpha */
00466                         rect = (uchar *) ibuf->rect;
00467                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
00468                                 rect[0] = rect[2];
00469                                 rect[1] = rect[2] = rect[3];
00470                                 rect += 4;
00471                         }
00472                 } else if (image.zsize == 3){
00473                         /* add alpha */
00474                         rect = (uchar *) ibuf->rect;
00475                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
00476                                 rect[0] = 255;
00477                                 rect += 4;
00478                         }
00479                 }
00480                 
00481         } else {        /* bpp == 2 */
00482                 
00483                 if (image.zsize == 1){
00484                         fbase = ibuf->rect_float;
00485                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
00486                                 fbase[0] = 1;
00487                                 fbase[1] = fbase[2] = fbase[3];
00488                                 fbase += 4;
00489                         }
00490                 } else if (image.zsize == 2){
00491                         /* grayscale with alpha */
00492                         fbase = ibuf->rect_float;
00493                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
00494                                 fbase[0] = fbase[2];
00495                                 fbase[1] = fbase[2] = fbase[3];
00496                                 fbase += 4;
00497                         }
00498                 } else if (image.zsize == 3){
00499                         /* add alpha */
00500                         fbase = ibuf->rect_float;
00501                         for (x = ibuf->x * ibuf->y; x > 0; x--) {
00502                                 fbase[0] = 1;
00503                                 fbase += 4;
00504                         }
00505                 }
00506                 
00507                 if (flags & IB_rect) {
00508                         IMB_rect_from_float(ibuf);
00509                 }
00510                 
00511         }
00512 
00513         ibuf->ftype = IMAGIC;
00514         ibuf->profile = IB_PROFILE_SRGB;
00515         
00516         test_endian_zbuf(ibuf);
00517         
00518         if (ibuf) {
00519                 if (ibuf->rect) 
00520                         IMB_convert_rgba_to_abgr(ibuf);
00521         }
00522 
00523         return(ibuf);
00524 }
00525 
00526 /* static utility functions for longimagedata */
00527 
00528 static void interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
00529 {
00530         lptr += z;
00531         while(n--) {
00532                 *lptr = *cptr++;
00533                 lptr += 4;
00534         }
00535 }
00536 
00537 static void interleaverow2(float *lptr, unsigned char *cptr, int z, int n)
00538 {
00539         lptr += z;
00540         while(n--) {
00541                 *lptr = ((cptr[0]<<8) | (cptr[1]<<0)) / (float)0xFFFF;          
00542                 cptr += 2;
00543                 lptr += 4;
00544         }
00545 }
00546 
00547 static void expandrow2(float *optr, unsigned char *iptr, int z)
00548 {
00549         unsigned short pixel, count;
00550         float pixel_f;
00551 
00552         optr += z;
00553         while(1) {
00554                 pixel = (iptr[0]<<8) | (iptr[1]<<0);
00555                 iptr += 2;
00556                 
00557                 if ( !(count = (pixel & 0x7f)) )
00558                         return;
00559                 if(pixel & 0x80) {
00560                         while(count>=8) {
00561                                 optr[0*4] = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
00562                                 optr[1*4] = ((iptr[2]<<8) | (iptr[3]<<0))/(float)0xFFFF;
00563                                 optr[2*4] = ((iptr[4]<<8) | (iptr[5]<<0))/(float)0xFFFF;
00564                                 optr[3*4] = ((iptr[6]<<8) | (iptr[7]<<0))/(float)0xFFFF;
00565                                 optr[4*4] = ((iptr[8]<<8) | (iptr[9]<<0))/(float)0xFFFF;
00566                                 optr[5*4] = ((iptr[10]<<8) | (iptr[11]<<0))/(float)0xFFFF;
00567                                 optr[6*4] = ((iptr[12]<<8) | (iptr[13]<<0))/(float)0xFFFF;
00568                                 optr[7*4] = ((iptr[14]<<8) | (iptr[15]<<0))/(float)0xFFFF;
00569                                 optr += 8*4;
00570                                 iptr += 8*2;
00571                                 count -= 8;
00572                         }
00573                         while(count--) {
00574                                 *optr = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
00575                                 iptr+=2;
00576                                 optr+=4;
00577                         }
00578                 } else {
00579                         pixel_f = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
00580                         iptr += 2;
00581 
00582                         while(count>=8) {
00583                                 optr[0*4] = pixel_f;
00584                                 optr[1*4] = pixel_f;
00585                                 optr[2*4] = pixel_f;
00586                                 optr[3*4] = pixel_f;
00587                                 optr[4*4] = pixel_f;
00588                                 optr[5*4] = pixel_f;
00589                                 optr[6*4] = pixel_f;
00590                                 optr[7*4] = pixel_f;
00591                                 optr += 8*4;
00592                                 count -= 8;
00593                         }
00594                         while(count--) {
00595                                 *optr = pixel_f;
00596                                 optr+=4;
00597                         }
00598                 }
00599         }       
00600 }
00601 
00602 static void expandrow(unsigned char *optr, unsigned char *iptr, int z)
00603 {
00604         unsigned char pixel, count;
00605 
00606         optr += z;
00607         while(1) {
00608                 pixel = *iptr++;
00609                 if ( !(count = (pixel & 0x7f)) )
00610                         return;
00611                 if(pixel & 0x80) {
00612                         while(count>=8) {
00613                                 optr[0*4] = iptr[0];
00614                                 optr[1*4] = iptr[1];
00615                                 optr[2*4] = iptr[2];
00616                                 optr[3*4] = iptr[3];
00617                                 optr[4*4] = iptr[4];
00618                                 optr[5*4] = iptr[5];
00619                                 optr[6*4] = iptr[6];
00620                                 optr[7*4] = iptr[7];
00621                                 optr += 8*4;
00622                                 iptr += 8;
00623                                 count -= 8;
00624                         }
00625                         while(count--) {
00626                                 *optr = *iptr++;
00627                                 optr+=4;
00628                         }
00629                 } else {
00630                         pixel = *iptr++;
00631                         while(count>=8) {
00632                                 optr[0*4] = pixel;
00633                                 optr[1*4] = pixel;
00634                                 optr[2*4] = pixel;
00635                                 optr[3*4] = pixel;
00636                                 optr[4*4] = pixel;
00637                                 optr[5*4] = pixel;
00638                                 optr[6*4] = pixel;
00639                                 optr[7*4] = pixel;
00640                                 optr += 8*4;
00641                                 count -= 8;
00642                         }
00643                         while(count--) {
00644                                 *optr = pixel;
00645                                 optr+=4;
00646                         }
00647                 }
00648         }
00649 }
00650 
00651 /*
00652  *      output_iris -
00653  *              copy an array of ints to an iris image file.  Each int
00654  *      represents one pixel.  xsize and ysize specify the dimensions of
00655  *      the pixel array.  zsize specifies what kind of image file to
00656  *      write out.  if zsize is 1, the luminance of the pixels are
00657  *      calculated, and a sinlge channel black and white image is saved.
00658  *      If zsize is 3, an RGB image file is saved.  If zsize is 4, an
00659  *      RGBA image file is saved.
00660  *
00661  *  Added: zbuf write
00662  */
00663 
00664 static int output_iris(unsigned int *lptr, int xsize, int ysize, int zsize, const char *name, int *zptr)
00665 {
00666         FILE *outf;
00667         IMAGE *image;
00668         int tablen, y, z, pos, len = 0;
00669         unsigned int *starttab, *lengthtab;
00670         unsigned char *rlebuf;
00671         unsigned int *lumbuf;
00672         int rlebuflen, goodwrite;
00673 
00674         goodwrite = 1;
00675         outf = fopen(name, "wb");
00676         if(!outf) return 0;
00677 
00678         tablen = ysize*zsize*sizeof(int);
00679 
00680         image = (IMAGE *)MEM_mallocN(sizeof(IMAGE), "iris image");
00681         starttab = (unsigned int *)MEM_mallocN(tablen, "iris starttab");
00682         lengthtab = (unsigned int *)MEM_mallocN(tablen, "iris lengthtab");
00683         rlebuflen = 1.05*xsize+10;
00684         rlebuf = (unsigned char *)MEM_mallocN(rlebuflen, "iris rlebuf");
00685         lumbuf = (unsigned int *)MEM_mallocN(xsize*sizeof(int), "iris lumbuf");
00686 
00687         memset(image, 0, sizeof(IMAGE));
00688         image->imagic = IMAGIC;
00689         image->type = RLE(1);
00690         if(zsize>1)
00691                 image->dim = 3;
00692         else
00693                 image->dim = 2;
00694         image->xsize = xsize;
00695         image->ysize = ysize;
00696         image->zsize = zsize;
00697         image->min = 0;
00698         image->max = 255;
00699         goodwrite *= writeheader(outf,image);
00700         fseek(outf,512+2*tablen,SEEK_SET);
00701         pos = 512+2*tablen;
00702         
00703         for (y = 0; y < ysize; y++) {
00704                 for (z = 0; z < zsize; z++) {
00705                         
00706                         if (zsize == 1) {
00707                                 lumrow((uchar *)lptr,(uchar *)lumbuf,xsize);
00708                                 len = compressrow((uchar *)lumbuf,rlebuf,CHANOFFSET(z),xsize);
00709                         }
00710                         else {
00711                                 if(z<4) {
00712                                         len = compressrow((uchar *)lptr, rlebuf,CHANOFFSET(z),xsize);
00713                                 }
00714                                 else if(z<8 && zptr) {
00715                                         len = compressrow((uchar *)zptr, rlebuf,CHANOFFSET(z-4),xsize);
00716                                 }
00717                         }
00718                         if(len>rlebuflen) {
00719                                 fprintf(stderr,"output_iris: rlebuf is too small - bad poop\n");
00720                                 exit(1);
00721                         }
00722                         goodwrite *= fwrite(rlebuf, len, 1, outf);
00723                         starttab[y+z*ysize] = pos;
00724                         lengthtab[y+z*ysize] = len;
00725                         pos += len;
00726                 }
00727                 lptr += xsize;
00728                 if(zptr) zptr += xsize;
00729         }
00730 
00731         fseek(outf,512,SEEK_SET);
00732         goodwrite *= writetab(outf,starttab,tablen);
00733         goodwrite *= writetab(outf,lengthtab,tablen);
00734         MEM_freeN(image);
00735         MEM_freeN(starttab);
00736         MEM_freeN(lengthtab);
00737         MEM_freeN(rlebuf);
00738         MEM_freeN(lumbuf);
00739         fclose(outf);
00740         if(goodwrite)
00741                 return 1;
00742         else {
00743                 fprintf(stderr,"output_iris: not enough space for image!!\n");
00744                 return 0;
00745         }
00746 }
00747 
00748 /* static utility functions for output_iris */
00749 
00750 static void lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
00751 {
00752         lumptr += CHANOFFSET(0);
00753         while(n--) {
00754                 *lumptr = ILUM(rgbptr[OFFSET_R],rgbptr[OFFSET_G],rgbptr[OFFSET_B]);
00755                 lumptr += 4;
00756                 rgbptr += 4;
00757         }
00758 }
00759 
00760 static int compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
00761 {
00762         unsigned char *iptr, *ibufend, *sptr, *optr;
00763         short todo, cc;
00764         int count;
00765 
00766         lbuf += z;
00767         iptr = lbuf;
00768         ibufend = iptr+cnt*4;
00769         optr = rlebuf;
00770 
00771         while(iptr<ibufend) {
00772                 sptr = iptr;
00773                 iptr += 8;
00774                 while((iptr<ibufend)&& ((iptr[-8]!=iptr[-4])||(iptr[-4]!=iptr[0])))
00775                         iptr+=4;
00776                 iptr -= 8;
00777                 count = (iptr-sptr)/4;
00778                 while(count) {
00779                         todo = count>126 ? 126:count;
00780                         count -= todo;
00781                         *optr++ = 0x80|todo;
00782                         while(todo>8) {
00783                                 optr[0] = sptr[0*4];
00784                                 optr[1] = sptr[1*4];
00785                                 optr[2] = sptr[2*4];
00786                                 optr[3] = sptr[3*4];
00787                                 optr[4] = sptr[4*4];
00788                                 optr[5] = sptr[5*4];
00789                                 optr[6] = sptr[6*4];
00790                                 optr[7] = sptr[7*4];
00791 
00792                                 optr += 8;
00793                                 sptr += 8*4;
00794                                 todo -= 8;
00795                         }
00796                         while(todo--) {
00797                                 *optr++ = *sptr;
00798                                 sptr += 4;
00799                         }
00800                 }
00801                 sptr = iptr;
00802                 cc = *iptr;
00803                 iptr += 4;
00804                 while( (iptr<ibufend) && (*iptr == cc) )
00805                         iptr += 4;
00806                 count = (iptr-sptr)/4;
00807                 while(count) {
00808                         todo = count>126 ? 126:count;
00809                         count -= todo;
00810                         *optr++ = todo;
00811                         *optr++ = cc;
00812                 }
00813         }
00814         *optr++ = 0;
00815         return optr - (unsigned char *)rlebuf;
00816 }
00817 
00818 int imb_saveiris(struct ImBuf * ibuf, const char *name, int flags)
00819 {
00820         short zsize;
00821         int ret;
00822 
00823         zsize = (ibuf->depth + 7) >> 3;
00824         if (flags & IB_zbuf &&  ibuf->zbuf != 0) zsize = 8;
00825         
00826         IMB_convert_rgba_to_abgr(ibuf);
00827         test_endian_zbuf(ibuf);
00828 
00829         ret = output_iris(ibuf->rect, ibuf->x, ibuf->y, zsize, name, ibuf->zbuf);
00830 
00831         /* restore! Quite clumsy, 2 times a switch... maybe better a malloc ? */
00832         IMB_convert_rgba_to_abgr(ibuf);
00833         test_endian_zbuf(ibuf);
00834 
00835         return(ret);
00836 }
00837