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 | * Given a Sun raster file, return a DISPIMAGE |
---|
39 | * containing its raster data and with size <= (xsize, ysize). |
---|
40 | */ |
---|
41 | |
---|
42 | #include <stdio.h> |
---|
43 | #include <stdlib.h> |
---|
44 | /* #include "rasterfile.h" */ |
---|
45 | #include "imginfo.h" |
---|
46 | |
---|
47 | extern int pnm_getint( FILE * ); |
---|
48 | |
---|
49 | IMG * |
---|
50 | pnmmakedisp(filename) |
---|
51 | char *filename; |
---|
52 | { |
---|
53 | register FILE *f; |
---|
54 | IMG *di = NULL; |
---|
55 | int maxpix = 0, pnmkind; |
---|
56 | int xsize, ysize; |
---|
57 | int y; |
---|
58 | int c; |
---|
59 | |
---|
60 | f = fopen(filename, "r"); |
---|
61 | if(f == NULL) { |
---|
62 | fprintf(stderr, "%s: cannot open: ", filename); |
---|
63 | perror(""); |
---|
64 | return NULL; |
---|
65 | } |
---|
66 | |
---|
67 | if(fgetc(f) != 'P') |
---|
68 | goto fail; |
---|
69 | |
---|
70 | pnmkind = fgetc(f); |
---|
71 | |
---|
72 | xsize = pnm_getint(f); |
---|
73 | ysize = pnm_getint(f); |
---|
74 | switch(pnmkind) { |
---|
75 | case '1': case '4': |
---|
76 | maxpix = 1; |
---|
77 | break; |
---|
78 | case '2': case '5': /* fall into ... */ |
---|
79 | case '3': case '6': |
---|
80 | maxpix = pnm_getint(f); |
---|
81 | break; |
---|
82 | default: |
---|
83 | goto fail; |
---|
84 | } |
---|
85 | if(xsize <= 0 || ysize <= 0 || maxpix <= 0) |
---|
86 | goto fail; |
---|
87 | |
---|
88 | if(pnmkind >= '4') { |
---|
89 | /* Skip to end of line, after which the binary data should follow */ |
---|
90 | while((c = fgetc(f)) != EOF && c != '\n') |
---|
91 | ; |
---|
92 | } |
---|
93 | |
---|
94 | di = (IMG *)malloc(sizeof(IMG)); |
---|
95 | if(pnmkind == '1' || pnmkind == '4') { |
---|
96 | di->type = IT_BIT; |
---|
97 | di->rowbytes = (xsize+31)/32 * 4; |
---|
98 | } else { |
---|
99 | di->type = IT_LONG; |
---|
100 | di->rowbytes = xsize*4; |
---|
101 | } |
---|
102 | di->xsize = xsize; |
---|
103 | di->ysize = ysize; |
---|
104 | di->data = malloc(di->rowbytes * ysize); |
---|
105 | if(di->data == NULL) { |
---|
106 | fprintf(stderr, "Can't malloc %d bytes of memory for image\n", |
---|
107 | di->rowbytes*ysize); |
---|
108 | exit(2); |
---|
109 | } |
---|
110 | for(y = ysize; --y >= 0; ) { |
---|
111 | register int v; |
---|
112 | register long *rp; |
---|
113 | register int x; |
---|
114 | |
---|
115 | rp = (long *)(di->data + y*di->rowbytes); |
---|
116 | x = xsize; |
---|
117 | switch(pnmkind) { |
---|
118 | case '1': |
---|
119 | do { |
---|
120 | *rp++ = 0xff000000 | -pnm_getint(f); |
---|
121 | } while(--x > 0); |
---|
122 | break; |
---|
123 | case '2': |
---|
124 | do { |
---|
125 | v = (255 * pnm_getint(f) / maxpix); |
---|
126 | *rp++ = 0xff000000 | (v << 16) | (v<<8) | v; |
---|
127 | } while(--x > 0); |
---|
128 | break; |
---|
129 | case '3': |
---|
130 | do { |
---|
131 | v = (255 * pnm_getint(f) / maxpix); |
---|
132 | v |= (255 * pnm_getint(f) / maxpix) << 8; |
---|
133 | *rp++ = v | (255 * pnm_getint(f) / maxpix) << 16; |
---|
134 | } while(--x > 0); |
---|
135 | break; |
---|
136 | case '4': |
---|
137 | fread(rp, (xsize+7)/8, 1, f); /* Bit mode */ |
---|
138 | break; |
---|
139 | case '5': |
---|
140 | if(maxpix != 255) { |
---|
141 | do { |
---|
142 | v = 255 * getc(f) / maxpix; |
---|
143 | *rp++ = 0xff000000 | (v << 16) | (v << 8) | v; |
---|
144 | } while(--x > 0); |
---|
145 | } else { |
---|
146 | do { |
---|
147 | v = getc(f); |
---|
148 | *rp++ = 0xff000000 | (v << 16) | (v << 8) | v; |
---|
149 | } while(--x > 0); |
---|
150 | } |
---|
151 | break; |
---|
152 | case '6': |
---|
153 | if(maxpix != 255) { |
---|
154 | do { |
---|
155 | v = 255 * getc(f) / maxpix; |
---|
156 | v |= (255 * getc(f) / maxpix) << 8; |
---|
157 | *rp++ = 0xff000000 | ((255 * getc(f) / maxpix) << 16) | v; |
---|
158 | } while(--x > 0); |
---|
159 | } else { |
---|
160 | do { |
---|
161 | v = getc(f); |
---|
162 | v |= getc(f) << 8; |
---|
163 | *rp++ = 0xff000000 | (getc(f) << 16) | v; |
---|
164 | } while(--x > 0); |
---|
165 | } |
---|
166 | break; |
---|
167 | } |
---|
168 | } |
---|
169 | |
---|
170 | if(f != NULL) |
---|
171 | fclose(f); |
---|
172 | return di; |
---|
173 | |
---|
174 | fail: |
---|
175 | if(di) { |
---|
176 | if(di->data) free(di->data); |
---|
177 | free(di); |
---|
178 | } |
---|
179 | if(f != NULL) |
---|
180 | fclose(f); |
---|
181 | return NULL; |
---|
182 | } |
---|