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