1 | /****************************************************************************** |
---|
2 | * SAGE - Scalable Adaptive Graphics Environment |
---|
3 | * |
---|
4 | * Copyright (C) 2004 Electronic Visualization Laboratory, |
---|
5 | * University of Illinois at Chicago |
---|
6 | * |
---|
7 | * All rights reserved. |
---|
8 | * |
---|
9 | * Redistribution and use in source and binary forms, with or without |
---|
10 | * modification, are permitted provided that the following conditions are met: |
---|
11 | * |
---|
12 | * * Redistributions of source code must retain the above copyright |
---|
13 | * notice, this list of conditions and the following disclaimer. |
---|
14 | * * Redistributions in binary form must reproduce the above |
---|
15 | * copyright notice, this list of conditions and the following disclaimer |
---|
16 | * in the documentation and/or other materials provided with the distribution. |
---|
17 | * * Neither the name of the University of Illinois at Chicago nor |
---|
18 | * the names of its contributors may be used to endorse or promote |
---|
19 | * products derived from this software without specific prior written permission. |
---|
20 | * |
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
22 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
23 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
24 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
25 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
26 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
27 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
28 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
29 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
31 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
32 | * |
---|
33 | * Direct questions, comments etc about SAGE to http://www.evl.uic.edu/cavern/forum/ |
---|
34 | * |
---|
35 | *****************************************************************************/ |
---|
36 | |
---|
37 | /* |
---|
38 | * iopen - |
---|
39 | * |
---|
40 | * Paul Haeberli - 1984 |
---|
41 | * |
---|
42 | */ |
---|
43 | #include <stdio.h> |
---|
44 | #include <errno.h> |
---|
45 | #include <stdarg.h> |
---|
46 | #include <stdlib.h> |
---|
47 | #include <memory.h> |
---|
48 | #include <unistd.h> |
---|
49 | #include <fcntl.h> |
---|
50 | #include <stdarg.h> |
---|
51 | |
---|
52 | #include "image.h" |
---|
53 | |
---|
54 | IMAGE *imgopen(); |
---|
55 | |
---|
56 | IMAGE *iopen(char *file, |
---|
57 | register char *mode, |
---|
58 | ...) |
---|
59 | { |
---|
60 | unsigned int type, dim, xsize, ysize, zsize; |
---|
61 | va_list ap; |
---|
62 | va_start(ap, mode); |
---|
63 | type = va_arg(ap, unsigned int); |
---|
64 | dim = va_arg(ap, unsigned int); |
---|
65 | xsize = va_arg(ap, unsigned int); |
---|
66 | ysize = va_arg(ap, unsigned int); |
---|
67 | zsize = va_arg(ap, unsigned int); |
---|
68 | va_end(ap); |
---|
69 | |
---|
70 | return(imgopen(0, file, mode, type, dim, xsize, ysize, zsize)); |
---|
71 | } |
---|
72 | |
---|
73 | IMAGE *fiopen(f, mode, type, dim, xsize, ysize, zsize) |
---|
74 | int f; |
---|
75 | register char *mode; |
---|
76 | unsigned int type, dim, xsize, ysize, zsize; |
---|
77 | { |
---|
78 | return(imgopen(f, 0, mode, type, dim, xsize, ysize, zsize)); |
---|
79 | } |
---|
80 | |
---|
81 | IMAGE *imgopen(f, file, mode, type, dim, xsize, ysize, zsize) |
---|
82 | char *file; |
---|
83 | int f; |
---|
84 | register char *mode; |
---|
85 | unsigned int type, dim, xsize, ysize, zsize; |
---|
86 | { |
---|
87 | register IMAGE *image; |
---|
88 | extern int errno; |
---|
89 | register int rw; |
---|
90 | int tablesize; |
---|
91 | register int i, max; |
---|
92 | |
---|
93 | image = (IMAGE*)malloc(sizeof(IMAGE)); |
---|
94 | bzero(image,sizeof(IMAGE)); |
---|
95 | rw = mode[1] == '+'; |
---|
96 | if(rw) { |
---|
97 | i_errhdlr("iopen: read/write mode not supported\n"); |
---|
98 | return NULL; |
---|
99 | } |
---|
100 | if (*mode=='w') { |
---|
101 | if (file) { |
---|
102 | f = creat(file, 0666); |
---|
103 | if (rw && f>=0) { |
---|
104 | close(f); |
---|
105 | f = open(file, 2); |
---|
106 | } |
---|
107 | } |
---|
108 | if (f < 0) { |
---|
109 | i_errhdlr("iopen: can't open output file %s\n",file); |
---|
110 | return NULL; |
---|
111 | } |
---|
112 | image->imagic = IMAGIC; |
---|
113 | image->type = type; |
---|
114 | image->xsize = xsize; |
---|
115 | image->ysize = 1; |
---|
116 | image->zsize = 1; |
---|
117 | if (dim>1) |
---|
118 | image->ysize = ysize; |
---|
119 | if (dim>2) |
---|
120 | image->zsize = zsize; |
---|
121 | if(image->zsize == 1) { |
---|
122 | image->dim = 2; |
---|
123 | if(image->ysize == 1) |
---|
124 | image->dim = 1; |
---|
125 | } else { |
---|
126 | image->dim = 3; |
---|
127 | } |
---|
128 | image->min = 10000000; |
---|
129 | image->max = 0; |
---|
130 | isetname(image,"no name"); |
---|
131 | image->wastebytes = 0; |
---|
132 | { |
---|
133 | static int one = 1; |
---|
134 | /* external format should be big-endian */ |
---|
135 | image->dorev = (*(char *)&one == 1) ? 1 : 0; |
---|
136 | } |
---|
137 | if (write(f,image,sizeof(IMAGE)) != sizeof(IMAGE)) { |
---|
138 | i_errhdlr("iopen: error on write of image header\n"); |
---|
139 | return NULL; |
---|
140 | } |
---|
141 | } else { |
---|
142 | if (file) |
---|
143 | f = open(file, rw? 2: 0); |
---|
144 | if (f < 0) |
---|
145 | return(NULL); |
---|
146 | if (read(f,image,sizeof(IMAGE)) != sizeof(IMAGE)) { |
---|
147 | i_errhdlr("iopen: error on read of image header\n"); |
---|
148 | return NULL; |
---|
149 | } |
---|
150 | if( ((image->imagic>>8) | ((image->imagic&0xff)<<8)) |
---|
151 | == IMAGIC ) { |
---|
152 | image->dorev = 1; |
---|
153 | cvtimage(image); |
---|
154 | } else |
---|
155 | image->dorev = 0; |
---|
156 | if (image->imagic != IMAGIC) { |
---|
157 | i_errhdlr("iopen: bad magic in image file %x\n",image->imagic); |
---|
158 | return NULL; |
---|
159 | } |
---|
160 | } |
---|
161 | if (rw) |
---|
162 | image->flags = _IORW; |
---|
163 | else if (*mode != 'r') |
---|
164 | image->flags = _IOWRT; |
---|
165 | else |
---|
166 | image->flags = _IOREAD; |
---|
167 | if(ISRLE(image->type)) { |
---|
168 | tablesize = image->ysize*image->zsize*sizeof(IMulong); |
---|
169 | image->rowstart = (IMulong *)malloc(tablesize); |
---|
170 | image->rowsize = (IMulong *)malloc(tablesize); |
---|
171 | if( image->rowstart == 0 || image->rowsize == 0 ) { |
---|
172 | i_errhdlr("iopen: error on table alloc\n"); |
---|
173 | return NULL; |
---|
174 | } |
---|
175 | image->rleend = 512L+2*tablesize; |
---|
176 | if (*mode=='w') { |
---|
177 | max = image->ysize*image->zsize; |
---|
178 | for(i=0; i<max; i++) { |
---|
179 | image->rowstart[i] = 0; |
---|
180 | image->rowsize[i] = -1; |
---|
181 | } |
---|
182 | } else { |
---|
183 | tablesize = image->ysize*image->zsize*sizeof(IMulong); |
---|
184 | lseek(f, 512L, 0); |
---|
185 | if (read(f,image->rowstart,tablesize) != tablesize) { |
---|
186 | i_errhdlr("iopen: error on read of rowstart\n"); |
---|
187 | return NULL; |
---|
188 | } |
---|
189 | if(image->dorev) |
---|
190 | cvtlongs(image->rowstart,tablesize); |
---|
191 | if (read(f,image->rowsize,tablesize) != tablesize) { |
---|
192 | i_errhdlr("iopen: error on read of rowsize\n"); |
---|
193 | return NULL; |
---|
194 | } |
---|
195 | if(image->dorev) |
---|
196 | cvtlongs(image->rowsize,tablesize); |
---|
197 | } |
---|
198 | } |
---|
199 | image->cnt = 0; |
---|
200 | image->ptr = 0; |
---|
201 | image->base = 0; |
---|
202 | if( (image->tmpbuf = ibufalloc(image)) == 0 ) { |
---|
203 | i_errhdlr("iopen: error on tmpbuf alloc %d\n",image->xsize); |
---|
204 | return NULL; |
---|
205 | } |
---|
206 | image->x = image->y = image->z = 0; |
---|
207 | image->file = f; |
---|
208 | image->offset = 512L; /* set up for img_optseek */ |
---|
209 | lseek(image->file, 512L, 0); |
---|
210 | return(image); |
---|
211 | } |
---|
212 | |
---|
213 | IMushort *ibufalloc( IMAGE *image ) |
---|
214 | { |
---|
215 | return (IMushort *)malloc(IBUFSIZE(image->xsize)); |
---|
216 | } |
---|
217 | |
---|
218 | int reverse( IMulong lwrd ) |
---|
219 | { |
---|
220 | return ((lwrd>>24) | |
---|
221 | (lwrd>>8 & 0xff00) | |
---|
222 | (lwrd<<8 & 0xff0000) | |
---|
223 | (lwrd<<24) ); |
---|
224 | } |
---|
225 | |
---|
226 | void cvtshorts( IMushort *buffer, int nbytes ) |
---|
227 | { |
---|
228 | register short i; |
---|
229 | register int nshorts = nbytes>>1; |
---|
230 | register IMushort swrd; |
---|
231 | |
---|
232 | for(i=0; i<nshorts; i++) { |
---|
233 | swrd = *buffer; |
---|
234 | *buffer++ = (swrd>>8) | (swrd<<8); |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | void cvtlongs( IMulong *buffer, int nbytes ) |
---|
239 | { |
---|
240 | register short i; |
---|
241 | register int nlongs = nbytes>>2; |
---|
242 | register IMulong lwrd; |
---|
243 | |
---|
244 | for(i=0; i<nlongs; i++) { |
---|
245 | lwrd = buffer[i]; |
---|
246 | buffer[i] = ((lwrd>>24) | |
---|
247 | (lwrd>>8 & 0xff00) | |
---|
248 | (lwrd<<8 & 0xff0000) | |
---|
249 | (lwrd<<24) ); |
---|
250 | } |
---|
251 | } |
---|
252 | |
---|
253 | void cvtimage( IMAGE *buffer ) |
---|
254 | { |
---|
255 | cvtshorts(&buffer->imagic,12); |
---|
256 | cvtlongs(&buffer->min,12); |
---|
257 | cvtlongs(&buffer->colormap,4); |
---|
258 | } |
---|
259 | |
---|
260 | static void (*i_errfunc)(); |
---|
261 | |
---|
262 | /* error handler for the image library. If the iseterror() routine |
---|
263 | has been called, sprintf's the args into a string and calls the |
---|
264 | error function. Otherwise calls fprintf with the args and then |
---|
265 | exit. This allows 'old' programs to assume that no errors |
---|
266 | ever need be worried about, while programs that know how and |
---|
267 | want to can handle the errors themselves. Olson, 11/88 |
---|
268 | */ |
---|
269 | void i_errhdlr(char *fmt, ...) /* most args currently used is 2 */ |
---|
270 | { |
---|
271 | va_list ap; |
---|
272 | |
---|
273 | va_start(ap, fmt); |
---|
274 | |
---|
275 | if(i_errfunc) { |
---|
276 | char ebuf[2048]; /* be generous; if an error includes a |
---|
277 | pathname, the maxlen is 1024, so we shouldn't ever |
---|
278 | overflow this! */ |
---|
279 | vsprintf(ebuf, fmt, ap); |
---|
280 | (*i_errfunc)(ebuf); |
---|
281 | } else { |
---|
282 | vfprintf(stderr, fmt, ap); |
---|
283 | } |
---|
284 | va_end(ap); |
---|
285 | exit(1); |
---|
286 | } |
---|
287 | |
---|
288 | /* this function sets the error handler for i_errhdlr */ |
---|
289 | void i_seterror( void (*func)() ) |
---|
290 | { |
---|
291 | i_errfunc = func; |
---|
292 | } |
---|