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