Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 524b588 | 1995-01-04 19:10:35 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 7 | Copyright (c) 2000, BeOpen.com. |
| 8 | Copyright (c) 1995-2000, Corporation for National Research Initiatives. |
| 9 | Copyright (c) 1990-1995, Stichting Mathematisch Centrum. |
| 10 | All rights reserved. |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 11 | |
Guido van Rossum | fd71b9e | 2000-06-30 23:50:40 +0000 | [diff] [blame^] | 12 | See the file "Misc/COPYRIGHT" for information on usage and |
| 13 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 14 | |
| 15 | ******************************************************************/ |
| 16 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 17 | /* IMGFILE module - Interface to sgi libimage */ |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 19 | /* XXX This modele should be done better at some point. It should return |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 20 | ** an object of image file class, and have routines to manipulate these |
| 21 | ** image files in a neater way (so you can get rgb images off a greyscale |
| 22 | ** file, for instance, or do a straight display without having to get the |
| 23 | ** image bits into python, etc). |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 24 | ** |
| 25 | ** Warning: this module is very non-reentrant (esp. the readscaled stuff) |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 26 | */ |
| 27 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 28 | #include "Python.h" |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 29 | |
| 30 | #include <gl/image.h> |
| 31 | |
Jack Jansen | 09cbf9a | 1993-01-19 15:33:13 +0000 | [diff] [blame] | 32 | #include "/usr/people/4Dgifts/iristools/include/izoom.h" |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 33 | |
Guido van Rossum | 665f919 | 1996-12-09 18:49:42 +0000 | [diff] [blame] | 34 | /* Bunch of missing extern decls; keep gcc -Wall happy... */ |
| 35 | extern void i_seterror(); |
| 36 | extern void iclose(); |
| 37 | extern void filterzoom(); |
| 38 | extern void putrow(); |
| 39 | extern void getrow(); |
| 40 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 41 | static PyObject * ImgfileError; /* Exception we raise for various trouble */ |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 42 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 43 | static int top_to_bottom; /* True if we want top-to-bottom images */ |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 45 | /* The image library does not always call the error hander :-(, |
| 46 | therefore we have a global variable indicating that it was called. |
| 47 | It is cleared by imgfile_open(). */ |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 49 | static int error_called; |
| 50 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 51 | |
| 52 | /* The error handler */ |
| 53 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 54 | static void |
| 55 | imgfile_error(str) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 56 | char *str; |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 57 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 58 | PyErr_SetString(ImgfileError, str); |
| 59 | error_called = 1; |
| 60 | return; /* To imglib, which will return a failure indicator */ |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 61 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 63 | |
| 64 | /* Open an image file and return a pointer to it. |
| 65 | Make sure we raise an exception if we fail. */ |
| 66 | |
| 67 | static IMAGE * |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 68 | imgfile_open(fname) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 69 | char *fname; |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 70 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 71 | IMAGE *image; |
| 72 | i_seterror(imgfile_error); |
| 73 | error_called = 0; |
| 74 | errno = 0; |
| 75 | if ( (image = iopen(fname, "r")) == NULL ) { |
| 76 | /* Error may already be set by imgfile_error */ |
| 77 | if ( !error_called ) { |
| 78 | if (errno) |
| 79 | PyErr_SetFromErrno(ImgfileError); |
| 80 | else |
| 81 | PyErr_SetString(ImgfileError, |
| 82 | "Can't open image file"); |
| 83 | } |
| 84 | return NULL; |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 85 | } |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 86 | return image; |
| 87 | } |
| 88 | |
| 89 | static PyObject * |
| 90 | imgfile_ttob(self, args) |
| 91 | PyObject *self; |
| 92 | PyObject *args; |
| 93 | { |
| 94 | int newval; |
| 95 | PyObject *rv; |
| 96 | |
| 97 | if (!PyArg_Parse(args, "i", &newval)) |
| 98 | return NULL; |
| 99 | rv = PyInt_FromLong(top_to_bottom); |
| 100 | top_to_bottom = newval; |
| 101 | return rv; |
| 102 | } |
| 103 | |
| 104 | static PyObject * |
| 105 | imgfile_read(self, args) |
| 106 | PyObject *self; |
| 107 | PyObject *args; |
| 108 | { |
| 109 | char *fname; |
| 110 | PyObject *rv; |
| 111 | int xsize, ysize, zsize; |
| 112 | char *cdatap; |
| 113 | long *idatap; |
| 114 | static short rs[8192], gs[8192], bs[8192]; |
| 115 | int x, y; |
| 116 | IMAGE *image; |
| 117 | int yfirst, ylast, ystep; |
| 118 | |
| 119 | if ( !PyArg_Parse(args, "s", &fname) ) |
| 120 | return NULL; |
| 121 | |
| 122 | if ( (image = imgfile_open(fname)) == NULL ) |
| 123 | return NULL; |
| 124 | |
| 125 | if ( image->colormap != CM_NORMAL ) { |
| 126 | iclose(image); |
| 127 | PyErr_SetString(ImgfileError, |
| 128 | "Can only handle CM_NORMAL images"); |
| 129 | return NULL; |
| 130 | } |
| 131 | if ( BPP(image->type) != 1 ) { |
| 132 | iclose(image); |
| 133 | PyErr_SetString(ImgfileError, |
| 134 | "Can't handle imgfiles with bpp!=1"); |
| 135 | return NULL; |
| 136 | } |
| 137 | xsize = image->xsize; |
| 138 | ysize = image->ysize; |
| 139 | zsize = image->zsize; |
| 140 | if ( zsize != 1 && zsize != 3) { |
| 141 | iclose(image); |
| 142 | PyErr_SetString(ImgfileError, |
| 143 | "Can only handle 1 or 3 byte pixels"); |
| 144 | return NULL; |
| 145 | } |
| 146 | if ( xsize > 8192 ) { |
| 147 | iclose(image); |
| 148 | PyErr_SetString(ImgfileError, |
| 149 | "Can't handle image with > 8192 columns"); |
| 150 | return NULL; |
| 151 | } |
| 152 | |
| 153 | if ( zsize == 3 ) zsize = 4; |
| 154 | rv = PyString_FromStringAndSize((char *)NULL, xsize*ysize*zsize); |
| 155 | if ( rv == NULL ) { |
| 156 | iclose(image); |
| 157 | return NULL; |
| 158 | } |
| 159 | cdatap = PyString_AsString(rv); |
| 160 | idatap = (long *)cdatap; |
| 161 | |
| 162 | if (top_to_bottom) { |
| 163 | yfirst = ysize-1; |
| 164 | ylast = -1; |
| 165 | ystep = -1; |
| 166 | } else { |
| 167 | yfirst = 0; |
| 168 | ylast = ysize; |
| 169 | ystep = 1; |
| 170 | } |
| 171 | for ( y=yfirst; y != ylast && !error_called; y += ystep ) { |
| 172 | if ( zsize == 1 ) { |
| 173 | getrow(image, rs, y, 0); |
| 174 | for(x=0; x<xsize; x++ ) |
| 175 | *cdatap++ = rs[x]; |
| 176 | } else { |
| 177 | getrow(image, rs, y, 0); |
| 178 | getrow(image, gs, y, 1); |
| 179 | getrow(image, bs, y, 2); |
| 180 | for(x=0; x<xsize; x++ ) |
| 181 | *idatap++ = (rs[x] & 0xff) | |
| 182 | ((gs[x] & 0xff)<<8) | |
| 183 | ((bs[x] & 0xff)<<16); |
| 184 | } |
| 185 | } |
| 186 | iclose(image); |
| 187 | if ( error_called ) { |
| 188 | Py_DECREF(rv); |
| 189 | return NULL; |
| 190 | } |
| 191 | return rv; |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Guido van Rossum | 234f942 | 1993-06-17 12:35:49 +0000 | [diff] [blame] | 194 | static IMAGE *glob_image; |
| 195 | static long *glob_datap; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 196 | static int glob_width, glob_z, glob_ysize; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 198 | static void |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 199 | xs_get(buf, y) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 200 | short *buf; |
| 201 | int y; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 202 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 203 | if (top_to_bottom) |
| 204 | getrow(glob_image, buf, (glob_ysize-1-y), glob_z); |
| 205 | else |
| 206 | getrow(glob_image, buf, y, glob_z); |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 207 | } |
| 208 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 209 | static void |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 210 | xs_put_c(buf, y) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 211 | short *buf; |
| 212 | int y; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 213 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 214 | char *datap = (char *)glob_datap + y*glob_width; |
| 215 | int width = glob_width; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 216 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 217 | while ( width-- ) |
| 218 | *datap++ = (*buf++) & 0xff; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 221 | static void |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 222 | xs_put_0(buf, y) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 223 | short *buf; |
| 224 | int y; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 225 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 226 | long *datap = glob_datap + y*glob_width; |
| 227 | int width = glob_width; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 228 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 229 | while ( width-- ) |
| 230 | *datap++ = (*buf++) & 0xff; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 231 | } |
Guido van Rossum | a376cc5 | 1996-12-05 23:43:35 +0000 | [diff] [blame] | 232 | static void |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 233 | xs_put_12(buf, y) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 234 | short *buf; |
| 235 | int y; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 236 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 237 | long *datap = glob_datap + y*glob_width; |
| 238 | int width = glob_width; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 239 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 240 | while ( width-- ) |
| 241 | *datap++ |= ((*buf++) & 0xff) << (glob_z*8); |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static void |
| 245 | xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 246 | IMAGE *image; |
| 247 | int xsize, ysize, zsize; |
| 248 | long *datap; |
| 249 | int xnew, ynew; |
| 250 | int fmode; |
| 251 | double blur; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 252 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 253 | glob_image = image; |
| 254 | glob_datap = datap; |
| 255 | glob_width = xnew; |
| 256 | glob_ysize = ysize; |
| 257 | if ( zsize == 1 ) { |
| 258 | glob_z = 0; |
| 259 | filterzoom(xs_get, xs_put_c, xsize, ysize, |
| 260 | xnew, ynew, fmode, blur); |
| 261 | } else { |
| 262 | glob_z = 0; |
| 263 | filterzoom(xs_get, xs_put_0, xsize, ysize, |
| 264 | xnew, ynew, fmode, blur); |
| 265 | glob_z = 1; |
| 266 | filterzoom(xs_get, xs_put_12, xsize, ysize, |
| 267 | xnew, ynew, fmode, blur); |
| 268 | glob_z = 2; |
| 269 | filterzoom(xs_get, xs_put_12, xsize, ysize, |
| 270 | xnew, ynew, fmode, blur); |
| 271 | } |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 274 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 275 | static PyObject * |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 276 | imgfile_readscaled(self, args) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 277 | PyObject *self; |
| 278 | PyObject *args; |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 279 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 280 | char *fname; |
| 281 | PyObject *rv; |
| 282 | int xsize, ysize, zsize; |
| 283 | char *cdatap; |
| 284 | long *idatap; |
| 285 | static short rs[8192], gs[8192], bs[8192]; |
| 286 | int x, y; |
| 287 | int xwtd, ywtd, xorig, yorig; |
| 288 | float xfac, yfac; |
| 289 | int cnt; |
| 290 | IMAGE *image; |
| 291 | char *filter; |
| 292 | double blur; |
| 293 | int extended; |
| 294 | int fmode = 0; |
| 295 | int yfirst, ylast, ystep; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 296 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 297 | /* |
| 298 | ** Parse args. Funny, since arg 4 and 5 are optional |
| 299 | ** (filter name and blur factor). Also, 4 or 5 arguments indicates |
| 300 | ** extended scale algorithm in stead of simple-minded pixel drop/dup. |
| 301 | */ |
| 302 | extended = 0; |
| 303 | cnt = PyTuple_Size(args); |
| 304 | if ( cnt == 5 ) { |
| 305 | extended = 1; |
| 306 | if ( !PyArg_Parse(args, "(siisd)", |
| 307 | &fname, &xwtd, &ywtd, &filter, &blur) ) |
| 308 | return NULL; |
| 309 | } else if ( cnt == 4 ) { |
| 310 | extended = 1; |
| 311 | if ( !PyArg_Parse(args, "(siis)", |
| 312 | &fname, &xwtd, &ywtd, &filter) ) |
| 313 | return NULL; |
| 314 | blur = 1.0; |
| 315 | } else if ( !PyArg_Parse(args, "(sii)", &fname, &xwtd, &ywtd) ) |
| 316 | return NULL; |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 317 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 318 | /* |
| 319 | ** Check parameters, open file and check type, rows, etc. |
| 320 | */ |
| 321 | if ( extended ) { |
| 322 | if ( strcmp(filter, "impulse") == 0 ) |
| 323 | fmode = IMPULSE; |
| 324 | else if ( strcmp( filter, "box") == 0 ) |
| 325 | fmode = BOX; |
| 326 | else if ( strcmp( filter, "triangle") == 0 ) |
| 327 | fmode = TRIANGLE; |
| 328 | else if ( strcmp( filter, "quadratic") == 0 ) |
| 329 | fmode = QUADRATIC; |
| 330 | else if ( strcmp( filter, "gaussian") == 0 ) |
| 331 | fmode = GAUSSIAN; |
| 332 | else { |
| 333 | PyErr_SetString(ImgfileError, "Unknown filter type"); |
| 334 | return NULL; |
| 335 | } |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 336 | } |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 337 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 338 | if ( (image = imgfile_open(fname)) == NULL ) |
| 339 | return NULL; |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 340 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 341 | if ( image->colormap != CM_NORMAL ) { |
| 342 | iclose(image); |
| 343 | PyErr_SetString(ImgfileError, |
| 344 | "Can only handle CM_NORMAL images"); |
| 345 | return NULL; |
| 346 | } |
| 347 | if ( BPP(image->type) != 1 ) { |
| 348 | iclose(image); |
| 349 | PyErr_SetString(ImgfileError, |
| 350 | "Can't handle imgfiles with bpp!=1"); |
| 351 | return NULL; |
| 352 | } |
| 353 | xsize = image->xsize; |
| 354 | ysize = image->ysize; |
| 355 | zsize = image->zsize; |
| 356 | if ( zsize != 1 && zsize != 3) { |
| 357 | iclose(image); |
| 358 | PyErr_SetString(ImgfileError, |
| 359 | "Can only handle 1 or 3 byte pixels"); |
| 360 | return NULL; |
| 361 | } |
| 362 | if ( xsize > 8192 ) { |
| 363 | iclose(image); |
| 364 | PyErr_SetString(ImgfileError, |
| 365 | "Can't handle image with > 8192 columns"); |
| 366 | return NULL; |
| 367 | } |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 368 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 369 | if ( zsize == 3 ) zsize = 4; |
| 370 | rv = PyString_FromStringAndSize(NULL, xwtd*ywtd*zsize); |
| 371 | if ( rv == NULL ) { |
| 372 | iclose(image); |
| 373 | return NULL; |
| 374 | } |
Guido van Rossum | 52fa3a6 | 1997-02-14 22:59:58 +0000 | [diff] [blame] | 375 | PyFPE_START_PROTECT("readscaled", return 0) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 376 | xfac = (float)xsize/(float)xwtd; |
| 377 | yfac = (float)ysize/(float)ywtd; |
Guido van Rossum | 45b8391 | 1997-03-14 04:32:50 +0000 | [diff] [blame] | 378 | PyFPE_END_PROTECT(yfac) |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 379 | cdatap = PyString_AsString(rv); |
| 380 | idatap = (long *)cdatap; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 381 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 382 | if ( extended ) { |
| 383 | xscale(image, xsize, ysize, zsize, |
| 384 | idatap, xwtd, ywtd, fmode, blur); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 385 | } else { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 386 | if (top_to_bottom) { |
| 387 | yfirst = ywtd-1; |
| 388 | ylast = -1; |
| 389 | ystep = -1; |
| 390 | } else { |
| 391 | yfirst = 0; |
| 392 | ylast = ywtd; |
| 393 | ystep = 1; |
| 394 | } |
| 395 | for ( y=yfirst; y != ylast && !error_called; y += ystep ) { |
| 396 | yorig = (int)(y*yfac); |
| 397 | if ( zsize == 1 ) { |
| 398 | getrow(image, rs, yorig, 0); |
| 399 | for(x=0; x<xwtd; x++ ) { |
| 400 | *cdatap++ = rs[(int)(x*xfac)]; |
| 401 | } |
| 402 | } else { |
| 403 | getrow(image, rs, yorig, 0); |
| 404 | getrow(image, gs, yorig, 1); |
| 405 | getrow(image, bs, yorig, 2); |
| 406 | for(x=0; x<xwtd; x++ ) { |
| 407 | xorig = (int)(x*xfac); |
| 408 | *idatap++ = (rs[xorig] & 0xff) | |
| 409 | ((gs[xorig] & 0xff)<<8) | |
| 410 | ((bs[xorig] & 0xff)<<16); |
| 411 | } |
| 412 | } |
| 413 | } |
| 414 | } |
| 415 | iclose(image); |
| 416 | if ( error_called ) { |
| 417 | Py_DECREF(rv); |
| 418 | return NULL; |
| 419 | } |
| 420 | return rv; |
| 421 | } |
| 422 | |
| 423 | static PyObject * |
| 424 | imgfile_getsizes(self, args) |
| 425 | PyObject *self; |
| 426 | PyObject *args; |
| 427 | { |
| 428 | char *fname; |
| 429 | PyObject *rv; |
| 430 | IMAGE *image; |
| 431 | |
| 432 | if ( !PyArg_Parse(args, "s", &fname) ) |
| 433 | return NULL; |
| 434 | |
| 435 | if ( (image = imgfile_open(fname)) == NULL ) |
| 436 | return NULL; |
| 437 | rv = Py_BuildValue("(iii)", image->xsize, image->ysize, image->zsize); |
| 438 | iclose(image); |
| 439 | return rv; |
| 440 | } |
| 441 | |
| 442 | static PyObject * |
| 443 | imgfile_write(self, args) |
| 444 | PyObject *self; |
| 445 | PyObject *args; |
| 446 | { |
| 447 | IMAGE *image; |
| 448 | char *fname; |
| 449 | int xsize, ysize, zsize, len; |
| 450 | char *cdatap; |
| 451 | long *idatap; |
| 452 | short rs[8192], gs[8192], bs[8192]; |
| 453 | short r, g, b; |
| 454 | long rgb; |
| 455 | int x, y; |
| 456 | int yfirst, ylast, ystep; |
| 457 | |
| 458 | |
| 459 | if ( !PyArg_Parse(args, "(ss#iii)", |
| 460 | &fname, &cdatap, &len, &xsize, &ysize, &zsize) ) |
| 461 | return NULL; |
| 462 | |
| 463 | if ( zsize != 1 && zsize != 3 ) { |
| 464 | PyErr_SetString(ImgfileError, |
| 465 | "Can only handle 1 or 3 byte pixels"); |
| 466 | return NULL; |
| 467 | } |
| 468 | if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) { |
| 469 | PyErr_SetString(ImgfileError, "Data does not match sizes"); |
| 470 | return NULL; |
| 471 | } |
| 472 | if ( xsize > 8192 ) { |
| 473 | PyErr_SetString(ImgfileError, |
| 474 | "Can't handle image with > 8192 columns"); |
| 475 | return NULL; |
| 476 | } |
| 477 | |
| 478 | error_called = 0; |
| 479 | errno = 0; |
| 480 | image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize); |
| 481 | if ( image == 0 ) { |
| 482 | if ( ! error_called ) { |
| 483 | if (errno) |
| 484 | PyErr_SetFromErrno(ImgfileError); |
| 485 | else |
| 486 | PyErr_SetString(ImgfileError, |
| 487 | "Can't create image file"); |
| 488 | } |
| 489 | return NULL; |
| 490 | } |
| 491 | |
| 492 | idatap = (long *)cdatap; |
| 493 | |
| 494 | if (top_to_bottom) { |
| 495 | yfirst = ysize-1; |
| 496 | ylast = -1; |
| 497 | ystep = -1; |
| 498 | } else { |
| 499 | yfirst = 0; |
| 500 | ylast = ysize; |
| 501 | ystep = 1; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 502 | } |
| 503 | for ( y=yfirst; y != ylast && !error_called; y += ystep ) { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 504 | if ( zsize == 1 ) { |
| 505 | for( x=0; x<xsize; x++ ) |
| 506 | rs[x] = *cdatap++; |
| 507 | putrow(image, rs, y, 0); |
| 508 | } else { |
| 509 | for( x=0; x<xsize; x++ ) { |
| 510 | rgb = *idatap++; |
| 511 | r = rgb & 0xff; |
| 512 | g = (rgb >> 8 ) & 0xff; |
| 513 | b = (rgb >> 16 ) & 0xff; |
| 514 | rs[x] = r; |
| 515 | gs[x] = g; |
| 516 | bs[x] = b; |
| 517 | } |
| 518 | putrow(image, rs, y, 0); |
| 519 | putrow(image, gs, y, 1); |
| 520 | putrow(image, bs, y, 2); |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 521 | } |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 522 | } |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 523 | iclose(image); |
| 524 | if ( error_called ) |
| 525 | return NULL; |
| 526 | Py_INCREF(Py_None); |
| 527 | return Py_None; |
Jack Jansen | 3accf98 | 1992-08-20 11:54:27 +0000 | [diff] [blame] | 528 | |
| 529 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 530 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 531 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 532 | static PyMethodDef imgfile_methods[] = { |
| 533 | { "getsizes", imgfile_getsizes }, |
| 534 | { "read", imgfile_read }, |
| 535 | { "readscaled", imgfile_readscaled, 1}, |
| 536 | { "write", imgfile_write }, |
| 537 | { "ttob", imgfile_ttob }, |
| 538 | { NULL, NULL } /* Sentinel */ |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
| 541 | |
| 542 | void |
| 543 | initimgfile() |
| 544 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 545 | PyObject *m, *d; |
| 546 | m = Py_InitModule("imgfile", imgfile_methods); |
| 547 | d = PyModule_GetDict(m); |
Guido van Rossum | 0cb96de | 1997-10-01 04:29:29 +0000 | [diff] [blame] | 548 | ImgfileError = PyErr_NewException("imgfile.error", NULL, NULL); |
| 549 | if (ImgfileError != NULL) |
| 550 | PyDict_SetItemString(d, "error", ImgfileError); |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 551 | } |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 552 | |
| 553 | |
| 554 | |