1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ AU"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
4 |
< |
/* |
5 |
< |
rad2pict - Convert an Radiance image to APPLE pict format. |
6 |
< |
|
7 |
< |
School of Architecture, Auckland University, Private Bag |
8 |
< |
Auckland, New Zealand |
9 |
< |
*/ |
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 |
> |
|
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]; |
17 |
< |
char pbuf[8192]; |
18 |
< |
int outbytes; |
19 |
< |
FILE *outf, *inf; |
20 |
< |
char **gargv; |
34 |
> |
/* First some utility routines */ |
35 |
|
|
36 |
|
putrect(xorg,yorg,xsize,ysize) |
37 |
|
int xorg, yorg, xsize, ysize; |
68 |
|
} |
69 |
|
|
70 |
|
putbyte(b) |
71 |
< |
unsigned char b; |
71 |
> |
int b; |
72 |
|
{ |
73 |
< |
char c[1]; |
74 |
< |
|
61 |
< |
c[0] = b; |
62 |
< |
if(!fwrite(c,1,1,outf)) { |
63 |
< |
fprintf(stderr,"%s: error on write\n", gargv[0]); |
73 |
> |
if (putc(b,stdout) == EOF && ferror(stdout)) { |
74 |
> |
fprintf(stderr,"%s: error on write\n", progname); |
75 |
|
exit(1); |
76 |
|
} |
77 |
|
outbytes++; |
81 |
|
unsigned char *buf; |
82 |
|
int n; |
83 |
|
{ |
84 |
< |
if(!fwrite(buf,n,1,outf)) { |
85 |
< |
fprintf(stderr,"topict: error on write\n"); |
84 |
> |
if(!fwrite(buf,n,1,stdout)) { |
85 |
> |
fprintf(stderr,"%s: error on write\n", progname); |
86 |
|
exit(1); |
87 |
|
} |
88 |
|
outbytes+=n; |
95 |
|
int xsize, ysize; |
96 |
|
int i, picsize; |
97 |
|
int ssizepos, lsizepos; |
98 |
+ |
SET_DEFAULT_BINARY(); |
99 |
+ |
SET_FILE_BINARY(stdin); |
100 |
+ |
SET_FILE_BINARY(stdout); |
101 |
+ |
progname = argv[0]; |
102 |
|
|
103 |
< |
gargv = argv; |
103 |
> |
for (i = 1; i < argc ; i++) |
104 |
> |
if (argv[i][0] == '-') |
105 |
> |
switch (argv[i][1]) { |
106 |
> |
case 'g': gamcor = atof(argv[++i]); |
107 |
> |
break; |
108 |
|
|
109 |
< |
if( argc<3 ) { |
110 |
< |
fprintf(stderr, "Usage: %s inimage out.pict\n", gargv[0]); |
111 |
< |
exit(1); |
112 |
< |
} |
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 |
< |
if( (inf=fopen(gargv[1],"rb")) == NULL ) { |
116 |
< |
fprintf(stderr,"%s: can't open input file %s\n",gargv[0], gargv[1]); |
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( (outf=fopen(gargv[2],"wb")) == NULL ) { |
144 |
< |
fprintf(stderr,"%s: can't open output file %s\n", gargv[0], gargv[1]); |
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", gamcor); |
153 |
+ |
fprintf(stderr, "Brightness adjust: %d\n", bradj); |
154 |
+ |
fprintf(stderr, "Verbose: %s\n", verbose ? "on" : "off"); |
155 |
+ |
#endif |
156 |
|
|
105 |
– |
if (checkheader(inf, COLRFMT, NULL) < 0 || |
106 |
– |
fgetresolu(&xsize, &ysize, inf) != (YMAJOR|YDECR)) { |
107 |
– |
fprintf(stderr, "%s: not a radiance picture\n", argv[1]); |
108 |
– |
exit(1); |
109 |
– |
} |
157 |
|
|
158 |
< |
setcolrgam(2.0); |
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(gamcor); |
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 */ |
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 */ |
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 */ |
184 |
> |
putashort(PICT_clipRgn); /* the clip region */ |
185 |
|
putashort(10); |
186 |
|
putrect(0,0,xsize,ysize); |
187 |
|
|
188 |
< |
putpict(xsize, ysize); |
188 |
> |
if (verbose) |
189 |
> |
fprintf(stderr, "%s: The picture is %d by %d, with a gamma of %f\n", |
190 |
> |
progname, xsize, ysize, gamcor); |
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(outf,ssizepos,0); |
198 |
> |
fseek(stdout,ssizepos,0); |
199 |
|
putashort(picsize&0xffff); |
200 |
< |
fseek(outf,lsizepos,0); |
200 |
> |
fseek(stdout,lsizepos,0); |
201 |
|
putalong(picsize); |
202 |
|
|
203 |
< |
fclose(outf); |
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; |
214 |
> |
int y; |
215 |
> |
int nbytes, rowbytes; |
216 |
> |
char *cbuf, *pbuf; |
217 |
|
|
218 |
+ |
cbuf = (char *)malloc(4 * xsize); |
219 |
+ |
|
220 |
+ |
if (cbuf == NULL) { |
221 |
+ |
fprintf(stderr, "%s: not enough memory\n", progname); |
222 |
+ |
exit(1); |
223 |
+ |
} |
224 |
+ |
|
225 |
+ |
pbuf = (char *)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 */ |
239 |
> |
putashort(rowbytes|0x8000); /* rowbytes */ |
240 |
|
putrect(0,0,xsize,ysize); /* bounds */ |
241 |
|
putashort(0); /* version */ |
242 |
|
|
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(inf, cbuf, xsize); |
266 |
> |
getrow(stdin, cbuf, xsize); |
267 |
|
|
268 |
|
nbytes = packbits(cbuf,pbuf,24*xsize); |
269 |
|
if(rowbytes>250) |
271 |
|
else |
272 |
|
putbyte(nbytes); |
273 |
|
putbytes(pbuf,nbytes); |
274 |
< |
} |
274 |
> |
} |
275 |
|
|
276 |
|
if(outbytes&1) |
277 |
|
putbyte(0); |
278 |
+ |
|
279 |
+ |
free(cbuf); |
280 |
+ |
free(pbuf); |
281 |
|
} |
282 |
|
|
283 |
< |
int |
284 |
< |
getrow(in, mybuff, xsize) |
285 |
< |
FILE *in; |
286 |
< |
char *mybuff; |
199 |
< |
int xsize; |
283 |
> |
int getrow(in, cbuf, xsize) |
284 |
> |
FILE *in; |
285 |
> |
char *cbuf; |
286 |
> |
int xsize; |
287 |
|
{ |
288 |
< |
COLOR color; |
289 |
< |
COLR *scanin = (COLR*) malloc(xsize * sizeof(COLR)); |
288 |
> |
extern char *tempbuffer(); /* defined in color.c */ |
289 |
> |
COLR *scanin = NULL; |
290 |
|
int x; |
291 |
|
|
292 |
< |
if (scanin == NULL) { |
293 |
< |
printf("scanin null"); |
292 |
> |
if ((scanin = (COLR *)tempbuffer(xsize*sizeof(COLR))) == NULL) { |
293 |
> |
fprintf(stderr, "%s: not enough memory\n", progname); |
294 |
> |
exit(1); |
295 |
|
} |
296 |
|
|
209 |
– |
|
297 |
|
if (freadcolrs(scanin, xsize, in) < 0) { |
298 |
< |
fprintf(stderr, " read error\n"); |
299 |
< |
exit(1); |
300 |
< |
} |
298 |
> |
fprintf(stderr, "%s: read error\n", progname); |
299 |
> |
exit(1); |
300 |
> |
} |
301 |
|
|
302 |
< |
colrs_gambs(scanin, xsize); |
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 |
< |
colr_color(color, scanin[x]); |
310 |
< |
cbuf[xsize * 0 + x] = color[RED] * 255; |
311 |
< |
cbuf[xsize * 1 + x] = color[GRN] * 255; |
312 |
< |
cbuf[xsize * 2 + x] = color[BLU] * 255; |
313 |
< |
} |
223 |
< |
free(scanin); |
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; |
321 |
> |
int bytes; /* UNUSED */ |
322 |
|
unsigned char *sptr; |
323 |
|
unsigned char *ibitsend; |
324 |
|
unsigned char *optr = pbits; |
326 |
|
|
327 |
|
nbytes = ((nbits-1)/8)+1; |
328 |
|
ibitsend = ibits+nbytes; |
329 |
< |
while (ibits<ibitsend) { |
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) { |
334 |
> |
if(ibits != ibitsend) { |
335 |
|
ibits -= 2; |
336 |
|
} |
337 |
|
count = ibits-sptr; |
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 |
|
} |