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