41
42
#include "IMB_allocimbuf.h"
42
43
#include "IMB_filetype.h"
45
#include "IMB_colormanagement.h"
46
#include "IMB_colormanagement_intern.h"
45
unsigned short imagic; /* stuff saved on disk . . */
53
unsigned int wastebytes;
55
unsigned int colormap;
49
unsigned short imagic; /* stuff saved on disk . . */
57
unsigned int wastebytes;
59
unsigned int colormap;
57
int file; /* stuff used in core only */
66
unsigned short *tmpbuf;
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 */
70
unsigned short *tmpbuf;
72
unsigned int rleend; /* for rle images */
73
unsigned int *rowstart; /* for rle images */
74
int *rowsize; /* for rle images */
73
77
#define RINTLUM (79)
74
78
#define GINTLUM (156)
75
79
#define BINTLUM (21)
77
#define ILUM(r,g,b) ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
79
#define OFFSET_R 0 /* this is byte order dependent */
84
#define CHANOFFSET(z) (3-(z)) /* this is byte order dependent */
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)
81
#define ILUM(r, g, b) ((int)(RINTLUM * (r) + GINTLUM * (g) + BINTLUM * (b)) >> 8)
83
#define OFFSET_R 0 /* this is byte order dependent */
88
#define CHANOFFSET(z) (3 - (z)) /* this is byte order dependent */
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
99
103
static void readheader(FILE *inf, IMAGE *image);
121
static uchar * file_data;
125
static uchar *file_data;
122
126
static int file_offset;
124
128
static unsigned short getshort(FILE *inf)
127
131
(void)inf; /* unused */
129
133
buf = file_data + file_offset;
130
134
file_offset += 2;
132
return (buf[0]<<8)+(buf[1]<<0);
136
return (buf[0] << 8) + (buf[1] << 0);
135
139
static unsigned int getlong(FILE *inf)
138
142
(void)inf; /* unused */
140
144
buf = file_data + file_offset;
141
145
file_offset += 4;
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);
146
150
static void putshort(FILE *outf, unsigned short val)
148
152
unsigned char buf[2];
152
fwrite(buf,2,1,outf);
156
fwrite(buf, 2, 1, outf);
155
159
static int putlong(FILE *outf, unsigned int val)
157
161
unsigned char buf[4];
163
return fwrite(buf,4,1,outf);
163
buf[0] = (val >> 24);
164
buf[1] = (val >> 16);
167
return fwrite(buf, 4, 1, outf);
166
170
static void readheader(FILE *inf, IMAGE *image)
177
181
static int writeheader(FILE *outf, IMAGE *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);
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);
196
return fwrite("no name", 8, 1, outf);
195
199
static int writetab(FILE *outf, unsigned int *tab, int len)
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])
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;
263
267
(void)size; /* unused */
265
269
if (!imb_is_a_iris(mem)) return NULL;
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);
267
274
/*printf("new iris\n");*/
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");
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");
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;
302
readtab(inf,starttab,tablen);
303
readtab(inf,lengthtab,tablen);
309
readtab(inf, starttab, tablen);
310
readtab(inf, lengthtab, tablen);
305
312
/* check data order */
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) {
314
cur = starttab[y+z*ysize];
321
cur = starttab[y + z * ysize];
325
332
zbase = (unsigned int *)ibuf->zbuf;
328
for (z=0; z<zsize; z++) {
335
for (z = 0; z < zsize; z++) {
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];
333
340
rledat = file_data + file_offset;
334
file_offset += lengthtab[y+z*ysize];
341
file_offset += lengthtab[y + z * ysize];
336
expandrow((uchar *)lptr, rledat, 3-z);
343
expandrow((uchar *)lptr, rledat, 3 - z);
344
for (y=0; y<ysize; y++) {
351
for (y = 0; y < ysize; y++) {
346
for (z=0; z<zsize; z++) {
353
for (z = 0; z < zsize; z++) {
348
file_offset = starttab[y+z*ysize];
355
file_offset = starttab[y + z * ysize];
350
357
rledat = file_data + file_offset;
351
file_offset += lengthtab[y+z*ysize];
358
file_offset += lengthtab[y + z * ysize];
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);
363
else { /* bpp == 2 */
370
else { /* bpp == 2 */
365
ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect)|IB_rectfloat);
372
ibuf = IMB_allocImBuf(xsize, ysize, 32, (flags & IB_rect) | IB_rectfloat);
367
374
fbase = ibuf->rect_float;
370
for (z=0; z<zsize; z++) {
377
for (z = 0; z < zsize; z++) {
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];
375
382
rledat = file_data + file_offset;
376
file_offset += lengthtab[y+z*ysize];
383
file_offset += lengthtab[y + z * ysize];
378
expandrow2(fptr, rledat, 3-z);
385
expandrow2(fptr, rledat, 3 - z);
379
386
fptr += xsize * 4;
386
for (y=0; y<ysize; y++) {
393
for (y = 0; y < ysize; y++) {
388
for (z=0; z<zsize; z++) {
395
for (z = 0; z < zsize; z++) {
390
file_offset = starttab[y+z*ysize];
397
file_offset = starttab[y + z * ysize];
392
399
rledat = file_data + file_offset;
393
file_offset += lengthtab[y+z*ysize];
400
file_offset += lengthtab[y + z * ysize];
395
expandrow2(fptr, rledat, 3-z);
402
expandrow2(fptr, rledat, 3 - z);
398
405
fptr += xsize * 4;
419
426
for (z = 0; z < zsize; z++) {
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;
424
431
for (y = 0; y < ysize; y++) {
426
interleaverow((uchar *)lptr, rledat, 3-z, xsize);
433
interleaverow((uchar *)lptr, rledat, 3 - z, xsize);
566
pixel = (iptr[0]<<8) | (iptr[1]<<0);
570
pixel = (iptr[0] << 8) | (iptr[1] << 0);
569
if ( !(count = (pixel & 0x7f)) )
573
if (!(count = (pixel & 0x7f)) )
571
575
if (pixel & 0x80) {
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;
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
589
while (count--) {
586
*optr = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
590
*optr = ((iptr[0] << 8) | (iptr[1] << 0)) / (float)0xFFFF;
592
pixel_f = ((iptr[0]<<8) | (iptr[1]<<0))/(float)0xFFFF;
596
pixel_f = ((iptr[0] << 8) | (iptr[1] << 0)) / (float)0xFFFF;
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;
607
611
while (count--) {
615
619
static void expandrow(unsigned char *optr, unsigned char *iptr, int z)
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.
689
693
outf = BLI_fopen(name, "wb");
690
694
if (!outf) return 0;
692
tablen = ysize*zsize*sizeof(int);
696
tablen = ysize * zsize * sizeof(int);
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");
701
705
memset(image, 0, sizeof(IMAGE));
702
706
image->imagic = IMAGIC;
703
707
image->type = RLE(1);
710
714
image->zsize = zsize;
712
716
image->max = 255;
713
goodwrite *= writeheader(outf,image);
714
fseek(outf,512+2*tablen,SEEK_SET);
717
goodwrite *= writeheader(outf, image);
718
fseek(outf, 512 + 2 * tablen, SEEK_SET);
719
pos = 512 + 2 * tablen;
717
721
for (y = 0; y < ysize; y++) {
718
722
for (z = 0; z < zsize; z++) {
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);
726
len = compressrow((uchar *)lptr, rlebuf,CHANOFFSET(z),xsize);
730
len = compressrow((uchar *)lptr, rlebuf, CHANOFFSET(z), xsize);
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);
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");
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;
742
746
if (zptr) zptr += xsize;
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);