blob: 3eb2f5534f3d2aeae0d33b14cf76e0be74f946f4 [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);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000171 return (int)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);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000203 return (int)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;
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000272 int y, z, tablen, new_size;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000273 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);
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000304 rlebuflen = (int) (1.05 * xsize +10);
305 if ((tablen / sizeof(Py_Int32)) != (ysize * zsize) ||
306 rlebuflen < 0) {
307 PyErr_NoMemory();
308 goto finally;
309 }
310
Guido van Rossum69011961998-04-23 20:23:00 +0000311 starttab = (Py_Int32 *)malloc(tablen);
312 lengthtab = (Py_Int32 *)malloc(tablen);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000313 rledat = (unsigned char *)malloc(rlebuflen);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000314 if (!starttab || !lengthtab || !rledat) {
315 PyErr_NoMemory();
316 goto finally;
317 }
318
319 fseek(inf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000320 readtab(inf, starttab, ysize*zsize);
321 readtab(inf, lengthtab, ysize*zsize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000322
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000323 /* check data order */
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000324 cur = 0;
325 badorder = 0;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000326 for(y = 0; y < ysize; y++) {
327 for(z = 0; z < zsize; z++) {
328 if (starttab[y + z * ysize] < cur) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000329 badorder = 1;
330 break;
331 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000332 cur = starttab[y +z * ysize];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000333 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000334 if (badorder)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000335 break;
336 }
337
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000338 fseek(inf, 512 + 2 * tablen, SEEK_SET);
339 cur = 512 + 2 * tablen;
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000340 new_size = xsize * ysize + TAGLEN;
341 if (new_size < 0 || (new_size * sizeof(Py_Int32)) < 0) {
342 PyErr_NoMemory();
343 goto finally;
344 }
345
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000346 rv = PyString_FromStringAndSize((char *)NULL,
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000347 new_size * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000348 if (rv == NULL)
349 goto finally;
350
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000351 base = (unsigned char *) PyString_AsString(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000352#ifdef ADD_TAGS
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000353 addlongimgtag(base,xsize,ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000354#endif
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000355 if (badorder) {
356 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000357 lptr = base;
358 if (reverse_order)
359 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000360 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000361 for (y = 0; y < ysize; y++) {
362 int idx = y + z * ysize;
363 if (cur != starttab[idx]) {
364 fseek(inf,starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000365 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000366 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000367 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000368 if (lengthtab[idx] > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000369 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000370 "rlebuf is too small");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000371 Py_DECREF(rv);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000372 rv = NULL;
373 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000374 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000375 fread(rledat, lengthtab[idx], 1, inf);
376 cur += lengthtab[idx];
377 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000378 if (reverse_order)
379 lptr -= xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000380 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000381 else
382 lptr += xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000383 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000384 }
385 }
386 } else {
387 lptr = base;
388 if (reverse_order)
389 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000390 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000391 for (y = 0; y < ysize; y++) {
392 for(z = 0; z < zsize; z++) {
393 int idx = y + z * ysize;
394 if (cur != starttab[idx]) {
395 fseek(inf, starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000396 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000397 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000398 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000399 fread(rledat, lengthtab[idx], 1, inf);
400 cur += lengthtab[idx];
401 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000402 }
403 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000404 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000405 else
Guido van Rossum69011961998-04-23 20:23:00 +0000406 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000407 }
408 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000409 if (zsize == 3)
410 setalpha(base, xsize * ysize);
411 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000412 copybw((Py_Int32 *) base, xsize * ysize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000413 }
414 else {
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000415 new_size = xsize * ysize + TAGLEN;
416 if (new_size < 0 || (new_size * sizeof(Py_Int32)) < 0) {
417 PyErr_NoMemory();
418 goto finally;
419 }
420
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000421 rv = PyString_FromStringAndSize((char *) 0,
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000422 new_size*sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000423 if (rv == NULL)
424 goto finally;
425
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000426 base = (unsigned char *) PyString_AsString(rv);
427#ifdef ADD_TAGS
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000428 addlongimgtag(base, xsize, ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000429#endif
430 verdat = (unsigned char *)malloc(xsize);
Neal Norwitzebcf8752006-08-12 03:18:50 +0000431 if (!verdat) {
432 Py_CLEAR(rv);
433 goto finally;
434 }
435
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000436 fseek(inf, 512, SEEK_SET);
437 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000438 lptr = base;
439 if (reverse_order)
440 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000441 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000442 for (y = 0; y < ysize; y++) {
443 fread(verdat, xsize, 1, inf);
444 interleaverow(lptr, verdat, 3-z, xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000445 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000446 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000447 else
Guido van Rossum69011961998-04-23 20:23:00 +0000448 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000449 }
450 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000451 if (zsize == 3)
452 setalpha(base, xsize * ysize);
453 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000454 copybw((Py_Int32 *) base, xsize * ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000455 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000456 finally:
Neal Norwitzebcf8752006-08-12 03:18:50 +0000457 if (starttab)
458 free(starttab);
459 if (lengthtab)
460 free(lengthtab);
461 if (rledat)
462 free(rledat);
463 if (verdat)
464 free(verdat);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000465 fclose(inf);
466 return rv;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000467}
468
469/* static utility functions for longimagedata */
470
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000471static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000472interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000473{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000474 lptr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000475 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000476 *lptr = *cptr++;
477 lptr += 4;
478 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000479}
480
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000481static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000482copybw(Py_Int32 *lptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000483{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000484 while (n >= 8) {
485 lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff));
486 lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff));
487 lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff));
488 lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff));
489 lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff));
490 lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff));
491 lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff));
492 lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000493 lptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000494 n -= 8;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000495 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000496 while (n--) {
497 *lptr = 0xff000000 + (0x010101 * (*lptr&0xff));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000498 lptr++;
499 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000500}
501
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000502static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000503setalpha(unsigned char *lptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000504{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000505 while (n >= 8) {
506 lptr[0 * 4] = 0xff;
507 lptr[1 * 4] = 0xff;
508 lptr[2 * 4] = 0xff;
509 lptr[3 * 4] = 0xff;
510 lptr[4 * 4] = 0xff;
511 lptr[5 * 4] = 0xff;
512 lptr[6 * 4] = 0xff;
513 lptr[7 * 4] = 0xff;
514 lptr += 4 * 8;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000515 n -= 8;
516 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000517 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000518 *lptr = 0xff;
519 lptr += 4;
520 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000521}
522
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000523static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000524expandrow(unsigned char *optr, unsigned char *iptr, int z)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000525{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000526 unsigned char pixel, count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000527
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000528 optr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000529 while (1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000530 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000531 if (!(count = (pixel & 0x7f)))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000532 return;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000533 if (pixel & 0x80) {
534 while (count >= 8) {
535 optr[0 * 4] = iptr[0];
536 optr[1 * 4] = iptr[1];
537 optr[2 * 4] = iptr[2];
538 optr[3 * 4] = iptr[3];
539 optr[4 * 4] = iptr[4];
540 optr[5 * 4] = iptr[5];
541 optr[6 * 4] = iptr[6];
542 optr[7 * 4] = iptr[7];
543 optr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000544 iptr += 8;
545 count -= 8;
546 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000547 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000548 *optr = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000549 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000550 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000551 }
552 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000553 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000554 while (count >= 8) {
555 optr[0 * 4] = pixel;
556 optr[1 * 4] = pixel;
557 optr[2 * 4] = pixel;
558 optr[3 * 4] = pixel;
559 optr[4 * 4] = pixel;
560 optr[5 * 4] = pixel;
561 optr[6 * 4] = pixel;
562 optr[7 * 4] = pixel;
563 optr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000564 count -= 8;
565 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000566 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000567 *optr = pixel;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000568 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000569 }
570 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000571 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000572}
573
574/*
575 * longstoimage -
576 * copy an array of longs to an iris image file. Each long
577 * represents one pixel. xsize and ysize specify the dimensions of
578 * the pixel array. zsize specifies what kind of image file to
579 * write out. if zsize is 1, the luminance of the pixels are
Thomas Wouters7e474022000-07-16 12:04:32 +0000580 * calculated, and a single channel black and white image is saved.
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000581 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
582 * RGBA image file is saved.
583 *
584 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000585static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000586longstoimage(PyObject *self, PyObject *args)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000587{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000588 unsigned char *lptr;
589 char *name;
590 int xsize, ysize, zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000591 FILE *outf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000592 IMAGE image;
593 int tablen, y, z, pos, len;
Guido van Rossum69011961998-04-23 20:23:00 +0000594 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000595 unsigned char *rlebuf = NULL;
596 unsigned char *lumbuf = NULL;
Martin v. Löwis18e16552006-02-15 17:27:45 +0000597 int rlebuflen;
598 Py_ssize_t goodwrite;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000599 PyObject *retval = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000600
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000601 if (!PyArg_ParseTuple(args, "s#iiis:longstoimage", &lptr, &len,
602 &xsize, &ysize, &zsize, &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000603 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000604
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000605 goodwrite = 1;
Guido van Rossum56809061997-02-21 15:19:03 +0000606 outf = fopen(name, "wb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000607 if (!outf) {
608 PyErr_SetString(ImgfileError, "can't open output file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000609 return NULL;
610 }
Guido van Rossum69011961998-04-23 20:23:00 +0000611 tablen = ysize * zsize * sizeof(Py_Int32);
Martin v. Löwis73c01d42008-02-14 11:26:18 +0000612 rlebuflen = (int) (1.05 * xsize + 10);
613
614 if ((tablen / sizeof(Py_Int32)) != (ysize * zsize) ||
615 rlebuflen < 0 || (xsize * sizeof(Py_Int32)) < 0) {
616 PyErr_NoMemory();
617 goto finally;
618 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000619
Guido van Rossum69011961998-04-23 20:23:00 +0000620 starttab = (Py_Int32 *)malloc(tablen);
621 lengthtab = (Py_Int32 *)malloc(tablen);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000622 rlebuf = (unsigned char *)malloc(rlebuflen);
Guido van Rossum69011961998-04-23 20:23:00 +0000623 lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000624 if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
625 PyErr_NoMemory();
626 goto finally;
627 }
628
629 memset(&image, 0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000630 image.imagic = IMAGIC;
631 image.type = RLE(1);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000632 if (zsize>1)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000633 image.dim = 3;
634 else
635 image.dim = 2;
636 image.xsize = xsize;
637 image.ysize = ysize;
638 image.zsize = zsize;
639 image.min = 0;
640 image.max = 255;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000641 goodwrite *= writeheader(outf, &image);
642 pos = 512 + 2 * tablen;
643 fseek(outf, pos, SEEK_SET);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000644 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000645 lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000646 for (y = 0; y < ysize; y++) {
647 for (z = 0; z < zsize; z++) {
648 if (zsize == 1) {
649 lumrow(lptr, lumbuf, xsize);
650 len = compressrow(lumbuf, rlebuf,
651 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000652 } else {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000653 len = compressrow(lptr, rlebuf,
654 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000655 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000656 if(len > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000657 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000658 "rlebuf is too small");
659 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000660 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000661 goodwrite *= fwrite(rlebuf, len, 1, outf);
662 starttab[y + z * ysize] = pos;
663 lengthtab[y + z * ysize] = len;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000664 pos += len;
665 }
666 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000667 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000668 else
Guido van Rossum69011961998-04-23 20:23:00 +0000669 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000670 }
671
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000672 fseek(outf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000673 goodwrite *= writetab(outf, starttab, ysize*zsize);
674 goodwrite *= writetab(outf, lengthtab, ysize*zsize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000675 if (goodwrite) {
676 Py_INCREF(Py_None);
677 retval = Py_None;
678 } else
679 PyErr_SetString(ImgfileError, "not enough space for image");
680
681 finally:
682 fclose(outf);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000683 free(starttab);
684 free(lengthtab);
685 free(rlebuf);
686 free(lumbuf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000687 return retval;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000688}
689
690/* static utility functions for longstoimage */
691
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000692static void
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000693lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000694{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000695 lumptr += CHANOFFSET(0);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000696 while (n--) {
697 *lumptr = ILUM(rgbptr[OFFSET_R],
698 rgbptr[OFFSET_G],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000699 rgbptr[OFFSET_B]);
700 lumptr += 4;
701 rgbptr += 4;
702 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000703}
704
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000705static int
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000706compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000707{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000708 unsigned char *iptr, *ibufend, *sptr, *optr;
709 short todo, cc;
Guido van Rossum69011961998-04-23 20:23:00 +0000710 Py_Int32 count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000711
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000712 lbuf += z;
713 iptr = lbuf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000714 ibufend = iptr + cnt * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000715 optr = rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000716
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000717 while(iptr < ibufend) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000718 sptr = iptr;
719 iptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000720 while ((iptr<ibufend) &&
721 ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
722 {
723 iptr += 4;
724 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000725 iptr -= 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000726 count = (iptr - sptr) / 4;
727 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000728 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000729 count -= todo;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000730 *optr++ = 0x80 | todo;
731 while (todo > 8) {
732 optr[0] = sptr[0 * 4];
733 optr[1] = sptr[1 * 4];
734 optr[2] = sptr[2 * 4];
735 optr[3] = sptr[3 * 4];
736 optr[4] = sptr[4 * 4];
737 optr[5] = sptr[5 * 4];
738 optr[6] = sptr[6 * 4];
739 optr[7] = sptr[7 * 4];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000740 optr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000741 sptr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000742 todo -= 8;
743 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000744 while (todo--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000745 *optr++ = *sptr;
746 sptr += 4;
747 }
748 }
749 sptr = iptr;
750 cc = *iptr;
751 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000752 while ((iptr < ibufend) && (*iptr == cc))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000753 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000754 count = (iptr - sptr) / 4;
755 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000756 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000757 count -= todo;
Guido van Rossum7844e381997-04-11 20:44:04 +0000758 *optr++ = (unsigned char) todo;
759 *optr++ = (unsigned char) cc;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000760 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000761 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000762 *optr++ = 0;
763 return optr - (unsigned char *)rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000764}
765
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000766static PyObject *
Peter Schneider-Kamp0659b4a2000-07-10 10:49:30 +0000767ttob(PyObject *self, PyObject *args)
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000768{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000769 int order, oldorder;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000770
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000771 if (!PyArg_ParseTuple(args, "i:ttob", &order))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000772 return NULL;
773 oldorder = reverse_order;
774 reverse_order = order;
775 return PyInt_FromLong(oldorder);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000776}
777
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000778static PyMethodDef
779rgbimg_methods[] = {
Neal Norwitzba3a16c2002-03-31 15:27:00 +0000780 {"sizeofimage", sizeofimage, METH_VARARGS},
781 {"longimagedata", longimagedata, METH_VARARGS},
782 {"longstoimage", longstoimage, METH_VARARGS},
783 {"ttob", ttob, METH_VARARGS},
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000784 {NULL, NULL} /* sentinel */
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000785};
786
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000787
Mark Hammondfe51c6d2002-08-02 02:27:13 +0000788PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +0000789initrgbimg(void)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000790{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000791 PyObject *m, *d;
792 m = Py_InitModule("rgbimg", rgbimg_methods);
Neal Norwitz1ac754f2006-01-19 06:09:39 +0000793 if (m == NULL)
794 return;
Georg Brandlb86a54f2006-02-17 11:29:04 +0000795
796 if (PyErr_Warn(PyExc_DeprecationWarning,
797 "the rgbimg module is deprecated"))
798 return;
799
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000800 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000801 ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
802 if (ImgfileError != NULL)
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000803 PyDict_SetItemString(d, "error", ImgfileError);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000804}