Don't reference removed files in Makefile
[python/dscho.git] / Modules / imgfile.c
blob9c318be136c84d4104bea5fc2f7a494272024c3e
1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3 The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* IMGFILE module - Interface to sgi libimage */
27 /* XXX 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).
33 ** Warning: this module is very non-reentrant (esp. the readscaled stuff)
36 #include "allobjects.h"
37 #include "modsupport.h"
39 #include <gl/image.h>
40 #include <errno.h>
42 #include "/usr/people/4Dgifts/iristools/include/izoom.h"
44 static object * ImgfileError; /* Exception we raise for various trouble */
46 static int top_to_bottom; /* True if we want top-to-bottom images */
48 /* The image library does not always call the error hander :-(,
49 therefore we have a global variable indicating that it was called.
50 It is cleared by imgfile_open(). */
52 static int error_called;
55 /* The error handler */
57 static imgfile_error(str)
58 char *str;
60 err_setstr(ImgfileError, str);
61 error_called = 1;
62 return; /* To imglib, which will return a failure indicator */
66 /* Open an image file and return a pointer to it.
67 Make sure we raise an exception if we fail. */
69 static IMAGE *
70 imgfile_open(fname)
71 char *fname;
73 IMAGE *image;
74 i_seterror(imgfile_error);
75 error_called = 0;
76 errno = 0;
77 if ( (image = iopen(fname, "r")) == NULL ) {
78 /* Error may already be set by imgfile_error */
79 if ( !error_called ) {
80 if (errno)
81 err_errno(ImgfileError);
82 else
83 err_setstr(ImgfileError, "Can't open image file");
85 return NULL;
87 return image;
90 static object *
91 imgfile_ttob(self, args)
92 object *self;
93 object *args;
95 int newval;
96 object *rv;
98 if (!getargs(args, "i", &newval))
99 return NULL;
100 rv = newintobject(top_to_bottom);
101 top_to_bottom = newval;
102 return rv;
105 static object *
106 imgfile_read(self, args)
107 object *self;
108 object *args;
110 char *fname;
111 object *rv;
112 int xsize, ysize, zsize;
113 char *cdatap;
114 long *idatap;
115 static short rs[8192], gs[8192], bs[8192];
116 int x, y;
117 IMAGE *image;
118 int yfirst, ylast, ystep;
120 if ( !getargs(args, "s", &fname) )
121 return NULL;
123 if ( (image = imgfile_open(fname)) == NULL )
124 return NULL;
126 if ( image->colormap != CM_NORMAL ) {
127 iclose(image);
128 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
129 return NULL;
131 if ( BPP(image->type) != 1 ) {
132 iclose(image);
133 err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
134 return NULL;
136 xsize = image->xsize;
137 ysize = image->ysize;
138 zsize = image->zsize;
139 if ( zsize != 1 && zsize != 3) {
140 iclose(image);
141 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
142 return NULL;
144 if ( xsize > 8192 ) {
145 iclose(image);
146 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
147 return NULL;
150 if ( zsize == 3 ) zsize = 4;
151 rv = newsizedstringobject((char *)NULL, xsize*ysize*zsize);
152 if ( rv == NULL ) {
153 iclose(image);
154 return NULL;
156 cdatap = getstringvalue(rv);
157 idatap = (long *)cdatap;
159 if (top_to_bottom) {
160 yfirst = ysize-1;
161 ylast = -1;
162 ystep = -1;
163 } else {
164 yfirst = 0;
165 ylast = ysize;
166 ystep = 1;
168 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
169 if ( zsize == 1 ) {
170 getrow(image, rs, y, 0);
171 for(x=0; x<xsize; x++ )
172 *cdatap++ = rs[x];
173 } else {
174 getrow(image, rs, y, 0);
175 getrow(image, gs, y, 1);
176 getrow(image, bs, y, 2);
177 for(x=0; x<xsize; x++ )
178 *idatap++ = (rs[x] & 0xff) |
179 ((gs[x] & 0xff)<<8) |
180 ((bs[x] & 0xff)<<16);
183 iclose(image);
184 if ( error_called ) {
185 DECREF(rv);
186 return NULL;
188 return rv;
191 static IMAGE *glob_image;
192 static long *glob_datap;
193 static int glob_width, glob_z, glob_ysize;
195 static
196 xs_get(buf, y)
197 short *buf;
198 int y;
200 if (top_to_bottom)
201 getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
202 else
203 getrow(glob_image, buf, y, glob_z);
206 static
207 xs_put_c(buf, y)
208 short *buf;
209 int y;
211 char *datap = (char *)glob_datap + y*glob_width;
212 int width = glob_width;
214 while ( width-- )
215 *datap++ = (*buf++) & 0xff;
218 static
219 xs_put_0(buf, y)
220 short *buf;
221 int y;
223 long *datap = glob_datap + y*glob_width;
224 int width = glob_width;
226 while ( width-- )
227 *datap++ = (*buf++) & 0xff;
229 static
230 xs_put_12(buf, y)
231 short *buf;
232 int y;
234 long *datap = glob_datap + y*glob_width;
235 int width = glob_width;
237 while ( width-- )
238 *datap++ |= ((*buf++) & 0xff) << (glob_z*8);
241 static void
242 xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
243 IMAGE *image;
244 int xsize, ysize, zsize;
245 long *datap;
246 int xnew, ynew;
247 int fmode;
248 double blur;
250 glob_image = image;
251 glob_datap = datap;
252 glob_width = xnew;
253 glob_ysize = ysize;
254 if ( zsize == 1 ) {
255 glob_z = 0;
256 filterzoom(xs_get, xs_put_c, xsize, ysize, xnew, ynew, fmode, blur);
257 } else {
258 glob_z = 0;
259 filterzoom(xs_get, xs_put_0, xsize, ysize, xnew, ynew, fmode, blur);
260 glob_z = 1;
261 filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
262 glob_z = 2;
263 filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
268 static object *
269 imgfile_readscaled(self, args)
270 object *self;
271 object *args;
273 char *fname;
274 object *rv;
275 int xsize, ysize, zsize;
276 char *cdatap;
277 long *idatap;
278 static short rs[8192], gs[8192], bs[8192];
279 int x, y;
280 int xwtd, ywtd, xorig, yorig;
281 float xfac, yfac;
282 int cnt;
283 IMAGE *image;
284 char *filter;
285 double blur;
286 int extended;
287 int fmode;
288 int yfirst, ylast, ystep;
291 ** Parse args. Funny, since arg 4 and 5 are optional
292 ** (filter name and blur factor). Also, 4 or 5 arguments indicates
293 ** extended scale algorithm in stead of simple-minded pixel drop/dup.
295 extended = 0;
296 cnt = gettuplesize(args);
297 if ( cnt == 5 ) {
298 extended = 1;
299 if ( !getargs(args, "(siisd)", &fname, &xwtd, &ywtd, &filter, &blur) )
300 return NULL;
301 } else if ( cnt == 4 ) {
302 extended = 1;
303 if ( !getargs(args, "(siis)", &fname, &xwtd, &ywtd, &filter) )
304 return NULL;
305 blur = 1.0;
306 } else if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) )
307 return NULL;
310 ** Check parameters, open file and check type, rows, etc.
312 if ( extended ) {
313 if ( strcmp(filter, "impulse") == 0 ) fmode = IMPULSE;
314 else if ( strcmp( filter, "box") == 0 ) fmode = BOX;
315 else if ( strcmp( filter, "triangle") == 0 ) fmode = TRIANGLE;
316 else if ( strcmp( filter, "quadratic") == 0 ) fmode = QUADRATIC;
317 else if ( strcmp( filter, "gaussian") == 0 ) fmode = GAUSSIAN;
318 else {
319 err_setstr(ImgfileError, "Unknown filter type");
320 return NULL;
324 if ( (image = imgfile_open(fname)) == NULL )
325 return NULL;
327 if ( image->colormap != CM_NORMAL ) {
328 iclose(image);
329 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
330 return NULL;
332 if ( BPP(image->type) != 1 ) {
333 iclose(image);
334 err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
335 return NULL;
337 xsize = image->xsize;
338 ysize = image->ysize;
339 zsize = image->zsize;
340 if ( zsize != 1 && zsize != 3) {
341 iclose(image);
342 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
343 return NULL;
345 if ( xsize > 8192 ) {
346 iclose(image);
347 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
348 return NULL;
351 if ( zsize == 3 ) zsize = 4;
352 rv = newsizedstringobject(NULL, xwtd*ywtd*zsize);
353 if ( rv == NULL ) {
354 iclose(image);
355 return NULL;
357 xfac = (float)xsize/(float)xwtd;
358 yfac = (float)ysize/(float)ywtd;
359 cdatap = getstringvalue(rv);
360 idatap = (long *)cdatap;
362 if ( extended ) {
363 xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur);
364 } else {
365 if (top_to_bottom) {
366 yfirst = ywtd-1;
367 ylast = -1;
368 ystep = -1;
369 } else {
370 yfirst = 0;
371 ylast = ywtd;
372 ystep = 1;
374 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
375 yorig = (int)(y*yfac);
376 if ( zsize == 1 ) {
377 getrow(image, rs, yorig, 0);
378 for(x=0; x<xwtd; x++ ) {
379 *cdatap++ = rs[(int)(x*xfac)];
381 } else {
382 getrow(image, rs, yorig, 0);
383 getrow(image, gs, yorig, 1);
384 getrow(image, bs, yorig, 2);
385 for(x=0; x<xwtd; x++ ) {
386 xorig = (int)(x*xfac);
387 *idatap++ = (rs[xorig] & 0xff) |
388 ((gs[xorig] & 0xff)<<8) |
389 ((bs[xorig] & 0xff)<<16);
394 iclose(image);
395 if ( error_called ) {
396 DECREF(rv);
397 return NULL;
399 return rv;
402 static object *
403 imgfile_getsizes(self, args)
404 object *self;
405 object *args;
407 char *fname;
408 object *rv;
409 IMAGE *image;
411 if ( !getargs(args, "s", &fname) )
412 return NULL;
414 if ( (image = imgfile_open(fname)) == NULL )
415 return NULL;
416 rv = mkvalue("(iii)", image->xsize, image->ysize, image->zsize);
417 iclose(image);
418 return rv;
421 static object *
422 imgfile_write(self, args)
423 object *self;
424 object *args;
426 IMAGE *image;
427 char *fname;
428 int xsize, ysize, zsize, len;
429 char *cdatap;
430 long *idatap;
431 short rs[8192], gs[8192], bs[8192];
432 short r, g, b;
433 long rgb;
434 int x, y;
435 int yfirst, ylast, ystep;
438 if ( !getargs(args, "(ss#iii)",
439 &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
440 return NULL;
442 if ( zsize != 1 && zsize != 3 ) {
443 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
444 return NULL;
446 if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
447 err_setstr(ImgfileError, "Data does not match sizes");
448 return NULL;
450 if ( xsize > 8192 ) {
451 err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
452 return NULL;
455 error_called = 0;
456 errno = 0;
457 image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
458 if ( image == 0 ) {
459 if ( ! error_called ) {
460 if (errno)
461 err_errno(ImgfileError);
462 else
463 err_setstr(ImgfileError, "Can't create image file");
465 return NULL;
468 idatap = (long *)cdatap;
470 if (top_to_bottom) {
471 yfirst = ysize-1;
472 ylast = -1;
473 ystep = -1;
474 } else {
475 yfirst = 0;
476 ylast = ysize;
477 ystep = 1;
479 for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
480 if ( zsize == 1 ) {
481 for( x=0; x<xsize; x++ )
482 rs[x] = *cdatap++;
483 putrow(image, rs, y, 0);
484 } else {
485 for( x=0; x<xsize; x++ ) {
486 rgb = *idatap++;
487 r = rgb & 0xff;
488 g = (rgb >> 8 ) & 0xff;
489 b = (rgb >> 16 ) & 0xff;
490 rs[x] = r;
491 gs[x] = g;
492 bs[x] = b;
494 putrow(image, rs, y, 0);
495 putrow(image, gs, y, 1);
496 putrow(image, bs, y, 2);
499 iclose(image);
500 if ( error_called )
501 return NULL;
502 INCREF(None);
503 return None;
508 static struct methodlist imgfile_methods[] = {
509 { "getsizes", imgfile_getsizes },
510 { "read", imgfile_read },
511 { "readscaled", imgfile_readscaled, 1},
512 { "write", imgfile_write },
513 { "ttob", imgfile_ttob },
514 { NULL, NULL } /* Sentinel */
518 void
519 initimgfile()
521 object *m, *d;
522 m = initmodule("imgfile", imgfile_methods);
523 d = getmoduledict(m);
524 ImgfileError = newstringobject("imgfile.error");
525 if ( ImgfileError == NULL || dictinsert(d, "error", ImgfileError) )
526 fatal("can't define imgfile.error");