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