1 |
#ifndef lint |
2 |
static char SCCSid[] = "$SunId$ Auckuni"; |
3 |
#endif |
4 |
|
5 |
/* |
6 |
* rad2pict - |
7 |
* Convert an Radiance image to APPLE pict format. |
8 |
* |
9 |
* Orginally Iris to PICT by Paul Haeberli - 1990 |
10 |
* Hacked into Rad to PICT by Russell Street 1990 |
11 |
* |
12 |
* History: |
13 |
* V 1 -- Does basic conversion |
14 |
* V 1.1 (2/11/91) -- Added options for Gamma |
15 |
* -- verbose option |
16 |
* -- man page |
17 |
* -- better about allocating buffers |
18 |
* V 1.2 (19/11/91) -- Revised to handle opening "The Radiance Way" |
19 |
* -- Added exposure level adjustment |
20 |
*/ |
21 |
|
22 |
#include <stdio.h> |
23 |
|
24 |
#include "pict.h" |
25 |
#include "color.h" |
26 |
#include "resolu.h" |
27 |
|
28 |
extern char *malloc(); |
29 |
|
30 |
int outbytes; /* This had better be 32 bits! */ |
31 |
char *progname; |
32 |
int verbose = 0; |
33 |
float gamma = 2.0; |
34 |
int bradj = 0; |
35 |
|
36 |
/* First some utility routines */ |
37 |
|
38 |
putrect(xorg,yorg,xsize,ysize) |
39 |
int xorg, yorg, xsize, ysize; |
40 |
{ |
41 |
putashort(yorg); |
42 |
putashort(xorg); |
43 |
putashort(ysize); |
44 |
putashort(xsize); |
45 |
} |
46 |
|
47 |
putfprect(xorg,yorg,xsize,ysize) |
48 |
int xorg, yorg, xsize, ysize; |
49 |
{ |
50 |
putalong(yorg<<16); |
51 |
putalong(xorg<<16); |
52 |
putalong(ysize<<16); |
53 |
putalong(xsize<<16); |
54 |
} |
55 |
|
56 |
putalong(l) |
57 |
long l; |
58 |
{ |
59 |
putbyte((l>>24)&0xff); |
60 |
putbyte((l>>16)&0xff); |
61 |
putbyte((l>>8)&0xff); |
62 |
putbyte((l>>0)&0xff); |
63 |
} |
64 |
|
65 |
putashort(s) |
66 |
short s; |
67 |
{ |
68 |
putbyte((s>>8)&0xff); |
69 |
putbyte((s>>0)&0xff); |
70 |
} |
71 |
|
72 |
putbyte(b) |
73 |
int b; |
74 |
{ |
75 |
if (putc(b,stdout) == EOF && ferror(stdout)) { |
76 |
fprintf(stderr,"%s: error on write\n", progname); |
77 |
exit(1); |
78 |
} |
79 |
outbytes++; |
80 |
} |
81 |
|
82 |
putbytes(buf,n) |
83 |
unsigned char *buf; |
84 |
int n; |
85 |
{ |
86 |
if(!fwrite(buf,n,1,stdout)) { |
87 |
fprintf(stderr,"%s: error on write\n", progname); |
88 |
exit(1); |
89 |
} |
90 |
outbytes+=n; |
91 |
} |
92 |
|
93 |
main(argc,argv) |
94 |
int argc; |
95 |
char **argv; |
96 |
{ |
97 |
int xsize, ysize; |
98 |
int i, picsize; |
99 |
int ssizepos, lsizepos; |
100 |
|
101 |
progname = argv[0]; |
102 |
|
103 |
for (i = 1; i < argc ; i++) |
104 |
if (argv[i][0] == '-') |
105 |
switch (argv[i][1]) { |
106 |
case 'g': gamma = atof(argv[++i]); |
107 |
break; |
108 |
|
109 |
case 'e': if (argv[i+1][0] != '+' && argv[i+1][0] != '-') |
110 |
usage(); |
111 |
else |
112 |
bradj = atoi(argv[++i]); |
113 |
break; |
114 |
|
115 |
case 'v': ; |
116 |
verbose = 1; |
117 |
break; |
118 |
|
119 |
case 'r': fprintf(stderr, "Sorry. Get a Macintosh :-)\n"); |
120 |
exit(1); |
121 |
|
122 |
case '-': i++; |
123 |
goto outofparse; |
124 |
break; /* NOTREACHED */ |
125 |
|
126 |
otherwise: usage(); |
127 |
break; |
128 |
} |
129 |
else |
130 |
break; |
131 |
|
132 |
outofparse: |
133 |
|
134 |
if (i < argc - 2) |
135 |
usage(); |
136 |
|
137 |
if (i <= argc - 1 && freopen(argv[i], "r", stdin) == NULL) { |
138 |
fprintf(stderr, "%s: can not open input \"%s\"\n", |
139 |
progname, argv[i]); |
140 |
exit(1); |
141 |
} |
142 |
|
143 |
if (i <= argc - 2 && freopen(argv[i+1], "w", stdout) == NULL) { |
144 |
fprintf(stderr, "%s: can not open input \"%s\"\n", |
145 |
progname, argv[i+1]); |
146 |
exit(1); |
147 |
} |
148 |
|
149 |
#ifdef DEBUG |
150 |
fprintf(stderr, "Input file: %s\n", i <= argc - 1 ? argv[i] : "stdin"); |
151 |
fprintf(stderr, "Outut file: %s\n", i <= argc - 2 ? argv[i+1] : "stdout" ); |
152 |
fprintf(stderr, "Gamma: %f\n", gamma); |
153 |
fprintf(stderr, "Brightness adjust: %d\n", bradj); |
154 |
fprintf(stderr, "Verbose: %s\n", verbose ? "on" : "off"); |
155 |
#endif |
156 |
|
157 |
|
158 |
/* OK. Now we read the size of the Radiance picture */ |
159 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
160 |
fgetresolu(&xsize, &ysize, stdin) < 0 /* != (YMAJOR|YDECR) */ ) { |
161 |
fprintf(stderr, "%s: not a radiance picture\n", progname); |
162 |
exit(1); |
163 |
} |
164 |
|
165 |
/* Set the gamma correction */ |
166 |
|
167 |
setcolrgam(gamma); |
168 |
|
169 |
for(i=0; i<HEADER_SIZE; i++) |
170 |
putbyte(0); |
171 |
|
172 |
ssizepos = outbytes; |
173 |
putashort(0); /* low 16 bits of file size less HEADER_SIZE */ |
174 |
putrect(0,0,xsize,ysize); /* bounding box of picture */ |
175 |
putashort(PICT_picVersion); |
176 |
putashort(0x02ff); /* version 2 pict */ |
177 |
putashort(PICT_reservedHeader); /* reserved header opcode */ |
178 |
|
179 |
lsizepos = outbytes; |
180 |
putalong(0); /* full size of the file */ |
181 |
putfprect(0,0,xsize,ysize); /* fixed point bounding box of picture */ |
182 |
putalong(0); /* reserved */ |
183 |
|
184 |
putashort(PICT_clipRgn); /* the clip region */ |
185 |
putashort(10); |
186 |
putrect(0,0,xsize,ysize); |
187 |
|
188 |
if (verbose) |
189 |
fprintf(stderr, "%s: The picture is %d by %d, with a gamma of %f\n", |
190 |
progname, xsize, ysize, gamma); |
191 |
|
192 |
|
193 |
putpict(xsize, ysize); /* Here is where all the work is done */ |
194 |
|
195 |
putashort(PICT_EndOfPicture); /* end of pict */ |
196 |
|
197 |
picsize = outbytes-HEADER_SIZE; |
198 |
fseek(stdout,ssizepos,0); |
199 |
putashort(picsize&0xffff); |
200 |
fseek(stdout,lsizepos,0); |
201 |
putalong(picsize); |
202 |
|
203 |
fclose(stdout); |
204 |
fclose(stdin); |
205 |
|
206 |
exit(0); |
207 |
return 0; /* lint fodder */ |
208 |
} |
209 |
|
210 |
putpict(xsize, ysize) |
211 |
int xsize; |
212 |
int ysize; |
213 |
{ |
214 |
int y; |
215 |
int nbytes, rowbytes; |
216 |
char *cbuf, *pbuf; |
217 |
|
218 |
cbuf = malloc(4 * xsize); |
219 |
|
220 |
if (cbuf == NULL) { |
221 |
fprintf(stderr, "%s: not enough memory\n", progname); |
222 |
exit(1); |
223 |
} |
224 |
|
225 |
pbuf = malloc(4 * xsize); |
226 |
|
227 |
if (pbuf == NULL) { |
228 |
fprintf(stderr, "%s: not enough memory\n", progname); |
229 |
exit(1); |
230 |
} |
231 |
|
232 |
putashort(PICT_Pack32BitsRect); /* 32 bit rgb */ |
233 |
rowbytes = 4*xsize; |
234 |
putalong(0x000000ff); /* base address */ |
235 |
|
236 |
|
237 |
if(rowbytes&1) |
238 |
rowbytes++; |
239 |
putashort(rowbytes|0x8000); /* rowbytes */ |
240 |
putrect(0,0,xsize,ysize); /* bounds */ |
241 |
putashort(0); /* version */ |
242 |
|
243 |
putashort(4); /* packtype */ |
244 |
putalong(0); /* packsize */ |
245 |
putalong(72<<16); /* hres */ |
246 |
putalong(72<<16); /* vres */ |
247 |
|
248 |
putashort(16); /* pixeltype */ |
249 |
putashort(32); /* pixelsize */ |
250 |
putashort(3); /* cmpcount */ |
251 |
|
252 |
|
253 |
putashort(8); /* cmpsize */ |
254 |
putalong(0); /* planebytes */ |
255 |
putalong(0); /* pmtable */ |
256 |
putalong(0); /* pmreserved */ |
257 |
|
258 |
|
259 |
putrect(0,0,xsize,ysize); /* scr rect */ |
260 |
putrect(0,0,xsize,ysize); /* dest rect */ |
261 |
|
262 |
|
263 |
putashort(0x40); /* transfer mode */ |
264 |
|
265 |
for(y=0; y<ysize; y++) { |
266 |
getrow(stdin, cbuf, xsize); |
267 |
|
268 |
nbytes = packbits(cbuf,pbuf,24*xsize); |
269 |
if(rowbytes>250) |
270 |
putashort(nbytes); |
271 |
else |
272 |
putbyte(nbytes); |
273 |
putbytes(pbuf,nbytes); |
274 |
} |
275 |
|
276 |
if(outbytes&1) |
277 |
putbyte(0); |
278 |
|
279 |
free(cbuf); |
280 |
free(pbuf); |
281 |
} |
282 |
|
283 |
int getrow(in, cbuf, xsize) |
284 |
FILE *in; |
285 |
char *cbuf; |
286 |
int xsize; |
287 |
{ |
288 |
extern char *tempbuffer(); /* defined in color.c */ |
289 |
COLR *scanin = NULL; |
290 |
int x; |
291 |
|
292 |
if ((scanin = (COLR *)tempbuffer(xsize*sizeof(COLR))) == NULL) { |
293 |
fprintf(stderr, "%s: not enough memory\n", progname); |
294 |
exit(1); |
295 |
} |
296 |
|
297 |
if (freadcolrs(scanin, xsize, in) < 0) { |
298 |
fprintf(stderr, "%s: read error\n", progname); |
299 |
exit(1); |
300 |
} |
301 |
|
302 |
if (bradj) /* Adjust exposure level */ |
303 |
shiftcolrs(scanin, xsize, bradj); |
304 |
|
305 |
|
306 |
colrs_gambs(scanin, xsize); /* Gamma correct it */ |
307 |
|
308 |
for (x = 0; x < xsize; x++) { |
309 |
cbuf[x] = scanin[x][RED]; |
310 |
cbuf[xsize + x] = scanin[x][GRN]; |
311 |
cbuf[2*xsize + x] = scanin[x][BLU]; |
312 |
} |
313 |
|
314 |
} |
315 |
|
316 |
|
317 |
packbits(ibits,pbits,nbits) |
318 |
unsigned char *ibits, *pbits; |
319 |
int nbits; |
320 |
{ |
321 |
int bytes; /* UNUSED */ |
322 |
unsigned char *sptr; |
323 |
unsigned char *ibitsend; |
324 |
unsigned char *optr = pbits; |
325 |
int nbytes, todo, cc, count; |
326 |
|
327 |
nbytes = ((nbits-1)/8)+1; |
328 |
ibitsend = ibits+nbytes; |
329 |
while(ibits<ibitsend) { |
330 |
sptr = ibits; |
331 |
ibits += 2; |
332 |
while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0]))) |
333 |
ibits++; |
334 |
if(ibits != ibitsend) { |
335 |
ibits -= 2; |
336 |
} |
337 |
count = ibits-sptr; |
338 |
while(count) { |
339 |
todo = count>127 ? 127:count; |
340 |
count -= todo; |
341 |
*optr++ = todo-1; |
342 |
while(todo--) |
343 |
*optr++ = *sptr++; |
344 |
} |
345 |
if(ibits == ibitsend) |
346 |
break; |
347 |
sptr = ibits; |
348 |
cc = *ibits++; |
349 |
while( (ibits<ibitsend) && (*ibits == cc) ) |
350 |
ibits++; |
351 |
count = ibits-sptr; |
352 |
while(count) { |
353 |
todo = count>128 ? 128:count; |
354 |
count -= todo; |
355 |
*optr++ = 257-todo; |
356 |
*optr++ = cc; |
357 |
} |
358 |
} |
359 |
return optr-pbits; |
360 |
} |
361 |
|
362 |
usage() |
363 |
{ |
364 |
fprintf(stderr, "Usage: %s [-v] [-g gamma] [infile [outfile]]\n", |
365 |
progname); |
366 |
exit(2); |
367 |
} |