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