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