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