blob: 19ce5179008de4f9f3854240e82953539a43ae76 [file] [log] [blame]
Guido van Rossumaa9de671992-03-04 16:40:03 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumaa9de671992-03-04 16:40:03 +00003Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
25/* IMGFILE module - Interface to sgi libimg */
26
Guido van Rossum2c4be641992-06-03 17:06:36 +000027/* XXX This modele should be done better at some point. It should return
Guido van Rossumaa9de671992-03-04 16:40:03 +000028** 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
41static object * ImgfileError;
42
43static char gfname[1024];
44static IMAGE *image;
45
Guido van Rossum3e941971992-03-23 18:21:32 +000046static int error_called;
47
48static 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 Rossumaa9de671992-03-04 16:40:03 +000055
56static
Guido van Rossum2c4be641992-06-03 17:06:36 +000057imgfile_open(fname)
Guido van Rossumaa9de671992-03-04 16:40:03 +000058 char *fname;
Guido van Rossum2c4be641992-06-03 17:06:36 +000059{
Guido van Rossum3e941971992-03-23 18:21:32 +000060 i_seterror(imgfile_error);
61 error_called = 0;
Guido van Rossumaa9de671992-03-04 16:40:03 +000062 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 Rossum3e941971992-03-23 18:21:32 +000068 /* Error may already be set by imgfile_error */
69 if ( !error_called )
70 err_setstr(ImgfileError, "Cannot open image file");
Guido van Rossumaa9de671992-03-04 16:40:03 +000071 return 0;
72 }
73 strcpy(gfname, fname);
74 return 1;
75}
76
77static object *
78imgfile_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 Rossum2c4be641992-06-03 17:06:36 +000090 if ( !getargs(args, "s", &fname) )
91 return 0;
92
93 if ( !imgfile_open(fname) )
Guido van Rossumaa9de671992-03-04 16:40:03 +000094 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 Rossum3e941971992-03-23 18:21:32 +0000118 for ( y=0; y < ysize && !error_called; y++ ) {
Guido van Rossumaa9de671992-03-04 16:40:03 +0000119 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 Rossum3e941971992-03-23 18:21:32 +0000133 if ( error_called ) {
134 DECREF(rv);
135 return NULL;
136 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000137 return rv;
138}
139
140static object *
Guido van Rossum2c4be641992-06-03 17:06:36 +0000141imgfile_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
212static object *
Guido van Rossumaa9de671992-03-04 16:40:03 +0000213imgfile_getsizes(self, args)
214 object *self;
215 object *args;
216{
217 char *fname;
218 object *rv;
219
Guido van Rossum2c4be641992-06-03 17:06:36 +0000220 if ( !getargs(args, "s", &fname) )
221 return 0;
222
223 if ( !imgfile_open(fname) )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000224 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
235static struct methodlist imgfile_methods[] = {
Guido van Rossum2c4be641992-06-03 17:06:36 +0000236 { "getsizes", imgfile_getsizes },
237 { "read", imgfile_read },
238 { "readscaled", imgfile_readscaled },
Guido van Rossumaa9de671992-03-04 16:40:03 +0000239 { 0, 0 }
240};
241
242
243void
244initimgfile()
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}