1 |
– |
/* Copyright (c) 1986 Regents of the University of California */ |
2 |
– |
|
1 |
|
#ifndef lint |
2 |
< |
static char SCCSid[] = "$SunId$ LBL"; |
2 |
> |
static const char RCSid[] = "$Id$"; |
3 |
|
#endif |
6 |
– |
|
4 |
|
/* |
5 |
|
* pvalue.c - program to print pixel values. |
6 |
|
* |
7 |
|
* 4/23/86 |
8 |
|
*/ |
9 |
|
|
10 |
< |
#include <stdio.h> |
11 |
< |
|
10 |
> |
#include "platform.h" |
11 |
> |
#include "standard.h" |
12 |
|
#include "color.h" |
13 |
+ |
#include "resolu.h" |
14 |
+ |
#include "view.h" |
15 |
|
|
16 |
< |
#define min(a,b) ((a)<(b)?(a):(b)) |
16 |
> |
#define min(a,b) ((a)<(b)?(a):(b)) |
17 |
|
|
18 |
< |
int xres = 0; /* resolution of input */ |
19 |
< |
int yres = 0; |
18 |
> |
/* what to put out (also RED, GRN, BLU) */ |
19 |
> |
#define ALL 3 |
20 |
> |
#define BRIGHT 4 |
21 |
|
|
22 |
< |
int uniq = 0; /* unique values? */ |
23 |
< |
|
24 |
< |
int original = 0; /* original values? */ |
25 |
< |
|
22 |
> |
RESOLU picres; /* resolution of picture */ |
23 |
> |
int uniq = 0; /* print only unique values? */ |
24 |
> |
int doexposure = 0; /* exposure change? (>100 to print) */ |
25 |
|
int dataonly = 0; /* data only format? */ |
26 |
< |
|
28 |
< |
int brightonly = 0; /* only brightness values? */ |
29 |
< |
|
26 |
> |
int putprim = ALL; /* what to put out */ |
27 |
|
int reverse = 0; /* reverse conversion? */ |
31 |
– |
|
28 |
|
int format = 'a'; /* input/output format */ |
29 |
+ |
const char *fmtid = "ascii"; /* format identifier for header */ |
30 |
+ |
int header = 1; /* do header? */ |
31 |
+ |
long skipbytes = 0; /* skip bytes in input? */ |
32 |
+ |
int swapbytes = 0; /* swap bytes? */ |
33 |
+ |
int interleave = 1; /* file is interleaved? */ |
34 |
+ |
int resolution = 1; /* put/get resolution string? */ |
35 |
+ |
int original = 0; /* convert to original values? */ |
36 |
+ |
int wrongformat = 0; /* wrong input format? */ |
37 |
+ |
double gamcor = 1.0; /* gamma correction */ |
38 |
|
|
39 |
< |
int header = 1; /* do header */ |
39 |
> |
RGBPRIMP outprims = stdprims; /* output primaries for reverse conversion */ |
40 |
> |
RGBPRIMS myprims; |
41 |
|
|
42 |
< |
double exposure = 1.0; |
42 |
> |
int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ |
43 |
> |
int rord[4]; /* reverse ordering */ |
44 |
|
|
45 |
+ |
COLOR exposure = WHTCOLOR; |
46 |
+ |
|
47 |
|
char *progname; |
48 |
|
|
49 |
|
FILE *fin; |
50 |
+ |
FILE *fin2 = NULL, *fin3 = NULL; /* for other color channels */ |
51 |
|
|
42 |
– |
int (*getval)(), (*putval)(); |
52 |
|
|
53 |
+ |
typedef int getfunc_t(COLOR col); |
54 |
+ |
typedef int putfunc_t(COLOR col); |
55 |
+ |
typedef double brightfunc_t(COLOR col); |
56 |
|
|
57 |
< |
main(argc, argv) |
58 |
< |
int argc; |
59 |
< |
char **argv; |
57 |
> |
getfunc_t *getval; |
58 |
> |
putfunc_t *putval; |
59 |
> |
brightfunc_t *mybright; |
60 |
> |
|
61 |
> |
static gethfunc checkhead; |
62 |
> |
static brightfunc_t rgb_bright, xyz_bright; |
63 |
> |
static getfunc_t getbascii, getbint, getbdouble, getbfloat, getbbyte, getbword; |
64 |
> |
static getfunc_t getcascii, getcint, getcdouble, getcfloat, getcbyte, getcword; |
65 |
> |
static putfunc_t putbascii, putbint, putbdouble, putbfloat, putbbyte, putbword; |
66 |
> |
static putfunc_t putcascii, putcint, putcdouble, putcfloat, putcbyte, putcword; |
67 |
> |
static putfunc_t putpascii, putpint, putpdouble, putpfloat, putpbyte, putpword; |
68 |
> |
|
69 |
> |
static int IDformat(const char *s); |
70 |
> |
static void set_io(void); |
71 |
> |
static void pixtoval(void); |
72 |
> |
static void valtopix(void); |
73 |
> |
|
74 |
> |
|
75 |
> |
static double |
76 |
> |
rgb_bright( |
77 |
> |
COLOR clr |
78 |
> |
) |
79 |
|
{ |
80 |
< |
extern double atof(); |
81 |
< |
extern int checkhead(); |
51 |
< |
int i; |
80 |
> |
return(bright(clr)); |
81 |
> |
} |
82 |
|
|
83 |
+ |
static double |
84 |
+ |
xyz_bright( |
85 |
+ |
COLOR clr |
86 |
+ |
) |
87 |
+ |
{ |
88 |
+ |
return(clr[CIEY]); |
89 |
+ |
} |
90 |
+ |
|
91 |
+ |
int |
92 |
+ |
main( |
93 |
+ |
int argc, |
94 |
+ |
char **argv |
95 |
+ |
) |
96 |
+ |
{ |
97 |
+ |
const char *inpmode = "r"; |
98 |
+ |
double d, expval = 1.0; |
99 |
+ |
int i; |
100 |
+ |
|
101 |
|
progname = argv[0]; |
102 |
+ |
mybright = &rgb_bright; /* default */ |
103 |
+ |
picres.rt = PIXSTANDARD; |
104 |
|
|
105 |
|
for (i = 1; i < argc; i++) |
106 |
< |
if (argv[i][0] == '-') |
106 |
> |
if (argv[i][0] == '-' || argv[i][0] == '+') |
107 |
|
switch (argv[i][1]) { |
108 |
< |
case 'h': /* no header */ |
109 |
< |
header = 0; |
108 |
> |
case 'h': /* header */ |
109 |
> |
header = argv[i][0] == '+'; |
110 |
|
break; |
111 |
+ |
case 'H': /* resolution string */ |
112 |
+ |
resolution = argv[i][0] == '+'; |
113 |
+ |
break; |
114 |
+ |
case 's': /* skip bytes in header */ |
115 |
+ |
skipbytes = atol(argv[++i]); |
116 |
+ |
break; |
117 |
|
case 'u': /* unique values */ |
118 |
< |
uniq = 1; |
118 |
> |
uniq = argv[i][0] == '-'; |
119 |
|
break; |
120 |
|
case 'o': /* original values */ |
121 |
< |
original = 1; |
121 |
> |
original = argv[i][0] == '-'; |
122 |
|
break; |
123 |
+ |
case 'O': /* original watts/sr/m^2 */ |
124 |
+ |
original = -(argv[i][0] == '-'); |
125 |
+ |
break; |
126 |
+ |
case 'g': /* gamma correction */ |
127 |
+ |
gamcor = atof(argv[i+1]); |
128 |
+ |
if (argv[i][0] == '+') |
129 |
+ |
gamcor = 1.0/gamcor; |
130 |
+ |
i++; |
131 |
+ |
break; |
132 |
+ |
case 'e': /* exposure correction */ |
133 |
+ |
d = atof(argv[i+1]); |
134 |
+ |
if (argv[i+1][0] == '-' || argv[i+1][0] == '+') |
135 |
+ |
d = pow(2.0, d); |
136 |
+ |
if (argv[i][0] == '-') |
137 |
+ |
expval *= d; |
138 |
+ |
scalecolor(exposure, d); |
139 |
+ |
doexposure++; |
140 |
+ |
i++; |
141 |
+ |
break; |
142 |
+ |
case 'R': /* reverse byte sequence */ |
143 |
+ |
if (argv[i][0] == '-') { |
144 |
+ |
ord[0]=BLU; ord[1]=GRN; ord[2]=RED; |
145 |
+ |
} else { |
146 |
+ |
ord[0]=RED; ord[1]=GRN; ord[2]=BLU; |
147 |
+ |
} |
148 |
+ |
break; |
149 |
|
case 'r': /* reverse conversion */ |
150 |
< |
reverse = 1; |
150 |
> |
reverse = argv[i][0] == '-'; |
151 |
|
break; |
152 |
+ |
case 'n': /* non-interleaved RGB */ |
153 |
+ |
interleave = argv[i][0] == '+'; |
154 |
+ |
break; |
155 |
|
case 'b': /* brightness values */ |
156 |
< |
brightonly = 1; |
156 |
> |
putprim = argv[i][0] == '-' ? BRIGHT : ALL; |
157 |
|
break; |
158 |
+ |
case 'p': /* primary controls */ |
159 |
+ |
switch (argv[i][2]) { |
160 |
+ |
/* these two options affect -r conversion */ |
161 |
+ |
case '\0': |
162 |
+ |
myprims[RED][CIEX] = atof(argv[++i]); |
163 |
+ |
myprims[RED][CIEY] = atof(argv[++i]); |
164 |
+ |
myprims[GRN][CIEX] = atof(argv[++i]); |
165 |
+ |
myprims[GRN][CIEY] = atof(argv[++i]); |
166 |
+ |
myprims[BLU][CIEX] = atof(argv[++i]); |
167 |
+ |
myprims[BLU][CIEY] = atof(argv[++i]); |
168 |
+ |
myprims[WHT][CIEX] = atof(argv[++i]); |
169 |
+ |
myprims[WHT][CIEY] = atof(argv[++i]); |
170 |
+ |
outprims = myprims; |
171 |
+ |
break; |
172 |
+ |
case 'x': case 'X': outprims = NULL; break; |
173 |
+ |
/* the following options affect +r only */ |
174 |
+ |
case 'r': case 'R': putprim = RED; break; |
175 |
+ |
case 'g': case 'G': putprim = GRN; break; |
176 |
+ |
case 'b': case 'B': putprim = BLU; break; |
177 |
+ |
default: goto unkopt; |
178 |
+ |
} |
179 |
+ |
break; |
180 |
|
case 'd': /* data only (no indices) */ |
181 |
< |
dataonly = 1; |
181 |
> |
dataonly = (argv[i][0] == '-'); |
182 |
|
switch (argv[i][2]) { |
183 |
|
case '\0': |
184 |
|
case 'a': /* ascii */ |
185 |
|
format = 'a'; |
186 |
+ |
fmtid = "ascii"; |
187 |
|
break; |
188 |
|
case 'i': /* integer */ |
189 |
+ |
format = 'i'; |
190 |
+ |
fmtid = "ascii"; |
191 |
+ |
break; |
192 |
|
case 'b': /* byte */ |
193 |
+ |
dataonly = 1; |
194 |
+ |
format = 'b'; |
195 |
+ |
fmtid = "byte"; |
196 |
+ |
break; |
197 |
+ |
case 'W': /* 16-bit swapped */ |
198 |
+ |
swapbytes = 1; |
199 |
+ |
case 'w': /* 16-bit */ |
200 |
+ |
dataonly = 1; |
201 |
+ |
format = 'w'; |
202 |
+ |
fmtid = "16-bit"; |
203 |
+ |
break; |
204 |
+ |
case 'F': /* swapped floats */ |
205 |
+ |
swapbytes = 1; |
206 |
|
case 'f': /* float */ |
207 |
+ |
dataonly = 1; |
208 |
+ |
format = 'f'; |
209 |
+ |
fmtid = "float"; |
210 |
+ |
break; |
211 |
+ |
case 'D': /* swapped doubles */ |
212 |
+ |
swapbytes = 1; |
213 |
|
case 'd': /* double */ |
214 |
< |
format = argv[i][2]; |
214 |
> |
dataonly = 1; |
215 |
> |
format = 'd'; |
216 |
> |
fmtid = "double"; |
217 |
|
break; |
218 |
|
default: |
219 |
|
goto unkopt; |
220 |
|
} |
221 |
|
break; |
222 |
|
case 'x': /* x resolution */ |
223 |
< |
xres = atoi(argv[++i]); |
223 |
> |
case 'X': |
224 |
> |
resolution = 0; |
225 |
> |
if (argv[i][0] == '-') |
226 |
> |
picres.rt |= XDECR; |
227 |
> |
else |
228 |
> |
picres.rt &= ~XDECR; |
229 |
> |
if (picres.yr == 0) |
230 |
> |
picres.rt &= ~YMAJOR; |
231 |
> |
picres.xr = atoi(argv[++i]); |
232 |
|
break; |
233 |
|
case 'y': /* y resolution */ |
234 |
< |
yres = atoi(argv[++i]); |
234 |
> |
case 'Y': |
235 |
> |
resolution = 0; |
236 |
> |
if (argv[i][0] == '-') |
237 |
> |
picres.rt |= YDECR; |
238 |
> |
else |
239 |
> |
picres.rt &= ~YDECR; |
240 |
> |
if (picres.xr == 0) |
241 |
> |
picres.rt |= YMAJOR; |
242 |
> |
picres.yr = atoi(argv[++i]); |
243 |
|
break; |
244 |
|
default: |
245 |
|
unkopt: |
250 |
|
} |
251 |
|
else |
252 |
|
break; |
253 |
< |
|
253 |
> |
/* recognize special formats */ |
254 |
> |
if (dataonly & (format == 'b')) { |
255 |
> |
if (putprim == ALL) |
256 |
> |
fmtid = "24-bit_rgb"; |
257 |
> |
else |
258 |
> |
fmtid = "8-bit_grey"; |
259 |
> |
} |
260 |
> |
if (dataonly & (format == 'w')) { |
261 |
> |
if (putprim == ALL) |
262 |
> |
fmtid = "48-bit_rgb"; |
263 |
> |
else |
264 |
> |
fmtid = "16-bit_grey"; |
265 |
> |
} |
266 |
> |
if (!reverse | ((format != 'a') & (format != 'i'))) |
267 |
> |
inpmode = "rb"; |
268 |
> |
/* assign reverse ordering */ |
269 |
> |
rord[ord[0]] = 0; |
270 |
> |
rord[ord[1]] = 1; |
271 |
> |
rord[ord[2]] = 2; |
272 |
> |
/* get input */ |
273 |
|
if (i == argc) { |
274 |
+ |
long n = skipbytes; |
275 |
+ |
if (inpmode[1] == 'b') |
276 |
+ |
SET_FILE_BINARY(stdin); |
277 |
+ |
while (n-- > 0) |
278 |
+ |
if (getchar() == EOF) { |
279 |
+ |
fprintf(stderr, |
280 |
+ |
"%s: cannot skip %ld bytes on standard input\n", |
281 |
+ |
progname, skipbytes); |
282 |
+ |
quit(1); |
283 |
+ |
} |
284 |
|
fin = stdin; |
285 |
< |
} else if (i == argc-1) { |
286 |
< |
if ((fin = fopen(argv[i], "r")) == NULL) { |
285 |
> |
} else if (i < argc) { |
286 |
> |
if ((fin = fopen(argv[i], inpmode)) == NULL) { |
287 |
|
fprintf(stderr, "%s: can't open file \"%s\"\n", |
288 |
|
progname, argv[i]); |
289 |
|
quit(1); |
290 |
|
} |
291 |
< |
} else { |
291 |
> |
if (reverse & (putprim != BRIGHT ) & (i == argc-3)) { |
292 |
> |
if ((fin2 = fopen(argv[i+1], inpmode)) == NULL) { |
293 |
> |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
294 |
> |
progname, argv[i+1]); |
295 |
> |
quit(1); |
296 |
> |
} |
297 |
> |
if ((fin3 = fopen(argv[i+2], inpmode)) == NULL) { |
298 |
> |
fprintf(stderr, "%s: can't open file \"%s\"\n", |
299 |
> |
progname, argv[i+2]); |
300 |
> |
quit(1); |
301 |
> |
} |
302 |
> |
interleave = -1; |
303 |
> |
} else if (i != argc-1) |
304 |
> |
fin = NULL; |
305 |
> |
if (reverse & (putprim != BRIGHT) & !interleave) { |
306 |
> |
fin2 = fopen(argv[i], inpmode); |
307 |
> |
fin3 = fopen(argv[i], inpmode); |
308 |
> |
} |
309 |
> |
if (skipbytes && (fseek(fin, skipbytes, SEEK_SET) || (fin2 != NULL && |
310 |
> |
fseek(fin2, skipbytes, SEEK_SET) | |
311 |
> |
fseek(fin3, skipbytes, SEEK_SET)))) { |
312 |
> |
fprintf(stderr, "%s: cannot skip %ld bytes on input\n", |
313 |
> |
progname, skipbytes); |
314 |
> |
quit(1); |
315 |
> |
} |
316 |
> |
} |
317 |
> |
if (fin == NULL) { |
318 |
|
fprintf(stderr, "%s: bad # file arguments\n", progname); |
319 |
|
quit(1); |
320 |
|
} |
118 |
– |
|
119 |
– |
set_io(); |
120 |
– |
|
321 |
|
if (reverse) { |
322 |
< |
if (header) /* get header */ |
323 |
< |
copyheader(fin, stdout); |
322 |
> |
SET_FILE_BINARY(stdout); |
323 |
> |
/* get header */ |
324 |
> |
if (header) { |
325 |
> |
getheader(fin, checkhead, stdout); |
326 |
> |
if (wrongformat) { |
327 |
> |
fprintf(stderr, "%s: wrong input format (expected %s)\n", |
328 |
> |
progname, fmtid); |
329 |
> |
quit(1); |
330 |
> |
} |
331 |
> |
if (fin2 != NULL) { |
332 |
> |
getheader(fin2, NULL, NULL); |
333 |
> |
getheader(fin3, NULL, NULL); |
334 |
> |
} |
335 |
> |
resolution &= (picres.xr <= 0) | (picres.yr <= 0); |
336 |
> |
} else { |
337 |
> |
newheader("RADIANCE", stdout); |
338 |
> |
} |
339 |
> |
/* get resolution */ |
340 |
> |
if ((resolution && !fgetsresolu(&picres, fin)) || |
341 |
> |
(picres.xr <= 0) | (picres.yr <= 0)) { |
342 |
> |
fprintf(stderr, "%s: missing resolution\n", progname); |
343 |
> |
quit(1); |
344 |
> |
} |
345 |
> |
if (resolution && fin2 != NULL) { |
346 |
> |
RESOLU pres2; |
347 |
> |
if (!fgetsresolu(&pres2, fin2) || |
348 |
> |
(pres2.rt != picres.rt) | |
349 |
> |
(pres2.xr != picres.xr) | |
350 |
> |
(pres2.yr != picres.yr) || |
351 |
> |
!fgetsresolu(&pres2, fin3) || |
352 |
> |
(pres2.rt != picres.rt) | |
353 |
> |
(pres2.xr != picres.xr) | |
354 |
> |
(pres2.yr != picres.yr)) { |
355 |
> |
fprintf(stderr, "%s: resolution mismatch\n", |
356 |
> |
progname); |
357 |
> |
quit(1); |
358 |
> |
} |
359 |
> |
} |
360 |
|
/* add to header */ |
361 |
|
printargs(i, argv, stdout); |
362 |
< |
printf("\n"); |
363 |
< |
if (yres <= 0 || xres <= 0) { |
364 |
< |
fprintf(stderr, "%s: missing x and y resolution\n", |
365 |
< |
progname); |
366 |
< |
quit(1); |
362 |
> |
if ((expval < .99) | (expval > 1.01)) |
363 |
> |
fputexpos(expval, stdout); |
364 |
> |
if (NCSAMP == 1) { |
365 |
> |
if (putprim == ALL) |
366 |
> |
putprim = BRIGHT; |
367 |
> |
} else { |
368 |
> |
if (NCSAMP != 3) { |
369 |
> |
fprintf(stderr, "%s: input NCOMP must be 1 or 3\n", |
370 |
> |
progname); |
371 |
> |
quit(1); |
372 |
> |
} |
373 |
> |
if (putprim != ALL) { |
374 |
> |
fprintf(stderr, "%s: NCOMP mismatch\n", progname); |
375 |
> |
quit(1); |
376 |
> |
} |
377 |
|
} |
378 |
< |
fputresolu(YMAJOR|YDECR, xres, yres, stdout); |
378 |
> |
if (outprims != NULL) { |
379 |
> |
if (outprims != stdprims) |
380 |
> |
fputprims(outprims, stdout); |
381 |
> |
fputformat(COLRFMT, stdout); |
382 |
> |
} else { /* XYZ data */ |
383 |
> |
if (original < 0) { |
384 |
> |
scalecolor(exposure, WHTEFFICACY); |
385 |
> |
doexposure++; |
386 |
> |
} |
387 |
> |
fputformat(CIEFMT, stdout); |
388 |
> |
} |
389 |
> |
putchar('\n'); |
390 |
> |
fputsresolu(&picres, stdout); /* always put resolution */ |
391 |
|
valtopix(); |
392 |
|
} else { |
393 |
+ |
if ((format != 'a') & (format != 'i')) |
394 |
+ |
SET_FILE_BINARY(stdout); |
395 |
|
/* get header */ |
396 |
< |
getheader(fin, checkhead); |
397 |
< |
|
398 |
< |
if (xres <= 0 || yres <= 0) /* get picture size */ |
399 |
< |
if (fgetresolu(&xres, &yres, fin) != (YMAJOR|YDECR)) { |
400 |
< |
fprintf(stderr, |
401 |
< |
"%s: missing x and y resolution\n", |
402 |
< |
progname); |
403 |
< |
quit(1); |
404 |
< |
} |
396 |
> |
picres.rt = PIXSTANDARD; |
397 |
> |
picres.xr = picres.yr = 0; |
398 |
> |
getheader(fin, checkhead, header ? stdout : (FILE *)NULL); |
399 |
> |
if (wrongformat) { |
400 |
> |
fprintf(stderr, |
401 |
> |
"%s: input not a Radiance RGBE picture\n", |
402 |
> |
progname); |
403 |
> |
quit(1); |
404 |
> |
} |
405 |
> |
if ((picres.xr <= 0) | (picres.yr <= 0) && |
406 |
> |
!fgetsresolu(&picres, fin)) { |
407 |
> |
fprintf(stderr, "%s: missing resolution\n", progname); |
408 |
> |
quit(1); |
409 |
> |
} |
410 |
> |
if ((original < 0) & (mybright == &xyz_bright)) { |
411 |
> |
scalecolor(exposure, 1./WHTEFFICACY); |
412 |
> |
doexposure++; |
413 |
> |
} |
414 |
|
if (header) { |
415 |
|
printargs(i, argv, stdout); |
416 |
< |
printf("\n"); |
416 |
> |
if (dataonly) |
417 |
> |
printf("%s%c\n", NCOMPSTR, "13"[putprim==ALL]); |
418 |
> |
if (dataonly && !resolution & !uniq) |
419 |
> |
printf("NCOLS=%d\nNROWS=%d\n", scanlen(&picres), |
420 |
> |
numscans(&picres)); |
421 |
> |
if ((expval < .99) | (expval > 1.01)) |
422 |
> |
fputexpos(expval, stdout); |
423 |
> |
if (swapbytes) { |
424 |
> |
if (nativebigendian()) |
425 |
> |
puts("BigEndian=0"); |
426 |
> |
else |
427 |
> |
puts("BigEndian=1"); |
428 |
> |
} else if ((format != 'a') & (format != 'i') & |
429 |
> |
(format != 'b')) |
430 |
> |
fputendian(stdout); |
431 |
> |
fputformat(fmtid, stdout); |
432 |
> |
putchar('\n'); |
433 |
|
} |
434 |
+ |
if (resolution) /* put resolution */ |
435 |
+ |
fputsresolu(&picres, stdout); |
436 |
|
pixtoval(); |
437 |
|
} |
438 |
|
|
439 |
|
quit(0); |
440 |
+ |
return 0; /* pro forma return */ |
441 |
|
} |
442 |
|
|
443 |
|
|
444 |
< |
checkhead(line) /* deal with line from header */ |
445 |
< |
char *line; |
444 |
> |
static int |
445 |
> |
IDformat(const char *s) |
446 |
|
{ |
447 |
< |
if (header) |
448 |
< |
fputs(line, stdout); |
449 |
< |
if (isexpos(line)) |
450 |
< |
exposure *= exposval(line); |
447 |
> |
if (!s || !*s) return(0); |
448 |
> |
if (!strcmp(s, "ascii")) return('a'); |
449 |
> |
if (!strcmp(s, "float")) return('f'); |
450 |
> |
if (!strcmp(s, "double")) return('d'); |
451 |
> |
if (!strcmp(s, "byte")) return('b'); |
452 |
> |
if (!strcmp(s, "16-bit")) return('w'); |
453 |
> |
return(0); |
454 |
|
} |
455 |
|
|
456 |
|
|
457 |
< |
pixtoval() /* convert picture to values */ |
457 |
> |
static int |
458 |
> |
checkhead( /* deal with line from header */ |
459 |
> |
char *line, |
460 |
> |
void *p |
461 |
> |
) |
462 |
|
{ |
463 |
< |
COLOR *scanln; |
463 |
> |
static char fmt[MAXFMTLEN]; |
464 |
> |
FILE *fout = (FILE *)p; |
465 |
> |
double d; |
466 |
> |
COLOR ctmp; |
467 |
> |
int rv; |
468 |
> |
|
469 |
> |
if (formatval(fmt, line)) { |
470 |
> |
if (reverse) { |
471 |
> |
if (format == 'a') { |
472 |
> |
format = IDformat(fmt); |
473 |
> |
if (format) |
474 |
> |
fmtid = fmt; |
475 |
> |
else |
476 |
> |
wrongformat = 1; |
477 |
> |
} else |
478 |
> |
wrongformat = strcmp(fmt, fmtid); |
479 |
> |
} else if (!strcmp(fmt, CIEFMT)) |
480 |
> |
mybright = &xyz_bright; |
481 |
> |
else if (!strcmp(fmt, COLRFMT) || !strcmp(fmt, SPECFMT)) |
482 |
> |
mybright = &rgb_bright; |
483 |
> |
else |
484 |
> |
wrongformat = 1; |
485 |
> |
return(1); |
486 |
> |
} |
487 |
> |
if (!strncmp(line,"NROWS=",6)) { |
488 |
> |
picres.yr = atoi(line+6); |
489 |
> |
return(1); |
490 |
> |
} |
491 |
> |
if (!strncmp(line,"NCOLS=",6)) { |
492 |
> |
picres.xr = atoi(line+6); |
493 |
> |
return(1); |
494 |
> |
} |
495 |
> |
if (isncomp(line)) { |
496 |
> |
NCSAMP = ncompval(line); |
497 |
> |
dataonly |= reverse; /* pretty safe assumption */ |
498 |
> |
return(1); |
499 |
> |
} |
500 |
> |
if (iswlsplit(line)) { |
501 |
> |
wlsplitval(WLPART, line); |
502 |
> |
return(1); |
503 |
> |
} |
504 |
> |
if (original && isexpos(line)) { |
505 |
> |
d = 1.0/exposval(line); |
506 |
> |
scalecolor(exposure, d); |
507 |
> |
doexposure++; |
508 |
> |
return(1); |
509 |
> |
} |
510 |
> |
if (original && iscolcor(line)) { |
511 |
> |
colcorval(ctmp, line); |
512 |
> |
setcolor(exposure, colval(exposure,RED)/colval(ctmp,RED), |
513 |
> |
colval(exposure,GRN)/colval(ctmp,GRN), |
514 |
> |
colval(exposure,BLU)/colval(ctmp,BLU)); |
515 |
> |
doexposure++; |
516 |
> |
return(1); |
517 |
> |
} |
518 |
> |
if ((rv = isbigendian(line)) >= 0) { |
519 |
> |
if (reverse) |
520 |
> |
swapbytes = (nativebigendian() != rv); |
521 |
> |
return(1); |
522 |
> |
} |
523 |
> |
if (fout != NULL) |
524 |
> |
fputs(line, fout); |
525 |
> |
return(0); |
526 |
> |
} |
527 |
> |
|
528 |
> |
|
529 |
> |
static void |
530 |
> |
pixtoval(void) /* convert picture to values */ |
531 |
> |
{ |
532 |
> |
COLOR *scanln; |
533 |
> |
int dogamma; |
534 |
|
COLOR lastc; |
535 |
< |
int y; |
536 |
< |
register int x; |
535 |
> |
RREAL hv[2]; |
536 |
> |
int startprim, endprim; |
537 |
> |
long startpos; |
538 |
> |
int x, y; |
539 |
|
|
540 |
< |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
540 |
> |
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
541 |
|
if (scanln == NULL) { |
542 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
543 |
|
quit(1); |
544 |
|
} |
545 |
< |
setcolor(lastc, 0.0, 0.0, 0.0); |
546 |
< |
for (y = yres-1; y >= 0; y--) { |
547 |
< |
if (freadscan(scanln, xres, fin) < 0) { |
548 |
< |
fprintf(stderr, "%s: read error\n", progname); |
545 |
> |
dogamma = gamcor < .95 || gamcor > 1.05; |
546 |
> |
if ((putprim == ALL) & !interleave) { |
547 |
> |
startprim = RED; endprim = BLU; |
548 |
> |
startpos = ftell(fin); |
549 |
> |
} else { |
550 |
> |
startprim = putprim; endprim = putprim; |
551 |
> |
} |
552 |
> |
for (putprim = startprim; putprim <= endprim; putprim++) { |
553 |
> |
if (putprim != startprim && fseek(fin, startpos, SEEK_SET)) { |
554 |
> |
fprintf(stderr, "%s: seek error on input file\n", |
555 |
> |
progname); |
556 |
|
quit(1); |
557 |
|
} |
558 |
< |
for (x = 0; x < xres; x++) { |
559 |
< |
if (uniq) |
560 |
< |
if ( scanln[x][RED] == lastc[RED] && |
561 |
< |
scanln[x][GRN] == lastc[GRN] && |
562 |
< |
scanln[x][BLU] == lastc[BLU] ) |
563 |
< |
continue; |
190 |
< |
else |
191 |
< |
copycolor(lastc, scanln[x]); |
192 |
< |
if (original) |
193 |
< |
scalecolor(scanln[x], 1.0/exposure); |
194 |
< |
if (!dataonly) |
195 |
< |
printf("%7d %7d ", x, y); |
196 |
< |
if ((*putval)(scanln[x], stdout) < 0) { |
197 |
< |
fprintf(stderr, "%s: write error\n", progname); |
558 |
> |
set_io(); |
559 |
> |
setcolor(lastc, 0.0, 0.0, 0.0); |
560 |
> |
for (y = 0; y < numscans(&picres); y++) { |
561 |
> |
if (fread2scan(scanln, scanlen(&picres), fin, |
562 |
> |
NCSAMP, WLPART) < 0) { |
563 |
> |
fprintf(stderr, "%s: read error\n", progname); |
564 |
|
quit(1); |
565 |
|
} |
566 |
+ |
for (x = 0; x < scanlen(&picres); x++) { |
567 |
+ |
if (uniq) { |
568 |
+ |
if ( colval(scanln[x],RED) == |
569 |
+ |
colval(lastc,RED) && |
570 |
+ |
colval(scanln[x],GRN) == |
571 |
+ |
colval(lastc,GRN) && |
572 |
+ |
colval(scanln[x],BLU) == |
573 |
+ |
colval(lastc,BLU) ) |
574 |
+ |
continue; |
575 |
+ |
else |
576 |
+ |
copycolor(lastc, scanln[x]); |
577 |
+ |
} |
578 |
+ |
if (doexposure) |
579 |
+ |
multcolor(scanln[x], exposure); |
580 |
+ |
if (dogamma) |
581 |
+ |
setcolor(scanln[x], |
582 |
+ |
pow(colval(scanln[x],RED), 1.0/gamcor), |
583 |
+ |
pow(colval(scanln[x],GRN), 1.0/gamcor), |
584 |
+ |
pow(colval(scanln[x],BLU), 1.0/gamcor)); |
585 |
+ |
if (!dataonly) { |
586 |
+ |
pix2loc(hv, &picres, x, y); |
587 |
+ |
printf("%7d %7d ", |
588 |
+ |
(int)(hv[0]*picres.xr), |
589 |
+ |
(int)(hv[1]*picres.yr)); |
590 |
+ |
} |
591 |
+ |
if ((*putval)(scanln[x]) < 0) { |
592 |
+ |
fprintf(stderr, "%s: write error\n", |
593 |
+ |
progname); |
594 |
+ |
quit(1); |
595 |
+ |
} |
596 |
+ |
} |
597 |
|
} |
598 |
|
} |
599 |
< |
free((char *)scanln); |
599 |
> |
free((void *)scanln); |
600 |
|
} |
601 |
|
|
602 |
|
|
603 |
< |
valtopix() /* convert values to a pixel file */ |
603 |
> |
static void |
604 |
> |
valtopix(void) /* convert values to a pixel file */ |
605 |
|
{ |
606 |
< |
COLOR *scanln; |
607 |
< |
int y; |
608 |
< |
register int x; |
606 |
> |
int dogamma; |
607 |
> |
COLOR *scanln; |
608 |
> |
COLR rgbe; |
609 |
> |
int x, y; |
610 |
|
|
611 |
< |
scanln = (COLOR *)malloc(xres*sizeof(COLOR)); |
611 |
> |
scanln = (COLOR *)malloc(scanlen(&picres)*sizeof(COLOR)); |
612 |
|
if (scanln == NULL) { |
613 |
|
fprintf(stderr, "%s: out of memory\n", progname); |
614 |
|
quit(1); |
615 |
|
} |
616 |
< |
for (y = yres-1; y >= 0; y--) { |
617 |
< |
for (x = 0; x < xres; x++) { |
618 |
< |
if (!dataonly) |
616 |
> |
dogamma = gamcor < .95 || gamcor > 1.05; |
617 |
> |
set_io(); |
618 |
> |
for (y = 0; y < numscans(&picres); y++) { |
619 |
> |
for (x = 0; x < scanlen(&picres); x++) { |
620 |
> |
if (!dataonly) { |
621 |
|
fscanf(fin, "%*d %*d"); |
622 |
< |
if ((*getval)(scanln[x], fin) < 0) { |
622 |
> |
if (fin2 != NULL) { |
623 |
> |
fscanf(fin2, "%*d %*d"); |
624 |
> |
fscanf(fin3, "%*d %*d"); |
625 |
> |
} |
626 |
> |
} |
627 |
> |
if ((*getval)(scanln[x]) < 0) { |
628 |
|
fprintf(stderr, "%s: read error\n", progname); |
629 |
|
quit(1); |
630 |
|
} |
631 |
+ |
if (dogamma) |
632 |
+ |
setcolor(scanln[x], |
633 |
+ |
pow(colval(scanln[x],RED), gamcor), |
634 |
+ |
pow(colval(scanln[x],GRN), gamcor), |
635 |
+ |
pow(colval(scanln[x],BLU), gamcor)); |
636 |
+ |
if (doexposure) |
637 |
+ |
multcolor(scanln[x], exposure); |
638 |
+ |
if (uniq) { /* uncompressed? */ |
639 |
+ |
setcolr(rgbe, scanln[x][RED], |
640 |
+ |
scanln[x][GRN], |
641 |
+ |
scanln[x][BLU]); |
642 |
+ |
if (putbinary(rgbe, sizeof(COLR), 1, stdout) != 1) |
643 |
+ |
goto writerr; |
644 |
+ |
} |
645 |
|
} |
646 |
< |
if (fwritescan(scanln, xres, stdout) < 0 |
647 |
< |
|| fflush(stdout) < 0) { |
648 |
< |
fprintf(stderr, "%s: write error\n", progname); |
229 |
< |
quit(1); |
230 |
< |
} |
646 |
> |
/* write scan if compressed */ |
647 |
> |
if (!uniq && fwritescan(scanln, scanlen(&picres), stdout) < 0) |
648 |
> |
goto writerr; |
649 |
|
} |
650 |
< |
free((char *)scanln); |
650 |
> |
free((void *)scanln); |
651 |
> |
return; |
652 |
> |
writerr: |
653 |
> |
fprintf(stderr, "%s: write error\n", progname); |
654 |
> |
quit(1); |
655 |
|
} |
656 |
|
|
657 |
|
|
658 |
+ |
void |
659 |
|
quit(code) |
660 |
|
int code; |
661 |
|
{ |
663 |
|
} |
664 |
|
|
665 |
|
|
666 |
< |
getcascii(col, fp) /* get an ascii color value from fp */ |
667 |
< |
COLOR col; |
668 |
< |
FILE *fp; |
666 |
> |
static int |
667 |
> |
getcascii( /* get an ascii color value from stream(s) */ |
668 |
> |
COLOR col |
669 |
> |
) |
670 |
|
{ |
671 |
< |
double vd[3]; |
671 |
> |
double vd[3]; |
672 |
|
|
673 |
< |
if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3) |
674 |
< |
return(-1); |
675 |
< |
setcolor(col, vd[0], vd[1], vd[2]); |
673 |
> |
if (fin2 == NULL) { |
674 |
> |
if (fscanf(fin, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3) |
675 |
> |
return(-1); |
676 |
> |
} else { |
677 |
> |
if (fscanf(fin, "%lf", &vd[0]) != 1 || |
678 |
> |
fscanf(fin2, "%lf", &vd[1]) != 1 || |
679 |
> |
fscanf(fin3, "%lf", &vd[2]) != 1) |
680 |
> |
return(-1); |
681 |
> |
} |
682 |
> |
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
683 |
|
return(0); |
684 |
|
} |
685 |
|
|
686 |
|
|
687 |
< |
getcdouble(col, fp) /* get a double color value from fp */ |
688 |
< |
COLOR col; |
689 |
< |
FILE *fp; |
687 |
> |
static int |
688 |
> |
getcdouble( /* get a double color value from stream(s) */ |
689 |
> |
COLOR col |
690 |
> |
) |
691 |
|
{ |
692 |
< |
double vd[3]; |
692 |
> |
double vd[3]; |
693 |
|
|
694 |
< |
if (fread(vd, sizeof(double), 3, fp) != 3) |
695 |
< |
return(-1); |
696 |
< |
setcolor(col, vd[0], vd[1], vd[2]); |
694 |
> |
if (fin2 == NULL) { |
695 |
> |
if (getbinary(vd, sizeof(double), 3, fin) != 3) |
696 |
> |
return(-1); |
697 |
> |
} else { |
698 |
> |
if (getbinary(vd, sizeof(double), 1, fin) != 1 || |
699 |
> |
getbinary(vd+1, sizeof(double), 1, fin2) != 1 || |
700 |
> |
getbinary(vd+2, sizeof(double), 1, fin3) != 1) |
701 |
> |
return(-1); |
702 |
> |
} |
703 |
> |
if (swapbytes) |
704 |
> |
swap64((char *)vd, 3); |
705 |
> |
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
706 |
|
return(0); |
707 |
|
} |
708 |
|
|
709 |
|
|
710 |
< |
getcfloat(col, fp) /* get a float color value from fp */ |
711 |
< |
COLOR col; |
712 |
< |
FILE *fp; |
710 |
> |
static int |
711 |
> |
getcfloat( /* get a float color value from stream(s) */ |
712 |
> |
COLOR col |
713 |
> |
) |
714 |
|
{ |
715 |
|
float vf[3]; |
716 |
|
|
717 |
< |
if (fread(vf, sizeof(float), 3, fp) != 3) |
718 |
< |
return(-1); |
719 |
< |
setcolor(col, vf[0], vf[1], vf[2]); |
717 |
> |
if (fin2 == NULL) { |
718 |
> |
if (getbinary(vf, sizeof(float), 3, fin) != 3) |
719 |
> |
return(-1); |
720 |
> |
} else { |
721 |
> |
if (getbinary(vf, sizeof(float), 1, fin) != 1 || |
722 |
> |
getbinary(vf+1, sizeof(float), 1, fin2) != 1 || |
723 |
> |
getbinary(vf+2, sizeof(float), 1, fin3) != 1) |
724 |
> |
return(-1); |
725 |
> |
} |
726 |
> |
if (swapbytes) |
727 |
> |
swap32((char *)vf, 3); |
728 |
> |
setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); |
729 |
|
return(0); |
730 |
|
} |
731 |
|
|
732 |
|
|
733 |
< |
getcint(col, fp) /* get an int color value from fp */ |
734 |
< |
COLOR col; |
735 |
< |
FILE *fp; |
733 |
> |
static int |
734 |
> |
getcint( /* get an int color value from stream(s) */ |
735 |
> |
COLOR col |
736 |
> |
) |
737 |
|
{ |
738 |
|
int vi[3]; |
739 |
|
|
740 |
< |
if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3) |
741 |
< |
return(-1); |
742 |
< |
setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.); |
740 |
> |
if (fin2 == NULL) { |
741 |
> |
if (fscanf(fin, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3) |
742 |
> |
return(-1); |
743 |
> |
} else { |
744 |
> |
if (fscanf(fin, "%d", &vi[0]) != 1 || |
745 |
> |
fscanf(fin2, "%d", &vi[1]) != 1 || |
746 |
> |
fscanf(fin3, "%d", &vi[2]) != 1) |
747 |
> |
return(-1); |
748 |
> |
} |
749 |
> |
setcolor(col, (vi[rord[RED]]+.5)/256., |
750 |
> |
(vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.); |
751 |
|
return(0); |
752 |
|
} |
753 |
|
|
754 |
|
|
755 |
< |
getcbyte(col, fp) /* get a byte color value from fp */ |
756 |
< |
COLOR col; |
757 |
< |
FILE *fp; |
755 |
> |
static int |
756 |
> |
getcbyte( /* get a byte color value from stream(s) */ |
757 |
> |
COLOR col |
758 |
> |
) |
759 |
|
{ |
760 |
< |
BYTE vb[3]; |
760 |
> |
uby8 vb[3]; |
761 |
|
|
762 |
< |
if (fread(vb, sizeof(BYTE), 3, fp) != 3) |
763 |
< |
return(-1); |
764 |
< |
setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.); |
762 |
> |
if (fin2 == NULL) { |
763 |
> |
if (getbinary(vb, sizeof(uby8), 3, fin) != 3) |
764 |
> |
return(-1); |
765 |
> |
} else { |
766 |
> |
if (getbinary(vb, sizeof(uby8), 1, fin) != 1 || |
767 |
> |
getbinary(vb+1, sizeof(uby8), 1, fin2) != 1 || |
768 |
> |
getbinary(vb+2, sizeof(uby8), 1, fin3) != 1) |
769 |
> |
return(-1); |
770 |
> |
} |
771 |
> |
setcolor(col, (vb[rord[RED]]+.5)/256., |
772 |
> |
(vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.); |
773 |
|
return(0); |
774 |
|
} |
775 |
|
|
776 |
|
|
777 |
< |
getbascii(col, fp) /* get an ascii brightness value from fp */ |
778 |
< |
COLOR col; |
779 |
< |
FILE *fp; |
777 |
> |
static int |
778 |
> |
getcword( /* get a 16-bit color value from stream(s) */ |
779 |
> |
COLOR col |
780 |
> |
) |
781 |
|
{ |
782 |
< |
double vd; |
782 |
> |
uint16 vw[3]; |
783 |
|
|
784 |
< |
if (fscanf(fp, "%lf", &vd) != 1) |
784 |
> |
if (fin2 == NULL) { |
785 |
> |
if (getbinary(vw, sizeof(uint16), 3, fin) != 3) |
786 |
> |
return(-1); |
787 |
> |
} else { |
788 |
> |
if (getbinary(vw, sizeof(uint16), 1, fin) != 1 || |
789 |
> |
getbinary(vw+1, sizeof(uint16), 1, fin2) != 1 || |
790 |
> |
getbinary(vw+2, sizeof(uint16), 1, fin3) != 1) |
791 |
> |
return(-1); |
792 |
> |
} |
793 |
> |
if (swapbytes) |
794 |
> |
swap16((char *)vw, 3); |
795 |
> |
setcolor(col, (vw[rord[RED]]+.5)/65536., |
796 |
> |
(vw[rord[GRN]]+.5)/65536., (vw[rord[BLU]]+.5)/65536.); |
797 |
> |
return(0); |
798 |
> |
} |
799 |
> |
|
800 |
> |
|
801 |
> |
static int |
802 |
> |
getbascii( /* get an ascii brightness value from fin */ |
803 |
> |
COLOR col |
804 |
> |
) |
805 |
> |
{ |
806 |
> |
double vd; |
807 |
> |
|
808 |
> |
if (fscanf(fin, "%lf", &vd) != 1) |
809 |
|
return(-1); |
810 |
|
setcolor(col, vd, vd, vd); |
811 |
|
return(0); |
812 |
|
} |
813 |
|
|
814 |
|
|
815 |
< |
getbdouble(col, fp) /* get a double brightness value from fp */ |
816 |
< |
COLOR col; |
817 |
< |
FILE *fp; |
815 |
> |
static int |
816 |
> |
getbdouble( /* get a double brightness value from fin */ |
817 |
> |
COLOR col |
818 |
> |
) |
819 |
|
{ |
820 |
< |
double vd; |
820 |
> |
double vd; |
821 |
|
|
822 |
< |
if (fread(&vd, sizeof(double), 1, fp) != 1) |
822 |
> |
if (getbinary(&vd, sizeof(double), 1, fin) != 1) |
823 |
|
return(-1); |
824 |
+ |
if (swapbytes) |
825 |
+ |
swap64((char *)&vd, 1); |
826 |
|
setcolor(col, vd, vd, vd); |
827 |
|
return(0); |
828 |
|
} |
829 |
|
|
830 |
|
|
831 |
< |
getbfloat(col, fp) /* get a float brightness value from fp */ |
832 |
< |
COLOR col; |
833 |
< |
FILE *fp; |
831 |
> |
static int |
832 |
> |
getbfloat( /* get a float brightness value from fin */ |
833 |
> |
COLOR col |
834 |
> |
) |
835 |
|
{ |
836 |
|
float vf; |
837 |
|
|
838 |
< |
if (fread(&vf, sizeof(float), 1, fp) != 1) |
838 |
> |
if (getbinary(&vf, sizeof(float), 1, fin) != 1) |
839 |
|
return(-1); |
840 |
+ |
if (swapbytes) |
841 |
+ |
swap32((char *)&vf, 1); |
842 |
|
setcolor(col, vf, vf, vf); |
843 |
|
return(0); |
844 |
|
} |
845 |
|
|
846 |
|
|
847 |
< |
getbint(col, fp) /* get an int brightness value from fp */ |
848 |
< |
COLOR col; |
849 |
< |
FILE *fp; |
847 |
> |
static int |
848 |
> |
getbint( /* get an int brightness value from fin */ |
849 |
> |
COLOR col |
850 |
> |
) |
851 |
|
{ |
852 |
|
int vi; |
853 |
< |
double d; |
853 |
> |
double d; |
854 |
|
|
855 |
< |
if (fscanf(fp, "%d", &vi) != 1) |
855 |
> |
if (fscanf(fin, "%d", &vi) != 1) |
856 |
|
return(-1); |
857 |
|
d = (vi+.5)/256.; |
858 |
|
setcolor(col, d, d, d); |
860 |
|
} |
861 |
|
|
862 |
|
|
863 |
< |
getbbyte(col, fp) /* get a byte brightness value from fp */ |
864 |
< |
COLOR col; |
865 |
< |
FILE *fp; |
863 |
> |
static int |
864 |
> |
getbbyte( /* get a byte brightness value from fin */ |
865 |
> |
COLOR col |
866 |
> |
) |
867 |
|
{ |
868 |
< |
BYTE vb; |
869 |
< |
double d; |
868 |
> |
uby8 vb; |
869 |
> |
double d; |
870 |
|
|
871 |
< |
if (fread(&vb, sizeof(BYTE), 1, fp) != 1) |
871 |
> |
if (getbinary(&vb, sizeof(uby8), 1, fin) != 1) |
872 |
|
return(-1); |
873 |
|
d = (vb+.5)/256.; |
874 |
|
setcolor(col, d, d, d); |
876 |
|
} |
877 |
|
|
878 |
|
|
879 |
< |
putcascii(col, fp) /* put an ascii color to fp */ |
880 |
< |
COLOR col; |
881 |
< |
FILE *fp; |
879 |
> |
static int |
880 |
> |
getbword( /* get a 16-bit brightness value from fin */ |
881 |
> |
COLOR col |
882 |
> |
) |
883 |
|
{ |
884 |
< |
fprintf(fp, "%15.3e %15.3e %15.3e\n", |
885 |
< |
colval(col,RED), |
383 |
< |
colval(col,GRN), |
384 |
< |
colval(col,BLU)); |
884 |
> |
uint16 vw; |
885 |
> |
double d; |
886 |
|
|
887 |
< |
return(ferror(fp) ? -1 : 0); |
887 |
> |
if (getbinary(&vw, sizeof(uint16), 1, fin) != 1) |
888 |
> |
return(-1); |
889 |
> |
if (swapbytes) |
890 |
> |
swap16((char *)&vw, 1); |
891 |
> |
d = (vw+.5)/65536.; |
892 |
> |
setcolor(col, d, d, d); |
893 |
> |
return(0); |
894 |
|
} |
895 |
|
|
896 |
|
|
897 |
< |
putcfloat(col, fp) /* put a float color to fp */ |
898 |
< |
COLOR col; |
899 |
< |
FILE *fp; |
897 |
> |
static int |
898 |
> |
putcascii( /* put an ascii color to stdout */ |
899 |
> |
COLOR col |
900 |
> |
) |
901 |
|
{ |
902 |
+ |
fprintf(stdout, "%15.3e %15.3e %15.3e\n", |
903 |
+ |
colval(col,ord[0]), |
904 |
+ |
colval(col,ord[1]), |
905 |
+ |
colval(col,ord[2])); |
906 |
+ |
|
907 |
+ |
return(ferror(stdout) ? -1 : 0); |
908 |
+ |
} |
909 |
+ |
|
910 |
+ |
|
911 |
+ |
static int |
912 |
+ |
putcfloat( /* put a float color to stdout */ |
913 |
+ |
COLOR col |
914 |
+ |
) |
915 |
+ |
{ |
916 |
|
float vf[3]; |
917 |
|
|
918 |
< |
vf[0] = colval(col,RED); |
919 |
< |
vf[1] = colval(col,GRN); |
920 |
< |
vf[2] = colval(col,BLU); |
921 |
< |
fwrite(vf, sizeof(float), 3, fp); |
918 |
> |
vf[0] = colval(col,ord[0]); |
919 |
> |
vf[1] = colval(col,ord[1]); |
920 |
> |
vf[2] = colval(col,ord[2]); |
921 |
> |
if (swapbytes) |
922 |
> |
swap32((char *)vf, 3); |
923 |
> |
putbinary(vf, sizeof(float), 3, stdout); |
924 |
|
|
925 |
< |
return(ferror(fp) ? -1 : 0); |
925 |
> |
return(ferror(stdout) ? -1 : 0); |
926 |
|
} |
927 |
|
|
928 |
|
|
929 |
< |
putcdouble(col, fp) /* put a double color to fp */ |
930 |
< |
COLOR col; |
931 |
< |
FILE *fp; |
929 |
> |
static int |
930 |
> |
putcdouble( /* put a double color to stdout */ |
931 |
> |
COLOR col |
932 |
> |
) |
933 |
|
{ |
934 |
< |
double vd[3]; |
934 |
> |
double vd[3]; |
935 |
|
|
936 |
< |
vd[0] = colval(col,RED); |
937 |
< |
vd[1] = colval(col,GRN); |
938 |
< |
vd[2] = colval(col,BLU); |
939 |
< |
fwrite(vd, sizeof(double), 3, fp); |
936 |
> |
vd[0] = colval(col,ord[0]); |
937 |
> |
vd[1] = colval(col,ord[1]); |
938 |
> |
vd[2] = colval(col,ord[2]); |
939 |
> |
if (swapbytes) |
940 |
> |
swap64((char *)vd, 3); |
941 |
> |
putbinary(vd, sizeof(double), 3, stdout); |
942 |
|
|
943 |
< |
return(ferror(fp) ? -1 : 0); |
943 |
> |
return(ferror(stdout) ? -1 : 0); |
944 |
|
} |
945 |
|
|
946 |
|
|
947 |
< |
putcint(col, fp) /* put an int color to fp */ |
948 |
< |
COLOR col; |
949 |
< |
FILE *fp; |
947 |
> |
static int |
948 |
> |
putcint( /* put an int color to stdout */ |
949 |
> |
COLOR col |
950 |
> |
) |
951 |
|
{ |
952 |
< |
fprintf(fp, "%d %d %d\n", |
953 |
< |
(int)(colval(col,RED)*256.), |
954 |
< |
(int)(colval(col,GRN)*256.), |
955 |
< |
(int)(colval(col,BLU)*256.)); |
952 |
> |
fprintf(stdout, "%d %d %d\n", |
953 |
> |
(int)(colval(col,ord[0])*256.), |
954 |
> |
(int)(colval(col,ord[1])*256.), |
955 |
> |
(int)(colval(col,ord[2])*256.)); |
956 |
|
|
957 |
< |
return(ferror(fp) ? -1 : 0); |
957 |
> |
return(ferror(stdout) ? -1 : 0); |
958 |
|
} |
959 |
|
|
960 |
|
|
961 |
< |
putcbyte(col, fp) /* put a byte color to fp */ |
962 |
< |
COLOR col; |
963 |
< |
FILE *fp; |
961 |
> |
static int |
962 |
> |
putcbyte( /* put a byte color to stdout */ |
963 |
> |
COLOR col |
964 |
> |
) |
965 |
|
{ |
966 |
< |
register int i; |
967 |
< |
BYTE vb[3]; |
966 |
> |
long i; |
967 |
> |
uby8 vb[3]; |
968 |
|
|
969 |
< |
i = colval(col,RED)*256.; |
969 |
> |
i = colval(col,ord[0])*256.; |
970 |
|
vb[0] = min(i,255); |
971 |
< |
i = colval(col,GRN)*256.; |
971 |
> |
i = colval(col,ord[1])*256.; |
972 |
|
vb[1] = min(i,255); |
973 |
< |
i = colval(col,BLU)*256.; |
973 |
> |
i = colval(col,ord[2])*256.; |
974 |
|
vb[2] = min(i,255); |
975 |
< |
fwrite(vb, sizeof(BYTE), 3, fp); |
975 |
> |
putbinary(vb, sizeof(uby8), 3, stdout); |
976 |
|
|
977 |
< |
return(ferror(fp) ? -1 : 0); |
977 |
> |
return(ferror(stdout) ? -1 : 0); |
978 |
|
} |
979 |
|
|
980 |
|
|
981 |
< |
putbascii(col, fp) /* put an ascii brightness to fp */ |
982 |
< |
COLOR col; |
983 |
< |
FILE *fp; |
981 |
> |
static int |
982 |
> |
putcword( /* put a 16-bit color to stdout */ |
983 |
> |
COLOR col |
984 |
> |
) |
985 |
|
{ |
986 |
< |
fprintf(fp, "%15.3e\n", bright(col)); |
986 |
> |
long i; |
987 |
> |
uint16 vw[3]; |
988 |
|
|
989 |
< |
return(ferror(fp) ? -1 : 0); |
989 |
> |
i = colval(col,ord[0])*65536.; |
990 |
> |
vw[0] = min(i,65535); |
991 |
> |
i = colval(col,ord[1])*65536.; |
992 |
> |
vw[1] = min(i,65535); |
993 |
> |
i = colval(col,ord[2])*65536.; |
994 |
> |
vw[2] = min(i,65535); |
995 |
> |
if (swapbytes) |
996 |
> |
swap16((char *)vw, 3); |
997 |
> |
putbinary(vw, sizeof(uint16), 3, stdout); |
998 |
> |
|
999 |
> |
return(ferror(stdout) ? -1 : 0); |
1000 |
|
} |
1001 |
|
|
1002 |
|
|
1003 |
< |
putbfloat(col, fp) /* put a float brightness to fp */ |
1004 |
< |
COLOR col; |
1005 |
< |
FILE *fp; |
1003 |
> |
static int |
1004 |
> |
putbascii( /* put an ascii brightness to stdout */ |
1005 |
> |
COLOR col |
1006 |
> |
) |
1007 |
|
{ |
1008 |
+ |
fprintf(stdout, "%15.3e\n", (*mybright)(col)); |
1009 |
+ |
|
1010 |
+ |
return(ferror(stdout) ? -1 : 0); |
1011 |
+ |
} |
1012 |
+ |
|
1013 |
+ |
|
1014 |
+ |
static int |
1015 |
+ |
putbfloat( /* put a float brightness to stdout */ |
1016 |
+ |
COLOR col |
1017 |
+ |
) |
1018 |
+ |
{ |
1019 |
|
float vf; |
1020 |
|
|
1021 |
< |
vf = bright(col); |
1022 |
< |
fwrite(&vf, sizeof(float), 1, fp); |
1021 |
> |
vf = (*mybright)(col); |
1022 |
> |
if (swapbytes) |
1023 |
> |
swap32((char *)&vf, 1); |
1024 |
> |
putbinary(&vf, sizeof(float), 1, stdout); |
1025 |
|
|
1026 |
< |
return(ferror(fp) ? -1 : 0); |
1026 |
> |
return(ferror(stdout) ? -1 : 0); |
1027 |
|
} |
1028 |
|
|
1029 |
|
|
1030 |
< |
putbdouble(col, fp) /* put a double brightness to fp */ |
1031 |
< |
COLOR col; |
1032 |
< |
FILE *fp; |
1030 |
> |
static int |
1031 |
> |
putbdouble( /* put a double brightness to stdout */ |
1032 |
> |
COLOR col |
1033 |
> |
) |
1034 |
|
{ |
1035 |
< |
double vd; |
1035 |
> |
double vd; |
1036 |
|
|
1037 |
< |
vd = bright(col); |
1038 |
< |
fwrite(&vd, sizeof(double), 1, fp); |
1037 |
> |
vd = (*mybright)(col); |
1038 |
> |
if (swapbytes) |
1039 |
> |
swap64((char *)&vd, 1); |
1040 |
> |
putbinary(&vd, sizeof(double), 1, stdout); |
1041 |
|
|
1042 |
< |
return(ferror(fp) ? -1 : 0); |
1042 |
> |
return(ferror(stdout) ? -1 : 0); |
1043 |
|
} |
1044 |
|
|
1045 |
|
|
1046 |
< |
putbint(col, fp) /* put an int brightness to fp */ |
1047 |
< |
COLOR col; |
1048 |
< |
FILE *fp; |
1046 |
> |
static int |
1047 |
> |
putbint( /* put an int brightness to stdout */ |
1048 |
> |
COLOR col |
1049 |
> |
) |
1050 |
|
{ |
1051 |
< |
fprintf(fp, "%d\n", (int)(bright(col)*256.)); |
1051 |
> |
fprintf(stdout, "%d\n", (int)((*mybright)(col)*256.)); |
1052 |
|
|
1053 |
< |
return(ferror(fp) ? -1 : 0); |
1053 |
> |
return(ferror(stdout) ? -1 : 0); |
1054 |
|
} |
1055 |
|
|
1056 |
|
|
1057 |
< |
putbbyte(col, fp) /* put a byte brightness to fp */ |
1058 |
< |
COLOR col; |
1059 |
< |
FILE *fp; |
1057 |
> |
static int |
1058 |
> |
putbbyte( /* put a byte brightness to stdout */ |
1059 |
> |
COLOR col |
1060 |
> |
) |
1061 |
|
{ |
1062 |
< |
register int i; |
1063 |
< |
BYTE vb; |
1062 |
> |
int i; |
1063 |
> |
uby8 vb; |
1064 |
|
|
1065 |
< |
i = bright(col)*256.; |
1065 |
> |
i = (*mybright)(col)*256.; |
1066 |
|
vb = min(i,255); |
1067 |
< |
fwrite(&vb, sizeof(BYTE), 1, fp); |
1067 |
> |
putbinary(&vb, sizeof(uby8), 1, stdout); |
1068 |
|
|
1069 |
< |
return(ferror(fp) ? -1 : 0); |
1069 |
> |
return(ferror(stdout) ? -1 : 0); |
1070 |
|
} |
1071 |
|
|
1072 |
|
|
1073 |
< |
set_io() /* set put and get functions */ |
1073 |
> |
static int |
1074 |
> |
putbword( /* put a 16-bit brightness to stdout */ |
1075 |
> |
COLOR col |
1076 |
> |
) |
1077 |
|
{ |
1078 |
+ |
long i; |
1079 |
+ |
uint16 vw; |
1080 |
+ |
|
1081 |
+ |
i = (*mybright)(col)*65536.; |
1082 |
+ |
vw = min(i,65535); |
1083 |
+ |
if (swapbytes) |
1084 |
+ |
swap16((char *)&vw, 1); |
1085 |
+ |
putbinary(&vw, sizeof(uint16), 1, stdout); |
1086 |
+ |
|
1087 |
+ |
return(ferror(stdout) ? -1 : 0); |
1088 |
+ |
} |
1089 |
+ |
|
1090 |
+ |
|
1091 |
+ |
static int |
1092 |
+ |
putpascii( /* put an ascii primary to stdout */ |
1093 |
+ |
COLOR col |
1094 |
+ |
) |
1095 |
+ |
{ |
1096 |
+ |
fprintf(stdout, "%15.3e\n", colval(col,putprim)); |
1097 |
+ |
|
1098 |
+ |
return(ferror(stdout) ? -1 : 0); |
1099 |
+ |
} |
1100 |
+ |
|
1101 |
+ |
|
1102 |
+ |
static int |
1103 |
+ |
putpfloat( /* put a float primary to stdout */ |
1104 |
+ |
COLOR col |
1105 |
+ |
) |
1106 |
+ |
{ |
1107 |
+ |
float vf; |
1108 |
+ |
|
1109 |
+ |
vf = colval(col,putprim); |
1110 |
+ |
if (swapbytes) |
1111 |
+ |
swap32((char *)&vf, 1); |
1112 |
+ |
putbinary(&vf, sizeof(float), 1, stdout); |
1113 |
+ |
|
1114 |
+ |
return(ferror(stdout) ? -1 : 0); |
1115 |
+ |
} |
1116 |
+ |
|
1117 |
+ |
|
1118 |
+ |
static int |
1119 |
+ |
putpdouble( /* put a double primary to stdout */ |
1120 |
+ |
COLOR col |
1121 |
+ |
) |
1122 |
+ |
{ |
1123 |
+ |
double vd; |
1124 |
+ |
|
1125 |
+ |
vd = colval(col,putprim); |
1126 |
+ |
if (swapbytes) |
1127 |
+ |
swap64((char *)&vd, 1); |
1128 |
+ |
putbinary(&vd, sizeof(double), 1, stdout); |
1129 |
+ |
|
1130 |
+ |
return(ferror(stdout) ? -1 : 0); |
1131 |
+ |
} |
1132 |
+ |
|
1133 |
+ |
|
1134 |
+ |
static int |
1135 |
+ |
putpint( /* put an int primary to stdout */ |
1136 |
+ |
COLOR col |
1137 |
+ |
) |
1138 |
+ |
{ |
1139 |
+ |
fprintf(stdout, "%d\n", (int)(colval(col,putprim)*256.)); |
1140 |
+ |
|
1141 |
+ |
return(ferror(stdout) ? -1 : 0); |
1142 |
+ |
} |
1143 |
+ |
|
1144 |
+ |
|
1145 |
+ |
static int |
1146 |
+ |
putpbyte( /* put a byte primary to stdout */ |
1147 |
+ |
COLOR col |
1148 |
+ |
) |
1149 |
+ |
{ |
1150 |
+ |
long i; |
1151 |
+ |
uby8 vb; |
1152 |
+ |
|
1153 |
+ |
i = colval(col,putprim)*256.; |
1154 |
+ |
vb = min(i,255); |
1155 |
+ |
putbinary(&vb, sizeof(uby8), 1, stdout); |
1156 |
+ |
|
1157 |
+ |
return(ferror(stdout) ? -1 : 0); |
1158 |
+ |
} |
1159 |
+ |
|
1160 |
+ |
|
1161 |
+ |
static int |
1162 |
+ |
putpword( /* put a 16-bit primary to stdout */ |
1163 |
+ |
COLOR col |
1164 |
+ |
) |
1165 |
+ |
{ |
1166 |
+ |
long i; |
1167 |
+ |
uint16 vw; |
1168 |
+ |
|
1169 |
+ |
i = colval(col,putprim)*65536.; |
1170 |
+ |
vw = min(i,65535); |
1171 |
+ |
if (swapbytes) |
1172 |
+ |
swap16((char *)&vw, 1); |
1173 |
+ |
putbinary(&vw, sizeof(uint16), 1, stdout); |
1174 |
+ |
|
1175 |
+ |
return(ferror(stdout) ? -1 : 0); |
1176 |
+ |
} |
1177 |
+ |
|
1178 |
+ |
|
1179 |
+ |
static void |
1180 |
+ |
set_io(void) /* set put and get functions */ |
1181 |
+ |
{ |
1182 |
|
switch (format) { |
1183 |
|
case 'a': /* ascii */ |
1184 |
< |
if (brightonly) { |
1184 |
> |
if (putprim == BRIGHT) { |
1185 |
|
getval = getbascii; |
1186 |
|
putval = putbascii; |
1187 |
+ |
} else if (putprim != ALL) { |
1188 |
+ |
getval = getbascii; |
1189 |
+ |
putval = putpascii; |
1190 |
|
} else { |
1191 |
|
getval = getcascii; |
1192 |
|
putval = putcascii; |
1193 |
+ |
if (reverse && !interleave) { |
1194 |
+ |
fprintf(stderr, |
1195 |
+ |
"%s: ASCII input files must be interleaved\n", |
1196 |
+ |
progname); |
1197 |
+ |
quit(1); |
1198 |
+ |
} |
1199 |
|
} |
1200 |
|
return; |
1201 |
|
case 'f': /* binary float */ |
1202 |
< |
if (brightonly) { |
1202 |
> |
if (putprim == BRIGHT) { |
1203 |
|
getval = getbfloat; |
1204 |
|
putval = putbfloat; |
1205 |
+ |
} else if (putprim != ALL) { |
1206 |
+ |
getval = getbfloat; |
1207 |
+ |
putval = putpfloat; |
1208 |
|
} else { |
1209 |
|
getval = getcfloat; |
1210 |
|
putval = putcfloat; |
1211 |
+ |
if (reverse && !interleave) { |
1212 |
+ |
if (fin2 == NULL) |
1213 |
+ |
goto namerr; |
1214 |
+ |
if (fseek(fin2, |
1215 |
+ |
(long)sizeof(float)*picres.xr*picres.yr, SEEK_CUR)) |
1216 |
+ |
goto seekerr; |
1217 |
+ |
if (fseek(fin3, |
1218 |
+ |
(long)sizeof(float)*2*picres.xr*picres.yr, SEEK_CUR)) |
1219 |
+ |
goto seekerr; |
1220 |
+ |
} |
1221 |
|
} |
1222 |
|
return; |
1223 |
|
case 'd': /* binary double */ |
1224 |
< |
if (brightonly) { |
1224 |
> |
if (putprim == BRIGHT) { |
1225 |
|
getval = getbdouble; |
1226 |
|
putval = putbdouble; |
1227 |
+ |
} else if (putprim != ALL) { |
1228 |
+ |
getval = getbdouble; |
1229 |
+ |
putval = putpdouble; |
1230 |
|
} else { |
1231 |
|
getval = getcdouble; |
1232 |
|
putval = putcdouble; |
1233 |
+ |
if (reverse && !interleave) { |
1234 |
+ |
if (fin2 == NULL) |
1235 |
+ |
goto namerr; |
1236 |
+ |
if (fseek(fin2, |
1237 |
+ |
(long)sizeof(double)*picres.xr*picres.yr, SEEK_CUR)) |
1238 |
+ |
goto seekerr; |
1239 |
+ |
if (fseek(fin3, |
1240 |
+ |
(long)sizeof(double)*2*picres.xr*picres.yr, SEEK_CUR)) |
1241 |
+ |
goto seekerr; |
1242 |
+ |
} |
1243 |
|
} |
1244 |
|
return; |
1245 |
|
case 'i': /* integer */ |
1246 |
< |
if (brightonly) { |
1246 |
> |
if (putprim == BRIGHT) { |
1247 |
|
getval = getbint; |
1248 |
|
putval = putbint; |
1249 |
+ |
} else if (putprim != ALL) { |
1250 |
+ |
getval = getbint; |
1251 |
+ |
putval = putpint; |
1252 |
|
} else { |
1253 |
|
getval = getcint; |
1254 |
|
putval = putcint; |
1255 |
+ |
if (reverse && !interleave) { |
1256 |
+ |
fprintf(stderr, |
1257 |
+ |
"%s: integer input files must be interleaved\n", |
1258 |
+ |
progname); |
1259 |
+ |
quit(1); |
1260 |
+ |
} |
1261 |
|
} |
1262 |
|
return; |
1263 |
|
case 'b': /* byte */ |
1264 |
< |
if (brightonly) { |
1264 |
> |
if (putprim == BRIGHT) { |
1265 |
|
getval = getbbyte; |
1266 |
|
putval = putbbyte; |
1267 |
+ |
} else if (putprim != ALL) { |
1268 |
+ |
getval = getbbyte; |
1269 |
+ |
putval = putpbyte; |
1270 |
|
} else { |
1271 |
|
getval = getcbyte; |
1272 |
|
putval = putcbyte; |
1273 |
+ |
if (reverse && !interleave) { |
1274 |
+ |
if (fin2 == NULL) |
1275 |
+ |
goto namerr; |
1276 |
+ |
if (fseek(fin2, |
1277 |
+ |
(long)sizeof(uby8)*picres.xr*picres.yr, SEEK_CUR)) |
1278 |
+ |
goto seekerr; |
1279 |
+ |
if (fseek(fin3, |
1280 |
+ |
(long)sizeof(uby8)*2*picres.xr*picres.yr, SEEK_CUR)) |
1281 |
+ |
goto seekerr; |
1282 |
+ |
} |
1283 |
|
} |
1284 |
|
return; |
1285 |
+ |
case 'w': /* 16-bit */ |
1286 |
+ |
if (putprim == BRIGHT) { |
1287 |
+ |
getval = getbword; |
1288 |
+ |
putval = putbword; |
1289 |
+ |
} else if (putprim != ALL) { |
1290 |
+ |
getval = getbword; |
1291 |
+ |
putval = putpword; |
1292 |
+ |
} else { |
1293 |
+ |
getval = getcword; |
1294 |
+ |
putval = putcword; |
1295 |
+ |
if (reverse && !interleave) { |
1296 |
+ |
if (fin2 == NULL) |
1297 |
+ |
goto namerr; |
1298 |
+ |
if (fseek(fin2, |
1299 |
+ |
(long)sizeof(uint16)*picres.xr*picres.yr, SEEK_CUR)) |
1300 |
+ |
goto seekerr; |
1301 |
+ |
if (fseek(fin3, |
1302 |
+ |
(long)sizeof(uint16)*2*picres.xr*picres.yr, SEEK_CUR)) |
1303 |
+ |
goto seekerr; |
1304 |
+ |
} |
1305 |
+ |
} |
1306 |
+ |
return; |
1307 |
|
} |
1308 |
+ |
/* badopt: */ /* label not used */ |
1309 |
+ |
fprintf(stderr, "%s: botched file type\n", progname); |
1310 |
+ |
quit(1); |
1311 |
+ |
namerr: |
1312 |
+ |
fprintf(stderr, "%s: non-interleaved file(s) must be named\n", |
1313 |
+ |
progname); |
1314 |
+ |
quit(1); |
1315 |
+ |
seekerr: |
1316 |
+ |
fprintf(stderr, "%s: cannot seek on interleaved input file\n", |
1317 |
+ |
progname); |
1318 |
+ |
quit(1); |
1319 |
|
} |