Exceptions raised during renaming in rotating file handlers are now passed to handleE...
[python.git] / Modules / rgbimgmodule.c
blob904c64b642ec2fdec4e0ad12bc36ab58e8f82f37
1 /*
2 * fastimg -
3 * Faster reading and writing of image files.
5 * This code should work on machines with any byte order.
7 * Could someone make this run real fast using multiple processors
8 * or how about using memory mapped files to speed it up?
10 * Paul Haeberli - 1991
12 * Changed to return sizes.
13 * Sjoerd Mullender - 1993
14 * Changed to incorporate into Python.
15 * Sjoerd Mullender - 1993
17 #include "Python.h"
19 #if SIZEOF_INT == 4
20 typedef int Py_Int32;
21 typedef unsigned int Py_UInt32;
22 #else
23 #if SIZEOF_LONG == 4
24 typedef long Py_Int32;
25 typedef unsigned long Py_UInt32;
26 #else
27 #error "No 4-byte integral type"
28 #endif
29 #endif
31 #include <string.h>
34 * from image.h
37 typedef struct {
38 unsigned short imagic; /* stuff saved on disk . . */
39 unsigned short type;
40 unsigned short dim;
41 unsigned short xsize;
42 unsigned short ysize;
43 unsigned short zsize;
44 Py_UInt32 min;
45 Py_UInt32 max;
46 Py_UInt32 wastebytes;
47 char name[80];
48 Py_UInt32 colormap;
50 Py_Int32 file; /* stuff used in core only */
51 unsigned short flags;
52 short dorev;
53 short x;
54 short y;
55 short z;
56 short cnt;
57 unsigned short *ptr;
58 unsigned short *base;
59 unsigned short *tmpbuf;
60 Py_UInt32 offset;
61 Py_UInt32 rleend; /* for rle images */
62 Py_UInt32 *rowstart; /* for rle images */
63 Py_Int32 *rowsize; /* for rle images */
64 } IMAGE;
66 #define IMAGIC 0732
68 #define TYPEMASK 0xff00
69 #define BPPMASK 0x00ff
70 #define ITYPE_VERBATIM 0x0000
71 #define ITYPE_RLE 0x0100
72 #define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE)
73 #define ISVERBATIM(type) (((type) & 0xff00) == ITYPE_VERBATIM)
74 #define BPP(type) ((type) & BPPMASK)
75 #define RLE(bpp) (ITYPE_RLE | (bpp))
76 #define VERBATIM(bpp) (ITYPE_VERBATIM | (bpp))
78 * end of image.h stuff
82 #define RINTLUM (79)
83 #define GINTLUM (156)
84 #define BINTLUM (21)
86 #define ILUM(r,g,b) ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
88 #define OFFSET_R 3 /* this is byte order dependent */
89 #define OFFSET_G 2
90 #define OFFSET_B 1
91 #define OFFSET_A 0
93 #define CHANOFFSET(z) (3-(z)) /* this is byte order dependent */
95 static void expandrow(unsigned char *, unsigned char *, int);
96 static void setalpha(unsigned char *, int);
97 static void copybw(Py_Int32 *, int);
98 static void interleaverow(unsigned char*, unsigned char*, int, int);
99 static int compressrow(unsigned char *, unsigned char *, int, int);
100 static void lumrow(unsigned char *, unsigned char *, int);
102 #ifdef ADD_TAGS
103 #define TAGLEN (5)
104 #else
105 #define TAGLEN (0)
106 #endif
108 static PyObject *ImgfileError;
110 static int reverse_order;
112 #ifdef ADD_TAGS
114 * addlongimgtag -
115 * this is used to extract image data from core dumps.
118 static void
119 addlongimgtag(Py_UInt32 *dptr, int xsize, int ysize)
121 dptr = dptr + (xsize * ysize);
122 dptr[0] = 0x12345678;
123 dptr[1] = 0x59493333;
124 dptr[2] = 0x69434222;
125 dptr[3] = xsize;
126 dptr[4] = ysize;
128 #endif
131 * byte order independent read/write of shorts and longs.
134 static unsigned short
135 getshort(FILE *inf)
137 unsigned char buf[2];
139 fread(buf, 2, 1, inf);
140 return (buf[0] << 8) + (buf[1] << 0);
143 static Py_UInt32
144 getlong(FILE *inf)
146 unsigned char buf[4];
148 fread(buf, 4, 1, inf);
149 return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0);
152 static void
153 putshort(FILE *outf, unsigned short val)
155 unsigned char buf[2];
157 buf[0] = (val >> 8);
158 buf[1] = (val >> 0);
159 fwrite(buf, 2, 1, outf);
162 static int
163 putlong(FILE *outf, Py_UInt32 val)
165 unsigned char buf[4];
167 buf[0] = (unsigned char) (val >> 24);
168 buf[1] = (unsigned char) (val >> 16);
169 buf[2] = (unsigned char) (val >> 8);
170 buf[3] = (unsigned char) (val >> 0);
171 return fwrite(buf, 4, 1, outf);
174 static void
175 readheader(FILE *inf, IMAGE *image)
177 memset(image ,0, sizeof(IMAGE));
178 image->imagic = getshort(inf);
179 image->type = getshort(inf);
180 image->dim = getshort(inf);
181 image->xsize = getshort(inf);
182 image->ysize = getshort(inf);
183 image->zsize = getshort(inf);
186 static int
187 writeheader(FILE *outf, IMAGE *image)
189 IMAGE t;
191 memset(&t, 0, sizeof(IMAGE));
192 fwrite(&t, sizeof(IMAGE), 1, outf);
193 fseek(outf, 0, SEEK_SET);
194 putshort(outf, image->imagic);
195 putshort(outf, image->type);
196 putshort(outf, image->dim);
197 putshort(outf, image->xsize);
198 putshort(outf, image->ysize);
199 putshort(outf, image->zsize);
200 putlong(outf, image->min);
201 putlong(outf, image->max);
202 putlong(outf, 0);
203 return fwrite("no name", 8, 1, outf);
206 static int
207 writetab(FILE *outf, /*unsigned*/ Py_Int32 *tab, int len)
209 int r = 0;
211 while(len) {
212 r = putlong(outf, *tab++);
213 len--;
215 return r;
218 static void
219 readtab(FILE *inf, /*unsigned*/ Py_Int32 *tab, int len)
221 while(len) {
222 *tab++ = getlong(inf);
223 len--;
228 * sizeofimage -
229 * return the xsize and ysize of an iris image file.
232 static PyObject *
233 sizeofimage(PyObject *self, PyObject *args)
235 char *name;
236 IMAGE image;
237 FILE *inf;
239 if (!PyArg_ParseTuple(args, "s:sizeofimage", &name))
240 return NULL;
242 inf = fopen(name, "rb");
243 if (!inf) {
244 PyErr_SetString(ImgfileError, "can't open image file");
245 return NULL;
247 readheader(inf, &image);
248 fclose(inf);
249 if (image.imagic != IMAGIC) {
250 PyErr_SetString(ImgfileError,
251 "bad magic number in image file");
252 return NULL;
254 return Py_BuildValue("(ii)", image.xsize, image.ysize);
258 * longimagedata -
259 * read in a B/W RGB or RGBA iris image file and return a
260 * pointer to an array of longs.
263 static PyObject *
264 longimagedata(PyObject *self, PyObject *args)
266 char *name;
267 unsigned char *base, *lptr;
268 unsigned char *rledat = NULL, *verdat = NULL;
269 Py_Int32 *starttab = NULL, *lengthtab = NULL;
270 FILE *inf = NULL;
271 IMAGE image;
272 int y, z, tablen;
273 int xsize, ysize, zsize;
274 int bpp, rle, cur, badorder;
275 int rlebuflen;
276 PyObject *rv = NULL;
278 if (!PyArg_ParseTuple(args, "s:longimagedata", &name))
279 return NULL;
281 inf = fopen(name,"rb");
282 if (!inf) {
283 PyErr_SetString(ImgfileError, "can't open image file");
284 return NULL;
286 readheader(inf,&image);
287 if (image.imagic != IMAGIC) {
288 PyErr_SetString(ImgfileError,
289 "bad magic number in image file");
290 goto finally;
292 rle = ISRLE(image.type);
293 bpp = BPP(image.type);
294 if (bpp != 1) {
295 PyErr_SetString(ImgfileError,
296 "image must have 1 byte per pix chan");
297 goto finally;
299 xsize = image.xsize;
300 ysize = image.ysize;
301 zsize = image.zsize;
302 if (rle) {
303 tablen = ysize * zsize * sizeof(Py_Int32);
304 starttab = (Py_Int32 *)malloc(tablen);
305 lengthtab = (Py_Int32 *)malloc(tablen);
306 rlebuflen = (int) (1.05 * xsize +10);
307 rledat = (unsigned char *)malloc(rlebuflen);
308 if (!starttab || !lengthtab || !rledat) {
309 PyErr_NoMemory();
310 goto finally;
313 fseek(inf, 512, SEEK_SET);
314 readtab(inf, starttab, ysize*zsize);
315 readtab(inf, lengthtab, ysize*zsize);
317 /* check data order */
318 cur = 0;
319 badorder = 0;
320 for(y = 0; y < ysize; y++) {
321 for(z = 0; z < zsize; z++) {
322 if (starttab[y + z * ysize] < cur) {
323 badorder = 1;
324 break;
326 cur = starttab[y +z * ysize];
328 if (badorder)
329 break;
332 fseek(inf, 512 + 2 * tablen, SEEK_SET);
333 cur = 512 + 2 * tablen;
334 rv = PyString_FromStringAndSize((char *)NULL,
335 (xsize * ysize + TAGLEN) * sizeof(Py_Int32));
336 if (rv == NULL)
337 goto finally;
339 base = (unsigned char *) PyString_AsString(rv);
340 #ifdef ADD_TAGS
341 addlongimgtag(base,xsize,ysize);
342 #endif
343 if (badorder) {
344 for (z = 0; z < zsize; z++) {
345 lptr = base;
346 if (reverse_order)
347 lptr += (ysize - 1) * xsize
348 * sizeof(Py_UInt32);
349 for (y = 0; y < ysize; y++) {
350 int idx = y + z * ysize;
351 if (cur != starttab[idx]) {
352 fseek(inf,starttab[idx],
353 SEEK_SET);
354 cur = starttab[idx];
356 if (lengthtab[idx] > rlebuflen) {
357 PyErr_SetString(ImgfileError,
358 "rlebuf is too small");
359 Py_DECREF(rv);
360 rv = NULL;
361 goto finally;
363 fread(rledat, lengthtab[idx], 1, inf);
364 cur += lengthtab[idx];
365 expandrow(lptr, rledat, 3-z);
366 if (reverse_order)
367 lptr -= xsize
368 * sizeof(Py_UInt32);
369 else
370 lptr += xsize
371 * sizeof(Py_UInt32);
374 } else {
375 lptr = base;
376 if (reverse_order)
377 lptr += (ysize - 1) * xsize
378 * sizeof(Py_UInt32);
379 for (y = 0; y < ysize; y++) {
380 for(z = 0; z < zsize; z++) {
381 int idx = y + z * ysize;
382 if (cur != starttab[idx]) {
383 fseek(inf, starttab[idx],
384 SEEK_SET);
385 cur = starttab[idx];
387 fread(rledat, lengthtab[idx], 1, inf);
388 cur += lengthtab[idx];
389 expandrow(lptr, rledat, 3-z);
391 if (reverse_order)
392 lptr -= xsize * sizeof(Py_UInt32);
393 else
394 lptr += xsize * sizeof(Py_UInt32);
397 if (zsize == 3)
398 setalpha(base, xsize * ysize);
399 else if (zsize < 3)
400 copybw((Py_Int32 *) base, xsize * ysize);
402 else {
403 rv = PyString_FromStringAndSize((char *) 0,
404 (xsize*ysize+TAGLEN)*sizeof(Py_Int32));
405 if (rv == NULL)
406 goto finally;
408 base = (unsigned char *) PyString_AsString(rv);
409 #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;
416 if (reverse_order)
417 lptr += (ysize - 1) * xsize
418 * sizeof(Py_UInt32);
419 for (y = 0; y < ysize; y++) {
420 fread(verdat, xsize, 1, inf);
421 interleaverow(lptr, verdat, 3-z, xsize);
422 if (reverse_order)
423 lptr -= xsize * sizeof(Py_UInt32);
424 else
425 lptr += xsize * sizeof(Py_UInt32);
428 if (zsize == 3)
429 setalpha(base, xsize * ysize);
430 else if (zsize < 3)
431 copybw((Py_Int32 *) base, xsize * ysize);
433 finally:
434 free(starttab);
435 free(lengthtab);
436 free(rledat);
437 free(verdat);
438 fclose(inf);
439 return rv;
442 /* static utility functions for longimagedata */
444 static void
445 interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
447 lptr += z;
448 while (n--) {
449 *lptr = *cptr++;
450 lptr += 4;
454 static void
455 copybw(Py_Int32 *lptr, int n)
457 while (n >= 8) {
458 lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff));
459 lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff));
460 lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff));
461 lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff));
462 lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff));
463 lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff));
464 lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff));
465 lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff));
466 lptr += 8;
467 n -= 8;
469 while (n--) {
470 *lptr = 0xff000000 + (0x010101 * (*lptr&0xff));
471 lptr++;
475 static void
476 setalpha(unsigned char *lptr, int n)
478 while (n >= 8) {
479 lptr[0 * 4] = 0xff;
480 lptr[1 * 4] = 0xff;
481 lptr[2 * 4] = 0xff;
482 lptr[3 * 4] = 0xff;
483 lptr[4 * 4] = 0xff;
484 lptr[5 * 4] = 0xff;
485 lptr[6 * 4] = 0xff;
486 lptr[7 * 4] = 0xff;
487 lptr += 4 * 8;
488 n -= 8;
490 while (n--) {
491 *lptr = 0xff;
492 lptr += 4;
496 static void
497 expandrow(unsigned char *optr, unsigned char *iptr, int z)
499 unsigned char pixel, count;
501 optr += z;
502 while (1) {
503 pixel = *iptr++;
504 if (!(count = (pixel & 0x7f)))
505 return;
506 if (pixel & 0x80) {
507 while (count >= 8) {
508 optr[0 * 4] = iptr[0];
509 optr[1 * 4] = iptr[1];
510 optr[2 * 4] = iptr[2];
511 optr[3 * 4] = iptr[3];
512 optr[4 * 4] = iptr[4];
513 optr[5 * 4] = iptr[5];
514 optr[6 * 4] = iptr[6];
515 optr[7 * 4] = iptr[7];
516 optr += 8 * 4;
517 iptr += 8;
518 count -= 8;
520 while (count--) {
521 *optr = *iptr++;
522 optr += 4;
525 else {
526 pixel = *iptr++;
527 while (count >= 8) {
528 optr[0 * 4] = pixel;
529 optr[1 * 4] = pixel;
530 optr[2 * 4] = pixel;
531 optr[3 * 4] = pixel;
532 optr[4 * 4] = pixel;
533 optr[5 * 4] = pixel;
534 optr[6 * 4] = pixel;
535 optr[7 * 4] = pixel;
536 optr += 8 * 4;
537 count -= 8;
539 while (count--) {
540 *optr = pixel;
541 optr += 4;
548 * longstoimage -
549 * copy an array of longs to an iris image file. Each long
550 * represents one pixel. xsize and ysize specify the dimensions of
551 * the pixel array. zsize specifies what kind of image file to
552 * write out. if zsize is 1, the luminance of the pixels are
553 * calculated, and a single channel black and white image is saved.
554 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
555 * RGBA image file is saved.
558 static PyObject *
559 longstoimage(PyObject *self, PyObject *args)
561 unsigned char *lptr;
562 char *name;
563 int xsize, ysize, zsize;
564 FILE *outf = NULL;
565 IMAGE image;
566 int tablen, y, z, pos, len;
567 Py_Int32 *starttab = NULL, *lengthtab = NULL;
568 unsigned char *rlebuf = NULL;
569 unsigned char *lumbuf = NULL;
570 int rlebuflen, goodwrite;
571 PyObject *retval = NULL;
573 if (!PyArg_ParseTuple(args, "s#iiis:longstoimage", &lptr, &len,
574 &xsize, &ysize, &zsize, &name))
575 return NULL;
577 goodwrite = 1;
578 outf = fopen(name, "wb");
579 if (!outf) {
580 PyErr_SetString(ImgfileError, "can't open output file");
581 return NULL;
583 tablen = ysize * zsize * sizeof(Py_Int32);
585 starttab = (Py_Int32 *)malloc(tablen);
586 lengthtab = (Py_Int32 *)malloc(tablen);
587 rlebuflen = (int) (1.05 * xsize + 10);
588 rlebuf = (unsigned char *)malloc(rlebuflen);
589 lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
590 if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
591 PyErr_NoMemory();
592 goto finally;
595 memset(&image, 0, sizeof(IMAGE));
596 image.imagic = IMAGIC;
597 image.type = RLE(1);
598 if (zsize>1)
599 image.dim = 3;
600 else
601 image.dim = 2;
602 image.xsize = xsize;
603 image.ysize = ysize;
604 image.zsize = zsize;
605 image.min = 0;
606 image.max = 255;
607 goodwrite *= writeheader(outf, &image);
608 pos = 512 + 2 * tablen;
609 fseek(outf, pos, SEEK_SET);
610 if (reverse_order)
611 lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
612 for (y = 0; y < ysize; y++) {
613 for (z = 0; z < zsize; z++) {
614 if (zsize == 1) {
615 lumrow(lptr, lumbuf, xsize);
616 len = compressrow(lumbuf, rlebuf,
617 CHANOFFSET(z), xsize);
618 } else {
619 len = compressrow(lptr, rlebuf,
620 CHANOFFSET(z), xsize);
622 if(len > rlebuflen) {
623 PyErr_SetString(ImgfileError,
624 "rlebuf is too small");
625 goto finally;
627 goodwrite *= fwrite(rlebuf, len, 1, outf);
628 starttab[y + z * ysize] = pos;
629 lengthtab[y + z * ysize] = len;
630 pos += len;
632 if (reverse_order)
633 lptr -= xsize * sizeof(Py_UInt32);
634 else
635 lptr += xsize * sizeof(Py_UInt32);
638 fseek(outf, 512, SEEK_SET);
639 goodwrite *= writetab(outf, starttab, ysize*zsize);
640 goodwrite *= writetab(outf, lengthtab, ysize*zsize);
641 if (goodwrite) {
642 Py_INCREF(Py_None);
643 retval = Py_None;
644 } else
645 PyErr_SetString(ImgfileError, "not enough space for image");
647 finally:
648 fclose(outf);
649 free(starttab);
650 free(lengthtab);
651 free(rlebuf);
652 free(lumbuf);
653 return retval;
656 /* static utility functions for longstoimage */
658 static void
659 lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
661 lumptr += CHANOFFSET(0);
662 while (n--) {
663 *lumptr = ILUM(rgbptr[OFFSET_R],
664 rgbptr[OFFSET_G],
665 rgbptr[OFFSET_B]);
666 lumptr += 4;
667 rgbptr += 4;
671 static int
672 compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
674 unsigned char *iptr, *ibufend, *sptr, *optr;
675 short todo, cc;
676 Py_Int32 count;
678 lbuf += z;
679 iptr = lbuf;
680 ibufend = iptr + cnt * 4;
681 optr = rlebuf;
683 while(iptr < ibufend) {
684 sptr = iptr;
685 iptr += 8;
686 while ((iptr<ibufend) &&
687 ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
689 iptr += 4;
691 iptr -= 8;
692 count = (iptr - sptr) / 4;
693 while (count) {
694 todo = count > 126 ? 126 : (short)count;
695 count -= todo;
696 *optr++ = 0x80 | todo;
697 while (todo > 8) {
698 optr[0] = sptr[0 * 4];
699 optr[1] = sptr[1 * 4];
700 optr[2] = sptr[2 * 4];
701 optr[3] = sptr[3 * 4];
702 optr[4] = sptr[4 * 4];
703 optr[5] = sptr[5 * 4];
704 optr[6] = sptr[6 * 4];
705 optr[7] = sptr[7 * 4];
706 optr += 8;
707 sptr += 8 * 4;
708 todo -= 8;
710 while (todo--) {
711 *optr++ = *sptr;
712 sptr += 4;
715 sptr = iptr;
716 cc = *iptr;
717 iptr += 4;
718 while ((iptr < ibufend) && (*iptr == cc))
719 iptr += 4;
720 count = (iptr - sptr) / 4;
721 while (count) {
722 todo = count > 126 ? 126 : (short)count;
723 count -= todo;
724 *optr++ = (unsigned char) todo;
725 *optr++ = (unsigned char) cc;
728 *optr++ = 0;
729 return optr - (unsigned char *)rlebuf;
732 static PyObject *
733 ttob(PyObject *self, PyObject *args)
735 int order, oldorder;
737 if (!PyArg_ParseTuple(args, "i:ttob", &order))
738 return NULL;
739 oldorder = reverse_order;
740 reverse_order = order;
741 return PyInt_FromLong(oldorder);
744 static PyMethodDef
745 rgbimg_methods[] = {
746 {"sizeofimage", sizeofimage, METH_VARARGS},
747 {"longimagedata", longimagedata, METH_VARARGS},
748 {"longstoimage", longstoimage, METH_VARARGS},
749 {"ttob", ttob, METH_VARARGS},
750 {NULL, NULL} /* sentinel */
754 PyMODINIT_FUNC
755 initrgbimg(void)
757 PyObject *m, *d;
758 m = Py_InitModule("rgbimg", rgbimg_methods);
759 d = PyModule_GetDict(m);
760 ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
761 if (ImgfileError != NULL)
762 PyDict_SetItemString(d, "error", ImgfileError);