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