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