Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | bab9d03 | 1992-04-05 14:26:55 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 3 | Netherlands. |
| 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 | |
| 25 | /* IMGFILE module - Interface to sgi libimg */ |
| 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). |
| 32 | */ |
| 33 | |
| 34 | #include "allobjects.h" |
| 35 | #include "modsupport.h" |
| 36 | |
| 37 | #include <gl/image.h> |
| 38 | |
| 39 | /* #include "sigtype.h" */ |
| 40 | |
| 41 | static object * ImgfileError; |
| 42 | |
| 43 | static char gfname[1024]; |
| 44 | static IMAGE *image; |
| 45 | |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 46 | static int error_called; |
| 47 | |
| 48 | static imgfile_error(str) |
| 49 | char *str; |
| 50 | { |
| 51 | err_setstr(ImgfileError, str); |
| 52 | error_called = 1; |
| 53 | return; /* To imglib, which will return a failure indictaor */ |
| 54 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 55 | |
| 56 | static |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 57 | imgfile_open(fname) |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 58 | char *fname; |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 59 | { |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 60 | i_seterror(imgfile_error); |
| 61 | error_called = 0; |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 62 | if ( image != NULL && strcmp(fname, gfname) != 0 ) { |
| 63 | iclose(image); |
| 64 | image = NULL; |
| 65 | gfname[0] = '\0'; |
| 66 | } |
| 67 | if ( (image=iopen(fname, "r")) == NULL ) { |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 68 | /* Error may already be set by imgfile_error */ |
| 69 | if ( !error_called ) |
| 70 | err_setstr(ImgfileError, "Cannot open image file"); |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 71 | return 0; |
| 72 | } |
| 73 | strcpy(gfname, fname); |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | static object * |
| 78 | imgfile_read(self, args) |
| 79 | object *self; |
| 80 | object *args; |
| 81 | { |
| 82 | char *fname; |
| 83 | object *rv; |
| 84 | int xsize, ysize, zsize; |
| 85 | char *cdatap; |
| 86 | long *idatap; |
| 87 | static short rs[8192], gs[8192], bs[8192]; |
| 88 | int x, y; |
| 89 | |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 90 | if ( !getargs(args, "s", &fname) ) |
| 91 | return 0; |
| 92 | |
| 93 | if ( !imgfile_open(fname) ) |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 94 | return NULL; |
| 95 | |
| 96 | if ( image->colormap != CM_NORMAL ) { |
| 97 | err_setstr(ImgfileError, "Can only handle CM_NORMAL images"); |
| 98 | return NULL; |
| 99 | } |
| 100 | if ( BPP(image->type) != 1 ) { |
| 101 | err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1"); |
| 102 | return NULL; |
| 103 | } |
| 104 | xsize = image->xsize; |
| 105 | ysize = image->ysize; |
| 106 | zsize = image->zsize; |
| 107 | if ( zsize != 1 && zsize != 3) { |
| 108 | err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); |
| 109 | return NULL; |
| 110 | } |
| 111 | |
| 112 | if ( zsize == 3 ) zsize = 4; |
| 113 | rv = newsizedstringobject(NULL, xsize*ysize*zsize); |
| 114 | if ( rv == NULL ) |
| 115 | return NULL; |
| 116 | cdatap = getstringvalue(rv); |
| 117 | idatap = (long *)cdatap; |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 118 | for ( y=0; y < ysize && !error_called; y++ ) { |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 119 | if ( zsize == 1 ) { |
| 120 | getrow(image, rs, y, 0); |
| 121 | for(x=0; x<xsize; x++ ) |
| 122 | *cdatap++ = rs[x]; |
| 123 | } else { |
| 124 | getrow(image, rs, y, 0); |
| 125 | getrow(image, gs, y, 1); |
| 126 | getrow(image, bs, y, 2); |
| 127 | for(x=0; x<xsize; x++ ) |
| 128 | *idatap++ = (rs[x] & 0xff) | |
| 129 | ((gs[x] & 0xff)<<8) | |
| 130 | ((bs[x] & 0xff)<<16); |
| 131 | } |
| 132 | } |
Jack Jansen | 3accf98 | 1992-08-20 11:54:27 +0000 | [diff] [blame^] | 133 | iclose(image); |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 134 | if ( error_called ) { |
| 135 | DECREF(rv); |
| 136 | return NULL; |
| 137 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 138 | return rv; |
| 139 | } |
| 140 | |
| 141 | static object * |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 142 | imgfile_readscaled(self, args) |
| 143 | object *self; |
| 144 | object *args; |
| 145 | { |
| 146 | char *fname; |
| 147 | object *rv; |
| 148 | int xsize, ysize, zsize; |
| 149 | char *cdatap; |
| 150 | long *idatap; |
| 151 | static short rs[8192], gs[8192], bs[8192]; |
| 152 | int x, y; |
| 153 | int xwtd, ywtd, xorig, yorig; |
| 154 | float xfac, yfac; |
| 155 | int cnt; |
| 156 | |
| 157 | if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) ) |
| 158 | return 0; |
| 159 | |
| 160 | if ( !imgfile_open(fname) ) |
| 161 | return NULL; |
| 162 | |
| 163 | if ( image->colormap != CM_NORMAL ) { |
| 164 | err_setstr(ImgfileError, "Can only handle CM_NORMAL images"); |
| 165 | return NULL; |
| 166 | } |
| 167 | if ( BPP(image->type) != 1 ) { |
| 168 | err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1"); |
| 169 | return NULL; |
| 170 | } |
| 171 | xsize = image->xsize; |
| 172 | ysize = image->ysize; |
| 173 | zsize = image->zsize; |
| 174 | if ( zsize != 1 && zsize != 3) { |
| 175 | err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | if ( zsize == 3 ) zsize = 4; |
| 180 | rv = newsizedstringobject(NULL, xwtd*ywtd*zsize); |
| 181 | xfac = (float)xsize/(float)xwtd; |
| 182 | yfac = (float)ysize/(float)ywtd; |
| 183 | if ( rv == NULL ) |
| 184 | return NULL; |
| 185 | cdatap = getstringvalue(rv); |
| 186 | idatap = (long *)cdatap; |
| 187 | for ( y=0; y < ywtd && !error_called; y++ ) { |
| 188 | yorig = (int)(y*yfac); |
| 189 | if ( zsize == 1 ) { |
| 190 | getrow(image, rs, yorig, 0); |
| 191 | for(x=0; x<xwtd; x++ ) { |
| 192 | *cdatap++ = rs[(int)(x*xfac)]; |
| 193 | } |
| 194 | } else { |
| 195 | getrow(image, rs, yorig, 0); |
| 196 | getrow(image, gs, yorig, 1); |
| 197 | getrow(image, bs, yorig, 2); |
| 198 | for(x=0; x<xwtd; x++ ) { |
| 199 | xorig = (int)(x*xfac); |
| 200 | *idatap++ = (rs[xorig] & 0xff) | |
| 201 | ((gs[xorig] & 0xff)<<8) | |
| 202 | ((bs[xorig] & 0xff)<<16); |
| 203 | } |
| 204 | } |
| 205 | } |
| 206 | if ( error_called ) { |
| 207 | DECREF(rv); |
| 208 | return NULL; |
| 209 | } |
| 210 | return rv; |
| 211 | } |
| 212 | |
| 213 | static object * |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 214 | imgfile_getsizes(self, args) |
| 215 | object *self; |
| 216 | object *args; |
| 217 | { |
| 218 | char *fname; |
| 219 | object *rv; |
| 220 | |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 221 | if ( !getargs(args, "s", &fname) ) |
| 222 | return 0; |
| 223 | |
| 224 | if ( !imgfile_open(fname) ) |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 225 | return NULL; |
| 226 | rv = newtupleobject(3); |
| 227 | if ( rv == NULL ) |
| 228 | return NULL; |
| 229 | settupleitem(rv, 0, newintobject(image->xsize)); |
| 230 | settupleitem(rv, 1, newintobject(image->ysize)); |
| 231 | settupleitem(rv, 2, newintobject(image->zsize)); |
| 232 | return rv; |
| 233 | } |
| 234 | |
Jack Jansen | 3accf98 | 1992-08-20 11:54:27 +0000 | [diff] [blame^] | 235 | static object * |
| 236 | imgfile_write(self, args) |
| 237 | object *self; |
| 238 | object *args; |
| 239 | { |
| 240 | IMAGE *image; |
| 241 | char *fname; |
| 242 | int xsize, ysize, zsize, len; |
| 243 | char *cdatap; |
| 244 | long *idatap; |
| 245 | short rs[8192], gs[8192], bs[8192]; |
| 246 | short r, g, b; |
| 247 | long rgb; |
| 248 | int x, y; |
| 249 | |
| 250 | if ( !getargs(args, "(ss#iii)", |
| 251 | &fname, &cdatap, &len, &xsize, &ysize, &zsize) ) |
| 252 | return 0; |
| 253 | |
| 254 | if ( zsize != 1 && zsize != 3 ) { |
| 255 | err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); |
| 256 | return 0; |
| 257 | } |
| 258 | if ( (zsize == 1 && len != xsize*ysize) || |
| 259 | (zsize == 3 && len != xsize*ysize*4) ) { |
| 260 | err_setstr(ImgfileError, "Data does not match sizes"); |
| 261 | return 0; |
| 262 | } |
| 263 | |
| 264 | printf("Opening %s, %d %d %d\n", fname, xsize, ysize, zsize); |
| 265 | image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize); |
| 266 | if ( image == 0 ) { |
| 267 | if ( ! error_called ) |
| 268 | err_setstr(ImgfileError, "Cannot create image file"); |
| 269 | return 0; |
| 270 | } |
| 271 | |
| 272 | idatap = (long *)cdatap; |
| 273 | |
| 274 | for( y=0; y<ysize && !error_called; y++ ) { |
| 275 | if ( zsize == 1 ) { |
| 276 | for( x=0; x<xsize; x++ ) |
| 277 | rs[x] = *cdatap++; |
| 278 | putrow(image, rs, y, 0); |
| 279 | } else { |
| 280 | for( x=0; x<xsize; x++ ) { |
| 281 | rgb = *idatap++; |
| 282 | r = rgb & 0xff; |
| 283 | g = (rgb >> 8 ) & 0xff; |
| 284 | b = (rgb >> 16 ) & 0xff; |
| 285 | rs[x] = r; |
| 286 | gs[x] = g; |
| 287 | bs[x] = b; |
| 288 | } |
| 289 | putrow(image, rs, y, 0); |
| 290 | putrow(image, gs, y, 1); |
| 291 | putrow(image, bs, y, 2); |
| 292 | } |
| 293 | } |
| 294 | iclose(image); |
| 295 | if ( error_called ) |
| 296 | return 0; |
| 297 | INCREF(None); |
| 298 | return None; |
| 299 | |
| 300 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 301 | |
| 302 | static struct methodlist imgfile_methods[] = { |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame] | 303 | { "getsizes", imgfile_getsizes }, |
| 304 | { "read", imgfile_read }, |
| 305 | { "readscaled", imgfile_readscaled }, |
Jack Jansen | 3accf98 | 1992-08-20 11:54:27 +0000 | [diff] [blame^] | 306 | { "write", imgfile_write }, |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 307 | { 0, 0 } |
| 308 | }; |
| 309 | |
| 310 | |
| 311 | void |
| 312 | initimgfile() |
| 313 | { |
| 314 | object *m, *d; |
| 315 | m = initmodule("imgfile", imgfile_methods); |
| 316 | d = getmoduledict(m); |
| 317 | ImgfileError = newstringobject("imgfile.error"); |
| 318 | if ( ImgfileError == NULL || dictinsert(d,"error",ImgfileError) ) |
| 319 | fatal("can't define imgfile.error"); |
| 320 | } |