Its time to stop listing (Unix, Windows) when we really mean "everything but Mac...
[python.git] / Modules / rgbimgmodule.c
blob0f9ee71f241f7a16bb77405603f81a9a53f54751
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 (int)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 (int)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 if (!verdat) {
414 Py_CLEAR(rv);
415 goto finally;
418 fseek(inf, 512, SEEK_SET);
419 for (z = 0; z < zsize; z++) {
420 lptr = base;
421 if (reverse_order)
422 lptr += (ysize - 1) * xsize
423 * sizeof(Py_UInt32);
424 for (y = 0; y < ysize; y++) {
425 fread(verdat, xsize, 1, inf);
426 interleaverow(lptr, verdat, 3-z, xsize);
427 if (reverse_order)
428 lptr -= xsize * sizeof(Py_UInt32);
429 else
430 lptr += xsize * sizeof(Py_UInt32);
433 if (zsize == 3)
434 setalpha(base, xsize * ysize);
435 else if (zsize < 3)
436 copybw((Py_Int32 *) base, xsize * ysize);
438 finally:
439 if (starttab)
440 free(starttab);
441 if (lengthtab)
442 free(lengthtab);
443 if (rledat)
444 free(rledat);
445 if (verdat)
446 free(verdat);
447 fclose(inf);
448 return rv;
451 /* static utility functions for longimagedata */
453 static void
454 interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
456 lptr += z;
457 while (n--) {
458 *lptr = *cptr++;
459 lptr += 4;
463 static void
464 copybw(Py_Int32 *lptr, int n)
466 while (n >= 8) {
467 lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff));
468 lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff));
469 lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff));
470 lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff));
471 lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff));
472 lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff));
473 lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff));
474 lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff));
475 lptr += 8;
476 n -= 8;
478 while (n--) {
479 *lptr = 0xff000000 + (0x010101 * (*lptr&0xff));
480 lptr++;
484 static void
485 setalpha(unsigned char *lptr, int n)
487 while (n >= 8) {
488 lptr[0 * 4] = 0xff;
489 lptr[1 * 4] = 0xff;
490 lptr[2 * 4] = 0xff;
491 lptr[3 * 4] = 0xff;
492 lptr[4 * 4] = 0xff;
493 lptr[5 * 4] = 0xff;
494 lptr[6 * 4] = 0xff;
495 lptr[7 * 4] = 0xff;
496 lptr += 4 * 8;
497 n -= 8;
499 while (n--) {
500 *lptr = 0xff;
501 lptr += 4;
505 static void
506 expandrow(unsigned char *optr, unsigned char *iptr, int z)
508 unsigned char pixel, count;
510 optr += z;
511 while (1) {
512 pixel = *iptr++;
513 if (!(count = (pixel & 0x7f)))
514 return;
515 if (pixel & 0x80) {
516 while (count >= 8) {
517 optr[0 * 4] = iptr[0];
518 optr[1 * 4] = iptr[1];
519 optr[2 * 4] = iptr[2];
520 optr[3 * 4] = iptr[3];
521 optr[4 * 4] = iptr[4];
522 optr[5 * 4] = iptr[5];
523 optr[6 * 4] = iptr[6];
524 optr[7 * 4] = iptr[7];
525 optr += 8 * 4;
526 iptr += 8;
527 count -= 8;
529 while (count--) {
530 *optr = *iptr++;
531 optr += 4;
534 else {
535 pixel = *iptr++;
536 while (count >= 8) {
537 optr[0 * 4] = pixel;
538 optr[1 * 4] = pixel;
539 optr[2 * 4] = pixel;
540 optr[3 * 4] = pixel;
541 optr[4 * 4] = pixel;
542 optr[5 * 4] = pixel;
543 optr[6 * 4] = pixel;
544 optr[7 * 4] = pixel;
545 optr += 8 * 4;
546 count -= 8;
548 while (count--) {
549 *optr = pixel;
550 optr += 4;
557 * longstoimage -
558 * copy an array of longs to an iris image file. Each long
559 * represents one pixel. xsize and ysize specify the dimensions of
560 * the pixel array. zsize specifies what kind of image file to
561 * write out. if zsize is 1, the luminance of the pixels are
562 * calculated, and a single channel black and white image is saved.
563 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
564 * RGBA image file is saved.
567 static PyObject *
568 longstoimage(PyObject *self, PyObject *args)
570 unsigned char *lptr;
571 char *name;
572 int xsize, ysize, zsize;
573 FILE *outf = NULL;
574 IMAGE image;
575 int tablen, y, z, pos, len;
576 Py_Int32 *starttab = NULL, *lengthtab = NULL;
577 unsigned char *rlebuf = NULL;
578 unsigned char *lumbuf = NULL;
579 int rlebuflen;
580 Py_ssize_t goodwrite;
581 PyObject *retval = NULL;
583 if (!PyArg_ParseTuple(args, "s#iiis:longstoimage", &lptr, &len,
584 &xsize, &ysize, &zsize, &name))
585 return NULL;
587 goodwrite = 1;
588 outf = fopen(name, "wb");
589 if (!outf) {
590 PyErr_SetString(ImgfileError, "can't open output file");
591 return NULL;
593 tablen = ysize * zsize * sizeof(Py_Int32);
595 starttab = (Py_Int32 *)malloc(tablen);
596 lengthtab = (Py_Int32 *)malloc(tablen);
597 rlebuflen = (int) (1.05 * xsize + 10);
598 rlebuf = (unsigned char *)malloc(rlebuflen);
599 lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
600 if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
601 PyErr_NoMemory();
602 goto finally;
605 memset(&image, 0, sizeof(IMAGE));
606 image.imagic = IMAGIC;
607 image.type = RLE(1);
608 if (zsize>1)
609 image.dim = 3;
610 else
611 image.dim = 2;
612 image.xsize = xsize;
613 image.ysize = ysize;
614 image.zsize = zsize;
615 image.min = 0;
616 image.max = 255;
617 goodwrite *= writeheader(outf, &image);
618 pos = 512 + 2 * tablen;
619 fseek(outf, pos, SEEK_SET);
620 if (reverse_order)
621 lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
622 for (y = 0; y < ysize; y++) {
623 for (z = 0; z < zsize; z++) {
624 if (zsize == 1) {
625 lumrow(lptr, lumbuf, xsize);
626 len = compressrow(lumbuf, rlebuf,
627 CHANOFFSET(z), xsize);
628 } else {
629 len = compressrow(lptr, rlebuf,
630 CHANOFFSET(z), xsize);
632 if(len > rlebuflen) {
633 PyErr_SetString(ImgfileError,
634 "rlebuf is too small");
635 goto finally;
637 goodwrite *= fwrite(rlebuf, len, 1, outf);
638 starttab[y + z * ysize] = pos;
639 lengthtab[y + z * ysize] = len;
640 pos += len;
642 if (reverse_order)
643 lptr -= xsize * sizeof(Py_UInt32);
644 else
645 lptr += xsize * sizeof(Py_UInt32);
648 fseek(outf, 512, SEEK_SET);
649 goodwrite *= writetab(outf, starttab, ysize*zsize);
650 goodwrite *= writetab(outf, lengthtab, ysize*zsize);
651 if (goodwrite) {
652 Py_INCREF(Py_None);
653 retval = Py_None;
654 } else
655 PyErr_SetString(ImgfileError, "not enough space for image");
657 finally:
658 fclose(outf);
659 free(starttab);
660 free(lengthtab);
661 free(rlebuf);
662 free(lumbuf);
663 return retval;
666 /* static utility functions for longstoimage */
668 static void
669 lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
671 lumptr += CHANOFFSET(0);
672 while (n--) {
673 *lumptr = ILUM(rgbptr[OFFSET_R],
674 rgbptr[OFFSET_G],
675 rgbptr[OFFSET_B]);
676 lumptr += 4;
677 rgbptr += 4;
681 static int
682 compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
684 unsigned char *iptr, *ibufend, *sptr, *optr;
685 short todo, cc;
686 Py_Int32 count;
688 lbuf += z;
689 iptr = lbuf;
690 ibufend = iptr + cnt * 4;
691 optr = rlebuf;
693 while(iptr < ibufend) {
694 sptr = iptr;
695 iptr += 8;
696 while ((iptr<ibufend) &&
697 ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
699 iptr += 4;
701 iptr -= 8;
702 count = (iptr - sptr) / 4;
703 while (count) {
704 todo = count > 126 ? 126 : (short)count;
705 count -= todo;
706 *optr++ = 0x80 | todo;
707 while (todo > 8) {
708 optr[0] = sptr[0 * 4];
709 optr[1] = sptr[1 * 4];
710 optr[2] = sptr[2 * 4];
711 optr[3] = sptr[3 * 4];
712 optr[4] = sptr[4 * 4];
713 optr[5] = sptr[5 * 4];
714 optr[6] = sptr[6 * 4];
715 optr[7] = sptr[7 * 4];
716 optr += 8;
717 sptr += 8 * 4;
718 todo -= 8;
720 while (todo--) {
721 *optr++ = *sptr;
722 sptr += 4;
725 sptr = iptr;
726 cc = *iptr;
727 iptr += 4;
728 while ((iptr < ibufend) && (*iptr == cc))
729 iptr += 4;
730 count = (iptr - sptr) / 4;
731 while (count) {
732 todo = count > 126 ? 126 : (short)count;
733 count -= todo;
734 *optr++ = (unsigned char) todo;
735 *optr++ = (unsigned char) cc;
738 *optr++ = 0;
739 return optr - (unsigned char *)rlebuf;
742 static PyObject *
743 ttob(PyObject *self, PyObject *args)
745 int order, oldorder;
747 if (!PyArg_ParseTuple(args, "i:ttob", &order))
748 return NULL;
749 oldorder = reverse_order;
750 reverse_order = order;
751 return PyInt_FromLong(oldorder);
754 static PyMethodDef
755 rgbimg_methods[] = {
756 {"sizeofimage", sizeofimage, METH_VARARGS},
757 {"longimagedata", longimagedata, METH_VARARGS},
758 {"longstoimage", longstoimage, METH_VARARGS},
759 {"ttob", ttob, METH_VARARGS},
760 {NULL, NULL} /* sentinel */
764 PyMODINIT_FUNC
765 initrgbimg(void)
767 PyObject *m, *d;
768 m = Py_InitModule("rgbimg", rgbimg_methods);
769 if (m == NULL)
770 return;
772 if (PyErr_Warn(PyExc_DeprecationWarning,
773 "the rgbimg module is deprecated"))
774 return;
776 d = PyModule_GetDict(m);
777 ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
778 if (ImgfileError != NULL)
779 PyDict_SetItemString(d, "error", ImgfileError);