1 |
greg |
1.1 |
#ifndef lint |
2 |
greg |
1.4 |
static char SCCSid[] = "$SunId$ LBL"; |
3 |
greg |
1.1 |
#endif |
4 |
greg |
1.4 |
|
5 |
greg |
1.1 |
/* |
6 |
|
|
rad2pict - Convert an Radiance image to APPLE pict format. |
7 |
|
|
|
8 |
|
|
School of Architecture, Auckland University, Private Bag |
9 |
|
|
Auckland, New Zealand |
10 |
|
|
*/ |
11 |
|
|
#include <stdio.h> |
12 |
|
|
#include "pict.h" |
13 |
|
|
#include "color.h" |
14 |
greg |
1.4 |
#include "resolu.h" |
15 |
greg |
1.3 |
|
16 |
|
|
extern char *malloc(); |
17 |
greg |
1.1 |
|
18 |
|
|
char cbuf[8192*5]; |
19 |
|
|
char pbuf[8192]; |
20 |
|
|
int outbytes; |
21 |
|
|
FILE *outf, *inf; |
22 |
|
|
char **gargv; |
23 |
|
|
|
24 |
|
|
putrect(xorg,yorg,xsize,ysize) |
25 |
|
|
int xorg, yorg, xsize, ysize; |
26 |
|
|
{ |
27 |
|
|
putashort(yorg); |
28 |
|
|
putashort(xorg); |
29 |
|
|
putashort(ysize); |
30 |
|
|
putashort(xsize); |
31 |
|
|
} |
32 |
|
|
|
33 |
|
|
putfprect(xorg,yorg,xsize,ysize) |
34 |
|
|
int xorg, yorg, xsize, ysize; |
35 |
|
|
{ |
36 |
|
|
putalong(yorg<<16); |
37 |
|
|
putalong(xorg<<16); |
38 |
|
|
putalong(ysize<<16); |
39 |
|
|
putalong(xsize<<16); |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
putalong(l) |
43 |
|
|
long l; |
44 |
|
|
{ |
45 |
|
|
putbyte((l>>24)&0xff); |
46 |
|
|
putbyte((l>>16)&0xff); |
47 |
|
|
putbyte((l>>8)&0xff); |
48 |
|
|
putbyte((l>>0)&0xff); |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
putashort(s) |
52 |
|
|
short s; |
53 |
|
|
{ |
54 |
|
|
putbyte((s>>8)&0xff); |
55 |
|
|
putbyte((s>>0)&0xff); |
56 |
|
|
} |
57 |
|
|
|
58 |
|
|
putbyte(b) |
59 |
|
|
unsigned char b; |
60 |
|
|
{ |
61 |
|
|
char c[1]; |
62 |
|
|
|
63 |
|
|
c[0] = b; |
64 |
|
|
if(!fwrite(c,1,1,outf)) { |
65 |
|
|
fprintf(stderr,"%s: error on write\n", gargv[0]); |
66 |
|
|
exit(1); |
67 |
|
|
} |
68 |
|
|
outbytes++; |
69 |
|
|
} |
70 |
|
|
|
71 |
|
|
putbytes(buf,n) |
72 |
|
|
unsigned char *buf; |
73 |
|
|
int n; |
74 |
|
|
{ |
75 |
|
|
if(!fwrite(buf,n,1,outf)) { |
76 |
|
|
fprintf(stderr,"topict: error on write\n"); |
77 |
|
|
exit(1); |
78 |
|
|
} |
79 |
|
|
outbytes+=n; |
80 |
|
|
} |
81 |
|
|
|
82 |
|
|
main(argc,argv) |
83 |
|
|
int argc; |
84 |
|
|
char **argv; |
85 |
|
|
{ |
86 |
|
|
int xsize, ysize; |
87 |
|
|
int i, picsize; |
88 |
|
|
int ssizepos, lsizepos; |
89 |
|
|
|
90 |
|
|
gargv = argv; |
91 |
|
|
|
92 |
|
|
if( argc<3 ) { |
93 |
|
|
fprintf(stderr, "Usage: %s inimage out.pict\n", gargv[0]); |
94 |
|
|
exit(1); |
95 |
|
|
} |
96 |
|
|
|
97 |
|
|
if( (inf=fopen(gargv[1],"rb")) == NULL ) { |
98 |
|
|
fprintf(stderr,"%s: can't open input file %s\n",gargv[0], gargv[1]); |
99 |
|
|
exit(1); |
100 |
|
|
} |
101 |
|
|
|
102 |
|
|
if( (outf=fopen(gargv[2],"wb")) == NULL ) { |
103 |
|
|
fprintf(stderr,"%s: can't open output file %s\n", gargv[0], gargv[1]); |
104 |
|
|
exit(1); |
105 |
|
|
} |
106 |
|
|
|
107 |
|
|
if (checkheader(inf, COLRFMT, NULL) < 0 || |
108 |
greg |
1.4 |
fgetresolu(&xsize, &ysize, inf) < 0) { |
109 |
greg |
1.1 |
fprintf(stderr, "%s: not a radiance picture\n", argv[1]); |
110 |
|
|
exit(1); |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
setcolrgam(2.0); |
114 |
|
|
|
115 |
|
|
for(i=0; i<HEADER_SIZE; i++) |
116 |
|
|
putbyte(0); |
117 |
|
|
ssizepos = outbytes; |
118 |
|
|
putashort(0); /* low 16 bits of file size less HEADER_SIZE */ |
119 |
|
|
putrect(0,0,xsize,ysize); /* bounding box of picture */ |
120 |
|
|
putashort(PICT_picVersion); |
121 |
|
|
putashort(0x02ff); /* version 2 pict */ |
122 |
|
|
putashort(PICT_reservedHeader); /* reserved header opcode */ |
123 |
|
|
|
124 |
|
|
lsizepos = outbytes; |
125 |
|
|
putalong(0); /* full size of the file */ |
126 |
|
|
putfprect(0,0,xsize,ysize); /* fixed point bounding box of picture */ |
127 |
|
|
putalong(0); /* reserved */ |
128 |
|
|
|
129 |
|
|
putashort(PICT_clipRgn); /* the clip region */ |
130 |
|
|
putashort(10); |
131 |
|
|
putrect(0,0,xsize,ysize); |
132 |
|
|
|
133 |
|
|
putpict(xsize, ysize); |
134 |
|
|
|
135 |
|
|
putashort(PICT_EndOfPicture); /* end of pict */ |
136 |
|
|
|
137 |
|
|
picsize = outbytes-HEADER_SIZE; |
138 |
|
|
fseek(outf,ssizepos,0); |
139 |
|
|
putashort(picsize&0xffff); |
140 |
|
|
fseek(outf,lsizepos,0); |
141 |
|
|
putalong(picsize); |
142 |
|
|
|
143 |
|
|
fclose(outf); |
144 |
|
|
exit(0); |
145 |
|
|
} |
146 |
|
|
|
147 |
|
|
putpict(xsize, ysize) |
148 |
|
|
int xsize; |
149 |
|
|
int ysize; |
150 |
|
|
{ |
151 |
|
|
int y; |
152 |
|
|
int nbytes, rowbytes; |
153 |
|
|
|
154 |
|
|
putashort(PICT_Pack32BitsRect); /* 32 bit rgb */ |
155 |
|
|
rowbytes = 4*xsize; |
156 |
|
|
putalong(0x000000ff); /* base address */ |
157 |
|
|
|
158 |
|
|
if(rowbytes&1) |
159 |
|
|
rowbytes++; |
160 |
|
|
putashort(rowbytes|0x8000); /* rowbytes */ |
161 |
|
|
putrect(0,0,xsize,ysize); /* bounds */ |
162 |
|
|
putashort(0); /* version */ |
163 |
|
|
|
164 |
|
|
putashort(4); /* packtype */ |
165 |
|
|
putalong(0); /* packsize */ |
166 |
|
|
putalong(72<<16); /* hres */ |
167 |
|
|
putalong(72<<16); /* vres */ |
168 |
|
|
|
169 |
|
|
putashort(16); /* pixeltype */ |
170 |
|
|
putashort(32); /* pixelsize */ |
171 |
|
|
putashort(3); /* cmpcount */ |
172 |
|
|
|
173 |
|
|
putashort(8); /* cmpsize */ |
174 |
|
|
putalong(0); /* planebytes */ |
175 |
|
|
putalong(0); /* pmtable */ |
176 |
|
|
putalong(0); /* pmreserved */ |
177 |
|
|
|
178 |
|
|
putrect(0,0,xsize,ysize); /* scr rect */ |
179 |
|
|
putrect(0,0,xsize,ysize); /* dest rect */ |
180 |
|
|
|
181 |
|
|
putashort(0x40); /* transfer mode */ |
182 |
|
|
for(y=0; y<ysize; y++) { |
183 |
|
|
getrow(inf, cbuf, xsize); |
184 |
|
|
|
185 |
|
|
nbytes = packbits(cbuf,pbuf,24*xsize); |
186 |
|
|
if(rowbytes>250) |
187 |
|
|
putashort(nbytes); |
188 |
|
|
else |
189 |
|
|
putbyte(nbytes); |
190 |
|
|
putbytes(pbuf,nbytes); |
191 |
|
|
} |
192 |
|
|
|
193 |
|
|
if(outbytes&1) |
194 |
|
|
putbyte(0); |
195 |
|
|
} |
196 |
|
|
|
197 |
greg |
1.2 |
int |
198 |
|
|
getrow(in, mybuff, xsize) |
199 |
|
|
FILE *in; |
200 |
|
|
char *mybuff; |
201 |
|
|
int xsize; |
202 |
greg |
1.1 |
{ |
203 |
|
|
COLOR color; |
204 |
|
|
COLR *scanin = (COLR*) malloc(xsize * sizeof(COLR)); |
205 |
|
|
int x; |
206 |
|
|
|
207 |
|
|
if (scanin == NULL) { |
208 |
|
|
printf("scanin null"); |
209 |
|
|
} |
210 |
|
|
|
211 |
|
|
|
212 |
|
|
if (freadcolrs(scanin, xsize, in) < 0) { |
213 |
|
|
fprintf(stderr, " read error\n"); |
214 |
|
|
exit(1); |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
colrs_gambs(scanin, xsize); |
218 |
|
|
|
219 |
|
|
for (x = 0; x < xsize; x++) { |
220 |
|
|
colr_color(color, scanin[x]); |
221 |
|
|
cbuf[xsize * 0 + x] = color[RED] * 255; |
222 |
|
|
cbuf[xsize * 1 + x] = color[GRN] * 255; |
223 |
|
|
cbuf[xsize * 2 + x] = color[BLU] * 255; |
224 |
|
|
} |
225 |
|
|
free(scanin); |
226 |
|
|
} |
227 |
|
|
|
228 |
|
|
packbits(ibits,pbits,nbits) |
229 |
|
|
unsigned char *ibits, *pbits; |
230 |
|
|
int nbits; |
231 |
|
|
{ |
232 |
|
|
int bytes; |
233 |
|
|
unsigned char *sptr; |
234 |
|
|
unsigned char *ibitsend; |
235 |
|
|
unsigned char *optr = pbits; |
236 |
|
|
int nbytes, todo, cc, count; |
237 |
|
|
|
238 |
|
|
nbytes = ((nbits-1)/8)+1; |
239 |
|
|
ibitsend = ibits+nbytes; |
240 |
|
|
while (ibits<ibitsend) { |
241 |
|
|
sptr = ibits; |
242 |
|
|
ibits += 2; |
243 |
|
|
while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0]))) |
244 |
|
|
ibits++; |
245 |
|
|
if(ibits != ibitsend) { |
246 |
|
|
ibits -= 2; |
247 |
|
|
} |
248 |
|
|
count = ibits-sptr; |
249 |
|
|
while(count) { |
250 |
|
|
todo = count>127 ? 127:count; |
251 |
|
|
count -= todo; |
252 |
|
|
*optr++ = todo-1; |
253 |
|
|
while(todo--) |
254 |
|
|
*optr++ = *sptr++; |
255 |
|
|
} |
256 |
|
|
if(ibits == ibitsend) |
257 |
|
|
break; |
258 |
|
|
sptr = ibits; |
259 |
|
|
cc = *ibits++; |
260 |
|
|
while( (ibits<ibitsend) && (*ibits == cc) ) |
261 |
|
|
ibits++; |
262 |
|
|
count = ibits-sptr; |
263 |
|
|
while(count) { |
264 |
|
|
todo = count>128 ? 128:count; |
265 |
|
|
count -= todo; |
266 |
|
|
*optr++ = 257-todo; |
267 |
|
|
*optr++ = cc; |
268 |
|
|
} |
269 |
|
|
} |
270 |
|
|
return optr-pbits; |
271 |
|
|
} |