blob: c8c42c2ac05a5226447daf144d3193329cc38f53 [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
Guido van Rossumb6775db1994-08-01 11:34:53 +000031#ifdef HAVE_UNISTD_H
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000032#include <unistd.h>
Guido van Rossumb6775db1994-08-01 11:34:53 +000033#endif
Sjoerd Mullender92fa23f1993-12-24 10:05:51 +000034#include <string.h>
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000035
36/*
37 * from image.h
38 *
39 */
40typedef struct {
Roger E. Masse5b0eba31997-01-03 18:51:01 +000041 unsigned short imagic; /* stuff saved on disk . . */
42 unsigned short type;
43 unsigned short dim;
44 unsigned short xsize;
45 unsigned short ysize;
46 unsigned short zsize;
Guido van Rossum69011961998-04-23 20:23:00 +000047 Py_UInt32 min;
48 Py_UInt32 max;
49 Py_UInt32 wastebytes;
Roger E. Masse5b0eba31997-01-03 18:51:01 +000050 char name[80];
Guido van Rossum69011961998-04-23 20:23:00 +000051 Py_UInt32 colormap;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000052
Guido van Rossum69011961998-04-23 20:23:00 +000053 Py_Int32 file; /* stuff used in core only */
Roger E. Masse5b0eba31997-01-03 18:51:01 +000054 unsigned short flags;
55 short dorev;
56 short x;
57 short y;
58 short z;
59 short cnt;
60 unsigned short *ptr;
61 unsigned short *base;
62 unsigned short *tmpbuf;
Guido van Rossum69011961998-04-23 20:23:00 +000063 Py_UInt32 offset;
64 Py_UInt32 rleend; /* for rle images */
65 Py_UInt32 *rowstart; /* for rle images */
66 Py_Int32 *rowsize; /* for rle images */
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +000067} IMAGE;
68
69#define IMAGIC 0732
70
71#define TYPEMASK 0xff00
72#define BPPMASK 0x00ff
73#define ITYPE_VERBATIM 0x0000
74#define ITYPE_RLE 0x0100
75#define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE)
76#define ISVERBATIM(type) (((type) & 0xff00) == ITYPE_VERBATIM)
77#define BPP(type) ((type) & BPPMASK)
78#define RLE(bpp) (ITYPE_RLE | (bpp))
79#define VERBATIM(bpp) (ITYPE_VERBATIM | (bpp))
80/*
81 * end of image.h stuff
82 *
83 */
84
85#define RINTLUM (79)
86#define GINTLUM (156)
87#define BINTLUM (21)
88
89#define ILUM(r,g,b) ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8)
90
91#define OFFSET_R 3 /* this is byte order dependent */
92#define OFFSET_G 2
93#define OFFSET_B 1
94#define OFFSET_A 0
95
96#define CHANOFFSET(z) (3-(z)) /* this is byte order dependent */
97
Tim Petersdbd9ba62000-07-09 03:09:57 +000098static void expandrow(unsigned char *, unsigned char *, int);
99static void setalpha(unsigned char *, int);
100static void copybw(Py_Int32 *, int);
101static void interleaverow(unsigned char*, unsigned char*, int, int);
102static int compressrow(unsigned char *, unsigned char *, int, int);
103static void lumrow(unsigned char *, unsigned char *, int);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000104
105#ifdef ADD_TAGS
106#define TAGLEN (5)
107#else
108#define TAGLEN (0)
109#endif
110
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000111static PyObject *ImgfileError;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000112
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000113static int reverse_order;
114
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000115#ifdef ADD_TAGS
116/*
117 * addlongimgtag -
118 * this is used to extract image data from core dumps.
119 *
120 */
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000121static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000122addlongimgtag(Py_UInt32 *dptr, int xsize, int ysize)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000123{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000124 dptr = dptr + (xsize * ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000125 dptr[0] = 0x12345678;
126 dptr[1] = 0x59493333;
127 dptr[2] = 0x69434222;
128 dptr[3] = xsize;
129 dptr[4] = ysize;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000130}
131#endif
132
133/*
134 * byte order independent read/write of shorts and longs.
135 *
136 */
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000137static unsigned short
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000138getshort(FILE *inf)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000139{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000140 unsigned char buf[2];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000141
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000142 fread(buf, 2, 1, inf);
143 return (buf[0] << 8) + (buf[1] << 0);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000144}
145
Guido van Rossum69011961998-04-23 20:23:00 +0000146static Py_UInt32
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000147getlong(FILE *inf)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000148{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000149 unsigned char buf[4];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000150
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000151 fread(buf, 4, 1, inf);
152 return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000153}
154
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000155static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000156putshort(FILE *outf, unsigned short val)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000157{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000158 unsigned char buf[2];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000159
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000160 buf[0] = (val >> 8);
161 buf[1] = (val >> 0);
162 fwrite(buf, 2, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000163}
164
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000165static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000166putlong(FILE *outf, Py_UInt32 val)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000167{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000168 unsigned char buf[4];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000169
Guido van Rossum7844e381997-04-11 20:44:04 +0000170 buf[0] = (unsigned char) (val >> 24);
171 buf[1] = (unsigned char) (val >> 16);
172 buf[2] = (unsigned char) (val >> 8);
173 buf[3] = (unsigned char) (val >> 0);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000174 return fwrite(buf, 4, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000175}
176
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000177static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000178readheader(FILE *inf, IMAGE *image)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000179{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000180 memset(image ,0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000181 image->imagic = getshort(inf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000182 image->type = getshort(inf);
183 image->dim = getshort(inf);
184 image->xsize = getshort(inf);
185 image->ysize = getshort(inf);
186 image->zsize = getshort(inf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000187}
188
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000189static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000190writeheader(FILE *outf, IMAGE *image)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000191{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000192 IMAGE t;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000193
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000194 memset(&t, 0, sizeof(IMAGE));
195 fwrite(&t, sizeof(IMAGE), 1, outf);
196 fseek(outf, 0, SEEK_SET);
197 putshort(outf, image->imagic);
198 putshort(outf, image->type);
199 putshort(outf, image->dim);
200 putshort(outf, image->xsize);
201 putshort(outf, image->ysize);
202 putshort(outf, image->zsize);
203 putlong(outf, image->min);
204 putlong(outf, image->max);
205 putlong(outf, 0);
206 return fwrite("no name", 8, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000207}
208
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000209static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000210writetab(FILE *outf, /*unsigned*/ Py_Int32 *tab, int len)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000211{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000212 int r = 0;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000213
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000214 while(len) {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000215 r = putlong(outf, *tab++);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000216 len--;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000217 }
218 return r;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000219}
220
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000221static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000222readtab(FILE *inf, /*unsigned*/ Py_Int32 *tab, int len)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000223{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000224 while(len) {
225 *tab++ = getlong(inf);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000226 len--;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000227 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000228}
229
230/*
231 * sizeofimage -
232 * return the xsize and ysize of an iris image file.
233 *
234 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000235static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000236sizeofimage(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000237{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000238 char *name;
239 IMAGE image;
240 FILE *inf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000241
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000242 if (!PyArg_Parse(args, "s", &name))
243 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000244
Guido van Rossumfd16d941997-04-11 19:12:20 +0000245 inf = fopen(name, "rb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000246 if (!inf) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000247 PyErr_SetString(ImgfileError, "can't open image file");
248 return NULL;
249 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000250 readheader(inf, &image);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000251 fclose(inf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000252 if (image.imagic != IMAGIC) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000253 PyErr_SetString(ImgfileError,
254 "bad magic number in image file");
255 return NULL;
256 }
257 return Py_BuildValue("(ii)", image.xsize, image.ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000258}
259
260/*
261 * longimagedata -
262 * read in a B/W RGB or RGBA iris image file and return a
263 * pointer to an array of longs.
264 *
265 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000266static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000267longimagedata(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000268{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000269 char *name;
270 unsigned char *base, *lptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000271 unsigned char *rledat = NULL, *verdat = NULL;
Guido van Rossum69011961998-04-23 20:23:00 +0000272 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000273 FILE *inf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000274 IMAGE image;
275 int y, z, tablen;
276 int xsize, ysize, zsize;
277 int bpp, rle, cur, badorder;
278 int rlebuflen;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000279 PyObject *rv = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000280
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000281 if (!PyArg_Parse(args, "s", &name))
282 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000283
Guido van Rossum56809061997-02-21 15:19:03 +0000284 inf = fopen(name,"rb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000285 if (!inf) {
286 PyErr_SetString(ImgfileError, "can't open image file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000287 return NULL;
288 }
289 readheader(inf,&image);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000290 if (image.imagic != IMAGIC) {
291 PyErr_SetString(ImgfileError,
292 "bad magic number in image file");
293 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000294 }
295 rle = ISRLE(image.type);
296 bpp = BPP(image.type);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000297 if (bpp != 1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000298 PyErr_SetString(ImgfileError,
299 "image must have 1 byte per pix chan");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000300 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000301 }
302 xsize = image.xsize;
303 ysize = image.ysize;
304 zsize = image.zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000305 if (rle) {
Guido van Rossum69011961998-04-23 20:23:00 +0000306 tablen = ysize * zsize * sizeof(Py_Int32);
307 starttab = (Py_Int32 *)malloc(tablen);
308 lengthtab = (Py_Int32 *)malloc(tablen);
Guido van Rossum7844e381997-04-11 20:44:04 +0000309 rlebuflen = (int) (1.05 * xsize +10);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000310 rledat = (unsigned char *)malloc(rlebuflen);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000311 if (!starttab || !lengthtab || !rledat) {
312 PyErr_NoMemory();
313 goto finally;
314 }
315
316 fseek(inf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000317 readtab(inf, starttab, ysize*zsize);
318 readtab(inf, lengthtab, ysize*zsize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000319
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000320 /* check data order */
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000321 cur = 0;
322 badorder = 0;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000323 for(y = 0; y < ysize; y++) {
324 for(z = 0; z < zsize; z++) {
325 if (starttab[y + z * ysize] < cur) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000326 badorder = 1;
327 break;
328 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000329 cur = starttab[y +z * ysize];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000330 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000331 if (badorder)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000332 break;
333 }
334
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000335 fseek(inf, 512 + 2 * tablen, SEEK_SET);
336 cur = 512 + 2 * tablen;
337 rv = PyString_FromStringAndSize((char *)NULL,
Guido van Rossum69011961998-04-23 20:23:00 +0000338 (xsize * ysize + TAGLEN) * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000339 if (rv == NULL)
340 goto finally;
341
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000342 base = (unsigned char *) PyString_AsString(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000343#ifdef ADD_TAGS
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000344 addlongimgtag(base,xsize,ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000345#endif
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000346 if (badorder) {
347 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000348 lptr = base;
349 if (reverse_order)
350 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000351 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000352 for (y = 0; y < ysize; y++) {
353 int idx = y + z * ysize;
354 if (cur != starttab[idx]) {
355 fseek(inf,starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000356 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000357 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000358 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000359 if (lengthtab[idx] > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000360 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000361 "rlebuf is too small");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000362 Py_DECREF(rv);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000363 rv = NULL;
364 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000365 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000366 fread(rledat, lengthtab[idx], 1, inf);
367 cur += lengthtab[idx];
368 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000369 if (reverse_order)
370 lptr -= xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000371 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000372 else
373 lptr += xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000374 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000375 }
376 }
377 } else {
378 lptr = base;
379 if (reverse_order)
380 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000381 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000382 for (y = 0; y < ysize; y++) {
383 for(z = 0; z < zsize; z++) {
384 int idx = y + z * ysize;
385 if (cur != starttab[idx]) {
386 fseek(inf, starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000387 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000388 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000389 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000390 fread(rledat, lengthtab[idx], 1, inf);
391 cur += lengthtab[idx];
392 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000393 }
394 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000395 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000396 else
Guido van Rossum69011961998-04-23 20:23:00 +0000397 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000398 }
399 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000400 if (zsize == 3)
401 setalpha(base, xsize * ysize);
402 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000403 copybw((Py_Int32 *) base, xsize * ysize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000404 }
405 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000406 rv = PyString_FromStringAndSize((char *) 0,
Guido van Rossum69011961998-04-23 20:23:00 +0000407 (xsize*ysize+TAGLEN)*sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000408 if (rv == NULL)
409 goto finally;
410
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000411 base = (unsigned char *) PyString_AsString(rv);
412#ifdef ADD_TAGS
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000413 addlongimgtag(base, xsize, ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000414#endif
415 verdat = (unsigned char *)malloc(xsize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000416 fseek(inf, 512, SEEK_SET);
417 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000418 lptr = base;
419 if (reverse_order)
420 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000421 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000422 for (y = 0; y < ysize; y++) {
423 fread(verdat, xsize, 1, inf);
424 interleaverow(lptr, verdat, 3-z, xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000425 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000426 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000427 else
Guido van Rossum69011961998-04-23 20:23:00 +0000428 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000429 }
430 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000431 if (zsize == 3)
432 setalpha(base, xsize * ysize);
433 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000434 copybw((Py_Int32 *) base, xsize * ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000435 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000436 finally:
437 free(starttab);
438 free(lengthtab);
439 free(rledat);
440 free(verdat);
441 fclose(inf);
442 return rv;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000443}
444
445/* static utility functions for longimagedata */
446
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000447static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000448interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000449{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000450 lptr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000451 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000452 *lptr = *cptr++;
453 lptr += 4;
454 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000455}
456
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000457static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000458copybw(Py_Int32 *lptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000459{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000460 while (n >= 8) {
461 lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff));
462 lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff));
463 lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff));
464 lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff));
465 lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff));
466 lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff));
467 lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff));
468 lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000469 lptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000470 n -= 8;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000471 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000472 while (n--) {
473 *lptr = 0xff000000 + (0x010101 * (*lptr&0xff));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000474 lptr++;
475 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000476}
477
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000478static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000479setalpha(unsigned char *lptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000480{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000481 while (n >= 8) {
482 lptr[0 * 4] = 0xff;
483 lptr[1 * 4] = 0xff;
484 lptr[2 * 4] = 0xff;
485 lptr[3 * 4] = 0xff;
486 lptr[4 * 4] = 0xff;
487 lptr[5 * 4] = 0xff;
488 lptr[6 * 4] = 0xff;
489 lptr[7 * 4] = 0xff;
490 lptr += 4 * 8;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000491 n -= 8;
492 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000493 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000494 *lptr = 0xff;
495 lptr += 4;
496 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000497}
498
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000499static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000500expandrow(unsigned char *optr, unsigned char *iptr, int z)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000501{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000502 unsigned char pixel, count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000503
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000504 optr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000505 while (1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000506 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000507 if (!(count = (pixel & 0x7f)))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000508 return;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000509 if (pixel & 0x80) {
510 while (count >= 8) {
511 optr[0 * 4] = iptr[0];
512 optr[1 * 4] = iptr[1];
513 optr[2 * 4] = iptr[2];
514 optr[3 * 4] = iptr[3];
515 optr[4 * 4] = iptr[4];
516 optr[5 * 4] = iptr[5];
517 optr[6 * 4] = iptr[6];
518 optr[7 * 4] = iptr[7];
519 optr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000520 iptr += 8;
521 count -= 8;
522 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000523 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000524 *optr = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000525 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000526 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000527 }
528 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000529 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000530 while (count >= 8) {
531 optr[0 * 4] = pixel;
532 optr[1 * 4] = pixel;
533 optr[2 * 4] = pixel;
534 optr[3 * 4] = pixel;
535 optr[4 * 4] = pixel;
536 optr[5 * 4] = pixel;
537 optr[6 * 4] = pixel;
538 optr[7 * 4] = pixel;
539 optr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000540 count -= 8;
541 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000542 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000543 *optr = pixel;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000544 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000545 }
546 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000547 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000548}
549
550/*
551 * longstoimage -
552 * copy an array of longs to an iris image file. Each long
553 * represents one pixel. xsize and ysize specify the dimensions of
554 * the pixel array. zsize specifies what kind of image file to
555 * write out. if zsize is 1, the luminance of the pixels are
Thomas Wouters7e474022000-07-16 12:04:32 +0000556 * calculated, and a single channel black and white image is saved.
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000557 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
558 * RGBA image file is saved.
559 *
560 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000561static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000562longstoimage(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000563{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000564 unsigned char *lptr;
565 char *name;
566 int xsize, ysize, zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000567 FILE *outf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000568 IMAGE image;
569 int tablen, y, z, pos, len;
Guido van Rossum69011961998-04-23 20:23:00 +0000570 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000571 unsigned char *rlebuf = NULL;
572 unsigned char *lumbuf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000573 int rlebuflen, goodwrite;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000574 PyObject *retval = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000575
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000576 if (!PyArg_Parse(args, "(s#iiis)", &lptr, &len, &xsize, &ysize, &zsize,
577 &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000578 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000579
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000580 goodwrite = 1;
Guido van Rossum56809061997-02-21 15:19:03 +0000581 outf = fopen(name, "wb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000582 if (!outf) {
583 PyErr_SetString(ImgfileError, "can't open output file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000584 return NULL;
585 }
Guido van Rossum69011961998-04-23 20:23:00 +0000586 tablen = ysize * zsize * sizeof(Py_Int32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000587
Guido van Rossum69011961998-04-23 20:23:00 +0000588 starttab = (Py_Int32 *)malloc(tablen);
589 lengthtab = (Py_Int32 *)malloc(tablen);
Guido van Rossum7844e381997-04-11 20:44:04 +0000590 rlebuflen = (int) (1.05 * xsize + 10);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000591 rlebuf = (unsigned char *)malloc(rlebuflen);
Guido van Rossum69011961998-04-23 20:23:00 +0000592 lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000593 if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
594 PyErr_NoMemory();
595 goto finally;
596 }
597
598 memset(&image, 0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000599 image.imagic = IMAGIC;
600 image.type = RLE(1);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000601 if (zsize>1)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000602 image.dim = 3;
603 else
604 image.dim = 2;
605 image.xsize = xsize;
606 image.ysize = ysize;
607 image.zsize = zsize;
608 image.min = 0;
609 image.max = 255;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000610 goodwrite *= writeheader(outf, &image);
611 pos = 512 + 2 * tablen;
612 fseek(outf, pos, SEEK_SET);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000613 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000614 lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000615 for (y = 0; y < ysize; y++) {
616 for (z = 0; z < zsize; z++) {
617 if (zsize == 1) {
618 lumrow(lptr, lumbuf, xsize);
619 len = compressrow(lumbuf, rlebuf,
620 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000621 } else {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000622 len = compressrow(lptr, rlebuf,
623 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000624 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000625 if(len > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000626 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000627 "rlebuf is too small");
628 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000629 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000630 goodwrite *= fwrite(rlebuf, len, 1, outf);
631 starttab[y + z * ysize] = pos;
632 lengthtab[y + z * ysize] = len;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000633 pos += len;
634 }
635 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000636 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000637 else
Guido van Rossum69011961998-04-23 20:23:00 +0000638 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000639 }
640
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000641 fseek(outf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000642 goodwrite *= writetab(outf, starttab, ysize*zsize);
643 goodwrite *= writetab(outf, lengthtab, ysize*zsize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000644 if (goodwrite) {
645 Py_INCREF(Py_None);
646 retval = Py_None;
647 } else
648 PyErr_SetString(ImgfileError, "not enough space for image");
649
650 finally:
651 fclose(outf);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000652 free(starttab);
653 free(lengthtab);
654 free(rlebuf);
655 free(lumbuf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000656 return retval;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000657}
658
659/* static utility functions for longstoimage */
660
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000661static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000662lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000663{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000664 lumptr += CHANOFFSET(0);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000665 while (n--) {
666 *lumptr = ILUM(rgbptr[OFFSET_R],
667 rgbptr[OFFSET_G],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000668 rgbptr[OFFSET_B]);
669 lumptr += 4;
670 rgbptr += 4;
671 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000672}
673
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000674static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000675compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000676{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000677 unsigned char *iptr, *ibufend, *sptr, *optr;
678 short todo, cc;
Guido van Rossum69011961998-04-23 20:23:00 +0000679 Py_Int32 count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000680
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000681 lbuf += z;
682 iptr = lbuf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000683 ibufend = iptr + cnt * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000684 optr = rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000685
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000686 while(iptr < ibufend) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000687 sptr = iptr;
688 iptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000689 while ((iptr<ibufend) &&
690 ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
691 {
692 iptr += 4;
693 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000694 iptr -= 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000695 count = (iptr - sptr) / 4;
696 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000697 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000698 count -= todo;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000699 *optr++ = 0x80 | todo;
700 while (todo > 8) {
701 optr[0] = sptr[0 * 4];
702 optr[1] = sptr[1 * 4];
703 optr[2] = sptr[2 * 4];
704 optr[3] = sptr[3 * 4];
705 optr[4] = sptr[4 * 4];
706 optr[5] = sptr[5 * 4];
707 optr[6] = sptr[6 * 4];
708 optr[7] = sptr[7 * 4];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000709 optr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000710 sptr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000711 todo -= 8;
712 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000713 while (todo--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000714 *optr++ = *sptr;
715 sptr += 4;
716 }
717 }
718 sptr = iptr;
719 cc = *iptr;
720 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000721 while ((iptr < ibufend) && (*iptr == cc))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000722 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000723 count = (iptr - sptr) / 4;
724 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000725 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000726 count -= todo;
Guido van Rossum7844e381997-04-11 20:44:04 +0000727 *optr++ = (unsigned char) todo;
728 *optr++ = (unsigned char) cc;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000729 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000730 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000731 *optr++ = 0;
732 return optr - (unsigned char *)rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000733}
734
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000735static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000736ttob(PyObject *self, PyObject *args)
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000737{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000738 int order, oldorder;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000739
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000740 if (!PyArg_Parse(args, "i", &order))
741 return NULL;
742 oldorder = reverse_order;
743 reverse_order = order;
744 return PyInt_FromLong(oldorder);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000745}
746
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000747static PyMethodDef
748rgbimg_methods[] = {
749 {"sizeofimage", sizeofimage},
750 {"longimagedata", longimagedata},
751 {"longstoimage", longstoimage},
752 {"ttob", ttob},
753 {NULL, NULL} /* sentinel */
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000754};
755
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000756
Guido van Rossum3886bb61998-12-04 18:50:17 +0000757DL_EXPORT(void)
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000758initrgbimg(void)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000759{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000760 PyObject *m, *d;
761 m = Py_InitModule("rgbimg", rgbimg_methods);
762 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000763 ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
764 if (ImgfileError != NULL)
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000765 PyDict_SetItemString(d, "error", ImgfileError);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000766}