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