blob: 4c452d656c957b264ce111e8c6c9f14162e4dd00 [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
46
47static
48imgfile_open(args)
49 object *args;
50{
51 char *fname;
52
53 if ( !getargs(args, "s", &fname) )
54 return 0;
55 if ( image != NULL && strcmp(fname, gfname) != 0 ) {
56 iclose(image);
57 image = NULL;
58 gfname[0] = '\0';
59 }
60 if ( (image=iopen(fname, "r")) == NULL ) {
61 err_setstr(ImgfileError, "Cannot open image file");
62 return 0;
63 }
64 strcpy(gfname, fname);
65 return 1;
66}
67
68static object *
69imgfile_read(self, args)
70 object *self;
71 object *args;
72{
73 char *fname;
74 object *rv;
75 int xsize, ysize, zsize;
76 char *cdatap;
77 long *idatap;
78 static short rs[8192], gs[8192], bs[8192];
79 int x, y;
80
81 if ( !imgfile_open(args) )
82 return NULL;
83
84 if ( image->colormap != CM_NORMAL ) {
85 err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
86 return NULL;
87 }
88 if ( BPP(image->type) != 1 ) {
89 err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1");
90 return NULL;
91 }
92 xsize = image->xsize;
93 ysize = image->ysize;
94 zsize = image->zsize;
95 if ( zsize != 1 && zsize != 3) {
96 err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
97 return NULL;
98 }
99
100 if ( zsize == 3 ) zsize = 4;
101 rv = newsizedstringobject(NULL, xsize*ysize*zsize);
102 if ( rv == NULL )
103 return NULL;
104 cdatap = getstringvalue(rv);
105 idatap = (long *)cdatap;
106 for ( y=0; y < ysize; y++ ) {
107 if ( zsize == 1 ) {
108 getrow(image, rs, y, 0);
109 for(x=0; x<xsize; x++ )
110 *cdatap++ = rs[x];
111 } else {
112 getrow(image, rs, y, 0);
113 getrow(image, gs, y, 1);
114 getrow(image, bs, y, 2);
115 for(x=0; x<xsize; x++ )
116 *idatap++ = (rs[x] & 0xff) |
117 ((gs[x] & 0xff)<<8) |
118 ((bs[x] & 0xff)<<16);
119 }
120 }
121 return rv;
122}
123
124static object *
125imgfile_getsizes(self, args)
126 object *self;
127 object *args;
128{
129 char *fname;
130 object *rv;
131
132 if ( !imgfile_open(args) )
133 return NULL;
134 rv = newtupleobject(3);
135 if ( rv == NULL )
136 return NULL;
137 settupleitem(rv, 0, newintobject(image->xsize));
138 settupleitem(rv, 1, newintobject(image->ysize));
139 settupleitem(rv, 2, newintobject(image->zsize));
140 return rv;
141}
142
143
144static struct methodlist imgfile_methods[] = {
145 { "getsizes", imgfile_getsizes },
146 { "read", imgfile_read },
147 { 0, 0 }
148};
149
150
151void
152initimgfile()
153{
154 object *m, *d;
155 m = initmodule("imgfile", imgfile_methods);
156 d = getmoduledict(m);
157 ImgfileError = newstringobject("imgfile.error");
158 if ( ImgfileError == NULL || dictinsert(d,"error",ImgfileError) )
159 fatal("can't define imgfile.error");
160}