blob: 8bf6d0642071dd4b2fdecbf751e14a583a801f99 [file] [log] [blame]
Guido van Rossumaa9de671992-03-04 16:40:03 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
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
27/*XXXX 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).
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
57imgfile_open(args)
58 object *args;
59{
60 char *fname;
61
62 if ( !getargs(args, "s", &fname) )
63 return 0;
Guido van Rossum3e941971992-03-23 18:21:32 +000064 i_seterror(imgfile_error);
65 error_called = 0;
Guido van Rossumaa9de671992-03-04 16:40:03 +000066 if ( image != NULL && strcmp(fname, gfname) != 0 ) {
67 iclose(image);
68 image = NULL;
69 gfname[0] = '\0';
70 }
71 if ( (image=iopen(fname, "r")) == NULL ) {
Guido van Rossum3e941971992-03-23 18:21:32 +000072 /* Error may already be set by imgfile_error */
73 if ( !error_called )
74 err_setstr(ImgfileError, "Cannot open image file");
Guido van Rossumaa9de671992-03-04 16:40:03 +000075 return 0;
76 }
77 strcpy(gfname, fname);
78 return 1;
79}
80
81static object *
82imgfile_read(self, args)
83 object *self;
84 object *args;
85{
86 char *fname;
87 object *rv;
88 int xsize, ysize, zsize;
89 char *cdatap;
90 long *idatap;
91 static short rs[8192], gs[8192], bs[8192];
92 int x, y;
93
94 if ( !imgfile_open(args) )
95 return NULL;
96
97 if ( image->colormap != CM_NORMAL ) {
98 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
99 return NULL;
100 }
101 if ( BPP(image->type) != 1 ) {
102 err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1");
103 return NULL;
104 }
105 xsize = image->xsize;
106 ysize = image->ysize;
107 zsize = image->zsize;
108 if ( zsize != 1 && zsize != 3) {
109 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
110 return NULL;
111 }
112
113 if ( zsize == 3 ) zsize = 4;
114 rv = newsizedstringobject(NULL, xsize*ysize*zsize);
115 if ( rv == NULL )
116 return NULL;
117 cdatap = getstringvalue(rv);
118 idatap = (long *)cdatap;
Guido van Rossum3e941971992-03-23 18:21:32 +0000119 for ( y=0; y < ysize && !error_called; y++ ) {
Guido van Rossumaa9de671992-03-04 16:40:03 +0000120 if ( zsize == 1 ) {
121 getrow(image, rs, y, 0);
122 for(x=0; x<xsize; x++ )
123 *cdatap++ = rs[x];
124 } else {
125 getrow(image, rs, y, 0);
126 getrow(image, gs, y, 1);
127 getrow(image, bs, y, 2);
128 for(x=0; x<xsize; x++ )
129 *idatap++ = (rs[x] & 0xff) |
130 ((gs[x] & 0xff)<<8) |
131 ((bs[x] & 0xff)<<16);
132 }
133 }
Guido van Rossum3e941971992-03-23 18:21:32 +0000134 if ( error_called ) {
135 DECREF(rv);
136 return NULL;
137 }
Guido van Rossumaa9de671992-03-04 16:40:03 +0000138 return rv;
139}
140
141static object *
142imgfile_getsizes(self, args)
143 object *self;
144 object *args;
145{
146 char *fname;
147 object *rv;
148
149 if ( !imgfile_open(args) )
150 return NULL;
151 rv = newtupleobject(3);
152 if ( rv == NULL )
153 return NULL;
154 settupleitem(rv, 0, newintobject(image->xsize));
155 settupleitem(rv, 1, newintobject(image->ysize));
156 settupleitem(rv, 2, newintobject(image->zsize));
157 return rv;
158}
159
160
161static struct methodlist imgfile_methods[] = {
162 { "getsizes", imgfile_getsizes },
163 { "read", imgfile_read },
164 { 0, 0 }
165};
166
167
168void
169initimgfile()
170{
171 object *m, *d;
172 m = initmodule("imgfile", imgfile_methods);
173 d = getmoduledict(m);
174 ImgfileError = newstringobject("imgfile.error");
175 if ( ImgfileError == NULL || dictinsert(d,"error",ImgfileError) )
176 fatal("can't define imgfile.error");
177}