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 | #include <fcntl.h> |
---|
38 | #include <stdio.h> |
---|
39 | #include <stdlib.h> |
---|
40 | #include <memory.h> |
---|
41 | #include "softpic.h" |
---|
42 | #include "imginfo.h" |
---|
43 | |
---|
44 | #define TRUE 1 |
---|
45 | #define FALSE 0 |
---|
46 | |
---|
47 | #define DEBUG FALSE |
---|
48 | |
---|
49 | static int xsize, ysize, nbits[10], file_chained[10], fields[10], file_type[10], ndsc; |
---|
50 | FILE *in_fle; |
---|
51 | float m, n; |
---|
52 | char *liner, *lineg, *lineb, *linea; |
---|
53 | |
---|
54 | static int check_file_header(fle) |
---|
55 | FILE *fle; |
---|
56 | { |
---|
57 | struct softhdr hd; |
---|
58 | |
---|
59 | fread(&hd, sizeof(struct softhdr), 1, fle); |
---|
60 | if (hd.id != SOFT_MAGIC) |
---|
61 | return FALSE; |
---|
62 | if (DEBUG) { |
---|
63 | fprintf(stderr, "creator version: %f\n", hd.version); |
---|
64 | fprintf(stderr, "internal comment: %s\n", hd.comment); |
---|
65 | } |
---|
66 | return TRUE; |
---|
67 | } |
---|
68 | |
---|
69 | static int check_pic_header(fle) |
---|
70 | FILE *fle; |
---|
71 | { |
---|
72 | struct softpict hd; |
---|
73 | |
---|
74 | fread(&hd, sizeof(struct softpict), 1, fle); |
---|
75 | if (hd.id != SPICT_MAGIC) |
---|
76 | return FALSE; |
---|
77 | if (DEBUG) { |
---|
78 | fprintf(stderr, "picture dimensions: %d X %d.\n", hd.width, hd.height); |
---|
79 | fprintf(stderr, "picture pixel ratio (X/Y): %f.\n", hd.ratio); |
---|
80 | fprintf(stderr, "picture fields: %s.\n", (hd.fields == 1)?"odd":(hd.fields == 2)?"even":"full-frame"); |
---|
81 | } |
---|
82 | xsize = hd.width; |
---|
83 | ysize = hd.height; |
---|
84 | liner = (char *)malloc(xsize * sizeof(char)); |
---|
85 | lineg = (char *)malloc(xsize * sizeof(char)); |
---|
86 | lineb = (char *)malloc(xsize * sizeof(char)); |
---|
87 | linea = (char *)malloc(xsize * sizeof(char)); |
---|
88 | bzero(liner, xsize*sizeof(char)); |
---|
89 | bzero(lineg, xsize*sizeof(char)); |
---|
90 | bzero(lineb, xsize*sizeof(char)); |
---|
91 | bzero(linea, xsize*sizeof(char)); |
---|
92 | return TRUE; |
---|
93 | } |
---|
94 | |
---|
95 | static void get_pack_chunk( FILE *fle, int n) |
---|
96 | { |
---|
97 | struct softpack hd; |
---|
98 | |
---|
99 | fread(&hd, sizeof(struct softpack), 1, fle); |
---|
100 | file_chained[n] = hd.chained; |
---|
101 | nbits[n] = hd.nbits; |
---|
102 | file_type[n] = hd.type; |
---|
103 | fields[n] = hd.fields; |
---|
104 | if (DEBUG) { |
---|
105 | fprintf(stderr, "pack info #%d:\n", n); |
---|
106 | fprintf(stderr, "\tnbits = %d\n", nbits[n]); |
---|
107 | fprintf(stderr, "\ttype = %x\n", file_type[n]); |
---|
108 | fprintf(stderr, "\tfields = %x\n", fields[n]); |
---|
109 | } |
---|
110 | ndsc = n; |
---|
111 | if (nbits[n] != 8) { |
---|
112 | fprintf(stderr, "Error: This program only handles 8 bit data.\n"); |
---|
113 | exit(1); |
---|
114 | } |
---|
115 | if (file_chained[n] != 0) get_pack_chunk(fle, (n+1)); |
---|
116 | } |
---|
117 | |
---|
118 | static void mixed_read_rle_part( FILE *fle, short cnt, int lne, int strt, int fld ) |
---|
119 | { |
---|
120 | register int i; |
---|
121 | register int mask; |
---|
122 | char r, g, b, a; |
---|
123 | |
---|
124 | mask = fields[fld]; |
---|
125 | if ((mask & SPICT_R) != 0) |
---|
126 | r = getc(fle); |
---|
127 | if ((mask & SPICT_G) != 0) |
---|
128 | g = getc(fle); |
---|
129 | if ((mask & SPICT_B) != 0) |
---|
130 | b = getc(fle); |
---|
131 | if ((mask & SPICT_A) != 0) |
---|
132 | a = getc(fle); |
---|
133 | for (i = strt; i<=strt+cnt; i++) { |
---|
134 | if ((mask & SPICT_R) != 0) |
---|
135 | liner[i] = r; |
---|
136 | if ((mask & SPICT_G) != 0) |
---|
137 | lineg[i] = g; |
---|
138 | if ((mask & SPICT_B) != 0) |
---|
139 | lineb[i] = b; |
---|
140 | if ((mask & SPICT_A) != 0) |
---|
141 | linea[i] = a; |
---|
142 | } |
---|
143 | } |
---|
144 | |
---|
145 | static void mixed_read_norm_part( FILE *fle, short cnt, int lne, int strt, int fld ) |
---|
146 | { |
---|
147 | register int i; |
---|
148 | |
---|
149 | for (i = strt; i<=strt+cnt; i++) { |
---|
150 | if ((fields[fld] & SPICT_R) != 0) |
---|
151 | liner[i] = getc(fle); |
---|
152 | if ((fields[fld] & SPICT_G) != 0) |
---|
153 | lineg[i] = getc(fle); |
---|
154 | if ((fields[fld] & SPICT_B) != 0) |
---|
155 | lineb[i] = getc(fle); |
---|
156 | if ((fields[fld] & SPICT_A) != 0) |
---|
157 | linea[i] = getc(fle); |
---|
158 | } |
---|
159 | } |
---|
160 | |
---|
161 | static void mixed_read_line( FILE *fle, int lne, int fld ) |
---|
162 | { |
---|
163 | int x = 0; |
---|
164 | short size; |
---|
165 | int cnt; |
---|
166 | |
---|
167 | do { |
---|
168 | cnt = getc(fle); |
---|
169 | if (cnt == 0x80) { |
---|
170 | size = getc(fle) << 8; |
---|
171 | size |= getc(fle); |
---|
172 | size--; |
---|
173 | } else |
---|
174 | size = cnt & 0x7f; |
---|
175 | if ((cnt & 0x80) == 0) |
---|
176 | mixed_read_norm_part(fle, size, lne, x, fld); |
---|
177 | else |
---|
178 | mixed_read_rle_part(fle, size, lne, x, fld); |
---|
179 | x += size + 1; |
---|
180 | } while (x < xsize); |
---|
181 | } |
---|
182 | |
---|
183 | static void output_line( int lne, IMG *im ) |
---|
184 | { |
---|
185 | int i; |
---|
186 | register char *l; |
---|
187 | register char *r, *g, *b, *a; |
---|
188 | |
---|
189 | l = (char *)&im->data[(ysize - lne - 1)*im->rowbytes]; |
---|
190 | a = linea; r = liner; g = lineg; b = lineb; |
---|
191 | i = xsize; |
---|
192 | do { |
---|
193 | *l++ = *a++; |
---|
194 | *l++ = *b++; |
---|
195 | *l++ = *g++; |
---|
196 | *l++ = *r++; |
---|
197 | } while(--i > 0); |
---|
198 | } |
---|
199 | |
---|
200 | static void load_file( FILE *fle, IMG *im ) |
---|
201 | { |
---|
202 | int y, fcnt; |
---|
203 | |
---|
204 | im->xsize = xsize; |
---|
205 | im->ysize = ysize; |
---|
206 | im->rowbytes = xsize*sizeof(long); |
---|
207 | im->data = malloc(ysize * im->rowbytes); |
---|
208 | for (y = 0; y < ysize; y++) { |
---|
209 | for (fcnt = 0; fcnt <= ndsc; fcnt++) { |
---|
210 | if (file_type[fcnt] == SPICT_MIXEDRLE) { |
---|
211 | mixed_read_line(fle, y, fcnt); |
---|
212 | } else { |
---|
213 | fprintf(stderr, "don't know pack type 0x%x\n", file_type[fcnt]); |
---|
214 | exit(1); |
---|
215 | } |
---|
216 | } |
---|
217 | output_line(y, im); |
---|
218 | } |
---|
219 | } |
---|
220 | |
---|
221 | static void close_files( FILE *in ) |
---|
222 | { |
---|
223 | fclose(in); |
---|
224 | free(liner); |
---|
225 | free(lineg); |
---|
226 | free(lineb); |
---|
227 | free(linea); |
---|
228 | } |
---|
229 | |
---|
230 | IMG *softmakedisp(name) |
---|
231 | char *name; |
---|
232 | { |
---|
233 | IMG *out; |
---|
234 | |
---|
235 | in_fle = fopen(name, "rb"); |
---|
236 | if(in_fle == NULL) |
---|
237 | return NULL; |
---|
238 | if (!check_file_header(in_fle)) { |
---|
239 | fprintf(stderr, "ERROR in file header! The world has already come to an end!\n"); |
---|
240 | return NULL; |
---|
241 | } |
---|
242 | if (!check_pic_header(in_fle)) { |
---|
243 | fprintf(stderr, "ERROR in picure header! The world will soon come to an end.\n"); |
---|
244 | return NULL; |
---|
245 | } |
---|
246 | out = (IMG *)malloc(sizeof(IMG)); |
---|
247 | out->type = IT_LONG; |
---|
248 | get_pack_chunk(in_fle, 0); |
---|
249 | load_file(in_fle, out); |
---|
250 | close_files(in_fle); |
---|
251 | return out; |
---|
252 | } |
---|