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