1 |
#ifndef lint |
2 |
static const char RCSid[] = "$Id: pcwarp.c,v 3.6 2018/08/02 18:33:44 greg Exp $"; |
3 |
#endif |
4 |
/* |
5 |
* Warp colors in Radiance picture to correct for input/output changes. |
6 |
*/ |
7 |
|
8 |
#include "rtio.h" |
9 |
#include "resolu.h" |
10 |
#include "color.h" |
11 |
#include "warp3d.h" |
12 |
|
13 |
char *progname; /* global argv[0] */ |
14 |
|
15 |
FILE *infp = NULL; /* input stream */ |
16 |
int xres, yres; /* input picture resolution */ |
17 |
|
18 |
WARP3D *cwarp; /* our warp map */ |
19 |
int iclip = CGAMUT_UPPER; /* input value gamut clipping */ |
20 |
int oclip = CGAMUT_LOWER; /* output value gamut clipping */ |
21 |
|
22 |
static void syserror(char *s); |
23 |
static void picwarp(void); |
24 |
|
25 |
|
26 |
int |
27 |
main( |
28 |
int argc, |
29 |
char *argv[] |
30 |
) |
31 |
{ |
32 |
static char picfmt[MAXFMTLEN] = PICFMT; |
33 |
int cwflags = 0; |
34 |
int rval; |
35 |
int i; |
36 |
|
37 |
progname = argv[0]; |
38 |
infp = stdin; |
39 |
/* get options */ |
40 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
41 |
switch (argv[i][1]) { |
42 |
case 'e': |
43 |
cwflags = W3EXACT; |
44 |
break; |
45 |
case 'f': |
46 |
cwflags = W3FAST; |
47 |
break; |
48 |
case 'i': |
49 |
iclip = 0; |
50 |
break; |
51 |
case 'o': |
52 |
oclip = CGAMUT; |
53 |
break; |
54 |
default: |
55 |
goto userr; |
56 |
} |
57 |
/* load warp map */ |
58 |
if (i >= argc) |
59 |
goto userr; |
60 |
if ((cwarp = load3dw(argv[i++], NULL)) == NULL) |
61 |
syserror("load3dw"); |
62 |
set3dwfl(cwarp, cwflags); |
63 |
/* open input and output pictures */ |
64 |
if (i < argc && (infp = fopen(argv[i], "r")) == NULL) |
65 |
syserror(argv[i]); |
66 |
if (i < argc-1 && freopen(argv[i+1], "w", stdout) == NULL) |
67 |
syserror(argv[i+1]); |
68 |
/* transfer header */ |
69 |
if ((rval = checkheader(infp, picfmt, stdout)) < 0) { |
70 |
fprintf(stderr, "%s: input not a Radiance picture\n", |
71 |
progname); |
72 |
exit(1); |
73 |
} |
74 |
if (rval) |
75 |
fputformat(picfmt, stdout); |
76 |
/* add new header info. */ |
77 |
printargs(i, argv, stdout); |
78 |
putchar('\n'); |
79 |
/* get picture size */ |
80 |
if ((rval = fgetresolu(&xres, &yres, infp)) < 0) { |
81 |
fprintf(stderr, "%s: bad picture size\n", progname); |
82 |
exit(1); |
83 |
} |
84 |
/* new picture size the same */ |
85 |
fputresolu(rval, xres, yres, stdout); |
86 |
/* warp those colors! */ |
87 |
picwarp(); |
88 |
exit(0); |
89 |
userr: |
90 |
fprintf(stderr, |
91 |
"Usage: %s [-i][-o][-e|-f] map.cwp [input.hdr [output.hdr]]\n", |
92 |
progname); |
93 |
exit(1); |
94 |
} |
95 |
|
96 |
|
97 |
static void |
98 |
syserror( /* print system error and exit */ |
99 |
char *s |
100 |
) |
101 |
{ |
102 |
fprintf(stderr, "%s: ", progname); |
103 |
perror(s); |
104 |
exit(2); |
105 |
} |
106 |
|
107 |
|
108 |
static void |
109 |
picwarp(void) /* warp our picture scanlines */ |
110 |
{ |
111 |
register COLOR *scan; |
112 |
long ngamut = 0; |
113 |
int rval; |
114 |
int y; |
115 |
register int x; |
116 |
|
117 |
scan = (COLOR *)malloc(xres*sizeof(COLOR)); |
118 |
if (scan == NULL) |
119 |
syserror("picwarp"); |
120 |
for (y = 0; y < yres; y++) { |
121 |
if (freadscan(scan, xres, infp) < 0) { |
122 |
fprintf(stderr, "%s: error reading input picture\n", |
123 |
progname); |
124 |
exit(1); |
125 |
} |
126 |
for (x = 0; x < xres; x++) { |
127 |
if (iclip) |
128 |
clipgamut(scan[x], bright(scan[x]), iclip, |
129 |
cblack, cwhite); |
130 |
rval = warp3d(scan[x], scan[x], cwarp); |
131 |
if (rval & W3ERROR) |
132 |
syserror("warp3d"); |
133 |
if (rval & W3BADMAP) { |
134 |
fprintf(stderr, "%s: singular color mapping\n", |
135 |
progname); |
136 |
exit(1); |
137 |
} |
138 |
if (rval & W3GAMUT) |
139 |
ngamut++; |
140 |
if (oclip) |
141 |
clipgamut(scan[x], bright(scan[x]), oclip, |
142 |
cblack, cwhite); |
143 |
} |
144 |
if (fwritescan(scan, xres, stdout) < 0) { |
145 |
fprintf(stderr, "%s: error writing output picture\n", |
146 |
progname); |
147 |
exit(1); |
148 |
} |
149 |
} |
150 |
if (ngamut >= (long)xres*yres/100) |
151 |
fprintf(stderr, "%s: warning - %ld%% of pixels out of gamut\n", |
152 |
progname, 100*ngamut/((long)xres*yres)); |
153 |
free((void *)scan); |
154 |
} |