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