1 |
< |
/* Copyright (c) 1991 Regents of the University of California */ |
1 |
> |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
|
3 |
|
#ifndef lint |
4 |
|
static char SCCSid[] = "$SunId$ LBL"; |
16 |
|
|
17 |
|
#include "resolu.h" |
18 |
|
|
19 |
< |
#define min(a,b) ((a)<(b)?(a):(b)) |
19 |
> |
#define min(a,b) ((a)<(b)?(a):(b)) |
20 |
|
|
21 |
< |
RESOLU picres; /* resolution of picture */ |
21 |
> |
RESOLU picres; /* resolution of picture */ |
22 |
|
|
23 |
|
int uniq = 0; /* print only unique values? */ |
24 |
|
|
33 |
|
int format = 'a'; /* input/output format */ |
34 |
|
char *fmtid = "ascii"; /* format identifier for header */ |
35 |
|
|
36 |
< |
int header = 1; /* do header */ |
36 |
> |
int header = 1; /* do header? */ |
37 |
|
|
38 |
+ |
int resolution = 1; /* put/get resolution string? */ |
39 |
+ |
|
40 |
|
int wrongformat = 0; /* wrong input format? */ |
41 |
|
|
42 |
< |
double gamcor = 1.0; /* gamma correction */ |
42 |
> |
double gamcor = 1.0; /* gamma correction */ |
43 |
|
|
44 |
+ |
int ord[3] = {RED, GRN, BLU}; /* RGB ordering */ |
45 |
+ |
int rord[4]; /* reverse ordering */ |
46 |
+ |
|
47 |
|
COLOR exposure = WHTCOLOR; |
48 |
|
|
49 |
|
char *progname; |
50 |
|
|
51 |
|
FILE *fin; |
52 |
|
|
48 |
– |
extern double atof(), pow(); |
49 |
– |
|
53 |
|
int (*getval)(), (*putval)(); |
54 |
|
|
55 |
|
|
68 |
|
case 'h': /* header */ |
69 |
|
header = argv[i][0] == '+'; |
70 |
|
break; |
71 |
+ |
case 'H': /* resolution string */ |
72 |
+ |
resolution = argv[i][0] == '+'; |
73 |
+ |
break; |
74 |
|
case 'u': /* unique values */ |
75 |
|
uniq = argv[i][0] == '-'; |
76 |
|
break; |
82 |
|
if (argv[i][0] == '+') |
83 |
|
gamcor = 1.0/gamcor; |
84 |
|
break; |
85 |
+ |
case 'R': /* reverse byte sequence */ |
86 |
+ |
if (argv[i][0] == '-') { |
87 |
+ |
ord[0]=BLU; ord[1]=GRN; ord[2]=RED; |
88 |
+ |
} else { |
89 |
+ |
ord[0]=RED; ord[1]=GRN; ord[2]=BLU; |
90 |
+ |
} |
91 |
+ |
break; |
92 |
|
case 'r': /* reverse conversion */ |
93 |
|
reverse = argv[i][0] == '-'; |
94 |
|
break; |
127 |
|
} |
128 |
|
break; |
129 |
|
case 'x': /* x resolution */ |
130 |
+ |
case 'X': /* x resolution */ |
131 |
+ |
resolution = 0; |
132 |
|
if (argv[i][0] == '-') |
133 |
|
picres.or |= XDECR; |
134 |
|
picres.xr = atoi(argv[++i]); |
135 |
|
break; |
136 |
|
case 'y': /* y resolution */ |
137 |
+ |
case 'Y': /* y resolution */ |
138 |
+ |
resolution = 0; |
139 |
|
if (argv[i][0] == '-') |
140 |
|
picres.or |= YDECR; |
141 |
|
if (picres.xr == 0) |
157 |
|
fmtid = "8-bit_grey"; |
158 |
|
else |
159 |
|
fmtid = "24-bit_rgb"; |
160 |
< |
|
160 |
> |
/* assign reverse ordering */ |
161 |
> |
rord[ord[0]] = 0; |
162 |
> |
rord[ord[1]] = 1; |
163 |
> |
rord[ord[2]] = 2; |
164 |
> |
/* get input */ |
165 |
|
if (i == argc) { |
166 |
|
fin = stdin; |
167 |
|
} else if (i == argc-1) { |
178 |
|
set_io(); |
179 |
|
|
180 |
|
if (reverse) { |
181 |
+ |
#ifdef MSDOS |
182 |
+ |
setmode(fileno(stdout), O_BINARY); |
183 |
+ |
if (format != 'a' && format != 'i') |
184 |
+ |
setmode(fileno(fin), O_BINARY); |
185 |
+ |
#endif |
186 |
|
/* get header */ |
187 |
|
if (header && checkheader(fin, fmtid, stdout) < 0) { |
188 |
|
fprintf(stderr, "%s: wrong input format\n", progname); |
189 |
|
quit(1); |
190 |
|
} |
191 |
< |
if (picres.xr <= 0 || picres.yr <= 0) /* get resolution */ |
192 |
< |
if (!fgetsresolu(&picres, fin)) { |
193 |
< |
fprintf(stderr, "%s: missing resolution\n", |
194 |
< |
progname); |
195 |
< |
quit(1); |
196 |
< |
} |
191 |
> |
/* get resolution */ |
192 |
> |
if ((resolution && !fgetsresolu(&picres, fin)) || |
193 |
> |
picres.xr <= 0 || picres.yr <= 0) { |
194 |
> |
fprintf(stderr, "%s: missing resolution\n", progname); |
195 |
> |
quit(1); |
196 |
> |
} |
197 |
|
/* add to header */ |
198 |
|
printargs(i, argv, stdout); |
199 |
|
fputformat(COLRFMT, stdout); |
200 |
|
putchar('\n'); |
201 |
< |
fputsresolu(&picres, stdout); |
201 |
> |
fputsresolu(&picres, stdout); /* always put resolution */ |
202 |
|
valtopix(); |
203 |
|
} else { |
204 |
+ |
#ifdef MSDOS |
205 |
+ |
setmode(fileno(fin), O_BINARY); |
206 |
+ |
if (format != 'a' && format != 'i') |
207 |
+ |
setmode(fileno(stdout), O_BINARY); |
208 |
+ |
#endif |
209 |
|
/* get header */ |
210 |
|
getheader(fin, checkhead, NULL); |
211 |
|
if (wrongformat) { |
213 |
|
progname); |
214 |
|
quit(1); |
215 |
|
} |
216 |
< |
|
217 |
< |
if (picres.xr <= 0 || picres.yr <= 0) /* get picture size */ |
218 |
< |
if (!fgetsresolu(&picres, fin)) { |
219 |
< |
fprintf(stderr, "%s: missing resolution\n", |
189 |
< |
progname); |
190 |
< |
quit(1); |
191 |
< |
} |
216 |
> |
if (!fgetsresolu(&picres, fin)) { |
217 |
> |
fprintf(stderr, "%s: missing resolution\n", progname); |
218 |
> |
quit(1); |
219 |
> |
} |
220 |
|
if (header) { |
221 |
|
printargs(i, argv, stdout); |
222 |
|
fputformat(fmtid, stdout); |
223 |
|
putchar('\n'); |
224 |
|
} |
225 |
+ |
if (resolution) /* put resolution */ |
226 |
+ |
fputsresolu(&picres, stdout); |
227 |
|
pixtoval(); |
228 |
|
} |
229 |
|
|
256 |
|
|
257 |
|
pixtoval() /* convert picture to values */ |
258 |
|
{ |
259 |
< |
register COLOR *scanln; |
259 |
> |
register COLOR *scanln; |
260 |
|
int dogamma; |
261 |
|
COLOR lastc; |
262 |
|
FLOAT hv[2]; |
311 |
|
valtopix() /* convert values to a pixel file */ |
312 |
|
{ |
313 |
|
int dogamma; |
314 |
< |
register COLOR *scanln; |
314 |
> |
register COLOR *scanln; |
315 |
|
int y; |
316 |
|
register int x; |
317 |
|
|
355 |
|
COLOR col; |
356 |
|
FILE *fp; |
357 |
|
{ |
358 |
< |
double vd[3]; |
358 |
> |
double vd[3]; |
359 |
|
|
360 |
|
if (fscanf(fp, "%lf %lf %lf", &vd[0], &vd[1], &vd[2]) != 3) |
361 |
|
return(-1); |
362 |
< |
setcolor(col, vd[0], vd[1], vd[2]); |
362 |
> |
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
363 |
|
return(0); |
364 |
|
} |
365 |
|
|
368 |
|
COLOR col; |
369 |
|
FILE *fp; |
370 |
|
{ |
371 |
< |
double vd[3]; |
371 |
> |
double vd[3]; |
372 |
|
|
373 |
|
if (fread((char *)vd, sizeof(double), 3, fp) != 3) |
374 |
|
return(-1); |
375 |
< |
setcolor(col, vd[0], vd[1], vd[2]); |
375 |
> |
setcolor(col, vd[rord[RED]], vd[rord[GRN]], vd[rord[BLU]]); |
376 |
|
return(0); |
377 |
|
} |
378 |
|
|
385 |
|
|
386 |
|
if (fread((char *)vf, sizeof(float), 3, fp) != 3) |
387 |
|
return(-1); |
388 |
< |
setcolor(col, vf[0], vf[1], vf[2]); |
388 |
> |
setcolor(col, vf[rord[RED]], vf[rord[GRN]], vf[rord[BLU]]); |
389 |
|
return(0); |
390 |
|
} |
391 |
|
|
398 |
|
|
399 |
|
if (fscanf(fp, "%d %d %d", &vi[0], &vi[1], &vi[2]) != 3) |
400 |
|
return(-1); |
401 |
< |
setcolor(col,(vi[0]+.5)/256.,(vi[1]+.5)/256.,(vi[2]+.5)/256.); |
401 |
> |
setcolor(col, (vi[rord[RED]]+.5)/256., |
402 |
> |
(vi[rord[GRN]]+.5)/256., (vi[rord[BLU]]+.5)/256.); |
403 |
|
return(0); |
404 |
|
} |
405 |
|
|
412 |
|
|
413 |
|
if (fread((char *)vb, sizeof(BYTE), 3, fp) != 3) |
414 |
|
return(-1); |
415 |
< |
setcolor(col,(vb[0]+.5)/256.,(vb[1]+.5)/256.,(vb[2]+.5)/256.); |
415 |
> |
setcolor(col, (vb[rord[RED]]+.5)/256., |
416 |
> |
(vb[rord[GRN]]+.5)/256., (vb[rord[BLU]]+.5)/256.); |
417 |
|
return(0); |
418 |
|
} |
419 |
|
|
422 |
|
COLOR col; |
423 |
|
FILE *fp; |
424 |
|
{ |
425 |
< |
double vd; |
425 |
> |
double vd; |
426 |
|
|
427 |
|
if (fscanf(fp, "%lf", &vd) != 1) |
428 |
|
return(-1); |
435 |
|
COLOR col; |
436 |
|
FILE *fp; |
437 |
|
{ |
438 |
< |
double vd; |
438 |
> |
double vd; |
439 |
|
|
440 |
|
if (fread((char *)&vd, sizeof(double), 1, fp) != 1) |
441 |
|
return(-1); |
462 |
|
FILE *fp; |
463 |
|
{ |
464 |
|
int vi; |
465 |
< |
double d; |
465 |
> |
double d; |
466 |
|
|
467 |
|
if (fscanf(fp, "%d", &vi) != 1) |
468 |
|
return(-1); |
477 |
|
FILE *fp; |
478 |
|
{ |
479 |
|
BYTE vb; |
480 |
< |
double d; |
480 |
> |
double d; |
481 |
|
|
482 |
|
if (fread((char *)&vb, sizeof(BYTE), 1, fp) != 1) |
483 |
|
return(-1); |
492 |
|
FILE *fp; |
493 |
|
{ |
494 |
|
fprintf(fp, "%15.3e %15.3e %15.3e\n", |
495 |
< |
colval(col,RED), |
496 |
< |
colval(col,GRN), |
497 |
< |
colval(col,BLU)); |
495 |
> |
colval(col,ord[0]), |
496 |
> |
colval(col,ord[1]), |
497 |
> |
colval(col,ord[2])); |
498 |
|
|
499 |
|
return(ferror(fp) ? -1 : 0); |
500 |
|
} |
506 |
|
{ |
507 |
|
float vf[3]; |
508 |
|
|
509 |
< |
vf[0] = colval(col,RED); |
510 |
< |
vf[1] = colval(col,GRN); |
511 |
< |
vf[2] = colval(col,BLU); |
509 |
> |
vf[0] = colval(col,ord[0]); |
510 |
> |
vf[1] = colval(col,ord[1]); |
511 |
> |
vf[2] = colval(col,ord[2]); |
512 |
|
fwrite((char *)vf, sizeof(float), 3, fp); |
513 |
|
|
514 |
|
return(ferror(fp) ? -1 : 0); |
519 |
|
COLOR col; |
520 |
|
FILE *fp; |
521 |
|
{ |
522 |
< |
double vd[3]; |
522 |
> |
double vd[3]; |
523 |
|
|
524 |
< |
vd[0] = colval(col,RED); |
525 |
< |
vd[1] = colval(col,GRN); |
526 |
< |
vd[2] = colval(col,BLU); |
524 |
> |
vd[0] = colval(col,ord[0]); |
525 |
> |
vd[1] = colval(col,ord[1]); |
526 |
> |
vd[2] = colval(col,ord[2]); |
527 |
|
fwrite((char *)vd, sizeof(double), 3, fp); |
528 |
|
|
529 |
|
return(ferror(fp) ? -1 : 0); |
535 |
|
FILE *fp; |
536 |
|
{ |
537 |
|
fprintf(fp, "%d %d %d\n", |
538 |
< |
(int)(colval(col,RED)*256.), |
539 |
< |
(int)(colval(col,GRN)*256.), |
540 |
< |
(int)(colval(col,BLU)*256.)); |
538 |
> |
(int)(colval(col,ord[0])*256.), |
539 |
> |
(int)(colval(col,ord[1])*256.), |
540 |
> |
(int)(colval(col,ord[2])*256.)); |
541 |
|
|
542 |
|
return(ferror(fp) ? -1 : 0); |
543 |
|
} |
550 |
|
register int i; |
551 |
|
BYTE vb[3]; |
552 |
|
|
553 |
< |
i = colval(col,RED)*256.; |
553 |
> |
i = colval(col,ord[0])*256.; |
554 |
|
vb[0] = min(i,255); |
555 |
< |
i = colval(col,GRN)*256.; |
555 |
> |
i = colval(col,ord[1])*256.; |
556 |
|
vb[1] = min(i,255); |
557 |
< |
i = colval(col,BLU)*256.; |
557 |
> |
i = colval(col,ord[2])*256.; |
558 |
|
vb[2] = min(i,255); |
559 |
|
fwrite((char *)vb, sizeof(BYTE), 3, fp); |
560 |
|
|
589 |
|
COLOR col; |
590 |
|
FILE *fp; |
591 |
|
{ |
592 |
< |
double vd; |
592 |
> |
double vd; |
593 |
|
|
594 |
|
vd = bright(col); |
595 |
|
fwrite((char *)&vd, sizeof(double), 1, fp); |