blob: a997ba48ec6ecccab3660a0561c15a85df3dbd7a [file] [log] [blame]
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +00001/*
2 * fastimg -
3 * Faster reading and writing of image files.
4 *
5 * This code should work on machines with any byte order.
6 *
7 * Could someone make this run real fast using multiple processors
8 * or how about using memory mapped files to speed it up?
9 *
10 * Paul Haeberli - 1991
11 *
12 * Changed to return sizes.
13 * Sjoerd Mullender - 1993
14 * Changed to incorporate into Python.
15 * Sjoerd Mullender - 1993
16 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +000017#include "Python.h"
18
Guido van Rossumb6775db1994-08-01 11:34:53 +000019#ifdef HAVE_UNISTD_H
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000020#include <unistd.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000021#endif
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +000022#include <string.h>
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000023
24/*
25 * from image.h
26 *
27 */
28typedef struct {
29 unsigned short imagic; /* stuff saved on disk . . */
30 unsigned short type;
31 unsigned short dim;
32 unsigned short xsize;
33 unsigned short ysize;
34 unsigned short zsize;
35 unsigned long min;
36 unsigned long max;
37 unsigned long wastebytes;
38 char name[80];
39 unsigned long colormap;
40
41 long file; /* stuff used in core only */
42 unsigned short flags;
43 short dorev;
44 short x;
45 short y;
46 short z;
47 short cnt;
48 unsigned short *ptr;
49 unsigned short *base;
50 unsigned short *tmpbuf;
51 unsigned long offset;
52 unsigned long rleend; /* for rle images */
53 unsigned long *rowstart; /* for rle images */
54 long *rowsize; /* for rle images */
55} IMAGE;
56
57#define IMAGIC 0732
58
59#define TYPEMASK 0xff00
60#define BPPMASK 0x00ff
61#define ITYPE_VERBATIM 0x0000
62#define ITYPE_RLE 0x0100
63#define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE)
64#define ISVERBATIM(type) (((type) & 0xff00) == ITYPE_VERBATIM)
65#define BPP(type) ((type) & BPPMASK)
66#define RLE(bpp) (ITYPE_RLE | (bpp))
67#define VERBATIM(bpp) (ITYPE_VERBATIM | (bpp))
68/*
69 * end of image.h stuff
70 *
71 */
72
73#define RINTLUM (79)
74#define GINTLUM (156)
75#define BINTLUM (21)
76
77#define ILUM(r,g,b) ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
78
79#define OFFSET_R 3 /* this is byte order dependent */
80#define OFFSET_G 2
81#define OFFSET_B 1
82#define OFFSET_A 0
83
84#define CHANOFFSET(z) (3-(z)) /* this is byte order dependent */
85
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +000086static void expandrow Py_PROTO((unsigned char *, unsigned char *, int));
87static void setalpha Py_PROTO((unsigned char *, int));
88static void copybw Py_PROTO((long *, int));
89static void interleaverow Py_PROTO((unsigned char*, unsigned char*, int, int));
90static int compressrow Py_PROTO((unsigned char *, unsigned char *, int, int));
91static void lumrow Py_PROTO((unsigned char *, unsigned char *, int));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000092
93#ifdef ADD_TAGS
94#define TAGLEN (5)
95#else
96#define TAGLEN (0)
97#endif
98
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +000099static PyObject *ImgfileError;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000100
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000101static int reverse_order;
102
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000103#ifdef ADD_TAGS
104/*
105 * addlongimgtag -
106 * this is used to extract image data from core dumps.
107 *
108 */
109addlongimgtag(dptr,xsize,ysize)
110unsigned long *dptr;
111int xsize, ysize;
112{
113 dptr = dptr+(xsize*ysize);
114 dptr[0] = 0x12345678;
115 dptr[1] = 0x59493333;
116 dptr[2] = 0x69434222;
117 dptr[3] = xsize;
118 dptr[4] = ysize;
119}
120#endif
121
122/*
123 * byte order independent read/write of shorts and longs.
124 *
125 */
126static unsigned short getshort(inf)
127FILE *inf;
128{
129 unsigned char buf[2];
130
131 fread(buf,2,1,inf);
132 return (buf[0]<<8)+(buf[1]<<0);
133}
134
135static unsigned long getlong(inf)
136FILE *inf;
137{
138 unsigned char buf[4];
139
140 fread(buf,4,1,inf);
141 return (buf[0]<<24)+(buf[1]<<16)+(buf[2]<<8)+(buf[3]<<0);
142}
143
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000144static void putshort(outf,val)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000145FILE *outf;
146unsigned short val;
147{
148 unsigned char buf[2];
149
150 buf[0] = (val>>8);
151 buf[1] = (val>>0);
152 fwrite(buf,2,1,outf);
153}
154
155static int putlong(outf,val)
156FILE *outf;
157unsigned long val;
158{
159 unsigned char buf[4];
160
161 buf[0] = (val>>24);
162 buf[1] = (val>>16);
163 buf[2] = (val>>8);
164 buf[3] = (val>>0);
165 return fwrite(buf,4,1,outf);
166}
167
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000168static void readheader(inf,image)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000169FILE *inf;
170IMAGE *image;
171{
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000172 memset(image,0,sizeof(IMAGE));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000173 image->imagic = getshort(inf);
174 image->type = getshort(inf);
175 image->dim = getshort(inf);
176 image->xsize = getshort(inf);
177 image->ysize = getshort(inf);
178 image->zsize = getshort(inf);
179}
180
181static int writeheader(outf,image)
182FILE *outf;
183IMAGE *image;
184{
185 IMAGE t;
186
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000187 memset(&t,0,sizeof(IMAGE));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000188 fwrite(&t,sizeof(IMAGE),1,outf);
189 fseek(outf,0,SEEK_SET);
190 putshort(outf,image->imagic);
191 putshort(outf,image->type);
192 putshort(outf,image->dim);
193 putshort(outf,image->xsize);
194 putshort(outf,image->ysize);
195 putshort(outf,image->zsize);
196 putlong(outf,image->min);
197 putlong(outf,image->max);
198 putlong(outf,0);
199 return fwrite("no name",8,1,outf);
200}
201
202static int writetab(outf,tab,len)
203FILE *outf;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000204/*unsigned*/ long *tab;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000205int len;
206{
Guido van Rossuma376cc51996-12-05 23:43:35 +0000207 int r = 0;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000208
209 while(len) {
210 r = putlong(outf,*tab++);
211 len -= 4;
212 }
213 return r;
214}
215
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000216static void readtab(inf,tab,len)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000217FILE *inf;
Guido van Rossumb6775db1994-08-01 11:34:53 +0000218/*unsigned*/ long *tab;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000219int len;
220{
221 while(len) {
222 *tab++ = getlong(inf);
223 len -= 4;
224 }
225}
226
227/*
228 * sizeofimage -
229 * return the xsize and ysize of an iris image file.
230 *
231 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000232static PyObject *
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000233sizeofimage(self, args)
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000234 PyObject *self, *args;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000235{
236 char *name;
237 IMAGE image;
238 FILE *inf;
239
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000240 if (!PyArg_Parse(args, "s", &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000241 return NULL;
242
243 inf = fopen(name,"r");
244 if(!inf) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000245 PyErr_SetString(ImgfileError, "can't open image file");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000246 return NULL;
247 }
248 readheader(inf,&image);
249 fclose(inf);
250 if(image.imagic != IMAGIC) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000251 PyErr_SetString(ImgfileError, "bad magic number in image file");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000252 return NULL;
253 }
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000254 return Py_BuildValue("(ii)", image.xsize, image.ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000255}
256
257/*
258 * longimagedata -
259 * read in a B/W RGB or RGBA iris image file and return a
260 * pointer to an array of longs.
261 *
262 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000263static PyObject *
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000264longimagedata(self, args)
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000265 PyObject *self, *args;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000266{
267 char *name;
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000268 unsigned char *base, *lptr;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000269 unsigned char *rledat, *verdat;
270 long *starttab, *lengthtab;
271 FILE *inf;
272 IMAGE image;
Guido van Rossum3bbc62e1995-01-02 19:30:30 +0000273 int y, z, tablen;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000274 int xsize, ysize, zsize;
275 int bpp, rle, cur, badorder;
276 int rlebuflen;
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000277 PyObject *rv;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000278
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000279 if (!PyArg_Parse(args, "s", &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000280 return NULL;
281
282 inf = fopen(name,"r");
283 if(!inf) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000284 PyErr_SetString(ImgfileError,"can't open image file");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000285 return NULL;
286 }
287 readheader(inf,&image);
288 if(image.imagic != IMAGIC) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000289 PyErr_SetString(ImgfileError,"bad magic number in image file");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000290 fclose(inf);
291 return NULL;
292 }
293 rle = ISRLE(image.type);
294 bpp = BPP(image.type);
295 if(bpp != 1 ) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000296 PyErr_SetString(ImgfileError,"image must have 1 byte per pix chan");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000297 fclose(inf);
298 return NULL;
299 }
300 xsize = image.xsize;
301 ysize = image.ysize;
302 zsize = image.zsize;
303 if(rle) {
304 tablen = ysize*zsize*sizeof(long);
305 starttab = (long *)malloc(tablen);
306 lengthtab = (long *)malloc(tablen);
307 rlebuflen = 1.05*xsize+10;
308 rledat = (unsigned char *)malloc(rlebuflen);
309 fseek(inf,512,SEEK_SET);
310 readtab(inf,starttab,tablen);
311 readtab(inf,lengthtab,tablen);
312
313/* check data order */
314 cur = 0;
315 badorder = 0;
316 for(y=0; y<ysize; y++) {
317 for(z=0; z<zsize; z++) {
318 if(starttab[y+z*ysize]<cur) {
319 badorder = 1;
320 break;
321 }
322 cur = starttab[y+z*ysize];
323 }
324 if(badorder)
325 break;
326 }
327
328 fseek(inf,512+2*tablen,SEEK_SET);
329 cur = 512+2*tablen;
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000330 rv = PyString_FromStringAndSize((char *) 0,
331 (xsize*ysize+TAGLEN)*sizeof(long));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000332 if (rv == NULL) {
333 fclose(inf);
334 free(lengthtab);
335 free(starttab);
336 free(rledat);
337 return NULL;
338 }
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000339 base = (unsigned char *) PyString_AsString(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000340#ifdef ADD_TAGS
341 addlongimgtag(base,xsize,ysize);
342#endif
343 if(badorder) {
344 for(z=0; z<zsize; z++) {
345 lptr = base;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000346 if (reverse_order)
347 lptr += (ysize - 1) * xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000348 for(y=0; y<ysize; y++) {
349 if(cur != starttab[y+z*ysize]) {
350 fseek(inf,starttab[y+z*ysize],SEEK_SET);
351 cur = starttab[y+z*ysize];
352 }
353 if(lengthtab[y+z*ysize]>rlebuflen) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000354 PyErr_SetString(ImgfileError,
355 "rlebuf is too small - bad poop");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000356 fclose(inf);
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000357 Py_DECREF(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000358 free(rledat);
359 free(starttab);
360 free(lengthtab);
361 return NULL;
362 }
363 fread(rledat,lengthtab[y+z*ysize],1,inf);
364 cur += lengthtab[y+z*ysize];
365 expandrow(lptr,rledat,3-z);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000366 if (reverse_order)
367 lptr -= xsize * sizeof(unsigned long);
368 else
369 lptr += xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000370 }
371 }
372 } else {
373 lptr = base;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000374 if (reverse_order)
375 lptr += (ysize - 1) * xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000376 for(y=0; y<ysize; y++) {
377 for(z=0; z<zsize; z++) {
378 if(cur != starttab[y+z*ysize]) {
379 fseek(inf,starttab[y+z*ysize],SEEK_SET);
380 cur = starttab[y+z*ysize];
381 }
382 fread(rledat,lengthtab[y+z*ysize],1,inf);
383 cur += lengthtab[y+z*ysize];
384 expandrow(lptr,rledat,3-z);
385 }
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000386 if (reverse_order)
387 lptr -= xsize * sizeof(unsigned long);
388 else
389 lptr += xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000390 }
391 }
392 if(zsize == 3)
393 setalpha(base,xsize*ysize);
394 else if(zsize<3)
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000395 copybw((long *) base,xsize*ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000396 fclose(inf);
397 free(starttab);
398 free(lengthtab);
399 free(rledat);
400 return rv;
401 } else {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000402 rv = PyString_FromStringAndSize((char *) 0,
403 (xsize*ysize+TAGLEN)*sizeof(long));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000404 if (rv == NULL) {
405 fclose(inf);
406 return NULL;
407 }
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000408 base = (unsigned char *) PyString_AsString(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000409#ifdef ADD_TAGS
410 addlongimgtag(base,xsize,ysize);
411#endif
412 verdat = (unsigned char *)malloc(xsize);
413 fseek(inf,512,SEEK_SET);
414 for(z=0; z<zsize; z++) {
415 lptr = base;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000416 if (reverse_order)
417 lptr += (ysize - 1) * xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000418 for(y=0; y<ysize; y++) {
419 fread(verdat,xsize,1,inf);
420 interleaverow(lptr,verdat,3-z,xsize);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000421 if (reverse_order)
422 lptr -= xsize * sizeof(unsigned long);
423 else
424 lptr += xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000425 }
426 }
427 if(zsize == 3)
428 setalpha(base,xsize*ysize);
429 else if(zsize<3)
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000430 copybw((long *) base,xsize*ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000431 fclose(inf);
432 free(verdat);
433 return rv;
434 }
435}
436
437/* static utility functions for longimagedata */
438
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000439static void interleaverow(lptr,cptr,z,n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000440unsigned char *lptr, *cptr;
441int z, n;
442{
443 lptr += z;
444 while(n--) {
445 *lptr = *cptr++;
446 lptr += 4;
447 }
448}
449
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000450static void copybw(lptr,n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000451long *lptr;
452int n;
453{
454 while(n>=8) {
455 lptr[0] = 0xff000000+(0x010101*(lptr[0]&0xff));
456 lptr[1] = 0xff000000+(0x010101*(lptr[1]&0xff));
457 lptr[2] = 0xff000000+(0x010101*(lptr[2]&0xff));
458 lptr[3] = 0xff000000+(0x010101*(lptr[3]&0xff));
459 lptr[4] = 0xff000000+(0x010101*(lptr[4]&0xff));
460 lptr[5] = 0xff000000+(0x010101*(lptr[5]&0xff));
461 lptr[6] = 0xff000000+(0x010101*(lptr[6]&0xff));
462 lptr[7] = 0xff000000+(0x010101*(lptr[7]&0xff));
463 lptr += 8;
464 n-=8;
465 }
466 while(n--) {
467 *lptr = 0xff000000+(0x010101*(*lptr&0xff));
468 lptr++;
469 }
470}
471
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000472static void setalpha(lptr,n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000473unsigned char *lptr;
474{
475 while(n>=8) {
476 lptr[0*4] = 0xff;
477 lptr[1*4] = 0xff;
478 lptr[2*4] = 0xff;
479 lptr[3*4] = 0xff;
480 lptr[4*4] = 0xff;
481 lptr[5*4] = 0xff;
482 lptr[6*4] = 0xff;
483 lptr[7*4] = 0xff;
484 lptr += 4*8;
485 n -= 8;
486 }
487 while(n--) {
488 *lptr = 0xff;
489 lptr += 4;
490 }
491}
492
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000493static void expandrow(optr,iptr,z)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000494unsigned char *optr, *iptr;
495int z;
496{
497 unsigned char pixel, count;
498
499 optr += z;
500 while(1) {
501 pixel = *iptr++;
502 if ( !(count = (pixel & 0x7f)) )
503 return;
504 if(pixel & 0x80) {
505 while(count>=8) {
506 optr[0*4] = iptr[0];
507 optr[1*4] = iptr[1];
508 optr[2*4] = iptr[2];
509 optr[3*4] = iptr[3];
510 optr[4*4] = iptr[4];
511 optr[5*4] = iptr[5];
512 optr[6*4] = iptr[6];
513 optr[7*4] = iptr[7];
514 optr += 8*4;
515 iptr += 8;
516 count -= 8;
517 }
518 while(count--) {
519 *optr = *iptr++;
520 optr+=4;
521 }
522 } else {
523 pixel = *iptr++;
524 while(count>=8) {
525 optr[0*4] = pixel;
526 optr[1*4] = pixel;
527 optr[2*4] = pixel;
528 optr[3*4] = pixel;
529 optr[4*4] = pixel;
530 optr[5*4] = pixel;
531 optr[6*4] = pixel;
532 optr[7*4] = pixel;
533 optr += 8*4;
534 count -= 8;
535 }
536 while(count--) {
537 *optr = pixel;
538 optr+=4;
539 }
540 }
541 }
542}
543
544/*
545 * longstoimage -
546 * copy an array of longs to an iris image file. Each long
547 * represents one pixel. xsize and ysize specify the dimensions of
548 * the pixel array. zsize specifies what kind of image file to
549 * write out. if zsize is 1, the luminance of the pixels are
550 * calculated, and a sinlge channel black and white image is saved.
551 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
552 * RGBA image file is saved.
553 *
554 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000555static PyObject *
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000556longstoimage(self, args)
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000557 PyObject *self, *args;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000558{
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000559 unsigned char *lptr;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000560 char *name;
561 int xsize, ysize, zsize;
562 FILE *outf;
563 IMAGE image;
564 int tablen, y, z, pos, len;
565 long *starttab, *lengthtab;
566 unsigned char *rlebuf;
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000567 unsigned char *lumbuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000568 int rlebuflen, goodwrite;
569
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000570 if (!PyArg_Parse(args, "(s#iiis)", &lptr, &len, &xsize, &ysize, &zsize,
571 &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000572 return NULL;
573
574 goodwrite = 1;
575 outf = fopen(name,"w");
576 if(!outf) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000577 PyErr_SetString(ImgfileError,"can't open output file");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000578 return NULL;
579 }
580 tablen = ysize*zsize*sizeof(long);
581
582 starttab = (long *)malloc(tablen);
583 lengthtab = (long *)malloc(tablen);
584 rlebuflen = 1.05*xsize+10;
585 rlebuf = (unsigned char *)malloc(rlebuflen);
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000586 lumbuf = (unsigned char *)malloc(xsize*sizeof(long));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000587
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +0000588 memset(&image,0,sizeof(IMAGE));
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000589 image.imagic = IMAGIC;
590 image.type = RLE(1);
591 if(zsize>1)
592 image.dim = 3;
593 else
594 image.dim = 2;
595 image.xsize = xsize;
596 image.ysize = ysize;
597 image.zsize = zsize;
598 image.min = 0;
599 image.max = 255;
600 goodwrite *= writeheader(outf,&image);
601 fseek(outf,512+2*tablen,SEEK_SET);
602 pos = 512+2*tablen;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000603 if (reverse_order)
604 lptr += (ysize - 1) * xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000605 for(y=0; y<ysize; y++) {
606 for(z=0; z<zsize; z++) {
607 if(zsize == 1) {
608 lumrow(lptr,lumbuf,xsize);
609 len = compressrow(lumbuf,rlebuf,CHANOFFSET(z),xsize);
610 } else {
611 len = compressrow(lptr,rlebuf,CHANOFFSET(z),xsize);
612 }
613 if(len>rlebuflen) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000614 PyErr_SetString(ImgfileError,"rlebuf is too small - bad poop");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000615 free(starttab);
616 free(lengthtab);
617 free(rlebuf);
618 free(lumbuf);
619 fclose(outf);
620 return NULL;
621 }
622 goodwrite *= fwrite(rlebuf,len,1,outf);
623 starttab[y+z*ysize] = pos;
624 lengthtab[y+z*ysize] = len;
625 pos += len;
626 }
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000627 if (reverse_order)
628 lptr -= xsize * sizeof(unsigned long);
629 else
630 lptr += xsize * sizeof(unsigned long);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000631 }
632
633 fseek(outf,512,SEEK_SET);
634 goodwrite *= writetab(outf,starttab,tablen);
635 goodwrite *= writetab(outf,lengthtab,tablen);
636 free(starttab);
637 free(lengthtab);
638 free(rlebuf);
639 free(lumbuf);
640 fclose(outf);
641 if(goodwrite) {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000642 Py_INCREF(Py_None);
643 return Py_None;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000644 } else {
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000645 PyErr_SetString(ImgfileError,"not enough space for image!!");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000646 return NULL;
647 }
648}
649
650/* static utility functions for longstoimage */
651
Guido van Rossum2977e5d1996-08-19 22:02:19 +0000652static void lumrow(rgbptr,lumptr,n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000653unsigned char *rgbptr, *lumptr;
654int n;
655{
656 lumptr += CHANOFFSET(0);
657 while(n--) {
658 *lumptr = ILUM(rgbptr[OFFSET_R],rgbptr[OFFSET_G],rgbptr[OFFSET_B]);
659 lumptr += 4;
660 rgbptr += 4;
661 }
662}
663
664static int compressrow(lbuf,rlebuf,z,cnt)
665unsigned char *lbuf, *rlebuf;
666int z, cnt;
667{
668 unsigned char *iptr, *ibufend, *sptr, *optr;
669 short todo, cc;
670 long count;
671
672 lbuf += z;
673 iptr = lbuf;
674 ibufend = iptr+cnt*4;
675 optr = rlebuf;
676
677 while(iptr<ibufend) {
678 sptr = iptr;
679 iptr += 8;
680 while((iptr<ibufend)&& ((iptr[-8]!=iptr[-4])||(iptr[-4]!=iptr[0])))
681 iptr+=4;
682 iptr -= 8;
683 count = (iptr-sptr)/4;
684 while(count) {
685 todo = count>126 ? 126:count;
686 count -= todo;
687 *optr++ = 0x80|todo;
688 while(todo>8) {
689 optr[0] = sptr[0*4];
690 optr[1] = sptr[1*4];
691 optr[2] = sptr[2*4];
692 optr[3] = sptr[3*4];
693 optr[4] = sptr[4*4];
694 optr[5] = sptr[5*4];
695 optr[6] = sptr[6*4];
696 optr[7] = sptr[7*4];
697 optr += 8;
698 sptr += 8*4;
699 todo -= 8;
700 }
701 while(todo--) {
702 *optr++ = *sptr;
703 sptr += 4;
704 }
705 }
706 sptr = iptr;
707 cc = *iptr;
708 iptr += 4;
709 while( (iptr<ibufend) && (*iptr == cc) )
710 iptr += 4;
711 count = (iptr-sptr)/4;
712 while(count) {
713 todo = count>126 ? 126:count;
714 count -= todo;
715 *optr++ = todo;
716 *optr++ = cc;
717 }
718 }
719 *optr++ = 0;
720 return optr - (unsigned char *)rlebuf;
721}
722
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000723static PyObject *
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000724ttob(self, args)
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000725 PyObject *self;
726 PyObject *args;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000727{
728 int order, oldorder;
729
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000730 if (!PyArg_Parse(args, "i", &order))
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000731 return NULL;
732 oldorder = reverse_order;
733 reverse_order = order;
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000734 return PyInt_FromLong(oldorder);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000735}
736
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000737static PyMethodDef rgbimg_methods[] = {
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000738 {"sizeofimage", sizeofimage},
739 {"longimagedata", longimagedata},
740 {"longstoimage", longstoimage},
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000741 {"ttob", ttob},
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000742 {NULL, NULL} /* sentinel */
743};
744
745void
746initrgbimg()
747{
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000748 PyObject *m, *d;
749 m = Py_InitModule("rgbimg", rgbimg_methods);
750 d = PyModule_GetDict(m);
751 ImgfileError = PyString_FromString("rgbimg.error");
752 if (ImgfileError == NULL || PyDict_SetItemString(d, "error", ImgfileError))
753 Py_FatalError("can't define rgbimg.error");
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000754}