~ubuntu-branches/ubuntu/trusty/blender/trusty

« back to all changes in this revision

Viewing changes to source/blender/imbuf/intern/iris.c

  • Committer: Package Import Robot
  • Author(s): Jeremy Bicha
  • Date: 2013-03-06 12:08:47 UTC
  • mfrom: (1.5.1) (14.1.8 experimental)
  • Revision ID: package-import@ubuntu.com-20130306120847-frjfaryb2zrotwcg
Tags: 2.66a-1ubuntu1
* Resynchronize with Debian (LP: #1076930, #1089256, #1052743, #999024,
  #1122888, #1147084)
* debian/control:
  - Lower build-depends on libavcodec-dev since we're not
    doing the libav9 transition in Ubuntu yet

Show diffs side-by-side

added added

removed removed

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