blob: 1c42b35ee5a063251011b1c055b53f718166ff34 [file] [log] [blame]
Guido van Rossumaa9de671992-03-04 16:40:03 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumaa9de671992-03-04 16:40:03 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumaa9de671992-03-04 16:40:03 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumaa9de671992-03-04 16:40:03 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumaa9de671992-03-04 16:40:03 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumaa9de671992-03-04 16:40:03 +000029
30******************************************************************/
31
Guido van Rossuma5f61381992-09-03 20:41:22 +000032/* IMGFILE module - Interface to sgi libimage */
Guido van Rossumaa9de671992-03-04 16:40:03 +000033
Guido van Rossum2c4be641992-06-03 17:06:36 +000034/* XXX This modele should be done better at some point. It should return
Guido van Rossumaa9de671992-03-04 16:40:03 +000035** 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 Jansen3c2eb5c1993-01-19 15:17:13 +000039**
40** Warning: this module is very non-reentrant (esp. the readscaled stuff)
Guido van Rossumaa9de671992-03-04 16:40:03 +000041*/
42
43#include "allobjects.h"
44#include "modsupport.h"
45
46#include <gl/image.h>
Guido van Rossuma5f61381992-09-03 20:41:22 +000047#include <errno.h>
Guido van Rossumaa9de671992-03-04 16:40:03 +000048
Jack Jansen09cbf9a1993-01-19 15:33:13 +000049#include "/usr/people/4Dgifts/iristools/include/izoom.h"
Jack Jansen3c2eb5c1993-01-19 15:17:13 +000050
Guido van Rossuma5f61381992-09-03 20:41:22 +000051static object * ImgfileError; /* Exception we raise for various trouble */
Guido van Rossumaa9de671992-03-04 16:40:03 +000052
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000053static int top_to_bottom; /* True if we want top-to-bottom images */
Guido van Rossumaa9de671992-03-04 16:40:03 +000054
Guido van Rossuma5f61381992-09-03 20:41:22 +000055/* The image library does not always call the error hander :-(,
56 therefore we have a global variable indicating that it was called.
57 It is cleared by imgfile_open(). */
Guido van Rossumaa9de671992-03-04 16:40:03 +000058
Guido van Rossum3e941971992-03-23 18:21:32 +000059static int error_called;
60
Guido van Rossuma5f61381992-09-03 20:41:22 +000061
62/* The error handler */
63
Guido van Rossum3e941971992-03-23 18:21:32 +000064static imgfile_error(str)
65 char *str;
66{
67 err_setstr(ImgfileError, str);
68 error_called = 1;
Guido van Rossum9bfef441993-03-29 10:43:31 +000069 return; /* To imglib, which will return a failure indicator */
Guido van Rossum3e941971992-03-23 18:21:32 +000070}
Guido van Rossumaa9de671992-03-04 16:40:03 +000071
Guido van Rossuma5f61381992-09-03 20:41:22 +000072
73/* Open an image file and return a pointer to it.
74 Make sure we raise an exception if we fail. */
75
76static IMAGE *
Guido van Rossum2c4be641992-06-03 17:06:36 +000077imgfile_open(fname)
Guido van Rossumaa9de671992-03-04 16:40:03 +000078 char *fname;
Guido van Rossum2c4be641992-06-03 17:06:36 +000079{
Guido van Rossuma5f61381992-09-03 20:41:22 +000080 IMAGE *image;
Guido van Rossum3e941971992-03-23 18:21:32 +000081 i_seterror(imgfile_error);
82 error_called = 0;
Guido van Rossuma5f61381992-09-03 20:41:22 +000083 errno = 0;
84 if ( (image = iopen(fname, "r")) == NULL ) {
Guido van Rossum3e941971992-03-23 18:21:32 +000085 /* Error may already be set by imgfile_error */
Guido van Rossuma5f61381992-09-03 20:41:22 +000086 if ( !error_called ) {
87 if (errno)
88 err_errno(ImgfileError);
89 else
90 err_setstr(ImgfileError, "Can't open image file");
91 }
92 return NULL;
Guido van Rossumaa9de671992-03-04 16:40:03 +000093 }
Guido van Rossuma5f61381992-09-03 20:41:22 +000094 return image;
Guido van Rossumaa9de671992-03-04 16:40:03 +000095}
96
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000097static object *
98imgfile_ttob(self, args)
99 object *self;
100 object *args;
101{
102 int newval;
103 object *rv;
104
105 if (!getargs(args, "i", &newval))
106 return NULL;
107 rv = newintobject(top_to_bottom);
108 top_to_bottom = newval;
109 return rv;
110}
Guido van Rossuma5f61381992-09-03 20:41:22 +0000111
Guido van Rossumaa9de671992-03-04 16:40:03 +0000112static object *
113imgfile_read(self, args)
114 object *self;
115 object *args;
116{
117 char *fname;
118 object *rv;
119 int xsize, ysize, zsize;
120 char *cdatap;
121 long *idatap;
122 static short rs[8192], gs[8192], bs[8192];
123 int x, y;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000124 IMAGE *image;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000125 int yfirst, ylast, ystep;
126
Guido van Rossum2c4be641992-06-03 17:06:36 +0000127 if ( !getargs(args, "s", &fname) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000128 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000129
Guido van Rossuma5f61381992-09-03 20:41:22 +0000130 if ( (image = imgfile_open(fname)) == NULL )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000131 return NULL;
132
133 if ( image->colormap != CM_NORMAL ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000134 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000135 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
136 return NULL;
137 }
138 if ( BPP(image->type) != 1 ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000139 iclose(image);
140 err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
Guido van Rossumaa9de671992-03-04 16:40:03 +0000141 return NULL;
142 }
143 xsize = image->xsize;
144 ysize = image->ysize;
145 zsize = image->zsize;
146 if ( zsize != 1 && zsize != 3) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000147 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000148 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
149 return NULL;
150 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000151 if ( xsize > 8192 ) {
152 iclose(image);
153 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
154 return NULL;
155 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000156
157 if ( zsize == 3 ) zsize = 4;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000158 rv = newsizedstringobject((char *)NULL, xsize*ysize*zsize);
159 if ( rv == NULL ) {
160 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000161 return NULL;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000162 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000163 cdatap = getstringvalue(rv);
164 idatap = (long *)cdatap;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000165
166 if (top_to_bottom) {
167 yfirst = ysize-1;
168 ylast = -1;
169 ystep = -1;
170 } else {
171 yfirst = 0;
172 ylast = ysize;
173 ystep = 1;
174 }
175 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Guido van Rossumaa9de671992-03-04 16:40:03 +0000176 if ( zsize == 1 ) {
177 getrow(image, rs, y, 0);
178 for(x=0; x<xsize; x++ )
179 *cdatap++ = rs[x];
180 } else {
181 getrow(image, rs, y, 0);
182 getrow(image, gs, y, 1);
183 getrow(image, bs, y, 2);
184 for(x=0; x<xsize; x++ )
185 *idatap++ = (rs[x] & 0xff) |
186 ((gs[x] & 0xff)<<8) |
187 ((bs[x] & 0xff)<<16);
188 }
189 }
Jack Jansen3accf981992-08-20 11:54:27 +0000190 iclose(image);
Guido van Rossum3e941971992-03-23 18:21:32 +0000191 if ( error_called ) {
192 DECREF(rv);
193 return NULL;
194 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000195 return rv;
196}
197
Guido van Rossum234f9421993-06-17 12:35:49 +0000198static IMAGE *glob_image;
199static long *glob_datap;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000200static int glob_width, glob_z, glob_ysize;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000201
202static
203xs_get(buf, y)
204 short *buf;
205 int y;
206{
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000207 if (top_to_bottom)
208 getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
209 else
210 getrow(glob_image, buf, y, glob_z);
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000211}
212
213static
214xs_put_c(buf, y)
215 short *buf;
216 int y;
217{
218 char *datap = (char *)glob_datap + y*glob_width;
219 int width = glob_width;
220
221 while ( width-- )
222 *datap++ = (*buf++) & 0xff;
223}
224
225static
226xs_put_0(buf, y)
227 short *buf;
228 int y;
229{
230 long *datap = glob_datap + y*glob_width;
231 int width = glob_width;
232
233 while ( width-- )
234 *datap++ = (*buf++) & 0xff;
235}
236static
237xs_put_12(buf, y)
238 short *buf;
239 int y;
240{
241 long *datap = glob_datap + y*glob_width;
242 int width = glob_width;
243
244 while ( width-- )
245 *datap++ |= ((*buf++) & 0xff) << (glob_z*8);
246}
247
248static void
249xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
250 IMAGE *image;
251 int xsize, ysize, zsize;
252 long *datap;
253 int xnew, ynew;
254 int fmode;
255 double blur;
256{
257 glob_image = image;
258 glob_datap = datap;
259 glob_width = xnew;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000260 glob_ysize = ysize;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000261 if ( zsize == 1 ) {
262 glob_z = 0;
263 filterzoom(xs_get, xs_put_c, xsize, ysize, xnew, ynew, fmode, blur);
264 } else {
265 glob_z = 0;
266 filterzoom(xs_get, xs_put_0, xsize, ysize, xnew, ynew, fmode, blur);
267 glob_z = 1;
268 filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
269 glob_z = 2;
270 filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
271 }
272}
273
Guido van Rossuma5f61381992-09-03 20:41:22 +0000274
Guido van Rossumaa9de671992-03-04 16:40:03 +0000275static object *
Guido van Rossum2c4be641992-06-03 17:06:36 +0000276imgfile_readscaled(self, args)
277 object *self;
278 object *args;
279{
280 char *fname;
281 object *rv;
282 int xsize, ysize, zsize;
283 char *cdatap;
284 long *idatap;
285 static short rs[8192], gs[8192], bs[8192];
286 int x, y;
287 int xwtd, ywtd, xorig, yorig;
288 float xfac, yfac;
289 int cnt;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000290 IMAGE *image;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000291 char *filter;
292 double blur;
293 int extended;
294 int fmode;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000295 int yfirst, ylast, ystep;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000296
297 /*
298 ** Parse args. Funny, since arg 4 and 5 are optional
299 ** (filter name and blur factor). Also, 4 or 5 arguments indicates
300 ** extended scale algorithm in stead of simple-minded pixel drop/dup.
301 */
302 extended = 0;
303 cnt = gettuplesize(args);
304 if ( cnt == 5 ) {
305 extended = 1;
306 if ( !getargs(args, "(siisd)", &fname, &xwtd, &ywtd, &filter, &blur) )
307 return NULL;
308 } else if ( cnt == 4 ) {
309 extended = 1;
310 if ( !getargs(args, "(siis)", &fname, &xwtd, &ywtd, &filter) )
311 return NULL;
312 blur = 1.0;
313 } else if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000314 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000315
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000316 /*
317 ** Check parameters, open file and check type, rows, etc.
318 */
319 if ( extended ) {
320 if ( strcmp(filter, "impulse") == 0 ) fmode = IMPULSE;
321 else if ( strcmp( filter, "box") == 0 ) fmode = BOX;
322 else if ( strcmp( filter, "triangle") == 0 ) fmode = TRIANGLE;
323 else if ( strcmp( filter, "quadratic") == 0 ) fmode = QUADRATIC;
324 else if ( strcmp( filter, "gaussian") == 0 ) fmode = GAUSSIAN;
325 else {
326 err_setstr(ImgfileError, "Unknown filter type");
327 return NULL;
328 }
329 }
330
Guido van Rossuma5f61381992-09-03 20:41:22 +0000331 if ( (image = imgfile_open(fname)) == NULL )
Guido van Rossum2c4be641992-06-03 17:06:36 +0000332 return NULL;
333
334 if ( image->colormap != CM_NORMAL ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000335 iclose(image);
Guido van Rossum2c4be641992-06-03 17:06:36 +0000336 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
337 return NULL;
338 }
339 if ( BPP(image->type) != 1 ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000340 iclose(image);
341 err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
Guido van Rossum2c4be641992-06-03 17:06:36 +0000342 return NULL;
343 }
344 xsize = image->xsize;
345 ysize = image->ysize;
346 zsize = image->zsize;
347 if ( zsize != 1 && zsize != 3) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000348 iclose(image);
Guido van Rossum2c4be641992-06-03 17:06:36 +0000349 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
350 return NULL;
351 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000352 if ( xsize > 8192 ) {
353 iclose(image);
354 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
355 return NULL;
356 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000357
358 if ( zsize == 3 ) zsize = 4;
359 rv = newsizedstringobject(NULL, xwtd*ywtd*zsize);
Guido van Rossuma5f61381992-09-03 20:41:22 +0000360 if ( rv == NULL ) {
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000361 iclose(image);
362 return NULL;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000363 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000364 xfac = (float)xsize/(float)xwtd;
365 yfac = (float)ysize/(float)ywtd;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000366 cdatap = getstringvalue(rv);
367 idatap = (long *)cdatap;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000368
369 if ( extended ) {
370 xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur);
371 } else {
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000372 if (top_to_bottom) {
373 yfirst = ywtd-1;
374 ylast = -1;
375 ystep = -1;
376 } else {
377 yfirst = 0;
378 ylast = ywtd;
379 ystep = 1;
380 }
381 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000382 yorig = (int)(y*yfac);
383 if ( zsize == 1 ) {
384 getrow(image, rs, yorig, 0);
385 for(x=0; x<xwtd; x++ ) {
386 *cdatap++ = rs[(int)(x*xfac)];
387 }
388 } else {
389 getrow(image, rs, yorig, 0);
390 getrow(image, gs, yorig, 1);
391 getrow(image, bs, yorig, 2);
392 for(x=0; x<xwtd; x++ ) {
393 xorig = (int)(x*xfac);
394 *idatap++ = (rs[xorig] & 0xff) |
395 ((gs[xorig] & 0xff)<<8) |
396 ((bs[xorig] & 0xff)<<16);
397 }
398 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000399 }
400 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000401 iclose(image);
Guido van Rossum2c4be641992-06-03 17:06:36 +0000402 if ( error_called ) {
403 DECREF(rv);
404 return NULL;
405 }
406 return rv;
407}
408
409static object *
Guido van Rossumaa9de671992-03-04 16:40:03 +0000410imgfile_getsizes(self, args)
411 object *self;
412 object *args;
413{
414 char *fname;
415 object *rv;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000416 IMAGE *image;
Guido van Rossumaa9de671992-03-04 16:40:03 +0000417
Guido van Rossum2c4be641992-06-03 17:06:36 +0000418 if ( !getargs(args, "s", &fname) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000419 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000420
Guido van Rossuma5f61381992-09-03 20:41:22 +0000421 if ( (image = imgfile_open(fname)) == NULL )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000422 return NULL;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000423 rv = mkvalue("(iii)", image->xsize, image->ysize, image->zsize);
424 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000425 return rv;
426}
427
Jack Jansen3accf981992-08-20 11:54:27 +0000428static object *
429imgfile_write(self, args)
430 object *self;
431 object *args;
432{
433 IMAGE *image;
434 char *fname;
435 int xsize, ysize, zsize, len;
436 char *cdatap;
437 long *idatap;
438 short rs[8192], gs[8192], bs[8192];
439 short r, g, b;
440 long rgb;
441 int x, y;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000442 int yfirst, ylast, ystep;
443
Jack Jansen3accf981992-08-20 11:54:27 +0000444
445 if ( !getargs(args, "(ss#iii)",
446 &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000447 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000448
449 if ( zsize != 1 && zsize != 3 ) {
450 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
Guido van Rossuma5f61381992-09-03 20:41:22 +0000451 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000452 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000453 if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
Jack Jansen3accf981992-08-20 11:54:27 +0000454 err_setstr(ImgfileError, "Data does not match sizes");
Guido van Rossuma5f61381992-09-03 20:41:22 +0000455 return NULL;
456 }
457 if ( xsize > 8192 ) {
458 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
459 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000460 }
461
Guido van Rossuma5f61381992-09-03 20:41:22 +0000462 error_called = 0;
463 errno = 0;
Jack Jansen3accf981992-08-20 11:54:27 +0000464 image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
465 if ( image == 0 ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000466 if ( ! error_called ) {
467 if (errno)
468 err_errno(ImgfileError);
469 else
470 err_setstr(ImgfileError, "Can't create image file");
471 }
472 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000473 }
474
475 idatap = (long *)cdatap;
476
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000477 if (top_to_bottom) {
478 yfirst = ysize-1;
479 ylast = -1;
480 ystep = -1;
481 } else {
482 yfirst = 0;
483 ylast = ysize;
484 ystep = 1;
485 }
486 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Jack Jansen3accf981992-08-20 11:54:27 +0000487 if ( zsize == 1 ) {
488 for( x=0; x<xsize; x++ )
489 rs[x] = *cdatap++;
490 putrow(image, rs, y, 0);
491 } else {
492 for( x=0; x<xsize; x++ ) {
493 rgb = *idatap++;
494 r = rgb & 0xff;
495 g = (rgb >> 8 ) & 0xff;
496 b = (rgb >> 16 ) & 0xff;
497 rs[x] = r;
498 gs[x] = g;
499 bs[x] = b;
500 }
501 putrow(image, rs, y, 0);
502 putrow(image, gs, y, 1);
503 putrow(image, bs, y, 2);
504 }
505 }
506 iclose(image);
507 if ( error_called )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000508 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000509 INCREF(None);
510 return None;
511
512}
Guido van Rossumaa9de671992-03-04 16:40:03 +0000513
Guido van Rossuma5f61381992-09-03 20:41:22 +0000514
Guido van Rossumaa9de671992-03-04 16:40:03 +0000515static struct methodlist imgfile_methods[] = {
Guido van Rossum2c4be641992-06-03 17:06:36 +0000516 { "getsizes", imgfile_getsizes },
517 { "read", imgfile_read },
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000518 { "readscaled", imgfile_readscaled, 1},
Jack Jansen3accf981992-08-20 11:54:27 +0000519 { "write", imgfile_write },
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000520 { "ttob", imgfile_ttob },
Guido van Rossuma5f61381992-09-03 20:41:22 +0000521 { NULL, NULL } /* Sentinel */
Guido van Rossumaa9de671992-03-04 16:40:03 +0000522};
523
524
525void
526initimgfile()
527{
528 object *m, *d;
529 m = initmodule("imgfile", imgfile_methods);
530 d = getmoduledict(m);
531 ImgfileError = newstringobject("imgfile.error");
Guido van Rossuma5f61381992-09-03 20:41:22 +0000532 if ( ImgfileError == NULL || dictinsert(d, "error", ImgfileError) )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000533 fatal("can't define imgfile.error");
534}