1 /***********************************************************
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
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"
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
)
60 err_setstr(ImgfileError
, str
);
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. */
74 i_seterror(imgfile_error
);
77 if ( (image
= iopen(fname
, "r")) == NULL
) {
78 /* Error may already be set by imgfile_error */
79 if ( !error_called
) {
81 err_errno(ImgfileError
);
83 err_setstr(ImgfileError
, "Can't open image file");
91 imgfile_ttob(self
, args
)
98 if (!getargs(args
, "i", &newval
))
100 rv
= newintobject(top_to_bottom
);
101 top_to_bottom
= newval
;
106 imgfile_read(self
, args
)
112 int xsize
, ysize
, zsize
;
115 static short rs
[8192], gs
[8192], bs
[8192];
118 int yfirst
, ylast
, ystep
;
120 if ( !getargs(args
, "s", &fname
) )
123 if ( (image
= imgfile_open(fname
)) == NULL
)
126 if ( image
->colormap
!= CM_NORMAL
) {
128 err_setstr(ImgfileError
, "Can only handle CM_NORMAL images");
131 if ( BPP(image
->type
) != 1 ) {
133 err_setstr(ImgfileError
, "Can't handle imgfiles with bpp!=1");
136 xsize
= image
->xsize
;
137 ysize
= image
->ysize
;
138 zsize
= image
->zsize
;
139 if ( zsize
!= 1 && zsize
!= 3) {
141 err_setstr(ImgfileError
, "Can only handle 1 or 3 byte pixels");
144 if ( xsize
> 8192 ) {
146 err_setstr(ImgfileError
, "Can't handle image with > 8192 columns");
150 if ( zsize
== 3 ) zsize
= 4;
151 rv
= newsizedstringobject((char *)NULL
, xsize
*ysize
*zsize
);
156 cdatap
= getstringvalue(rv
);
157 idatap
= (long *)cdatap
;
168 for ( y
=yfirst
; y
!= ylast
&& !error_called
; y
+= ystep
) {
170 getrow(image
, rs
, y
, 0);
171 for(x
=0; x
<xsize
; x
++ )
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);
184 if ( error_called
) {
191 static IMAGE
*glob_image
;
192 static long *glob_datap
;
193 static int glob_width
, glob_z
, glob_ysize
;
201 getrow(glob_image
, buf
, (glob_ysize
-1-y
), glob_z
);
203 getrow(glob_image
, buf
, y
, glob_z
);
211 char *datap
= (char *)glob_datap
+ y
*glob_width
;
212 int width
= glob_width
;
215 *datap
++ = (*buf
++) & 0xff;
223 long *datap
= glob_datap
+ y
*glob_width
;
224 int width
= glob_width
;
227 *datap
++ = (*buf
++) & 0xff;
234 long *datap
= glob_datap
+ y
*glob_width
;
235 int width
= glob_width
;
238 *datap
++ |= ((*buf
++) & 0xff) << (glob_z
*8);
242 xscale(image
, xsize
, ysize
, zsize
, datap
, xnew
, ynew
, fmode
, blur
)
244 int xsize
, ysize
, zsize
;
256 filterzoom(xs_get
, xs_put_c
, xsize
, ysize
, xnew
, ynew
, fmode
, blur
);
259 filterzoom(xs_get
, xs_put_0
, xsize
, ysize
, xnew
, ynew
, fmode
, blur
);
261 filterzoom(xs_get
, xs_put_12
, xsize
, ysize
, xnew
, ynew
, fmode
, blur
);
263 filterzoom(xs_get
, xs_put_12
, xsize
, ysize
, xnew
, ynew
, fmode
, blur
);
269 imgfile_readscaled(self
, args
)
275 int xsize
, ysize
, zsize
;
278 static short rs
[8192], gs
[8192], bs
[8192];
280 int xwtd
, ywtd
, xorig
, yorig
;
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.
296 cnt
= gettuplesize(args
);
299 if ( !getargs(args
, "(siisd)", &fname
, &xwtd
, &ywtd
, &filter
, &blur
) )
301 } else if ( cnt
== 4 ) {
303 if ( !getargs(args
, "(siis)", &fname
, &xwtd
, &ywtd
, &filter
) )
306 } else if ( !getargs(args
, "(sii)", &fname
, &xwtd
, &ywtd
) )
310 ** Check parameters, open file and check type, rows, etc.
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
;
319 err_setstr(ImgfileError
, "Unknown filter type");
324 if ( (image
= imgfile_open(fname
)) == NULL
)
327 if ( image
->colormap
!= CM_NORMAL
) {
329 err_setstr(ImgfileError
, "Can only handle CM_NORMAL images");
332 if ( BPP(image
->type
) != 1 ) {
334 err_setstr(ImgfileError
, "Can't handle imgfiles with bpp!=1");
337 xsize
= image
->xsize
;
338 ysize
= image
->ysize
;
339 zsize
= image
->zsize
;
340 if ( zsize
!= 1 && zsize
!= 3) {
342 err_setstr(ImgfileError
, "Can only handle 1 or 3 byte pixels");
345 if ( xsize
> 8192 ) {
347 err_setstr(ImgfileError
, "Can't handle image with > 8192 columns");
351 if ( zsize
== 3 ) zsize
= 4;
352 rv
= newsizedstringobject(NULL
, xwtd
*ywtd
*zsize
);
357 xfac
= (float)xsize
/(float)xwtd
;
358 yfac
= (float)ysize
/(float)ywtd
;
359 cdatap
= getstringvalue(rv
);
360 idatap
= (long *)cdatap
;
363 xscale(image
, xsize
, ysize
, zsize
, idatap
, xwtd
, ywtd
, fmode
, blur
);
374 for ( y
=yfirst
; y
!= ylast
&& !error_called
; y
+= ystep
) {
375 yorig
= (int)(y
*yfac
);
377 getrow(image
, rs
, yorig
, 0);
378 for(x
=0; x
<xwtd
; x
++ ) {
379 *cdatap
++ = rs
[(int)(x
*xfac
)];
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);
395 if ( error_called
) {
403 imgfile_getsizes(self
, args
)
411 if ( !getargs(args
, "s", &fname
) )
414 if ( (image
= imgfile_open(fname
)) == NULL
)
416 rv
= mkvalue("(iii)", image
->xsize
, image
->ysize
, image
->zsize
);
422 imgfile_write(self
, args
)
428 int xsize
, ysize
, zsize
, len
;
431 short rs
[8192], gs
[8192], bs
[8192];
435 int yfirst
, ylast
, ystep
;
438 if ( !getargs(args
, "(ss#iii)",
439 &fname
, &cdatap
, &len
, &xsize
, &ysize
, &zsize
) )
442 if ( zsize
!= 1 && zsize
!= 3 ) {
443 err_setstr(ImgfileError
, "Can only handle 1 or 3 byte pixels");
446 if ( len
!= xsize
* ysize
* (zsize
== 1 ? 1 : 4) ) {
447 err_setstr(ImgfileError
, "Data does not match sizes");
450 if ( xsize
> 8192 ) {
451 err_setstr(ImgfileError
, "Can't handle image with > 8192 columns");
457 image
=iopen(fname
, "w", RLE(1), 3, xsize
, ysize
, zsize
);
459 if ( ! error_called
) {
461 err_errno(ImgfileError
);
463 err_setstr(ImgfileError
, "Can't create image file");
468 idatap
= (long *)cdatap
;
479 for ( y
=yfirst
; y
!= ylast
&& !error_called
; y
+= ystep
) {
481 for( x
=0; x
<xsize
; x
++ )
483 putrow(image
, rs
, y
, 0);
485 for( x
=0; x
<xsize
; x
++ ) {
488 g
= (rgb
>> 8 ) & 0xff;
489 b
= (rgb
>> 16 ) & 0xff;
494 putrow(image
, rs
, y
, 0);
495 putrow(image
, gs
, y
, 1);
496 putrow(image
, bs
, y
, 2);
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 */
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");