blob: 76f123ce1477a0e3b38f4d023573f55b62fc0fcd [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 Rossuma376cc51996-12-05 23:43:35 +000064static void
65imgfile_error(str)
Guido van Rossum3e941971992-03-23 18:21:32 +000066 char *str;
67{
68 err_setstr(ImgfileError, str);
69 error_called = 1;
Guido van Rossum9bfef441993-03-29 10:43:31 +000070 return; /* To imglib, which will return a failure indicator */
Guido van Rossum3e941971992-03-23 18:21:32 +000071}
Guido van Rossumaa9de671992-03-04 16:40:03 +000072
Guido van Rossuma5f61381992-09-03 20:41:22 +000073
74/* Open an image file and return a pointer to it.
75 Make sure we raise an exception if we fail. */
76
77static IMAGE *
Guido van Rossum2c4be641992-06-03 17:06:36 +000078imgfile_open(fname)
Guido van Rossumaa9de671992-03-04 16:40:03 +000079 char *fname;
Guido van Rossum2c4be641992-06-03 17:06:36 +000080{
Guido van Rossuma5f61381992-09-03 20:41:22 +000081 IMAGE *image;
Guido van Rossum3e941971992-03-23 18:21:32 +000082 i_seterror(imgfile_error);
83 error_called = 0;
Guido van Rossuma5f61381992-09-03 20:41:22 +000084 errno = 0;
85 if ( (image = iopen(fname, "r")) == NULL ) {
Guido van Rossum3e941971992-03-23 18:21:32 +000086 /* Error may already be set by imgfile_error */
Guido van Rossuma5f61381992-09-03 20:41:22 +000087 if ( !error_called ) {
88 if (errno)
89 err_errno(ImgfileError);
90 else
91 err_setstr(ImgfileError, "Can't open image file");
92 }
93 return NULL;
Guido van Rossumaa9de671992-03-04 16:40:03 +000094 }
Guido van Rossuma5f61381992-09-03 20:41:22 +000095 return image;
Guido van Rossumaa9de671992-03-04 16:40:03 +000096}
97
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +000098static object *
99imgfile_ttob(self, args)
100 object *self;
101 object *args;
102{
103 int newval;
104 object *rv;
105
106 if (!getargs(args, "i", &newval))
107 return NULL;
108 rv = newintobject(top_to_bottom);
109 top_to_bottom = newval;
110 return rv;
111}
Guido van Rossuma5f61381992-09-03 20:41:22 +0000112
Guido van Rossumaa9de671992-03-04 16:40:03 +0000113static object *
114imgfile_read(self, args)
115 object *self;
116 object *args;
117{
118 char *fname;
119 object *rv;
120 int xsize, ysize, zsize;
121 char *cdatap;
122 long *idatap;
123 static short rs[8192], gs[8192], bs[8192];
124 int x, y;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000125 IMAGE *image;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000126 int yfirst, ylast, ystep;
127
Guido van Rossum2c4be641992-06-03 17:06:36 +0000128 if ( !getargs(args, "s", &fname) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000129 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000130
Guido van Rossuma5f61381992-09-03 20:41:22 +0000131 if ( (image = imgfile_open(fname)) == NULL )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000132 return NULL;
133
134 if ( image->colormap != CM_NORMAL ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000135 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000136 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
137 return NULL;
138 }
139 if ( BPP(image->type) != 1 ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000140 iclose(image);
141 err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
Guido van Rossumaa9de671992-03-04 16:40:03 +0000142 return NULL;
143 }
144 xsize = image->xsize;
145 ysize = image->ysize;
146 zsize = image->zsize;
147 if ( zsize != 1 && zsize != 3) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000148 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000149 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
150 return NULL;
151 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000152 if ( xsize > 8192 ) {
153 iclose(image);
154 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
155 return NULL;
156 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000157
158 if ( zsize == 3 ) zsize = 4;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000159 rv = newsizedstringobject((char *)NULL, xsize*ysize*zsize);
160 if ( rv == NULL ) {
161 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000162 return NULL;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000163 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000164 cdatap = getstringvalue(rv);
165 idatap = (long *)cdatap;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000166
167 if (top_to_bottom) {
168 yfirst = ysize-1;
169 ylast = -1;
170 ystep = -1;
171 } else {
172 yfirst = 0;
173 ylast = ysize;
174 ystep = 1;
175 }
176 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Guido van Rossumaa9de671992-03-04 16:40:03 +0000177 if ( zsize == 1 ) {
178 getrow(image, rs, y, 0);
179 for(x=0; x<xsize; x++ )
180 *cdatap++ = rs[x];
181 } else {
182 getrow(image, rs, y, 0);
183 getrow(image, gs, y, 1);
184 getrow(image, bs, y, 2);
185 for(x=0; x<xsize; x++ )
186 *idatap++ = (rs[x] & 0xff) |
187 ((gs[x] & 0xff)<<8) |
188 ((bs[x] & 0xff)<<16);
189 }
190 }
Jack Jansen3accf981992-08-20 11:54:27 +0000191 iclose(image);
Guido van Rossum3e941971992-03-23 18:21:32 +0000192 if ( error_called ) {
193 DECREF(rv);
194 return NULL;
195 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000196 return rv;
197}
198
Guido van Rossum234f9421993-06-17 12:35:49 +0000199static IMAGE *glob_image;
200static long *glob_datap;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000201static int glob_width, glob_z, glob_ysize;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000202
Guido van Rossuma376cc51996-12-05 23:43:35 +0000203static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000204xs_get(buf, y)
205 short *buf;
206 int y;
207{
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000208 if (top_to_bottom)
209 getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
210 else
211 getrow(glob_image, buf, y, glob_z);
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000212}
213
Guido van Rossuma376cc51996-12-05 23:43:35 +0000214static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000215xs_put_c(buf, y)
216 short *buf;
217 int y;
218{
219 char *datap = (char *)glob_datap + y*glob_width;
220 int width = glob_width;
221
222 while ( width-- )
223 *datap++ = (*buf++) & 0xff;
224}
225
Guido van Rossuma376cc51996-12-05 23:43:35 +0000226static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000227xs_put_0(buf, y)
228 short *buf;
229 int y;
230{
231 long *datap = glob_datap + y*glob_width;
232 int width = glob_width;
233
234 while ( width-- )
235 *datap++ = (*buf++) & 0xff;
236}
Guido van Rossuma376cc51996-12-05 23:43:35 +0000237static void
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000238xs_put_12(buf, y)
239 short *buf;
240 int y;
241{
242 long *datap = glob_datap + y*glob_width;
243 int width = glob_width;
244
245 while ( width-- )
246 *datap++ |= ((*buf++) & 0xff) << (glob_z*8);
247}
248
249static void
250xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
251 IMAGE *image;
252 int xsize, ysize, zsize;
253 long *datap;
254 int xnew, ynew;
255 int fmode;
256 double blur;
257{
258 glob_image = image;
259 glob_datap = datap;
260 glob_width = xnew;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000261 glob_ysize = ysize;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000262 if ( zsize == 1 ) {
263 glob_z = 0;
264 filterzoom(xs_get, xs_put_c, xsize, ysize, xnew, ynew, fmode, blur);
265 } else {
266 glob_z = 0;
267 filterzoom(xs_get, xs_put_0, xsize, ysize, xnew, ynew, fmode, blur);
268 glob_z = 1;
269 filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
270 glob_z = 2;
271 filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
272 }
273}
274
Guido van Rossuma5f61381992-09-03 20:41:22 +0000275
Guido van Rossumaa9de671992-03-04 16:40:03 +0000276static object *
Guido van Rossum2c4be641992-06-03 17:06:36 +0000277imgfile_readscaled(self, args)
278 object *self;
279 object *args;
280{
281 char *fname;
282 object *rv;
283 int xsize, ysize, zsize;
284 char *cdatap;
285 long *idatap;
286 static short rs[8192], gs[8192], bs[8192];
287 int x, y;
288 int xwtd, ywtd, xorig, yorig;
289 float xfac, yfac;
290 int cnt;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000291 IMAGE *image;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000292 char *filter;
293 double blur;
294 int extended;
Guido van Rossuma376cc51996-12-05 23:43:35 +0000295 int fmode = 0;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000296 int yfirst, ylast, ystep;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000297
298 /*
299 ** Parse args. Funny, since arg 4 and 5 are optional
300 ** (filter name and blur factor). Also, 4 or 5 arguments indicates
301 ** extended scale algorithm in stead of simple-minded pixel drop/dup.
302 */
303 extended = 0;
304 cnt = gettuplesize(args);
305 if ( cnt == 5 ) {
306 extended = 1;
307 if ( !getargs(args, "(siisd)", &fname, &xwtd, &ywtd, &filter, &blur) )
308 return NULL;
309 } else if ( cnt == 4 ) {
310 extended = 1;
311 if ( !getargs(args, "(siis)", &fname, &xwtd, &ywtd, &filter) )
312 return NULL;
313 blur = 1.0;
314 } else if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000315 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000316
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000317 /*
318 ** Check parameters, open file and check type, rows, etc.
319 */
320 if ( extended ) {
321 if ( strcmp(filter, "impulse") == 0 ) fmode = IMPULSE;
322 else if ( strcmp( filter, "box") == 0 ) fmode = BOX;
323 else if ( strcmp( filter, "triangle") == 0 ) fmode = TRIANGLE;
324 else if ( strcmp( filter, "quadratic") == 0 ) fmode = QUADRATIC;
325 else if ( strcmp( filter, "gaussian") == 0 ) fmode = GAUSSIAN;
326 else {
327 err_setstr(ImgfileError, "Unknown filter type");
328 return NULL;
329 }
330 }
331
Guido van Rossuma5f61381992-09-03 20:41:22 +0000332 if ( (image = imgfile_open(fname)) == NULL )
Guido van Rossum2c4be641992-06-03 17:06:36 +0000333 return NULL;
334
335 if ( image->colormap != CM_NORMAL ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000336 iclose(image);
Guido van Rossum2c4be641992-06-03 17:06:36 +0000337 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
338 return NULL;
339 }
340 if ( BPP(image->type) != 1 ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000341 iclose(image);
342 err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
Guido van Rossum2c4be641992-06-03 17:06:36 +0000343 return NULL;
344 }
345 xsize = image->xsize;
346 ysize = image->ysize;
347 zsize = image->zsize;
348 if ( zsize != 1 && zsize != 3) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000349 iclose(image);
Guido van Rossum2c4be641992-06-03 17:06:36 +0000350 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
351 return NULL;
352 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000353 if ( xsize > 8192 ) {
354 iclose(image);
355 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
356 return NULL;
357 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000358
359 if ( zsize == 3 ) zsize = 4;
360 rv = newsizedstringobject(NULL, xwtd*ywtd*zsize);
Guido van Rossuma5f61381992-09-03 20:41:22 +0000361 if ( rv == NULL ) {
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000362 iclose(image);
363 return NULL;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000364 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000365 xfac = (float)xsize/(float)xwtd;
366 yfac = (float)ysize/(float)ywtd;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000367 cdatap = getstringvalue(rv);
368 idatap = (long *)cdatap;
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000369
370 if ( extended ) {
371 xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur);
372 } else {
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000373 if (top_to_bottom) {
374 yfirst = ywtd-1;
375 ylast = -1;
376 ystep = -1;
377 } else {
378 yfirst = 0;
379 ylast = ywtd;
380 ystep = 1;
381 }
382 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000383 yorig = (int)(y*yfac);
384 if ( zsize == 1 ) {
385 getrow(image, rs, yorig, 0);
386 for(x=0; x<xwtd; x++ ) {
387 *cdatap++ = rs[(int)(x*xfac)];
388 }
389 } else {
390 getrow(image, rs, yorig, 0);
391 getrow(image, gs, yorig, 1);
392 getrow(image, bs, yorig, 2);
393 for(x=0; x<xwtd; x++ ) {
394 xorig = (int)(x*xfac);
395 *idatap++ = (rs[xorig] & 0xff) |
396 ((gs[xorig] & 0xff)<<8) |
397 ((bs[xorig] & 0xff)<<16);
398 }
399 }
Guido van Rossum2c4be641992-06-03 17:06:36 +0000400 }
401 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000402 iclose(image);
Guido van Rossum2c4be641992-06-03 17:06:36 +0000403 if ( error_called ) {
404 DECREF(rv);
405 return NULL;
406 }
407 return rv;
408}
409
410static object *
Guido van Rossumaa9de671992-03-04 16:40:03 +0000411imgfile_getsizes(self, args)
412 object *self;
413 object *args;
414{
415 char *fname;
416 object *rv;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000417 IMAGE *image;
Guido van Rossumaa9de671992-03-04 16:40:03 +0000418
Guido van Rossum2c4be641992-06-03 17:06:36 +0000419 if ( !getargs(args, "s", &fname) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000420 return NULL;
Guido van Rossum2c4be641992-06-03 17:06:36 +0000421
Guido van Rossuma5f61381992-09-03 20:41:22 +0000422 if ( (image = imgfile_open(fname)) == NULL )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000423 return NULL;
Guido van Rossuma5f61381992-09-03 20:41:22 +0000424 rv = mkvalue("(iii)", image->xsize, image->ysize, image->zsize);
425 iclose(image);
Guido van Rossumaa9de671992-03-04 16:40:03 +0000426 return rv;
427}
428
Jack Jansen3accf981992-08-20 11:54:27 +0000429static object *
430imgfile_write(self, args)
431 object *self;
432 object *args;
433{
434 IMAGE *image;
435 char *fname;
436 int xsize, ysize, zsize, len;
437 char *cdatap;
438 long *idatap;
439 short rs[8192], gs[8192], bs[8192];
440 short r, g, b;
441 long rgb;
442 int x, y;
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000443 int yfirst, ylast, ystep;
444
Jack Jansen3accf981992-08-20 11:54:27 +0000445
446 if ( !getargs(args, "(ss#iii)",
447 &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000448 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000449
450 if ( zsize != 1 && zsize != 3 ) {
451 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
Guido van Rossuma5f61381992-09-03 20:41:22 +0000452 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000453 }
Guido van Rossuma5f61381992-09-03 20:41:22 +0000454 if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
Jack Jansen3accf981992-08-20 11:54:27 +0000455 err_setstr(ImgfileError, "Data does not match sizes");
Guido van Rossuma5f61381992-09-03 20:41:22 +0000456 return NULL;
457 }
458 if ( xsize > 8192 ) {
459 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
460 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000461 }
462
Guido van Rossuma5f61381992-09-03 20:41:22 +0000463 error_called = 0;
464 errno = 0;
Jack Jansen3accf981992-08-20 11:54:27 +0000465 image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
466 if ( image == 0 ) {
Guido van Rossuma5f61381992-09-03 20:41:22 +0000467 if ( ! error_called ) {
468 if (errno)
469 err_errno(ImgfileError);
470 else
471 err_setstr(ImgfileError, "Can't create image file");
472 }
473 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000474 }
475
476 idatap = (long *)cdatap;
477
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000478 if (top_to_bottom) {
479 yfirst = ysize-1;
480 ylast = -1;
481 ystep = -1;
482 } else {
483 yfirst = 0;
484 ylast = ysize;
485 ystep = 1;
486 }
487 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
Jack Jansen3accf981992-08-20 11:54:27 +0000488 if ( zsize == 1 ) {
489 for( x=0; x<xsize; x++ )
490 rs[x] = *cdatap++;
491 putrow(image, rs, y, 0);
492 } else {
493 for( x=0; x<xsize; x++ ) {
494 rgb = *idatap++;
495 r = rgb & 0xff;
496 g = (rgb >> 8 ) & 0xff;
497 b = (rgb >> 16 ) & 0xff;
498 rs[x] = r;
499 gs[x] = g;
500 bs[x] = b;
501 }
502 putrow(image, rs, y, 0);
503 putrow(image, gs, y, 1);
504 putrow(image, bs, y, 2);
505 }
506 }
507 iclose(image);
508 if ( error_called )
Guido van Rossuma5f61381992-09-03 20:41:22 +0000509 return NULL;
Jack Jansen3accf981992-08-20 11:54:27 +0000510 INCREF(None);
511 return None;
512
513}
Guido van Rossumaa9de671992-03-04 16:40:03 +0000514
Guido van Rossuma5f61381992-09-03 20:41:22 +0000515
Guido van Rossumaa9de671992-03-04 16:40:03 +0000516static struct methodlist imgfile_methods[] = {
Guido van Rossum2c4be641992-06-03 17:06:36 +0000517 { "getsizes", imgfile_getsizes },
518 { "read", imgfile_read },
Jack Jansen3c2eb5c1993-01-19 15:17:13 +0000519 { "readscaled", imgfile_readscaled, 1},
Jack Jansen3accf981992-08-20 11:54:27 +0000520 { "write", imgfile_write },
Sjoerd Mullendera9c3c221993-10-11 12:54:31 +0000521 { "ttob", imgfile_ttob },
Guido van Rossuma5f61381992-09-03 20:41:22 +0000522 { NULL, NULL } /* Sentinel */
Guido van Rossumaa9de671992-03-04 16:40:03 +0000523};
524
525
526void
527initimgfile()
528{
529 object *m, *d;
530 m = initmodule("imgfile", imgfile_methods);
531 d = getmoduledict(m);
532 ImgfileError = newstringobject("imgfile.error");
Guido van Rossuma5f61381992-09-03 20:41:22 +0000533 if ( ImgfileError == NULL || dictinsert(d, "error", ImgfileError) )
Guido van Rossumaa9de671992-03-04 16:40:03 +0000534 fatal("can't define imgfile.error");
535}