Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 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 | |
| 27 | /*XXXX This modele should be done better at some point. It should return |
| 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 |
| 57 | imgfile_open(args) |
| 58 | object *args; |
| 59 | { |
| 60 | char *fname; |
| 61 | |
| 62 | if ( !getargs(args, "s", &fname) ) |
| 63 | return 0; |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 64 | i_seterror(imgfile_error); |
| 65 | error_called = 0; |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 66 | if ( image != NULL && strcmp(fname, gfname) != 0 ) { |
| 67 | iclose(image); |
| 68 | image = NULL; |
| 69 | gfname[0] = '\0'; |
| 70 | } |
| 71 | if ( (image=iopen(fname, "r")) == NULL ) { |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 72 | /* Error may already be set by imgfile_error */ |
| 73 | if ( !error_called ) |
| 74 | err_setstr(ImgfileError, "Cannot open image file"); |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | strcpy(gfname, fname); |
| 78 | return 1; |
| 79 | } |
| 80 | |
| 81 | static object * |
| 82 | imgfile_read(self, args) |
| 83 | object *self; |
| 84 | object *args; |
| 85 | { |
| 86 | char *fname; |
| 87 | object *rv; |
| 88 | int xsize, ysize, zsize; |
| 89 | char *cdatap; |
| 90 | long *idatap; |
| 91 | static short rs[8192], gs[8192], bs[8192]; |
| 92 | int x, y; |
| 93 | |
| 94 | if ( !imgfile_open(args) ) |
| 95 | return NULL; |
| 96 | |
| 97 | if ( image->colormap != CM_NORMAL ) { |
| 98 | err_setstr(ImgfileError, "Can only handle CM_NORMAL images"); |
| 99 | return NULL; |
| 100 | } |
| 101 | if ( BPP(image->type) != 1 ) { |
| 102 | err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1"); |
| 103 | return NULL; |
| 104 | } |
| 105 | xsize = image->xsize; |
| 106 | ysize = image->ysize; |
| 107 | zsize = image->zsize; |
| 108 | if ( zsize != 1 && zsize != 3) { |
| 109 | err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); |
| 110 | return NULL; |
| 111 | } |
| 112 | |
| 113 | if ( zsize == 3 ) zsize = 4; |
| 114 | rv = newsizedstringobject(NULL, xsize*ysize*zsize); |
| 115 | if ( rv == NULL ) |
| 116 | return NULL; |
| 117 | cdatap = getstringvalue(rv); |
| 118 | idatap = (long *)cdatap; |
Guido van Rossum | 3e94197 | 1992-03-23 18:21:32 +0000 | [diff] [blame] | 119 | for ( y=0; y < ysize && !error_called; y++ ) { |
Guido van Rossum | aa9de67 | 1992-03-04 16:40:03 +0000 | [diff] [blame] | 120 | if ( zsize == 1 ) { |
| 121 | getrow(image, rs, y, 0); |
| 122 | for(x=0; x<xsize; x++ ) |
| 123 | *cdatap++ = rs[x]; |
| 124 | } else { |
| 125 | getrow(image, rs, y, 0); |
| 126 | getrow(image, gs, y, 1); |
| 127 | getrow(image, bs, y, 2); |
| 128 | for(x=0; x<xsize; x++ ) |
| 129 | *idatap++ = (rs[x] & 0xff) | |
| 130 | ((gs[x] & 0xff)<<8) | |
| 131 | ((bs[x] & 0xff)<<16); |
| 132 | } |
| 133 | } |
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 * |
| 142 | imgfile_getsizes(self, args) |
| 143 | object *self; |
| 144 | object *args; |
| 145 | { |
| 146 | char *fname; |
| 147 | object *rv; |
| 148 | |
| 149 | if ( !imgfile_open(args) ) |
| 150 | return NULL; |
| 151 | rv = newtupleobject(3); |
| 152 | if ( rv == NULL ) |
| 153 | return NULL; |
| 154 | settupleitem(rv, 0, newintobject(image->xsize)); |
| 155 | settupleitem(rv, 1, newintobject(image->ysize)); |
| 156 | settupleitem(rv, 2, newintobject(image->zsize)); |
| 157 | return rv; |
| 158 | } |
| 159 | |
| 160 | |
| 161 | static struct methodlist imgfile_methods[] = { |
| 162 | { "getsizes", imgfile_getsizes }, |
| 163 | { "read", imgfile_read }, |
| 164 | { 0, 0 } |
| 165 | }; |
| 166 | |
| 167 | |
| 168 | void |
| 169 | initimgfile() |
| 170 | { |
| 171 | object *m, *d; |
| 172 | m = initmodule("imgfile", imgfile_methods); |
| 173 | d = getmoduledict(m); |
| 174 | ImgfileError = newstringobject("imgfile.error"); |
| 175 | if ( ImgfileError == NULL || dictinsert(d,"error",ImgfileError) ) |
| 176 | fatal("can't define imgfile.error"); |
| 177 | } |