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