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 | } |
| 390 | xfac = (float)xsize/(float)xwtd; |
| 391 | yfac = (float)ysize/(float)ywtd; |
| 392 | cdatap = PyString_AsString(rv); |
| 393 | idatap = (long *)cdatap; |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 394 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 395 | if ( extended ) { |
| 396 | xscale(image, xsize, ysize, zsize, |
| 397 | idatap, xwtd, ywtd, fmode, blur); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 398 | } else { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 399 | if (top_to_bottom) { |
| 400 | yfirst = ywtd-1; |
| 401 | ylast = -1; |
| 402 | ystep = -1; |
| 403 | } else { |
| 404 | yfirst = 0; |
| 405 | ylast = ywtd; |
| 406 | ystep = 1; |
| 407 | } |
| 408 | for ( y=yfirst; y != ylast && !error_called; y += ystep ) { |
| 409 | yorig = (int)(y*yfac); |
| 410 | if ( zsize == 1 ) { |
| 411 | getrow(image, rs, yorig, 0); |
| 412 | for(x=0; x<xwtd; x++ ) { |
| 413 | *cdatap++ = rs[(int)(x*xfac)]; |
| 414 | } |
| 415 | } else { |
| 416 | getrow(image, rs, yorig, 0); |
| 417 | getrow(image, gs, yorig, 1); |
| 418 | getrow(image, bs, yorig, 2); |
| 419 | for(x=0; x<xwtd; x++ ) { |
| 420 | xorig = (int)(x*xfac); |
| 421 | *idatap++ = (rs[xorig] & 0xff) | |
| 422 | ((gs[xorig] & 0xff)<<8) | |
| 423 | ((bs[xorig] & 0xff)<<16); |
| 424 | } |
| 425 | } |
| 426 | } |
| 427 | } |
| 428 | iclose(image); |
| 429 | if ( error_called ) { |
| 430 | Py_DECREF(rv); |
| 431 | return NULL; |
| 432 | } |
| 433 | return rv; |
| 434 | } |
| 435 | |
| 436 | static PyObject * |
| 437 | imgfile_getsizes(self, args) |
| 438 | PyObject *self; |
| 439 | PyObject *args; |
| 440 | { |
| 441 | char *fname; |
| 442 | PyObject *rv; |
| 443 | IMAGE *image; |
| 444 | |
| 445 | if ( !PyArg_Parse(args, "s", &fname) ) |
| 446 | return NULL; |
| 447 | |
| 448 | if ( (image = imgfile_open(fname)) == NULL ) |
| 449 | return NULL; |
| 450 | rv = Py_BuildValue("(iii)", image->xsize, image->ysize, image->zsize); |
| 451 | iclose(image); |
| 452 | return rv; |
| 453 | } |
| 454 | |
| 455 | static PyObject * |
| 456 | imgfile_write(self, args) |
| 457 | PyObject *self; |
| 458 | PyObject *args; |
| 459 | { |
| 460 | IMAGE *image; |
| 461 | char *fname; |
| 462 | int xsize, ysize, zsize, len; |
| 463 | char *cdatap; |
| 464 | long *idatap; |
| 465 | short rs[8192], gs[8192], bs[8192]; |
| 466 | short r, g, b; |
| 467 | long rgb; |
| 468 | int x, y; |
| 469 | int yfirst, ylast, ystep; |
| 470 | |
| 471 | |
| 472 | if ( !PyArg_Parse(args, "(ss#iii)", |
| 473 | &fname, &cdatap, &len, &xsize, &ysize, &zsize) ) |
| 474 | return NULL; |
| 475 | |
| 476 | if ( zsize != 1 && zsize != 3 ) { |
| 477 | PyErr_SetString(ImgfileError, |
| 478 | "Can only handle 1 or 3 byte pixels"); |
| 479 | return NULL; |
| 480 | } |
| 481 | if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) { |
| 482 | PyErr_SetString(ImgfileError, "Data does not match sizes"); |
| 483 | return NULL; |
| 484 | } |
| 485 | if ( xsize > 8192 ) { |
| 486 | PyErr_SetString(ImgfileError, |
| 487 | "Can't handle image with > 8192 columns"); |
| 488 | return NULL; |
| 489 | } |
| 490 | |
| 491 | error_called = 0; |
| 492 | errno = 0; |
| 493 | image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize); |
| 494 | if ( image == 0 ) { |
| 495 | if ( ! error_called ) { |
| 496 | if (errno) |
| 497 | PyErr_SetFromErrno(ImgfileError); |
| 498 | else |
| 499 | PyErr_SetString(ImgfileError, |
| 500 | "Can't create image file"); |
| 501 | } |
| 502 | return NULL; |
| 503 | } |
| 504 | |
| 505 | idatap = (long *)cdatap; |
| 506 | |
| 507 | if (top_to_bottom) { |
| 508 | yfirst = ysize-1; |
| 509 | ylast = -1; |
| 510 | ystep = -1; |
| 511 | } else { |
| 512 | yfirst = 0; |
| 513 | ylast = ysize; |
| 514 | ystep = 1; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 515 | } |
| 516 | for ( y=yfirst; y != ylast && !error_called; y += ystep ) { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 517 | if ( zsize == 1 ) { |
| 518 | for( x=0; x<xsize; x++ ) |
| 519 | rs[x] = *cdatap++; |
| 520 | putrow(image, rs, y, 0); |
| 521 | } else { |
| 522 | for( x=0; x<xsize; x++ ) { |
| 523 | rgb = *idatap++; |
| 524 | r = rgb & 0xff; |
| 525 | g = (rgb >> 8 ) & 0xff; |
| 526 | b = (rgb >> 16 ) & 0xff; |
| 527 | rs[x] = r; |
| 528 | gs[x] = g; |
| 529 | bs[x] = b; |
| 530 | } |
| 531 | putrow(image, rs, y, 0); |
| 532 | putrow(image, gs, y, 1); |
| 533 | putrow(image, bs, y, 2); |
Jack Jansen | 3c2eb5c | 1993-01-19 15:17:13 +0000 | [diff] [blame] | 534 | } |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 535 | } |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 536 | iclose(image); |
| 537 | if ( error_called ) |
| 538 | return NULL; |
| 539 | Py_INCREF(Py_None); |
| 540 | return Py_None; |
Jack Jansen | 3accf98 | 1992-08-20 11:54:27 +0000 | [diff] [blame] | 541 | |
| 542 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 543 | |
Guido van Rossum | a5f6138 | 1992-09-03 20:41:22 +0000 | [diff] [blame] | 544 | |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 545 | static PyMethodDef imgfile_methods[] = { |
| 546 | { "getsizes", imgfile_getsizes }, |
| 547 | { "read", imgfile_read }, |
| 548 | { "readscaled", imgfile_readscaled, 1}, |
| 549 | { "write", imgfile_write }, |
| 550 | { "ttob", imgfile_ttob }, |
| 551 | { NULL, NULL } /* Sentinel */ |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 552 | }; |
| 553 | |
| 554 | |
| 555 | void |
| 556 | initimgfile() |
| 557 | { |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 558 | PyObject *m, *d; |
| 559 | m = Py_InitModule("imgfile", imgfile_methods); |
| 560 | d = PyModule_GetDict(m); |
| 561 | ImgfileError = PyString_FromString("imgfile.error"); |
| 562 | if ( ImgfileError == NULL |
| 563 | || PyDict_SetItemString(d, "error", ImgfileError) ) |
| 564 | Py_FatalError("can't define imgfile.error"); |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 565 | } |
Roger E. Masse | 7f33e40 | 1996-12-20 21:56:08 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | |