1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static char SCCSid[] = "$SunId$ Auckuni"; |
3 |
|
#endif |
4 |
|
|
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 |
< |
*/ |
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 |
< |
extern char *malloc(); |
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 |
< |
char cbuf[8192*5]; |
19 |
< |
char pbuf[8192]; |
20 |
< |
int outbytes; |
21 |
< |
FILE *outf, *inf; |
22 |
< |
char **gargv; |
35 |
> |
/* First some utility routines */ |
36 |
|
|
37 |
|
putrect(xorg,yorg,xsize,ysize) |
38 |
|
int xorg, yorg, xsize, ysize; |
69 |
|
} |
70 |
|
|
71 |
|
putbyte(b) |
72 |
< |
unsigned char b; |
72 |
> |
int b; |
73 |
|
{ |
74 |
< |
char c[1]; |
75 |
< |
|
63 |
< |
c[0] = b; |
64 |
< |
if(!fwrite(c,1,1,outf)) { |
65 |
< |
fprintf(stderr,"%s: error on write\n", gargv[0]); |
74 |
> |
if (putc(b,stdout) == EOF && ferror(stdout)) { |
75 |
> |
fprintf(stderr,"%s: error on write\n", progname); |
76 |
|
exit(1); |
77 |
|
} |
78 |
|
outbytes++; |
82 |
|
unsigned char *buf; |
83 |
|
int n; |
84 |
|
{ |
85 |
< |
if(!fwrite(buf,n,1,outf)) { |
86 |
< |
fprintf(stderr,"topict: error on write\n"); |
85 |
> |
if(!fwrite(buf,n,1,stdout)) { |
86 |
> |
fprintf(stderr,"%s: error on write\n", progname); |
87 |
|
exit(1); |
88 |
|
} |
89 |
|
outbytes+=n; |
97 |
|
int i, picsize; |
98 |
|
int ssizepos, lsizepos; |
99 |
|
|
100 |
< |
gargv = argv; |
100 |
> |
progname = argv[0]; |
101 |
|
|
102 |
< |
if( argc<3 ) { |
103 |
< |
fprintf(stderr, "Usage: %s inimage out.pict\n", gargv[0]); |
104 |
< |
exit(1); |
105 |
< |
} |
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 |
< |
if( (inf=fopen(gargv[1],"rb")) == NULL ) { |
109 |
< |
fprintf(stderr,"%s: can't open input file %s\n",gargv[0], gargv[1]); |
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( (outf=fopen(gargv[2],"wb")) == NULL ) { |
143 |
< |
fprintf(stderr,"%s: can't open output file %s\n", gargv[0], gargv[1]); |
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 |
< |
if (checkheader(inf, COLRFMT, NULL) < 0 || |
157 |
< |
fgetresolu(&xsize, &ysize, inf) < 0) { |
158 |
< |
fprintf(stderr, "%s: not a radiance picture\n", argv[1]); |
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 |
< |
} |
162 |
> |
} |
163 |
|
|
164 |
< |
setcolrgam(2.0); |
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 */ |
184 |
|
putashort(10); |
185 |
|
putrect(0,0,xsize,ysize); |
186 |
|
|
187 |
< |
putpict(xsize, ysize); |
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(outf,ssizepos,0); |
197 |
> |
fseek(stdout,ssizepos,0); |
198 |
|
putashort(picsize&0xffff); |
199 |
< |
fseek(outf,lsizepos,0); |
199 |
> |
fseek(stdout,lsizepos,0); |
200 |
|
putalong(picsize); |
201 |
|
|
202 |
< |
fclose(outf); |
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; |
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 */ |
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(inf, cbuf, xsize); |
265 |
> |
getrow(stdin, cbuf, xsize); |
266 |
|
|
267 |
|
nbytes = packbits(cbuf,pbuf,24*xsize); |
268 |
|
if(rowbytes>250) |
270 |
|
else |
271 |
|
putbyte(nbytes); |
272 |
|
putbytes(pbuf,nbytes); |
273 |
< |
} |
273 |
> |
} |
274 |
|
|
275 |
|
if(outbytes&1) |
276 |
|
putbyte(0); |
277 |
+ |
|
278 |
+ |
free(cbuf); |
279 |
+ |
free(pbuf); |
280 |
|
} |
281 |
|
|
282 |
< |
int |
283 |
< |
getrow(in, mybuff, xsize) |
284 |
< |
FILE *in; |
285 |
< |
char *mybuff; |
201 |
< |
int xsize; |
282 |
> |
int getrow(in, cbuf, xsize) |
283 |
> |
FILE *in; |
284 |
> |
char *cbuf; |
285 |
> |
int xsize; |
286 |
|
{ |
287 |
< |
COLOR color; |
288 |
< |
COLR *scanin = (COLR*) malloc(xsize * sizeof(COLR)); |
287 |
> |
extern char *tempbuffer(); /* defined in color.c */ |
288 |
> |
COLR *scanin = NULL; |
289 |
|
int x; |
290 |
|
|
291 |
< |
if (scanin == NULL) { |
292 |
< |
printf("scanin null"); |
291 |
> |
if ((scanin = (COLR *)tempbuffer(xsize*sizeof(COLR))) == NULL) { |
292 |
> |
fprintf(stderr, "%s: not enough memory\n", progname); |
293 |
> |
exit(1); |
294 |
|
} |
295 |
|
|
211 |
– |
|
296 |
|
if (freadcolrs(scanin, xsize, in) < 0) { |
297 |
< |
fprintf(stderr, " read error\n"); |
297 |
> |
fprintf(stderr, "%s: read error\n", progname); |
298 |
|
exit(1); |
299 |
< |
} |
299 |
> |
} |
300 |
|
|
301 |
< |
colrs_gambs(scanin, xsize); |
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 |
< |
colr_color(color, scanin[x]); |
309 |
< |
cbuf[xsize * 0 + x] = color[RED] * 255; |
310 |
< |
cbuf[xsize * 1 + x] = color[GRN] * 255; |
311 |
< |
cbuf[xsize * 2 + x] = color[BLU] * 255; |
312 |
< |
} |
225 |
< |
free(scanin); |
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; |
320 |
> |
int bytes; /* UNUSED */ |
321 |
|
unsigned char *sptr; |
322 |
|
unsigned char *ibitsend; |
323 |
|
unsigned char *optr = pbits; |
325 |
|
|
326 |
|
nbytes = ((nbits-1)/8)+1; |
327 |
|
ibitsend = ibits+nbytes; |
328 |
< |
while (ibits<ibitsend) { |
328 |
> |
while(ibits<ibitsend) { |
329 |
|
sptr = ibits; |
330 |
|
ibits += 2; |
331 |
|
while((ibits<ibitsend)&&((ibits[-2]!=ibits[-1])||(ibits[-1]!=ibits[0]))) |
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 |
|
} |