blob: 904c64b642ec2fdec4e0ad12bc36ab58e8f82f37 [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 Rossum69011961998-04-23 20:23:00 +000019#if SIZEOF_INT == 4
20typedef int Py_Int32;
21typedef unsigned int Py_UInt32;
22#else
23#if SIZEOF_LONG == 4
24typedef long Py_Int32;
25typedef unsigned long Py_UInt32;
26#else
27#error "No 4-byte integral type"
28#endif
29#endif
30
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +000031#include <string.h>
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000032
33/*
34 * from image.h
35 *
36 */
37typedef struct {
Roger E. Masse5b0eba31997-01-03 18:51:01 +000038 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;
Guido van Rossum69011961998-04-23 20:23:00 +000044 Py_UInt32 min;
45 Py_UInt32 max;
46 Py_UInt32 wastebytes;
Roger E. Masse5b0eba31997-01-03 18:51:01 +000047 char name[80];
Guido van Rossum69011961998-04-23 20:23:00 +000048 Py_UInt32 colormap;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000049
Guido van Rossum69011961998-04-23 20:23:00 +000050 Py_Int32 file; /* stuff used in core only */
Roger E. Masse5b0eba31997-01-03 18:51:01 +000051 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;
Guido van Rossum69011961998-04-23 20:23:00 +000060 Py_UInt32 offset;
61 Py_UInt32 rleend; /* for rle images */
62 Py_UInt32 *rowstart; /* for rle images */
63 Py_Int32 *rowsize; /* for rle images */
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000064} IMAGE;
65
66#define IMAGIC 0732
67
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))
77/*
78 * end of image.h stuff
79 *
80 */
81
82#define RINTLUM (79)
83#define GINTLUM (156)
84#define BINTLUM (21)
85
86#define ILUM(r,g,b) ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
87
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
92
93#define CHANOFFSET(z) (3-(z)) /* this is byte order dependent */
94
Tim Petersdbd9ba62000-07-09 03:09:57 +000095static void expandrow(unsigned char *, unsigned char *, int);
96static void setalpha(unsigned char *, int);
97static void copybw(Py_Int32 *, int);
98static void interleaverow(unsigned char*, unsigned char*, int, int);
99static int compressrow(unsigned char *, unsigned char *, int, int);
100static void lumrow(unsigned char *, unsigned char *, int);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000101
102#ifdef ADD_TAGS
103#define TAGLEN (5)
104#else
105#define TAGLEN (0)
106#endif
107
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000108static PyObject *ImgfileError;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000109
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000110static int reverse_order;
111
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000112#ifdef ADD_TAGS
113/*
114 * addlongimgtag -
115 * this is used to extract image data from core dumps.
116 *
117 */
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000118static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000119addlongimgtag(Py_UInt32 *dptr, int xsize, int ysize)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000120{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000121 dptr = dptr + (xsize * ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000122 dptr[0] = 0x12345678;
123 dptr[1] = 0x59493333;
124 dptr[2] = 0x69434222;
125 dptr[3] = xsize;
126 dptr[4] = ysize;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000127}
128#endif
129
130/*
131 * byte order independent read/write of shorts and longs.
132 *
133 */
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000134static unsigned short
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000135getshort(FILE *inf)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000136{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000137 unsigned char buf[2];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000138
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000139 fread(buf, 2, 1, inf);
140 return (buf[0] << 8) + (buf[1] << 0);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000141}
142
Guido van Rossum69011961998-04-23 20:23:00 +0000143static Py_UInt32
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000144getlong(FILE *inf)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000145{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000146 unsigned char buf[4];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000147
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000148 fread(buf, 4, 1, inf);
149 return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000150}
151
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000152static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000153putshort(FILE *outf, unsigned short val)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000154{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000155 unsigned char buf[2];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000156
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000157 buf[0] = (val >> 8);
158 buf[1] = (val >> 0);
159 fwrite(buf, 2, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000160}
161
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000162static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000163putlong(FILE *outf, Py_UInt32 val)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000164{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000165 unsigned char buf[4];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000166
Guido van Rossum7844e381997-04-11 20:44:04 +0000167 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);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000171 return fwrite(buf, 4, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000172}
173
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000174static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000175readheader(FILE *inf, IMAGE *image)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000176{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000177 memset(image ,0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000178 image->imagic = getshort(inf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000179 image->type = getshort(inf);
180 image->dim = getshort(inf);
181 image->xsize = getshort(inf);
182 image->ysize = getshort(inf);
183 image->zsize = getshort(inf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000184}
185
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000186static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000187writeheader(FILE *outf, IMAGE *image)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000188{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000189 IMAGE t;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000190
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000191 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);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000204}
205
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000206static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000207writetab(FILE *outf, /*unsigned*/ Py_Int32 *tab, int len)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000208{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000209 int r = 0;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000210
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000211 while(len) {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000212 r = putlong(outf, *tab++);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000213 len--;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000214 }
215 return r;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000216}
217
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000218static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000219readtab(FILE *inf, /*unsigned*/ Py_Int32 *tab, int len)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000220{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000221 while(len) {
222 *tab++ = getlong(inf);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000223 len--;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000224 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000225}
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 *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000233sizeofimage(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000234{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000235 char *name;
236 IMAGE image;
237 FILE *inf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000238
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000239 if (!PyArg_ParseTuple(args, "s:sizeofimage", &name))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000240 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000241
Guido van Rossumfd16d941997-04-11 19:12:20 +0000242 inf = fopen(name, "rb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000243 if (!inf) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000244 PyErr_SetString(ImgfileError, "can't open image file");
245 return NULL;
246 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000247 readheader(inf, &image);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000248 fclose(inf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000249 if (image.imagic != IMAGIC) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000250 PyErr_SetString(ImgfileError,
251 "bad magic number in image file");
252 return NULL;
253 }
254 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 *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000264longimagedata(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000265{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000266 char *name;
267 unsigned char *base, *lptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000268 unsigned char *rledat = NULL, *verdat = NULL;
Guido van Rossum69011961998-04-23 20:23:00 +0000269 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000270 FILE *inf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000271 IMAGE image;
272 int y, z, tablen;
273 int xsize, ysize, zsize;
274 int bpp, rle, cur, badorder;
275 int rlebuflen;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000276 PyObject *rv = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000277
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000278 if (!PyArg_ParseTuple(args, "s:longimagedata", &name))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000279 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000280
Guido van Rossum56809061997-02-21 15:19:03 +0000281 inf = fopen(name,"rb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000282 if (!inf) {
283 PyErr_SetString(ImgfileError, "can't open image file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000284 return NULL;
285 }
286 readheader(inf,&image);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000287 if (image.imagic != IMAGIC) {
288 PyErr_SetString(ImgfileError,
289 "bad magic number in image file");
290 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000291 }
292 rle = ISRLE(image.type);
293 bpp = BPP(image.type);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000294 if (bpp != 1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000295 PyErr_SetString(ImgfileError,
296 "image must have 1 byte per pix chan");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000297 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000298 }
299 xsize = image.xsize;
300 ysize = image.ysize;
301 zsize = image.zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000302 if (rle) {
Guido van Rossum69011961998-04-23 20:23:00 +0000303 tablen = ysize * zsize * sizeof(Py_Int32);
304 starttab = (Py_Int32 *)malloc(tablen);
305 lengthtab = (Py_Int32 *)malloc(tablen);
Guido van Rossum7844e381997-04-11 20:44:04 +0000306 rlebuflen = (int) (1.05 * xsize +10);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000307 rledat = (unsigned char *)malloc(rlebuflen);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000308 if (!starttab || !lengthtab || !rledat) {
309 PyErr_NoMemory();
310 goto finally;
311 }
312
313 fseek(inf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000314 readtab(inf, starttab, ysize*zsize);
315 readtab(inf, lengthtab, ysize*zsize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000316
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000317 /* check data order */
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000318 cur = 0;
319 badorder = 0;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000320 for(y = 0; y < ysize; y++) {
321 for(z = 0; z < zsize; z++) {
322 if (starttab[y + z * ysize] < cur) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000323 badorder = 1;
324 break;
325 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000326 cur = starttab[y +z * ysize];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000327 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000328 if (badorder)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000329 break;
330 }
331
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000332 fseek(inf, 512 + 2 * tablen, SEEK_SET);
333 cur = 512 + 2 * tablen;
334 rv = PyString_FromStringAndSize((char *)NULL,
Guido van Rossum69011961998-04-23 20:23:00 +0000335 (xsize * ysize + TAGLEN) * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000336 if (rv == NULL)
337 goto finally;
338
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000339 base = (unsigned char *) PyString_AsString(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000340#ifdef ADD_TAGS
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000341 addlongimgtag(base,xsize,ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000342#endif
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000343 if (badorder) {
344 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000345 lptr = base;
346 if (reverse_order)
347 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000348 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000349 for (y = 0; y < ysize; y++) {
350 int idx = y + z * ysize;
351 if (cur != starttab[idx]) {
352 fseek(inf,starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000353 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000354 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000355 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000356 if (lengthtab[idx] > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000357 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000358 "rlebuf is too small");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000359 Py_DECREF(rv);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000360 rv = NULL;
361 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000362 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000363 fread(rledat, lengthtab[idx], 1, inf);
364 cur += lengthtab[idx];
365 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000366 if (reverse_order)
367 lptr -= xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000368 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000369 else
370 lptr += xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000371 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000372 }
373 }
374 } else {
375 lptr = base;
376 if (reverse_order)
377 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000378 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000379 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],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000384 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000385 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000386 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000387 fread(rledat, lengthtab[idx], 1, inf);
388 cur += lengthtab[idx];
389 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000390 }
391 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000392 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000393 else
Guido van Rossum69011961998-04-23 20:23:00 +0000394 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000395 }
396 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000397 if (zsize == 3)
398 setalpha(base, xsize * ysize);
399 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000400 copybw((Py_Int32 *) base, xsize * ysize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000401 }
402 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000403 rv = PyString_FromStringAndSize((char *) 0,
Guido van Rossum69011961998-04-23 20:23:00 +0000404 (xsize*ysize+TAGLEN)*sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000405 if (rv == NULL)
406 goto finally;
407
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000408 base = (unsigned char *) PyString_AsString(rv);
409#ifdef ADD_TAGS
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000410 addlongimgtag(base, xsize, ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000411#endif
412 verdat = (unsigned char *)malloc(xsize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000413 fseek(inf, 512, SEEK_SET);
414 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000415 lptr = base;
416 if (reverse_order)
417 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000418 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000419 for (y = 0; y < ysize; y++) {
420 fread(verdat, xsize, 1, inf);
421 interleaverow(lptr, verdat, 3-z, xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000422 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000423 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000424 else
Guido van Rossum69011961998-04-23 20:23:00 +0000425 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000426 }
427 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000428 if (zsize == 3)
429 setalpha(base, xsize * ysize);
430 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000431 copybw((Py_Int32 *) base, xsize * ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000432 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000433 finally:
434 free(starttab);
435 free(lengthtab);
436 free(rledat);
437 free(verdat);
438 fclose(inf);
439 return rv;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000440}
441
442/* static utility functions for longimagedata */
443
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000444static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000445interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000446{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000447 lptr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000448 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000449 *lptr = *cptr++;
450 lptr += 4;
451 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000452}
453
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000454static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000455copybw(Py_Int32 *lptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000456{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000457 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));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000466 lptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000467 n -= 8;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000468 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000469 while (n--) {
470 *lptr = 0xff000000 + (0x010101 * (*lptr&0xff));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000471 lptr++;
472 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000473}
474
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000475static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000476setalpha(unsigned char *lptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000477{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000478 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;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000488 n -= 8;
489 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000490 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000491 *lptr = 0xff;
492 lptr += 4;
493 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000494}
495
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000496static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000497expandrow(unsigned char *optr, unsigned char *iptr, int z)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000498{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000499 unsigned char pixel, count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000500
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000501 optr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000502 while (1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000503 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000504 if (!(count = (pixel & 0x7f)))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000505 return;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000506 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;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000517 iptr += 8;
518 count -= 8;
519 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000520 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000521 *optr = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000522 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000523 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000524 }
525 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000526 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000527 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;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000537 count -= 8;
538 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000539 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000540 *optr = pixel;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000541 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000542 }
543 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000544 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000545}
546
547/*
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
Thomas Wouters7e474022000-07-16 12:04:32 +0000553 * calculated, and a single channel black and white image is saved.
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000554 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
555 * RGBA image file is saved.
556 *
557 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000558static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000559longstoimage(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000560{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000561 unsigned char *lptr;
562 char *name;
563 int xsize, ysize, zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000564 FILE *outf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000565 IMAGE image;
566 int tablen, y, z, pos, len;
Guido van Rossum69011961998-04-23 20:23:00 +0000567 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000568 unsigned char *rlebuf = NULL;
569 unsigned char *lumbuf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000570 int rlebuflen, goodwrite;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000571 PyObject *retval = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000572
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000573 if (!PyArg_ParseTuple(args, "s#iiis:longstoimage", &lptr, &len,
574 &xsize, &ysize, &zsize, &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000575 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000576
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000577 goodwrite = 1;
Guido van Rossum56809061997-02-21 15:19:03 +0000578 outf = fopen(name, "wb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000579 if (!outf) {
580 PyErr_SetString(ImgfileError, "can't open output file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000581 return NULL;
582 }
Guido van Rossum69011961998-04-23 20:23:00 +0000583 tablen = ysize * zsize * sizeof(Py_Int32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000584
Guido van Rossum69011961998-04-23 20:23:00 +0000585 starttab = (Py_Int32 *)malloc(tablen);
586 lengthtab = (Py_Int32 *)malloc(tablen);
Guido van Rossum7844e381997-04-11 20:44:04 +0000587 rlebuflen = (int) (1.05 * xsize + 10);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000588 rlebuf = (unsigned char *)malloc(rlebuflen);
Guido van Rossum69011961998-04-23 20:23:00 +0000589 lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000590 if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
591 PyErr_NoMemory();
592 goto finally;
593 }
594
595 memset(&image, 0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000596 image.imagic = IMAGIC;
597 image.type = RLE(1);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000598 if (zsize>1)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000599 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;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000607 goodwrite *= writeheader(outf, &image);
608 pos = 512 + 2 * tablen;
609 fseek(outf, pos, SEEK_SET);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000610 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000611 lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000612 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);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000618 } else {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000619 len = compressrow(lptr, rlebuf,
620 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000621 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000622 if(len > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000623 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000624 "rlebuf is too small");
625 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000626 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000627 goodwrite *= fwrite(rlebuf, len, 1, outf);
628 starttab[y + z * ysize] = pos;
629 lengthtab[y + z * ysize] = len;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000630 pos += len;
631 }
632 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000633 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000634 else
Guido van Rossum69011961998-04-23 20:23:00 +0000635 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000636 }
637
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000638 fseek(outf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000639 goodwrite *= writetab(outf, starttab, ysize*zsize);
640 goodwrite *= writetab(outf, lengthtab, ysize*zsize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000641 if (goodwrite) {
642 Py_INCREF(Py_None);
643 retval = Py_None;
644 } else
645 PyErr_SetString(ImgfileError, "not enough space for image");
646
647 finally:
648 fclose(outf);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000649 free(starttab);
650 free(lengthtab);
651 free(rlebuf);
652 free(lumbuf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000653 return retval;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000654}
655
656/* static utility functions for longstoimage */
657
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000658static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000659lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000660{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000661 lumptr += CHANOFFSET(0);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000662 while (n--) {
663 *lumptr = ILUM(rgbptr[OFFSET_R],
664 rgbptr[OFFSET_G],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000665 rgbptr[OFFSET_B]);
666 lumptr += 4;
667 rgbptr += 4;
668 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000669}
670
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000671static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000672compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000673{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000674 unsigned char *iptr, *ibufend, *sptr, *optr;
675 short todo, cc;
Guido van Rossum69011961998-04-23 20:23:00 +0000676 Py_Int32 count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000677
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000678 lbuf += z;
679 iptr = lbuf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000680 ibufend = iptr + cnt * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000681 optr = rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000682
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000683 while(iptr < ibufend) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000684 sptr = iptr;
685 iptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000686 while ((iptr<ibufend) &&
687 ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
688 {
689 iptr += 4;
690 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000691 iptr -= 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000692 count = (iptr - sptr) / 4;
693 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000694 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000695 count -= todo;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000696 *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];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000706 optr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000707 sptr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000708 todo -= 8;
709 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000710 while (todo--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000711 *optr++ = *sptr;
712 sptr += 4;
713 }
714 }
715 sptr = iptr;
716 cc = *iptr;
717 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000718 while ((iptr < ibufend) && (*iptr == cc))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000719 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000720 count = (iptr - sptr) / 4;
721 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000722 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000723 count -= todo;
Guido van Rossum7844e381997-04-11 20:44:04 +0000724 *optr++ = (unsigned char) todo;
725 *optr++ = (unsigned char) cc;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000726 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000727 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000728 *optr++ = 0;
729 return optr - (unsigned char *)rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000730}
731
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000732static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000733ttob(PyObject *self, PyObject *args)
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000734{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000735 int order, oldorder;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000736
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000737 if (!PyArg_ParseTuple(args, "i:ttob", &order))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000738 return NULL;
739 oldorder = reverse_order;
740 reverse_order = order;
741 return PyInt_FromLong(oldorder);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000742}
743
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000744static PyMethodDef
745rgbimg_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000746 {"sizeofimage", sizeofimage, METH_VARARGS},
747 {"longimagedata", longimagedata, METH_VARARGS},
748 {"longstoimage", longstoimage, METH_VARARGS},
749 {"ttob", ttob, METH_VARARGS},
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000750 {NULL, NULL} /* sentinel */
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000751};
752
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000753
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000754PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000755initrgbimg(void)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000756{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000757 PyObject *m, *d;
758 m = Py_InitModule("rgbimg", rgbimg_methods);
759 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000760 ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
761 if (ImgfileError != NULL)
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000762 PyDict_SetItemString(d, "error", ImgfileError);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000763}