blob: d0956cb35d8862204968a803d1479b98dac60d0d [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
122addlongimgtag(dptr, xsize, ysize)
Guido van Rossum69011961998-04-23 20:23:00 +0000123 Py_UInt32 *dptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000124 int xsize, ysize;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000125{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000126 dptr = dptr + (xsize * ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000127 dptr[0] = 0x12345678;
128 dptr[1] = 0x59493333;
129 dptr[2] = 0x69434222;
130 dptr[3] = xsize;
131 dptr[4] = ysize;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000132}
133#endif
134
135/*
136 * byte order independent read/write of shorts and longs.
137 *
138 */
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000139static unsigned short
140getshort(inf)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000141 FILE *inf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000142{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000143 unsigned char buf[2];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000144
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000145 fread(buf, 2, 1, inf);
146 return (buf[0] << 8) + (buf[1] << 0);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000147}
148
Guido van Rossum69011961998-04-23 20:23:00 +0000149static Py_UInt32
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000150getlong(inf)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000151 FILE *inf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000152{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000153 unsigned char buf[4];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000154
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000155 fread(buf, 4, 1, inf);
156 return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000157}
158
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000159static void
160putshort(outf, val)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000161 FILE *outf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000162 unsigned short val;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000163{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000164 unsigned char buf[2];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000165
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000166 buf[0] = (val >> 8);
167 buf[1] = (val >> 0);
168 fwrite(buf, 2, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000169}
170
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000171static int
172putlong(outf, val)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000173 FILE *outf;
Guido van Rossum69011961998-04-23 20:23:00 +0000174 Py_UInt32 val;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000175{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000176 unsigned char buf[4];
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000177
Guido van Rossum7844e381997-04-11 20:44:04 +0000178 buf[0] = (unsigned char) (val >> 24);
179 buf[1] = (unsigned char) (val >> 16);
180 buf[2] = (unsigned char) (val >> 8);
181 buf[3] = (unsigned char) (val >> 0);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000182 return fwrite(buf, 4, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000183}
184
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000185static void
186readheader(inf, image)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000187 FILE *inf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000188 IMAGE *image;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000189{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000190 memset(image ,0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000191 image->imagic = getshort(inf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000192 image->type = getshort(inf);
193 image->dim = getshort(inf);
194 image->xsize = getshort(inf);
195 image->ysize = getshort(inf);
196 image->zsize = getshort(inf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000197}
198
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000199static int
200writeheader(outf, image)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000201 FILE *outf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000202 IMAGE *image;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000203{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000204 IMAGE t;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000205
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000206 memset(&t, 0, sizeof(IMAGE));
207 fwrite(&t, sizeof(IMAGE), 1, outf);
208 fseek(outf, 0, SEEK_SET);
209 putshort(outf, image->imagic);
210 putshort(outf, image->type);
211 putshort(outf, image->dim);
212 putshort(outf, image->xsize);
213 putshort(outf, image->ysize);
214 putshort(outf, image->zsize);
215 putlong(outf, image->min);
216 putlong(outf, image->max);
217 putlong(outf, 0);
218 return fwrite("no name", 8, 1, outf);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000219}
220
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000221static int
222writetab(outf, tab, len)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000223 FILE *outf;
Guido van Rossum69011961998-04-23 20:23:00 +0000224 /*unsigned*/ Py_Int32 *tab;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000225 int len;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000226{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000227 int r = 0;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000228
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000229 while(len) {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000230 r = putlong(outf, *tab++);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000231 len--;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000232 }
233 return r;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000234}
235
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000236static void
237readtab(inf, tab, len)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000238 FILE *inf;
Guido van Rossum69011961998-04-23 20:23:00 +0000239 /*unsigned*/ Py_Int32 *tab;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000240 int len;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000241{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000242 while(len) {
243 *tab++ = getlong(inf);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000244 len--;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000245 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000246}
247
248/*
249 * sizeofimage -
250 * return the xsize and ysize of an iris image file.
251 *
252 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000253static PyObject *
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000254sizeofimage(self, args)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000255 PyObject *self, *args;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000256{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000257 char *name;
258 IMAGE image;
259 FILE *inf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000260
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000261 if (!PyArg_Parse(args, "s", &name))
262 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000263
Guido van Rossumfd16d941997-04-11 19:12:20 +0000264 inf = fopen(name, "rb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000265 if (!inf) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000266 PyErr_SetString(ImgfileError, "can't open image file");
267 return NULL;
268 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000269 readheader(inf, &image);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000270 fclose(inf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000271 if (image.imagic != IMAGIC) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000272 PyErr_SetString(ImgfileError,
273 "bad magic number in image file");
274 return NULL;
275 }
276 return Py_BuildValue("(ii)", image.xsize, image.ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000277}
278
279/*
280 * longimagedata -
281 * read in a B/W RGB or RGBA iris image file and return a
282 * pointer to an array of longs.
283 *
284 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000285static PyObject *
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000286longimagedata(self, args)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000287 PyObject *self, *args;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000288{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000289 char *name;
290 unsigned char *base, *lptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000291 unsigned char *rledat = NULL, *verdat = NULL;
Guido van Rossum69011961998-04-23 20:23:00 +0000292 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000293 FILE *inf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000294 IMAGE image;
295 int y, z, tablen;
296 int xsize, ysize, zsize;
297 int bpp, rle, cur, badorder;
298 int rlebuflen;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000299 PyObject *rv = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000300
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000301 if (!PyArg_Parse(args, "s", &name))
302 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000303
Guido van Rossum56809061997-02-21 15:19:03 +0000304 inf = fopen(name,"rb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000305 if (!inf) {
306 PyErr_SetString(ImgfileError, "can't open image file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000307 return NULL;
308 }
309 readheader(inf,&image);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000310 if (image.imagic != IMAGIC) {
311 PyErr_SetString(ImgfileError,
312 "bad magic number in image file");
313 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000314 }
315 rle = ISRLE(image.type);
316 bpp = BPP(image.type);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000317 if (bpp != 1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000318 PyErr_SetString(ImgfileError,
319 "image must have 1 byte per pix chan");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000320 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000321 }
322 xsize = image.xsize;
323 ysize = image.ysize;
324 zsize = image.zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000325 if (rle) {
Guido van Rossum69011961998-04-23 20:23:00 +0000326 tablen = ysize * zsize * sizeof(Py_Int32);
327 starttab = (Py_Int32 *)malloc(tablen);
328 lengthtab = (Py_Int32 *)malloc(tablen);
Guido van Rossum7844e381997-04-11 20:44:04 +0000329 rlebuflen = (int) (1.05 * xsize +10);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000330 rledat = (unsigned char *)malloc(rlebuflen);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000331 if (!starttab || !lengthtab || !rledat) {
332 PyErr_NoMemory();
333 goto finally;
334 }
335
336 fseek(inf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000337 readtab(inf, starttab, ysize*zsize);
338 readtab(inf, lengthtab, ysize*zsize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000339
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000340 /* check data order */
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000341 cur = 0;
342 badorder = 0;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000343 for(y = 0; y < ysize; y++) {
344 for(z = 0; z < zsize; z++) {
345 if (starttab[y + z * ysize] < cur) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000346 badorder = 1;
347 break;
348 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000349 cur = starttab[y +z * ysize];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000350 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000351 if (badorder)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000352 break;
353 }
354
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000355 fseek(inf, 512 + 2 * tablen, SEEK_SET);
356 cur = 512 + 2 * tablen;
357 rv = PyString_FromStringAndSize((char *)NULL,
Guido van Rossum69011961998-04-23 20:23:00 +0000358 (xsize * ysize + TAGLEN) * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000359 if (rv == NULL)
360 goto finally;
361
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000362 base = (unsigned char *) PyString_AsString(rv);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000363#ifdef ADD_TAGS
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000364 addlongimgtag(base,xsize,ysize);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000365#endif
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000366 if (badorder) {
367 for (z = 0; z < zsize; z++) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000368 lptr = base;
369 if (reverse_order)
370 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000371 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000372 for (y = 0; y < ysize; y++) {
373 int idx = y + z * ysize;
374 if (cur != starttab[idx]) {
375 fseek(inf,starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000376 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000377 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000378 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000379 if (lengthtab[idx] > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000380 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000381 "rlebuf is too small");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000382 Py_DECREF(rv);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000383 rv = NULL;
384 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000385 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000386 fread(rledat, lengthtab[idx], 1, inf);
387 cur += lengthtab[idx];
388 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000389 if (reverse_order)
390 lptr -= xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000391 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000392 else
393 lptr += xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000394 * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000395 }
396 }
397 } else {
398 lptr = base;
399 if (reverse_order)
400 lptr += (ysize - 1) * xsize
Guido van Rossum69011961998-04-23 20:23:00 +0000401 * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000402 for (y = 0; y < ysize; y++) {
403 for(z = 0; z < zsize; z++) {
404 int idx = y + z * ysize;
405 if (cur != starttab[idx]) {
406 fseek(inf, starttab[idx],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000407 SEEK_SET);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000408 cur = starttab[idx];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000409 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000410 fread(rledat, lengthtab[idx], 1, inf);
411 cur += lengthtab[idx];
412 expandrow(lptr, rledat, 3-z);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000413 }
414 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000415 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000416 else
Guido van Rossum69011961998-04-23 20:23:00 +0000417 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000418 }
419 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000420 if (zsize == 3)
421 setalpha(base, xsize * ysize);
422 else if (zsize < 3)
Guido van Rossum69011961998-04-23 20:23:00 +0000423 copybw((Py_Int32 *) base, xsize * ysize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000424 }
425 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000426 rv = PyString_FromStringAndSize((char *) 0,
Guido van Rossum69011961998-04-23 20:23:00 +0000427 (xsize*ysize+TAGLEN)*sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000428 if (rv == NULL)
429 goto finally;
430
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000431 base = (unsigned char *) PyString_AsString(rv);
432#ifdef ADD_TAGS
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000433 addlongimgtag(base, xsize, ysize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000434#endif
435 verdat = (unsigned char *)malloc(xsize);
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:
457 free(starttab);
458 free(lengthtab);
459 free(rledat);
460 free(verdat);
461 fclose(inf);
462 return rv;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000463}
464
465/* static utility functions for longimagedata */
466
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000467static void
468interleaverow(lptr, cptr, z, n)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000469 unsigned char *lptr, *cptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000470 int z, n;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000471{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000472 lptr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000473 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000474 *lptr = *cptr++;
475 lptr += 4;
476 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000477}
478
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000479static void
480copybw(lptr, n)
Guido van Rossum69011961998-04-23 20:23:00 +0000481 Py_Int32 *lptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000482 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
503setalpha(lptr, n)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000504 unsigned char *lptr;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000505{
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000506 while (n >= 8) {
507 lptr[0 * 4] = 0xff;
508 lptr[1 * 4] = 0xff;
509 lptr[2 * 4] = 0xff;
510 lptr[3 * 4] = 0xff;
511 lptr[4 * 4] = 0xff;
512 lptr[5 * 4] = 0xff;
513 lptr[6 * 4] = 0xff;
514 lptr[7 * 4] = 0xff;
515 lptr += 4 * 8;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000516 n -= 8;
517 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000518 while (n--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000519 *lptr = 0xff;
520 lptr += 4;
521 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000522}
523
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000524static void
525expandrow(optr, iptr, z)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000526 unsigned char *optr, *iptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000527 int z;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000528{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000529 unsigned char pixel, count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000530
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000531 optr += z;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000532 while (1) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000533 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000534 if (!(count = (pixel & 0x7f)))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000535 return;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000536 if (pixel & 0x80) {
537 while (count >= 8) {
538 optr[0 * 4] = iptr[0];
539 optr[1 * 4] = iptr[1];
540 optr[2 * 4] = iptr[2];
541 optr[3 * 4] = iptr[3];
542 optr[4 * 4] = iptr[4];
543 optr[5 * 4] = iptr[5];
544 optr[6 * 4] = iptr[6];
545 optr[7 * 4] = iptr[7];
546 optr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000547 iptr += 8;
548 count -= 8;
549 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000550 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000551 *optr = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000552 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000553 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000554 }
555 else {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000556 pixel = *iptr++;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000557 while (count >= 8) {
558 optr[0 * 4] = pixel;
559 optr[1 * 4] = pixel;
560 optr[2 * 4] = pixel;
561 optr[3 * 4] = pixel;
562 optr[4 * 4] = pixel;
563 optr[5 * 4] = pixel;
564 optr[6 * 4] = pixel;
565 optr[7 * 4] = pixel;
566 optr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000567 count -= 8;
568 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000569 while (count--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000570 *optr = pixel;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000571 optr += 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000572 }
573 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000574 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000575}
576
577/*
578 * longstoimage -
579 * copy an array of longs to an iris image file. Each long
580 * represents one pixel. xsize and ysize specify the dimensions of
581 * the pixel array. zsize specifies what kind of image file to
582 * write out. if zsize is 1, the luminance of the pixels are
583 * calculated, and a sinlge channel black and white image is saved.
584 * If zsize is 3, an RGB image file is saved. If zsize is 4, an
585 * RGBA image file is saved.
586 *
587 */
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000588static PyObject *
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000589longstoimage(self, args)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000590 PyObject *self, *args;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000591{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000592 unsigned char *lptr;
593 char *name;
594 int xsize, ysize, zsize;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000595 FILE *outf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000596 IMAGE image;
597 int tablen, y, z, pos, len;
Guido van Rossum69011961998-04-23 20:23:00 +0000598 Py_Int32 *starttab = NULL, *lengthtab = NULL;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000599 unsigned char *rlebuf = NULL;
600 unsigned char *lumbuf = NULL;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000601 int rlebuflen, goodwrite;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000602 PyObject *retval = NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000603
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000604 if (!PyArg_Parse(args, "(s#iiis)", &lptr, &len, &xsize, &ysize, &zsize,
605 &name))
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000606 return NULL;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000607
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000608 goodwrite = 1;
Guido van Rossum56809061997-02-21 15:19:03 +0000609 outf = fopen(name, "wb");
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000610 if (!outf) {
611 PyErr_SetString(ImgfileError, "can't open output file");
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000612 return NULL;
613 }
Guido van Rossum69011961998-04-23 20:23:00 +0000614 tablen = ysize * zsize * sizeof(Py_Int32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000615
Guido van Rossum69011961998-04-23 20:23:00 +0000616 starttab = (Py_Int32 *)malloc(tablen);
617 lengthtab = (Py_Int32 *)malloc(tablen);
Guido van Rossum7844e381997-04-11 20:44:04 +0000618 rlebuflen = (int) (1.05 * xsize + 10);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000619 rlebuf = (unsigned char *)malloc(rlebuflen);
Guido van Rossum69011961998-04-23 20:23:00 +0000620 lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32));
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000621 if (!starttab || !lengthtab || !rlebuf || !lumbuf) {
622 PyErr_NoMemory();
623 goto finally;
624 }
625
626 memset(&image, 0, sizeof(IMAGE));
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000627 image.imagic = IMAGIC;
628 image.type = RLE(1);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000629 if (zsize>1)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000630 image.dim = 3;
631 else
632 image.dim = 2;
633 image.xsize = xsize;
634 image.ysize = ysize;
635 image.zsize = zsize;
636 image.min = 0;
637 image.max = 255;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000638 goodwrite *= writeheader(outf, &image);
639 pos = 512 + 2 * tablen;
640 fseek(outf, pos, SEEK_SET);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000641 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000642 lptr += (ysize - 1) * xsize * sizeof(Py_UInt32);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000643 for (y = 0; y < ysize; y++) {
644 for (z = 0; z < zsize; z++) {
645 if (zsize == 1) {
646 lumrow(lptr, lumbuf, xsize);
647 len = compressrow(lumbuf, rlebuf,
648 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000649 } else {
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000650 len = compressrow(lptr, rlebuf,
651 CHANOFFSET(z), xsize);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000652 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000653 if(len > rlebuflen) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000654 PyErr_SetString(ImgfileError,
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000655 "rlebuf is too small");
656 goto finally;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000657 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000658 goodwrite *= fwrite(rlebuf, len, 1, outf);
659 starttab[y + z * ysize] = pos;
660 lengthtab[y + z * ysize] = len;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000661 pos += len;
662 }
663 if (reverse_order)
Guido van Rossum69011961998-04-23 20:23:00 +0000664 lptr -= xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000665 else
Guido van Rossum69011961998-04-23 20:23:00 +0000666 lptr += xsize * sizeof(Py_UInt32);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000667 }
668
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000669 fseek(outf, 512, SEEK_SET);
Guido van Rossum0b82fe71997-05-22 20:24:07 +0000670 goodwrite *= writetab(outf, starttab, ysize*zsize);
671 goodwrite *= writetab(outf, lengthtab, ysize*zsize);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000672 if (goodwrite) {
673 Py_INCREF(Py_None);
674 retval = Py_None;
675 } else
676 PyErr_SetString(ImgfileError, "not enough space for image");
677
678 finally:
679 fclose(outf);
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000680 free(starttab);
681 free(lengthtab);
682 free(rlebuf);
683 free(lumbuf);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000684 return retval;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000685}
686
687/* static utility functions for longstoimage */
688
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000689static void
690lumrow(rgbptr, lumptr, n)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000691 unsigned char *rgbptr, *lumptr;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000692 int n;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000693{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000694 lumptr += CHANOFFSET(0);
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000695 while (n--) {
696 *lumptr = ILUM(rgbptr[OFFSET_R],
697 rgbptr[OFFSET_G],
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000698 rgbptr[OFFSET_B]);
699 lumptr += 4;
700 rgbptr += 4;
701 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000702}
703
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000704static int
705compressrow(lbuf, rlebuf, z, cnt)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000706 unsigned char *lbuf, *rlebuf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000707 int z, cnt;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000708{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000709 unsigned char *iptr, *ibufend, *sptr, *optr;
710 short todo, cc;
Guido van Rossum69011961998-04-23 20:23:00 +0000711 Py_Int32 count;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000712
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000713 lbuf += z;
714 iptr = lbuf;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000715 ibufend = iptr + cnt * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000716 optr = rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000717
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000718 while(iptr < ibufend) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000719 sptr = iptr;
720 iptr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000721 while ((iptr<ibufend) &&
722 ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0])))
723 {
724 iptr += 4;
725 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000726 iptr -= 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000727 count = (iptr - sptr) / 4;
728 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000729 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000730 count -= todo;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000731 *optr++ = 0x80 | todo;
732 while (todo > 8) {
733 optr[0] = sptr[0 * 4];
734 optr[1] = sptr[1 * 4];
735 optr[2] = sptr[2 * 4];
736 optr[3] = sptr[3 * 4];
737 optr[4] = sptr[4 * 4];
738 optr[5] = sptr[5 * 4];
739 optr[6] = sptr[6 * 4];
740 optr[7] = sptr[7 * 4];
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000741 optr += 8;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000742 sptr += 8 * 4;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000743 todo -= 8;
744 }
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000745 while (todo--) {
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000746 *optr++ = *sptr;
747 sptr += 4;
748 }
749 }
750 sptr = iptr;
751 cc = *iptr;
752 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000753 while ((iptr < ibufend) && (*iptr == cc))
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000754 iptr += 4;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000755 count = (iptr - sptr) / 4;
756 while (count) {
Guido van Rossum7844e381997-04-11 20:44:04 +0000757 todo = count > 126 ? 126 : (short)count;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000758 count -= todo;
Guido van Rossum7844e381997-04-11 20:44:04 +0000759 *optr++ = (unsigned char) todo;
760 *optr++ = (unsigned char) cc;
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000761 }
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000762 }
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000763 *optr++ = 0;
764 return optr - (unsigned char *)rlebuf;
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000765}
766
Barry Warsaw7bd9fbd1996-12-11 21:33:16 +0000767static PyObject *
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000768ttob(self, args)
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000769 PyObject *self;
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000770 PyObject *args;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000771{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000772 int order, oldorder;
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000773
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000774 if (!PyArg_Parse(args, "i", &order))
775 return NULL;
776 oldorder = reverse_order;
777 reverse_order = order;
778 return PyInt_FromLong(oldorder);
Sjoerd Mullender0d2d3971993-12-24 14:51:14 +0000779}
780
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000781static PyMethodDef
782rgbimg_methods[] = {
783 {"sizeofimage", sizeofimage},
784 {"longimagedata", longimagedata},
785 {"longstoimage", longstoimage},
786 {"ttob", ttob},
787 {NULL, NULL} /* sentinel */
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000788};
789
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000790
Guido van Rossum3886bb61998-12-04 18:50:17 +0000791DL_EXPORT(void)
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000792initrgbimg()
793{
Roger E. Masse5b0eba31997-01-03 18:51:01 +0000794 PyObject *m, *d;
795 m = Py_InitModule("rgbimg", rgbimg_methods);
796 d = PyModule_GetDict(m);
Guido van Rossum0cb96de1997-10-01 04:29:29 +0000797 ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL);
798 if (ImgfileError != NULL)
Barry Warsaw2dc8c2c1997-01-09 22:29:12 +0000799 PyDict_SetItemString(d, "error", ImgfileError);
Sjoerd Mullenderc4f169c1993-12-21 17:06:12 +0000800}