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