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