Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 1 | /* |
| 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 Warsaw | 7bd9fbd | 1996-12-11 21:33:16 +0000 | [diff] [blame] | 17 | #include "Python.h" |
| 18 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 19 | #if SIZEOF_INT == 4 |
| 20 | typedef int Py_Int32; |
| 21 | typedef unsigned int Py_UInt32; |
| 22 | #else |
| 23 | #if SIZEOF_LONG == 4 |
| 24 | typedef long Py_Int32; |
| 25 | typedef unsigned long Py_UInt32; |
| 26 | #else |
| 27 | #error "No 4-byte integral type" |
| 28 | #endif |
| 29 | #endif |
| 30 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 31 | #ifdef HAVE_UNISTD_H |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 32 | #include <unistd.h> |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 33 | #endif |
Sjoerd Mullender | 92fa23f | 1993-12-24 10:05:51 +0000 | [diff] [blame] | 34 | #include <string.h> |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 35 | |
| 36 | /* |
| 37 | * from image.h |
| 38 | * |
| 39 | */ |
| 40 | typedef struct { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 41 | 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 Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 47 | Py_UInt32 min; |
| 48 | Py_UInt32 max; |
| 49 | Py_UInt32 wastebytes; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 50 | char name[80]; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 51 | Py_UInt32 colormap; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 52 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 53 | Py_Int32 file; /* stuff used in core only */ |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 54 | 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 Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 63 | 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 Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 67 | } 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 Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 98 | static void expandrow(unsigned char *, unsigned char *, int); |
| 99 | static void setalpha(unsigned char *, int); |
| 100 | static void copybw(Py_Int32 *, int); |
| 101 | static void interleaverow(unsigned char*, unsigned char*, int, int); |
| 102 | static int compressrow(unsigned char *, unsigned char *, int, int); |
| 103 | static void lumrow(unsigned char *, unsigned char *, int); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 104 | |
| 105 | #ifdef ADD_TAGS |
| 106 | #define TAGLEN (5) |
| 107 | #else |
| 108 | #define TAGLEN (0) |
| 109 | #endif |
| 110 | |
Barry Warsaw | 7bd9fbd | 1996-12-11 21:33:16 +0000 | [diff] [blame] | 111 | static PyObject *ImgfileError; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 112 | |
Sjoerd Mullender | 0d2d397 | 1993-12-24 14:51:14 +0000 | [diff] [blame] | 113 | static int reverse_order; |
| 114 | |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 115 | #ifdef ADD_TAGS |
| 116 | /* |
| 117 | * addlongimgtag - |
| 118 | * this is used to extract image data from core dumps. |
| 119 | * |
| 120 | */ |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 121 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 122 | addlongimgtag(Py_UInt32 *dptr, int xsize, int ysize) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 123 | { |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 124 | dptr = dptr + (xsize * ysize); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 125 | dptr[0] = 0x12345678; |
| 126 | dptr[1] = 0x59493333; |
| 127 | dptr[2] = 0x69434222; |
| 128 | dptr[3] = xsize; |
| 129 | dptr[4] = ysize; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 130 | } |
| 131 | #endif |
| 132 | |
| 133 | /* |
| 134 | * byte order independent read/write of shorts and longs. |
| 135 | * |
| 136 | */ |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 137 | static unsigned short |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 138 | getshort(FILE *inf) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 139 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 140 | unsigned char buf[2]; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 141 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 142 | fread(buf, 2, 1, inf); |
| 143 | return (buf[0] << 8) + (buf[1] << 0); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 144 | } |
| 145 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 146 | static Py_UInt32 |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 147 | getlong(FILE *inf) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 148 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 149 | unsigned char buf[4]; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 150 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 151 | fread(buf, 4, 1, inf); |
| 152 | return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 155 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 156 | putshort(FILE *outf, unsigned short val) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 157 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 158 | unsigned char buf[2]; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 159 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 160 | buf[0] = (val >> 8); |
| 161 | buf[1] = (val >> 0); |
| 162 | fwrite(buf, 2, 1, outf); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 163 | } |
| 164 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 165 | static int |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 166 | putlong(FILE *outf, Py_UInt32 val) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 167 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 168 | unsigned char buf[4]; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 169 | |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 170 | 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 Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 174 | return fwrite(buf, 4, 1, outf); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 175 | } |
| 176 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 177 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 178 | readheader(FILE *inf, IMAGE *image) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 179 | { |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 180 | memset(image ,0, sizeof(IMAGE)); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 181 | image->imagic = getshort(inf); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 182 | 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 Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 187 | } |
| 188 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 189 | static int |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 190 | writeheader(FILE *outf, IMAGE *image) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 191 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 192 | IMAGE t; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 193 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 194 | 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 Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 209 | static int |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 210 | writetab(FILE *outf, /*unsigned*/ Py_Int32 *tab, int len) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 211 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 212 | int r = 0; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 213 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 214 | while(len) { |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 215 | r = putlong(outf, *tab++); |
Guido van Rossum | 0b82fe7 | 1997-05-22 20:24:07 +0000 | [diff] [blame] | 216 | len--; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 217 | } |
| 218 | return r; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 221 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 222 | readtab(FILE *inf, /*unsigned*/ Py_Int32 *tab, int len) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 223 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 224 | while(len) { |
| 225 | *tab++ = getlong(inf); |
Guido van Rossum | 0b82fe7 | 1997-05-22 20:24:07 +0000 | [diff] [blame] | 226 | len--; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 227 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | /* |
| 231 | * sizeofimage - |
| 232 | * return the xsize and ysize of an iris image file. |
| 233 | * |
| 234 | */ |
Barry Warsaw | 7bd9fbd | 1996-12-11 21:33:16 +0000 | [diff] [blame] | 235 | static PyObject * |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 236 | sizeofimage(PyObject *self, PyObject *args) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 237 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 238 | char *name; |
| 239 | IMAGE image; |
| 240 | FILE *inf; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 241 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 242 | if (!PyArg_Parse(args, "s", &name)) |
| 243 | return NULL; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 244 | |
Guido van Rossum | fd16d94 | 1997-04-11 19:12:20 +0000 | [diff] [blame] | 245 | inf = fopen(name, "rb"); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 246 | if (!inf) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 247 | PyErr_SetString(ImgfileError, "can't open image file"); |
| 248 | return NULL; |
| 249 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 250 | readheader(inf, &image); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 251 | fclose(inf); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 252 | if (image.imagic != IMAGIC) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 253 | PyErr_SetString(ImgfileError, |
| 254 | "bad magic number in image file"); |
| 255 | return NULL; |
| 256 | } |
| 257 | return Py_BuildValue("(ii)", image.xsize, image.ysize); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 258 | } |
| 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 Warsaw | 7bd9fbd | 1996-12-11 21:33:16 +0000 | [diff] [blame] | 266 | static PyObject * |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 267 | longimagedata(PyObject *self, PyObject *args) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 268 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 269 | char *name; |
| 270 | unsigned char *base, *lptr; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 271 | unsigned char *rledat = NULL, *verdat = NULL; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 272 | Py_Int32 *starttab = NULL, *lengthtab = NULL; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 273 | FILE *inf = NULL; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 274 | IMAGE image; |
| 275 | int y, z, tablen; |
| 276 | int xsize, ysize, zsize; |
| 277 | int bpp, rle, cur, badorder; |
| 278 | int rlebuflen; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 279 | PyObject *rv = NULL; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 280 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 281 | if (!PyArg_Parse(args, "s", &name)) |
| 282 | return NULL; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 283 | |
Guido van Rossum | 5680906 | 1997-02-21 15:19:03 +0000 | [diff] [blame] | 284 | inf = fopen(name,"rb"); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 285 | if (!inf) { |
| 286 | PyErr_SetString(ImgfileError, "can't open image file"); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 287 | return NULL; |
| 288 | } |
| 289 | readheader(inf,&image); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 290 | if (image.imagic != IMAGIC) { |
| 291 | PyErr_SetString(ImgfileError, |
| 292 | "bad magic number in image file"); |
| 293 | goto finally; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 294 | } |
| 295 | rle = ISRLE(image.type); |
| 296 | bpp = BPP(image.type); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 297 | if (bpp != 1) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 298 | PyErr_SetString(ImgfileError, |
| 299 | "image must have 1 byte per pix chan"); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 300 | goto finally; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 301 | } |
| 302 | xsize = image.xsize; |
| 303 | ysize = image.ysize; |
| 304 | zsize = image.zsize; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 305 | if (rle) { |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 306 | tablen = ysize * zsize * sizeof(Py_Int32); |
| 307 | starttab = (Py_Int32 *)malloc(tablen); |
| 308 | lengthtab = (Py_Int32 *)malloc(tablen); |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 309 | rlebuflen = (int) (1.05 * xsize +10); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 310 | rledat = (unsigned char *)malloc(rlebuflen); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 311 | if (!starttab || !lengthtab || !rledat) { |
| 312 | PyErr_NoMemory(); |
| 313 | goto finally; |
| 314 | } |
| 315 | |
| 316 | fseek(inf, 512, SEEK_SET); |
Guido van Rossum | 0b82fe7 | 1997-05-22 20:24:07 +0000 | [diff] [blame] | 317 | readtab(inf, starttab, ysize*zsize); |
| 318 | readtab(inf, lengthtab, ysize*zsize); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 319 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 320 | /* check data order */ |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 321 | cur = 0; |
| 322 | badorder = 0; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 323 | for(y = 0; y < ysize; y++) { |
| 324 | for(z = 0; z < zsize; z++) { |
| 325 | if (starttab[y + z * ysize] < cur) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 326 | badorder = 1; |
| 327 | break; |
| 328 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 329 | cur = starttab[y +z * ysize]; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 330 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 331 | if (badorder) |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 332 | break; |
| 333 | } |
| 334 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 335 | fseek(inf, 512 + 2 * tablen, SEEK_SET); |
| 336 | cur = 512 + 2 * tablen; |
| 337 | rv = PyString_FromStringAndSize((char *)NULL, |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 338 | (xsize * ysize + TAGLEN) * sizeof(Py_Int32)); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 339 | if (rv == NULL) |
| 340 | goto finally; |
| 341 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 342 | base = (unsigned char *) PyString_AsString(rv); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 343 | #ifdef ADD_TAGS |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 344 | addlongimgtag(base,xsize,ysize); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 345 | #endif |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 346 | if (badorder) { |
| 347 | for (z = 0; z < zsize; z++) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 348 | lptr = base; |
| 349 | if (reverse_order) |
| 350 | lptr += (ysize - 1) * xsize |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 351 | * sizeof(Py_UInt32); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 352 | for (y = 0; y < ysize; y++) { |
| 353 | int idx = y + z * ysize; |
| 354 | if (cur != starttab[idx]) { |
| 355 | fseek(inf,starttab[idx], |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 356 | SEEK_SET); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 357 | cur = starttab[idx]; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 358 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 359 | if (lengthtab[idx] > rlebuflen) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 360 | PyErr_SetString(ImgfileError, |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 361 | "rlebuf is too small"); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 362 | Py_DECREF(rv); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 363 | rv = NULL; |
| 364 | goto finally; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 365 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 366 | fread(rledat, lengthtab[idx], 1, inf); |
| 367 | cur += lengthtab[idx]; |
| 368 | expandrow(lptr, rledat, 3-z); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 369 | if (reverse_order) |
| 370 | lptr -= xsize |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 371 | * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 372 | else |
| 373 | lptr += xsize |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 374 | * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | } else { |
| 378 | lptr = base; |
| 379 | if (reverse_order) |
| 380 | lptr += (ysize - 1) * xsize |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 381 | * sizeof(Py_UInt32); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 382 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 387 | SEEK_SET); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 388 | cur = starttab[idx]; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 389 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 390 | fread(rledat, lengthtab[idx], 1, inf); |
| 391 | cur += lengthtab[idx]; |
| 392 | expandrow(lptr, rledat, 3-z); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 393 | } |
| 394 | if (reverse_order) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 395 | lptr -= xsize * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 396 | else |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 397 | lptr += xsize * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 398 | } |
| 399 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 400 | if (zsize == 3) |
| 401 | setalpha(base, xsize * ysize); |
| 402 | else if (zsize < 3) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 403 | copybw((Py_Int32 *) base, xsize * ysize); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 404 | } |
| 405 | else { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 406 | rv = PyString_FromStringAndSize((char *) 0, |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 407 | (xsize*ysize+TAGLEN)*sizeof(Py_Int32)); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 408 | if (rv == NULL) |
| 409 | goto finally; |
| 410 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 411 | base = (unsigned char *) PyString_AsString(rv); |
| 412 | #ifdef ADD_TAGS |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 413 | addlongimgtag(base, xsize, ysize); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 414 | #endif |
| 415 | verdat = (unsigned char *)malloc(xsize); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 416 | fseek(inf, 512, SEEK_SET); |
| 417 | for (z = 0; z < zsize; z++) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 418 | lptr = base; |
| 419 | if (reverse_order) |
| 420 | lptr += (ysize - 1) * xsize |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 421 | * sizeof(Py_UInt32); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 422 | for (y = 0; y < ysize; y++) { |
| 423 | fread(verdat, xsize, 1, inf); |
| 424 | interleaverow(lptr, verdat, 3-z, xsize); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 425 | if (reverse_order) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 426 | lptr -= xsize * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 427 | else |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 428 | lptr += xsize * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 429 | } |
| 430 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 431 | if (zsize == 3) |
| 432 | setalpha(base, xsize * ysize); |
| 433 | else if (zsize < 3) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 434 | copybw((Py_Int32 *) base, xsize * ysize); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 435 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 436 | finally: |
| 437 | free(starttab); |
| 438 | free(lengthtab); |
| 439 | free(rledat); |
| 440 | free(verdat); |
| 441 | fclose(inf); |
| 442 | return rv; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 443 | } |
| 444 | |
| 445 | /* static utility functions for longimagedata */ |
| 446 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 447 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 448 | interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 449 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 450 | lptr += z; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 451 | while (n--) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 452 | *lptr = *cptr++; |
| 453 | lptr += 4; |
| 454 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 455 | } |
| 456 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 457 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 458 | copybw(Py_Int32 *lptr, int n) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 459 | { |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 460 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 469 | lptr += 8; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 470 | n -= 8; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 471 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 472 | while (n--) { |
| 473 | *lptr = 0xff000000 + (0x010101 * (*lptr&0xff)); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 474 | lptr++; |
| 475 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 476 | } |
| 477 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 478 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 479 | setalpha(unsigned char *lptr, int n) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 480 | { |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 481 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 491 | n -= 8; |
| 492 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 493 | while (n--) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 494 | *lptr = 0xff; |
| 495 | lptr += 4; |
| 496 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 499 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 500 | expandrow(unsigned char *optr, unsigned char *iptr, int z) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 501 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 502 | unsigned char pixel, count; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 503 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 504 | optr += z; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 505 | while (1) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 506 | pixel = *iptr++; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 507 | if (!(count = (pixel & 0x7f))) |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 508 | return; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 509 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 520 | iptr += 8; |
| 521 | count -= 8; |
| 522 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 523 | while (count--) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 524 | *optr = *iptr++; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 525 | optr += 4; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 526 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 527 | } |
| 528 | else { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 529 | pixel = *iptr++; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 530 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 540 | count -= 8; |
| 541 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 542 | while (count--) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 543 | *optr = pixel; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 544 | optr += 4; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 545 | } |
| 546 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 547 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 548 | } |
| 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 Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 556 | * calculated, and a single channel black and white image is saved. |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 557 | * If zsize is 3, an RGB image file is saved. If zsize is 4, an |
| 558 | * RGBA image file is saved. |
| 559 | * |
| 560 | */ |
Barry Warsaw | 7bd9fbd | 1996-12-11 21:33:16 +0000 | [diff] [blame] | 561 | static PyObject * |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 562 | longstoimage(PyObject *self, PyObject *args) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 563 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 564 | unsigned char *lptr; |
| 565 | char *name; |
| 566 | int xsize, ysize, zsize; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 567 | FILE *outf = NULL; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 568 | IMAGE image; |
| 569 | int tablen, y, z, pos, len; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 570 | Py_Int32 *starttab = NULL, *lengthtab = NULL; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 571 | unsigned char *rlebuf = NULL; |
| 572 | unsigned char *lumbuf = NULL; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 573 | int rlebuflen, goodwrite; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 574 | PyObject *retval = NULL; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 575 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 576 | if (!PyArg_Parse(args, "(s#iiis)", &lptr, &len, &xsize, &ysize, &zsize, |
| 577 | &name)) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 578 | return NULL; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 579 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 580 | goodwrite = 1; |
Guido van Rossum | 5680906 | 1997-02-21 15:19:03 +0000 | [diff] [blame] | 581 | outf = fopen(name, "wb"); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 582 | if (!outf) { |
| 583 | PyErr_SetString(ImgfileError, "can't open output file"); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 584 | return NULL; |
| 585 | } |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 586 | tablen = ysize * zsize * sizeof(Py_Int32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 587 | |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 588 | starttab = (Py_Int32 *)malloc(tablen); |
| 589 | lengthtab = (Py_Int32 *)malloc(tablen); |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 590 | rlebuflen = (int) (1.05 * xsize + 10); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 591 | rlebuf = (unsigned char *)malloc(rlebuflen); |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 592 | lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32)); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 593 | if (!starttab || !lengthtab || !rlebuf || !lumbuf) { |
| 594 | PyErr_NoMemory(); |
| 595 | goto finally; |
| 596 | } |
| 597 | |
| 598 | memset(&image, 0, sizeof(IMAGE)); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 599 | image.imagic = IMAGIC; |
| 600 | image.type = RLE(1); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 601 | if (zsize>1) |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 602 | 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 Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 610 | goodwrite *= writeheader(outf, &image); |
| 611 | pos = 512 + 2 * tablen; |
| 612 | fseek(outf, pos, SEEK_SET); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 613 | if (reverse_order) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 614 | lptr += (ysize - 1) * xsize * sizeof(Py_UInt32); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 615 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 621 | } else { |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 622 | len = compressrow(lptr, rlebuf, |
| 623 | CHANOFFSET(z), xsize); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 624 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 625 | if(len > rlebuflen) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 626 | PyErr_SetString(ImgfileError, |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 627 | "rlebuf is too small"); |
| 628 | goto finally; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 629 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 630 | goodwrite *= fwrite(rlebuf, len, 1, outf); |
| 631 | starttab[y + z * ysize] = pos; |
| 632 | lengthtab[y + z * ysize] = len; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 633 | pos += len; |
| 634 | } |
| 635 | if (reverse_order) |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 636 | lptr -= xsize * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 637 | else |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 638 | lptr += xsize * sizeof(Py_UInt32); |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 641 | fseek(outf, 512, SEEK_SET); |
Guido van Rossum | 0b82fe7 | 1997-05-22 20:24:07 +0000 | [diff] [blame] | 642 | goodwrite *= writetab(outf, starttab, ysize*zsize); |
| 643 | goodwrite *= writetab(outf, lengthtab, ysize*zsize); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 644 | 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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 652 | free(starttab); |
| 653 | free(lengthtab); |
| 654 | free(rlebuf); |
| 655 | free(lumbuf); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 656 | return retval; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 657 | } |
| 658 | |
| 659 | /* static utility functions for longstoimage */ |
| 660 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 661 | static void |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 662 | lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 663 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 664 | lumptr += CHANOFFSET(0); |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 665 | while (n--) { |
| 666 | *lumptr = ILUM(rgbptr[OFFSET_R], |
| 667 | rgbptr[OFFSET_G], |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 668 | rgbptr[OFFSET_B]); |
| 669 | lumptr += 4; |
| 670 | rgbptr += 4; |
| 671 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 672 | } |
| 673 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 674 | static int |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 675 | compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 676 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 677 | unsigned char *iptr, *ibufend, *sptr, *optr; |
| 678 | short todo, cc; |
Guido van Rossum | 6901196 | 1998-04-23 20:23:00 +0000 | [diff] [blame] | 679 | Py_Int32 count; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 680 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 681 | lbuf += z; |
| 682 | iptr = lbuf; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 683 | ibufend = iptr + cnt * 4; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 684 | optr = rlebuf; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 685 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 686 | while(iptr < ibufend) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 687 | sptr = iptr; |
| 688 | iptr += 8; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 689 | while ((iptr<ibufend) && |
| 690 | ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0]))) |
| 691 | { |
| 692 | iptr += 4; |
| 693 | } |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 694 | iptr -= 8; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 695 | count = (iptr - sptr) / 4; |
| 696 | while (count) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 697 | todo = count > 126 ? 126 : (short)count; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 698 | count -= todo; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 699 | *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. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 709 | optr += 8; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 710 | sptr += 8 * 4; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 711 | todo -= 8; |
| 712 | } |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 713 | while (todo--) { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 714 | *optr++ = *sptr; |
| 715 | sptr += 4; |
| 716 | } |
| 717 | } |
| 718 | sptr = iptr; |
| 719 | cc = *iptr; |
| 720 | iptr += 4; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 721 | while ((iptr < ibufend) && (*iptr == cc)) |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 722 | iptr += 4; |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 723 | count = (iptr - sptr) / 4; |
| 724 | while (count) { |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 725 | todo = count > 126 ? 126 : (short)count; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 726 | count -= todo; |
Guido van Rossum | 7844e38 | 1997-04-11 20:44:04 +0000 | [diff] [blame] | 727 | *optr++ = (unsigned char) todo; |
| 728 | *optr++ = (unsigned char) cc; |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 729 | } |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 730 | } |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 731 | *optr++ = 0; |
| 732 | return optr - (unsigned char *)rlebuf; |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Barry Warsaw | 7bd9fbd | 1996-12-11 21:33:16 +0000 | [diff] [blame] | 735 | static PyObject * |
Peter Schneider-Kamp | 0659b4a | 2000-07-10 10:49:30 +0000 | [diff] [blame] | 736 | ttob(PyObject *self, PyObject *args) |
Sjoerd Mullender | 0d2d397 | 1993-12-24 14:51:14 +0000 | [diff] [blame] | 737 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 738 | int order, oldorder; |
Sjoerd Mullender | 0d2d397 | 1993-12-24 14:51:14 +0000 | [diff] [blame] | 739 | |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 740 | if (!PyArg_Parse(args, "i", &order)) |
| 741 | return NULL; |
| 742 | oldorder = reverse_order; |
| 743 | reverse_order = order; |
| 744 | return PyInt_FromLong(oldorder); |
Sjoerd Mullender | 0d2d397 | 1993-12-24 14:51:14 +0000 | [diff] [blame] | 745 | } |
| 746 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 747 | static PyMethodDef |
| 748 | rgbimg_methods[] = { |
| 749 | {"sizeofimage", sizeofimage}, |
| 750 | {"longimagedata", longimagedata}, |
| 751 | {"longstoimage", longstoimage}, |
| 752 | {"ttob", ttob}, |
| 753 | {NULL, NULL} /* sentinel */ |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 754 | }; |
| 755 | |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 756 | |
Guido van Rossum | 3886bb6 | 1998-12-04 18:50:17 +0000 | [diff] [blame] | 757 | DL_EXPORT(void) |
Thomas Wouters | f3f33dc | 2000-07-21 06:00:07 +0000 | [diff] [blame] | 758 | initrgbimg(void) |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 759 | { |
Roger E. Masse | 5b0eba3 | 1997-01-03 18:51:01 +0000 | [diff] [blame] | 760 | PyObject *m, *d; |
| 761 | m = Py_InitModule("rgbimg", rgbimg_methods); |
| 762 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 763 | ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL); |
| 764 | if (ImgfileError != NULL) |
Barry Warsaw | 2dc8c2c | 1997-01-09 22:29:12 +0000 | [diff] [blame] | 765 | PyDict_SetItemString(d, "error", ImgfileError); |
Sjoerd Mullender | c4f169c | 1993-12-21 17:06:12 +0000 | [diff] [blame] | 766 | } |