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