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 | } |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 133 | if ( error_called ) { |
| 134 | DECREF(rv); |
| 135 | return NULL; |
| 136 | } |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 137 | return rv; |
| 138 | } |
| 139 | |
| 140 | static object * |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame^] | 141 | imgfile_readscaled(self, args) |
| 142 | object *self; |
| 143 | object *args; |
| 144 | { |
| 145 | char *fname; |
| 146 | object *rv; |
| 147 | int xsize, ysize, zsize; |
| 148 | char *cdatap; |
| 149 | long *idatap; |
| 150 | static short rs[8192], gs[8192], bs[8192]; |
| 151 | int x, y; |
| 152 | int xwtd, ywtd, xorig, yorig; |
| 153 | float xfac, yfac; |
| 154 | int cnt; |
| 155 | |
| 156 | if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) ) |
| 157 | return 0; |
| 158 | |
| 159 | if ( !imgfile_open(fname) ) |
| 160 | return NULL; |
| 161 | |
| 162 | if ( image->colormap != CM_NORMAL ) { |
| 163 | err_setstr(ImgfileError, "Can only handle CM_NORMAL images"); |
| 164 | return NULL; |
| 165 | } |
| 166 | if ( BPP(image->type) != 1 ) { |
| 167 | err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1"); |
| 168 | return NULL; |
| 169 | } |
| 170 | xsize = image->xsize; |
| 171 | ysize = image->ysize; |
| 172 | zsize = image->zsize; |
| 173 | if ( zsize != 1 && zsize != 3) { |
| 174 | err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); |
| 175 | return NULL; |
| 176 | } |
| 177 | |
| 178 | if ( zsize == 3 ) zsize = 4; |
| 179 | rv = newsizedstringobject(NULL, xwtd*ywtd*zsize); |
| 180 | xfac = (float)xsize/(float)xwtd; |
| 181 | yfac = (float)ysize/(float)ywtd; |
| 182 | if ( rv == NULL ) |
| 183 | return NULL; |
| 184 | cdatap = getstringvalue(rv); |
| 185 | idatap = (long *)cdatap; |
| 186 | for ( y=0; y < ywtd && !error_called; y++ ) { |
| 187 | yorig = (int)(y*yfac); |
| 188 | if ( zsize == 1 ) { |
| 189 | getrow(image, rs, yorig, 0); |
| 190 | for(x=0; x<xwtd; x++ ) { |
| 191 | *cdatap++ = rs[(int)(x*xfac)]; |
| 192 | } |
| 193 | } else { |
| 194 | getrow(image, rs, yorig, 0); |
| 195 | getrow(image, gs, yorig, 1); |
| 196 | getrow(image, bs, yorig, 2); |
| 197 | for(x=0; x<xwtd; x++ ) { |
| 198 | xorig = (int)(x*xfac); |
| 199 | *idatap++ = (rs[xorig] & 0xff) | |
| 200 | ((gs[xorig] & 0xff)<<8) | |
| 201 | ((bs[xorig] & 0xff)<<16); |
| 202 | } |
| 203 | } |
| 204 | } |
| 205 | if ( error_called ) { |
| 206 | DECREF(rv); |
| 207 | return NULL; |
| 208 | } |
| 209 | return rv; |
| 210 | } |
| 211 | |
| 212 | static object * |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 213 | imgfile_getsizes(self, args) |
| 214 | object *self; |
| 215 | object *args; |
| 216 | { |
| 217 | char *fname; |
| 218 | object *rv; |
| 219 | |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame^] | 220 | if ( !getargs(args, "s", &fname) ) |
| 221 | return 0; |
| 222 | |
| 223 | if ( !imgfile_open(fname) ) |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 224 | return NULL; |
| 225 | rv = newtupleobject(3); |
| 226 | if ( rv == NULL ) |
| 227 | return NULL; |
| 228 | settupleitem(rv, 0, newintobject(image->xsize)); |
| 229 | settupleitem(rv, 1, newintobject(image->ysize)); |
| 230 | settupleitem(rv, 2, newintobject(image->zsize)); |
| 231 | return rv; |
| 232 | } |
| 233 | |
| 234 | |
| 235 | static struct methodlist imgfile_methods[] = { |
Guido van Rossum | 2c4be64 | 1992-06-03 17:06:36 +0000 | [diff] [blame^] | 236 | { "getsizes", imgfile_getsizes }, |
| 237 | { "read", imgfile_read }, |
| 238 | { "readscaled", imgfile_readscaled }, |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 239 | { 0, 0 } |
| 240 | }; |
| 241 | |
| 242 | |
| 243 | void |
| 244 | initimgfile() |
| 245 | { |
| 246 | object *m, *d; |
| 247 | m = initmodule("imgfile", imgfile_methods); |
| 248 | d = getmoduledict(m); |
| 249 | ImgfileError = newstringobject("imgfile.error"); |
| 250 | if ( ImgfileError == NULL || dictinsert(d,"error",ImgfileError) ) |
| 251 | fatal("can't define imgfile.error"); |
| 252 | } |