1 |
greg |
2.1 |
/* Copyright (c) 1995 Regents of the University of California */ |
2 |
|
|
|
3 |
|
|
#ifndef lint |
4 |
|
|
static char SCCSid[] = "$SunId$ LBL"; |
5 |
|
|
#endif |
6 |
|
|
|
7 |
|
|
/* |
8 |
|
|
* Calibrate a scanned MacBeth Color Checker Chart |
9 |
|
|
* |
10 |
|
|
* Produce a .cal file suitable for use with pcomb. |
11 |
|
|
*/ |
12 |
|
|
|
13 |
|
|
#include <stdio.h> |
14 |
|
|
#ifdef MSDOS |
15 |
|
|
#include <fcntl.h> |
16 |
|
|
#endif |
17 |
|
|
#include "color.h" |
18 |
|
|
#include "resolu.h" |
19 |
|
|
#include "pmap.h" |
20 |
|
|
|
21 |
greg |
2.4 |
/* MacBeth colors */ |
22 |
|
|
#define DarkSkin 0 |
23 |
|
|
#define LightSkin 1 |
24 |
|
|
#define BlueSky 2 |
25 |
|
|
#define Foliage 3 |
26 |
|
|
#define BlueFlower 4 |
27 |
|
|
#define BluishGreen 5 |
28 |
|
|
#define Orange 6 |
29 |
|
|
#define PurplishBlue 7 |
30 |
|
|
#define ModerateRed 8 |
31 |
|
|
#define Purple 9 |
32 |
|
|
#define YellowGreen 10 |
33 |
|
|
#define OrangeYellow 11 |
34 |
|
|
#define Blue 12 |
35 |
|
|
#define Green 13 |
36 |
|
|
#define Red 14 |
37 |
|
|
#define Yellow 15 |
38 |
|
|
#define Magenta 16 |
39 |
|
|
#define Cyan 17 |
40 |
|
|
#define White 18 |
41 |
|
|
#define Neutral8 19 |
42 |
|
|
#define Neutral65 20 |
43 |
|
|
#define Neutral5 21 |
44 |
|
|
#define Neutral35 22 |
45 |
|
|
#define Black 23 |
46 |
|
|
/* computed from 5nm spectral measurements */ |
47 |
|
|
/* CIE 1931 2 degree obs, equal-energy white */ |
48 |
greg |
2.1 |
float mbxyY[24][3] = { |
49 |
|
|
{0.462, 0.3769, 0.0932961}, /* DarkSkin */ |
50 |
|
|
{0.4108, 0.3542, 0.410348}, /* LightSkin */ |
51 |
|
|
{0.2626, 0.267, 0.181554}, /* BlueSky */ |
52 |
|
|
{0.36, 0.4689, 0.108447}, /* Foliage */ |
53 |
|
|
{0.2977, 0.2602, 0.248407}, /* BlueFlower */ |
54 |
|
|
{0.2719, 0.3485, 0.401156}, /* BluishGreen */ |
55 |
|
|
{0.52, 0.4197, 0.357899}, /* Orange */ |
56 |
|
|
{0.229, 0.1866, 0.103911}, /* PurplishBlue */ |
57 |
|
|
{0.4909, 0.3262, 0.242615}, /* ModerateRed */ |
58 |
|
|
{0.3361, 0.2249, 0.0600102}, /* Purple */ |
59 |
|
|
{0.3855, 0.4874, 0.42963}, /* YellowGreen */ |
60 |
|
|
{0.4853, 0.4457, 0.476343}, /* OrangeYellow */ |
61 |
|
|
{0.2026, 0.1369, 0.0529249}, /* Blue */ |
62 |
|
|
{0.3007, 0.4822, 0.221226}, /* Green */ |
63 |
|
|
{0.5805, 0.3238, 0.162167}, /* Red */ |
64 |
|
|
{0.4617, 0.472, 0.64909}, /* Yellow */ |
65 |
|
|
{0.4178, 0.2625, 0.233662}, /* Magenta */ |
66 |
|
|
{0.2038, 0.2508, 0.167275}, /* Cyan */ |
67 |
|
|
{0.3358, 0.337, 0.916877}, /* White */ |
68 |
|
|
{0.3338, 0.3348, 0.604678}, /* Neutral.8 */ |
69 |
|
|
{0.3333, 0.3349, 0.364566}, /* Neutral.65 */ |
70 |
|
|
{0.3353, 0.3359, 0.200238}, /* Neutral.5 */ |
71 |
|
|
{0.3363, 0.336, 0.0878721}, /* Neutral.35 */ |
72 |
|
|
{0.3346, 0.3349, 0.0308383} /* Black */ |
73 |
|
|
}; |
74 |
|
|
|
75 |
|
|
COLOR mbRGB[24]; /* MacBeth RGB values */ |
76 |
|
|
|
77 |
|
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
78 |
greg |
2.4 |
short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; |
79 |
greg |
2.1 |
|
80 |
greg |
2.8 |
#define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
81 |
|
|
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
82 |
greg |
2.4 |
|
83 |
greg |
2.8 |
#define SATFLGS (1L<<Red|1L<<Green|1L<<Blue|1L<<Magenta|1L<<Yellow| \ |
84 |
|
|
1L<<Cyan|1L<<Orange|1L<<Purple|1L<<PurplishBlue| \ |
85 |
|
|
1L<<YellowGreen|1<<OrangeYellow|1L<<BlueFlower) |
86 |
greg |
2.4 |
|
87 |
greg |
2.8 |
#define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ |
88 |
|
|
1L<<BluishGreen|1L<<ModerateRed) |
89 |
greg |
2.7 |
|
90 |
greg |
2.8 |
#define REQFLGS NEUFLGS /* need these colors */ |
91 |
|
|
#define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */ |
92 |
|
|
|
93 |
greg |
2.9 |
#define RG_BORD 0 /* patch border */ |
94 |
|
|
#define RG_CENT 01 /* central region of patch */ |
95 |
|
|
#define RG_ORIG 02 /* original color region */ |
96 |
|
|
#define RG_CORR 04 /* corrected color region */ |
97 |
greg |
2.7 |
|
98 |
greg |
2.1 |
int xmax, ymax; /* input image dimensions */ |
99 |
|
|
int bounds[4][2]; /* image coordinates of chart corners */ |
100 |
|
|
double imgxfm[3][3]; /* coordinate transformation matrix */ |
101 |
|
|
|
102 |
greg |
2.7 |
COLOR inpRGB[24]; /* measured or scanned input colors */ |
103 |
|
|
long inpflags = 0; /* flags of which colors were input */ |
104 |
greg |
2.8 |
long gmtflags = 0; /* flags of out-of-gamut colors */ |
105 |
greg |
2.1 |
|
106 |
greg |
2.2 |
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
107 |
greg |
2.1 |
double solmat[3][3]; /* color mapping matrix */ |
108 |
|
|
|
109 |
greg |
2.4 |
FILE *debugfp = NULL; /* debug output picture */ |
110 |
greg |
2.1 |
char *progname; |
111 |
|
|
|
112 |
|
|
extern char *malloc(); |
113 |
|
|
|
114 |
|
|
|
115 |
|
|
main(argc, argv) |
116 |
|
|
int argc; |
117 |
|
|
char **argv; |
118 |
|
|
{ |
119 |
greg |
2.7 |
int inpispic = 1; |
120 |
greg |
2.1 |
int i; |
121 |
|
|
|
122 |
|
|
progname = argv[0]; |
123 |
greg |
2.7 |
for (i = 1; i < argc && argv[i][0] == '-'; i++) |
124 |
|
|
switch (argv[i][1]) { |
125 |
|
|
case 'd': /* debug output */ |
126 |
|
|
i++; |
127 |
|
|
if (badarg(argc-i, argv+i, "s")) |
128 |
|
|
goto userr; |
129 |
|
|
if ((debugfp = fopen(argv[i], "w")) == NULL) { |
130 |
|
|
perror(argv[i]); |
131 |
|
|
exit(1); |
132 |
|
|
} |
133 |
greg |
2.1 |
#ifdef MSDOS |
134 |
greg |
2.7 |
setmode(fileno(debugfp), O_BINARY); |
135 |
greg |
2.1 |
#endif |
136 |
greg |
2.7 |
newheader("RADIANCE", debugfp); /* start */ |
137 |
|
|
printargs(argc, argv, debugfp); /* header */ |
138 |
|
|
break; |
139 |
|
|
case 'p': /* picture position */ |
140 |
|
|
if (badarg(argc-i-1, argv+i+1, "iiiiiiii")) |
141 |
|
|
goto userr; |
142 |
|
|
bounds[0][0] = atoi(argv[++i]); |
143 |
|
|
bounds[0][1] = atoi(argv[++i]); |
144 |
|
|
bounds[1][0] = atoi(argv[++i]); |
145 |
|
|
bounds[1][1] = atoi(argv[++i]); |
146 |
|
|
bounds[2][0] = atoi(argv[++i]); |
147 |
|
|
bounds[2][1] = atoi(argv[++i]); |
148 |
|
|
bounds[3][0] = atoi(argv[++i]); |
149 |
|
|
bounds[3][1] = atoi(argv[++i]); |
150 |
|
|
inpispic = 2; |
151 |
|
|
break; |
152 |
|
|
case 'c': /* color input */ |
153 |
|
|
inpispic = 0; |
154 |
|
|
break; |
155 |
|
|
default: |
156 |
|
|
goto userr; |
157 |
|
|
} |
158 |
|
|
/* open files */ |
159 |
|
|
if (i < argc && freopen(argv[i], "r", stdin) == NULL) { |
160 |
greg |
2.1 |
perror(argv[1]); |
161 |
|
|
exit(1); |
162 |
|
|
} |
163 |
greg |
2.7 |
if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { |
164 |
greg |
2.1 |
perror(argv[2]); |
165 |
|
|
exit(1); |
166 |
|
|
} |
167 |
greg |
2.7 |
if (inpispic) { /* load input picture header */ |
168 |
greg |
2.1 |
#ifdef MSDOS |
169 |
greg |
2.7 |
setmode(fileno(stdin), O_BINARY); |
170 |
greg |
2.1 |
#endif |
171 |
greg |
2.7 |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
172 |
|
|
fgetresolu(&xmax, &ymax, stdin) < 0) { |
173 |
|
|
fprintf(stderr, "%s: bad input picture\n", progname); |
174 |
|
|
exit(1); |
175 |
|
|
} |
176 |
|
|
} else { /* else set default xmax and ymax */ |
177 |
|
|
xmax = 512; |
178 |
|
|
ymax = 2*512/3; |
179 |
greg |
2.1 |
} |
180 |
greg |
2.7 |
if (inpispic != 2) { /* use default boundaries */ |
181 |
greg |
2.1 |
bounds[0][0] = bounds[2][0] = .029*xmax + .5; |
182 |
|
|
bounds[0][1] = bounds[1][1] = .956*ymax + .5; |
183 |
|
|
bounds[1][0] = bounds[3][0] = .971*xmax + .5; |
184 |
|
|
bounds[2][1] = bounds[3][1] = .056*ymax + .5; |
185 |
|
|
} |
186 |
|
|
init(); /* initialize */ |
187 |
greg |
2.7 |
if (inpispic) /* get picture colors */ |
188 |
|
|
getpicture(); |
189 |
|
|
else |
190 |
|
|
getcolors(); |
191 |
greg |
2.2 |
compute(); /* compute color mapping */ |
192 |
greg |
2.4 |
/* print comment */ |
193 |
greg |
2.10 |
printf("{\n\tColor correction file computed by:\n\t\t"); |
194 |
greg |
2.7 |
printargs(argc, argv, stdout); |
195 |
greg |
2.10 |
printf("\n\tUsage: pcomb -f %s uncorrected.pic > corrected.pic\n", |
196 |
|
|
i+1 < argc ? argv[i+1] : "{this_file}"); |
197 |
greg |
2.7 |
printf("}\n"); |
198 |
greg |
2.1 |
putmapping(); /* put out color mapping */ |
199 |
greg |
2.7 |
if (debugfp != NULL) /* put out debug picture */ |
200 |
|
|
if (inpispic) |
201 |
|
|
picdebug(); |
202 |
|
|
else |
203 |
|
|
clrdebug(); |
204 |
greg |
2.1 |
exit(0); |
205 |
|
|
userr: |
206 |
greg |
2.7 |
fprintf(stderr, |
207 |
|
|
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", |
208 |
greg |
2.1 |
progname); |
209 |
greg |
2.7 |
fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", |
210 |
|
|
progname); |
211 |
greg |
2.1 |
exit(1); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
|
215 |
|
|
init() /* initialize */ |
216 |
|
|
{ |
217 |
|
|
double quad[4][2]; |
218 |
greg |
2.7 |
register int i; |
219 |
greg |
2.1 |
/* make coordinate transformation */ |
220 |
|
|
quad[0][0] = bounds[0][0]; |
221 |
|
|
quad[0][1] = bounds[0][1]; |
222 |
|
|
quad[1][0] = bounds[1][0]; |
223 |
|
|
quad[1][1] = bounds[1][1]; |
224 |
|
|
quad[2][0] = bounds[3][0]; |
225 |
|
|
quad[2][1] = bounds[3][1]; |
226 |
|
|
quad[3][0] = bounds[2][0]; |
227 |
|
|
quad[3][1] = bounds[2][1]; |
228 |
|
|
|
229 |
|
|
if (pmap_quad_rect(0., 0., 6., 4., quad, imgxfm) == PMAP_BAD) { |
230 |
|
|
fprintf(stderr, "%s: bad chart boundaries\n", progname); |
231 |
|
|
exit(1); |
232 |
|
|
} |
233 |
greg |
2.7 |
/* map MacBeth colors to RGB space */ |
234 |
|
|
for (i = 0; i < 24; i++) |
235 |
|
|
xyY2RGB(mbRGB[i], mbxyY[i]); |
236 |
greg |
2.1 |
} |
237 |
|
|
|
238 |
|
|
|
239 |
|
|
int |
240 |
greg |
2.9 |
chartndx(x, y, np) /* find color number for position */ |
241 |
greg |
2.1 |
int x, y; |
242 |
greg |
2.9 |
int *np; |
243 |
greg |
2.1 |
{ |
244 |
|
|
double ipos[3], cpos[3]; |
245 |
|
|
int ix, iy; |
246 |
|
|
double fx, fy; |
247 |
|
|
|
248 |
|
|
ipos[0] = x; |
249 |
|
|
ipos[1] = y; |
250 |
|
|
ipos[2] = 1; |
251 |
|
|
mx3d_transform(ipos, imgxfm, cpos); |
252 |
|
|
cpos[0] /= cpos[2]; |
253 |
|
|
cpos[1] /= cpos[2]; |
254 |
|
|
if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.) |
255 |
greg |
2.9 |
return(RG_BORD); |
256 |
greg |
2.1 |
ix = cpos[0]; |
257 |
|
|
iy = cpos[1]; |
258 |
|
|
fx = cpos[0] - ix; |
259 |
|
|
fy = cpos[1] - iy; |
260 |
greg |
2.9 |
*np = iy*6 + ix; |
261 |
|
|
if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65) |
262 |
|
|
return(RG_CENT); |
263 |
|
|
if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95) |
264 |
|
|
return(RG_BORD); |
265 |
|
|
if (fx >= 0.5) /* right side is corrected */ |
266 |
|
|
return(RG_CORR); |
267 |
|
|
return(RG_ORIG); /* left side is original */ |
268 |
greg |
2.1 |
} |
269 |
|
|
|
270 |
|
|
|
271 |
greg |
2.7 |
getpicture() /* load in picture colors */ |
272 |
greg |
2.1 |
{ |
273 |
|
|
COLR *scanln; |
274 |
|
|
COLOR pval; |
275 |
|
|
int ccount[24]; |
276 |
|
|
double d; |
277 |
greg |
2.9 |
int y, i; |
278 |
|
|
register int x; |
279 |
greg |
2.1 |
|
280 |
|
|
scanln = (COLR *)malloc(xmax*sizeof(COLR)); |
281 |
|
|
if (scanln == NULL) { |
282 |
|
|
perror(progname); |
283 |
|
|
exit(1); |
284 |
|
|
} |
285 |
|
|
for (i = 0; i < 24; i++) { |
286 |
greg |
2.7 |
setcolor(inpRGB[i], 0., 0., 0.); |
287 |
greg |
2.1 |
ccount[i] = 0; |
288 |
|
|
} |
289 |
|
|
for (y = ymax-1; y >= 0; y--) { |
290 |
|
|
if (freadcolrs(scanln, xmax, stdin) < 0) { |
291 |
|
|
fprintf(stderr, "%s: error reading input picture\n", |
292 |
|
|
progname); |
293 |
|
|
exit(1); |
294 |
|
|
} |
295 |
greg |
2.9 |
for (x = 0; x < xmax; x++) |
296 |
|
|
if (chartndx(x, y, &i) == RG_CENT) { |
297 |
greg |
2.1 |
colr_color(pval, scanln[x]); |
298 |
greg |
2.7 |
addcolor(inpRGB[i], pval); |
299 |
greg |
2.1 |
ccount[i]++; |
300 |
|
|
} |
301 |
|
|
} |
302 |
greg |
2.7 |
for (i = 0; i < 24; i++) { /* compute averages */ |
303 |
|
|
if (ccount[i] == 0) |
304 |
|
|
continue; |
305 |
|
|
d = 1./ccount[i]; |
306 |
|
|
scalecolor(inpRGB[i], d); |
307 |
|
|
inpflags |= 1L<<i; |
308 |
|
|
} |
309 |
|
|
free((char *)scanln); |
310 |
|
|
} |
311 |
|
|
|
312 |
|
|
|
313 |
|
|
getcolors() /* get xyY colors from standard input */ |
314 |
|
|
{ |
315 |
|
|
int gotwhite = 0; |
316 |
|
|
COLOR whiteclr; |
317 |
|
|
int n; |
318 |
|
|
float xyYin[3]; |
319 |
|
|
|
320 |
|
|
while (fgetval(stdin, 'i', &n) == 1) { /* read colors */ |
321 |
|
|
if (n < 0 | n > 24 || |
322 |
|
|
fgetval(stdin, 'f', &xyYin[0]) != 1 || |
323 |
|
|
fgetval(stdin, 'f', &xyYin[1]) != 1 || |
324 |
|
|
fgetval(stdin, 'f', &xyYin[2]) != 1 || |
325 |
|
|
xyYin[0] < 0. | xyYin[0] > 1. | |
326 |
|
|
xyYin[1] < 0. | xyYin[1] > 1.) { |
327 |
|
|
fprintf(stderr, "%s: bad color input data\n", |
328 |
greg |
2.1 |
progname); |
329 |
|
|
exit(1); |
330 |
|
|
} |
331 |
greg |
2.7 |
if (n == 0) { /* calibration white */ |
332 |
|
|
xyY2RGB(whiteclr, xyYin); |
333 |
|
|
gotwhite++; |
334 |
|
|
} else { /* standard color */ |
335 |
|
|
n--; |
336 |
|
|
xyY2RGB(inpRGB[n], xyYin); |
337 |
|
|
inpflags |= 1L<<n; |
338 |
|
|
} |
339 |
greg |
2.1 |
} |
340 |
greg |
2.7 |
/* normalize colors */ |
341 |
|
|
if (!gotwhite) { |
342 |
|
|
if (!(inpflags & 1L<<White)) { |
343 |
|
|
fprintf(stderr, "%s: missing input for White\n", |
344 |
|
|
progname); |
345 |
|
|
exit(1); |
346 |
|
|
} |
347 |
|
|
setcolor(whiteclr, |
348 |
|
|
colval(inpRGB[White],RED)/colval(mbRGB[White],RED), |
349 |
|
|
colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN), |
350 |
|
|
colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU)); |
351 |
|
|
} |
352 |
|
|
for (n = 0; n < 24; n++) |
353 |
|
|
if (inpflags & 1L<<n) |
354 |
|
|
setcolor(inpRGB[n], |
355 |
|
|
colval(inpRGB[n],RED)/colval(whiteclr,RED), |
356 |
|
|
colval(inpRGB[n],GRN)/colval(whiteclr,GRN), |
357 |
|
|
colval(inpRGB[n],BLU)/colval(whiteclr,BLU)); |
358 |
greg |
2.1 |
} |
359 |
|
|
|
360 |
|
|
|
361 |
greg |
2.2 |
bresp(y, x) /* piecewise linear interpolation of primaries */ |
362 |
|
|
COLOR y, x; |
363 |
greg |
2.1 |
{ |
364 |
greg |
2.2 |
register int i, n; |
365 |
greg |
2.1 |
|
366 |
greg |
2.2 |
for (i = 0; i < 3; i++) { |
367 |
greg |
2.5 |
for (n = 0; n < NMBNEU-2; n++) |
368 |
|
|
if (colval(x,i) < colval(bramp[n+1][0],i)) |
369 |
|
|
break; |
370 |
greg |
2.8 |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
371 |
greg |
2.2 |
colval(bramp[n][1],i) + |
372 |
|
|
(colval(x,i) - colval(bramp[n][0],i)) * |
373 |
|
|
colval(bramp[n+1][1],i)) / |
374 |
|
|
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
375 |
|
|
} |
376 |
greg |
2.1 |
} |
377 |
|
|
|
378 |
|
|
|
379 |
greg |
2.2 |
compute() /* compute color mapping */ |
380 |
greg |
2.1 |
{ |
381 |
greg |
2.8 |
COLOR clrin[24], clrout[24]; |
382 |
|
|
long cflags; |
383 |
|
|
COLOR ctmp; |
384 |
|
|
register int i, j, n; |
385 |
greg |
2.7 |
/* did we get what we need? */ |
386 |
|
|
if ((inpflags & REQFLGS) != REQFLGS) { |
387 |
|
|
fprintf(stderr, "%s: missing required input colors\n", |
388 |
|
|
progname); |
389 |
|
|
exit(1); |
390 |
greg |
2.2 |
} |
391 |
greg |
2.1 |
/* compute piecewise luminance curve */ |
392 |
|
|
for (i = 0; i < NMBNEU; i++) { |
393 |
greg |
2.7 |
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
394 |
greg |
2.2 |
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
395 |
greg |
2.1 |
} |
396 |
greg |
2.8 |
/* compute color mapping */ |
397 |
|
|
do { |
398 |
|
|
cflags = inpflags & ~gmtflags; |
399 |
|
|
n = 0; /* compute transform matrix */ |
400 |
|
|
for (i = 0; i < 24; i++) |
401 |
|
|
if (cflags & 1L<<i) { |
402 |
|
|
bresp(clrin[n], inpRGB[i]); |
403 |
|
|
copycolor(clrout[n], mbRGB[i]); |
404 |
|
|
n++; |
405 |
|
|
} |
406 |
|
|
compsoln(clrin, clrout, n); |
407 |
|
|
/* check out-of-gamut colors */ |
408 |
|
|
for (i = 0; i < 24; i++) |
409 |
|
|
if (cflags & 1L<<i) { |
410 |
|
|
cresp(ctmp, mbRGB[i]); |
411 |
|
|
for (j = 0; j < 3; j++) |
412 |
|
|
if (colval(ctmp,j) <= 0. || |
413 |
|
|
colval(ctmp,j) >= 1.) { |
414 |
|
|
gmtflags |= 1L<<i; |
415 |
|
|
break; |
416 |
|
|
} |
417 |
|
|
} |
418 |
|
|
} while (cflags & gmtflags); |
419 |
|
|
if (gmtflags & MODFLGS) |
420 |
|
|
fprintf(stderr, |
421 |
|
|
"%s: warning - some moderate colors are out of gamut\n", |
422 |
|
|
progname); |
423 |
greg |
2.2 |
} |
424 |
|
|
|
425 |
|
|
|
426 |
|
|
putmapping() /* put out color mapping for pcomb -f */ |
427 |
|
|
{ |
428 |
|
|
static char cchar[3] = {'r', 'g', 'b'}; |
429 |
|
|
register int i, j; |
430 |
greg |
2.1 |
/* print brightness mapping */ |
431 |
greg |
2.2 |
for (j = 0; j < 3; j++) { |
432 |
|
|
printf("%cxa(i) : select(i", cchar[j]); |
433 |
|
|
for (i = 0; i < NMBNEU; i++) |
434 |
|
|
printf(",%g", colval(bramp[i][0],j)); |
435 |
|
|
printf(");\n"); |
436 |
|
|
printf("%cya(i) : select(i", cchar[j]); |
437 |
|
|
for (i = 0; i < NMBNEU; i++) |
438 |
|
|
printf(",%g", colval(bramp[i][1],j)); |
439 |
|
|
printf(");\n"); |
440 |
|
|
printf("%c = %ci(1);\n", cchar[j], cchar[j]); |
441 |
|
|
printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n", |
442 |
|
|
cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j], |
443 |
|
|
cchar[j], cchar[j]); |
444 |
|
|
printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]); |
445 |
|
|
printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", |
446 |
|
|
cchar[j], cchar[j], cchar[j], |
447 |
|
|
cchar[j], cchar[j], cchar[j]); |
448 |
|
|
printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n", |
449 |
|
|
cchar[j], cchar[j], cchar[j], |
450 |
|
|
cchar[j], cchar[j]); |
451 |
|
|
printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n", |
452 |
|
|
cchar[j], cchar[j], cchar[j], cchar[j]); |
453 |
|
|
} |
454 |
greg |
2.1 |
/* print color mapping */ |
455 |
greg |
2.2 |
printf("ro = %g*rn + %g*gn + %g*bn ;\n", |
456 |
greg |
2.3 |
solmat[0][0], solmat[0][1], solmat[0][2]); |
457 |
greg |
2.2 |
printf("go = %g*rn + %g*gn + %g*bn ;\n", |
458 |
greg |
2.3 |
solmat[1][0], solmat[1][1], solmat[1][2]); |
459 |
greg |
2.2 |
printf("bo = %g*rn + %g*gn + %g*bn ;\n", |
460 |
greg |
2.3 |
solmat[2][0], solmat[2][1], solmat[2][2]); |
461 |
greg |
2.1 |
} |
462 |
|
|
|
463 |
|
|
|
464 |
greg |
2.4 |
compsoln(cin, cout, n) /* solve 3xN system using least-squares */ |
465 |
greg |
2.2 |
COLOR cin[], cout[]; |
466 |
greg |
2.1 |
int n; |
467 |
|
|
{ |
468 |
|
|
extern double mx3d_adjoint(), fabs(); |
469 |
|
|
double mat[3][3], invmat[3][3]; |
470 |
|
|
double det; |
471 |
|
|
double colv[3], rowv[3]; |
472 |
greg |
2.4 |
register int i, j, k; |
473 |
greg |
2.1 |
|
474 |
greg |
2.8 |
if (n < 3) { |
475 |
|
|
fprintf(stderr, "%s: too few colors to match!\n", progname); |
476 |
greg |
2.1 |
exit(1); |
477 |
|
|
} |
478 |
greg |
2.4 |
if (n == 3) |
479 |
|
|
for (i = 0; i < 3; i++) |
480 |
|
|
for (j = 0; j < 3; j++) |
481 |
|
|
mat[i][j] = colval(cin[j],i); |
482 |
|
|
else { /* compute A^t A */ |
483 |
|
|
for (i = 0; i < 3; i++) |
484 |
|
|
for (j = i; j < 3; j++) { |
485 |
|
|
mat[i][j] = 0.; |
486 |
|
|
for (k = 0; k < n; k++) |
487 |
|
|
mat[i][j] += colval(cin[k],i) * |
488 |
|
|
colval(cin[k],j); |
489 |
|
|
} |
490 |
|
|
for (i = 1; i < 3; i++) /* using symmetry */ |
491 |
|
|
for (j = 0; j < i; j++) |
492 |
|
|
mat[i][j] = mat[j][i]; |
493 |
|
|
} |
494 |
greg |
2.1 |
det = mx3d_adjoint(mat, invmat); |
495 |
|
|
if (fabs(det) < 1e-4) { |
496 |
|
|
fprintf(stderr, "%s: cannot compute color mapping\n", |
497 |
|
|
progname); |
498 |
|
|
solmat[0][0] = solmat[1][1] = solmat[2][2] = 1.; |
499 |
|
|
solmat[0][1] = solmat[0][2] = solmat[1][0] = |
500 |
|
|
solmat[1][2] = solmat[2][0] = solmat[2][1] = 0.; |
501 |
|
|
return; |
502 |
|
|
} |
503 |
|
|
for (i = 0; i < 3; i++) |
504 |
|
|
for (j = 0; j < 3; j++) |
505 |
|
|
invmat[i][j] /= det; |
506 |
|
|
for (i = 0; i < 3; i++) { |
507 |
greg |
2.4 |
if (n == 3) |
508 |
|
|
for (j = 0; j < 3; j++) |
509 |
|
|
colv[j] = colval(cout[j],i); |
510 |
|
|
else |
511 |
|
|
for (j = 0; j < 3; j++) { |
512 |
|
|
colv[j] = 0.; |
513 |
|
|
for (k = 0; k < n; k++) |
514 |
|
|
colv[j] += colval(cout[k],i) * |
515 |
|
|
colval(cin[k],j); |
516 |
|
|
} |
517 |
greg |
2.3 |
mx3d_transform(colv, invmat, rowv); |
518 |
greg |
2.1 |
for (j = 0; j < 3; j++) |
519 |
greg |
2.3 |
solmat[i][j] = rowv[j]; |
520 |
greg |
2.1 |
} |
521 |
|
|
} |
522 |
|
|
|
523 |
greg |
2.3 |
|
524 |
greg |
2.1 |
cvtcolor(cout, cin) /* convert color according to our mapping */ |
525 |
|
|
COLOR cout, cin; |
526 |
|
|
{ |
527 |
greg |
2.8 |
COLOR ctmp; |
528 |
|
|
|
529 |
|
|
bresp(ctmp, cin); |
530 |
|
|
cresp(cout, ctmp); |
531 |
|
|
if (colval(cout,RED) < 0.) |
532 |
|
|
colval(cout,RED) = 0.; |
533 |
|
|
if (colval(cout,GRN) < 0.) |
534 |
|
|
colval(cout,GRN) = 0.; |
535 |
|
|
if (colval(cout,BLU) < 0.) |
536 |
|
|
colval(cout,BLU) = 0.; |
537 |
|
|
} |
538 |
|
|
|
539 |
|
|
|
540 |
|
|
cresp(cout, cin) /* transform color according to matrix */ |
541 |
|
|
COLOR cout, cin; |
542 |
|
|
{ |
543 |
greg |
2.1 |
double r, g, b; |
544 |
|
|
|
545 |
greg |
2.8 |
r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1] |
546 |
|
|
+ colval(cin,2)*solmat[0][2]; |
547 |
|
|
g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1] |
548 |
|
|
+ colval(cin,2)*solmat[1][2]; |
549 |
|
|
b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1] |
550 |
|
|
+ colval(cin,2)*solmat[2][2]; |
551 |
greg |
2.2 |
setcolor(cout, r, g, b); |
552 |
greg |
2.1 |
} |
553 |
|
|
|
554 |
|
|
|
555 |
greg |
2.7 |
xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */ |
556 |
|
|
COLOR rgbout; |
557 |
|
|
register float xyYin[3]; |
558 |
greg |
2.1 |
{ |
559 |
greg |
2.7 |
COLOR ctmp; |
560 |
|
|
double d; |
561 |
|
|
|
562 |
|
|
d = xyYin[2] / xyYin[1]; |
563 |
|
|
ctmp[0] = xyYin[0] * d; |
564 |
|
|
ctmp[1] = xyYin[2]; |
565 |
|
|
ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; |
566 |
|
|
cie_rgb(rgbout, ctmp); |
567 |
|
|
} |
568 |
|
|
|
569 |
|
|
|
570 |
|
|
picdebug() /* put out debugging picture */ |
571 |
|
|
{ |
572 |
greg |
2.8 |
static COLOR blkcol = BLKCOLOR; |
573 |
greg |
2.1 |
COLOR *scan; |
574 |
greg |
2.9 |
int y, i; |
575 |
|
|
register int x, rg; |
576 |
greg |
2.1 |
|
577 |
|
|
if (fseek(stdin, 0L, 0) == EOF) { |
578 |
|
|
fprintf(stderr, "%s: cannot seek on input picture\n", progname); |
579 |
|
|
exit(1); |
580 |
|
|
} |
581 |
|
|
getheader(stdin, NULL, NULL); /* skip input header */ |
582 |
|
|
fgetresolu(&xmax, &ymax, stdin); |
583 |
|
|
/* allocate scanline */ |
584 |
|
|
scan = (COLOR *)malloc(xmax*sizeof(COLOR)); |
585 |
|
|
if (scan == NULL) { |
586 |
|
|
perror(progname); |
587 |
|
|
exit(1); |
588 |
|
|
} |
589 |
|
|
/* finish debug header */ |
590 |
greg |
2.5 |
fputformat(COLRFMT, debugfp); |
591 |
greg |
2.1 |
putc('\n', debugfp); |
592 |
|
|
fprtresolu(xmax, ymax, debugfp); |
593 |
greg |
2.7 |
/* write debug picture */ |
594 |
greg |
2.1 |
for (y = ymax-1; y >= 0; y--) { |
595 |
|
|
if (freadscan(scan, xmax, stdin) < 0) { |
596 |
|
|
fprintf(stderr, "%s: error rereading input picture\n", |
597 |
|
|
progname); |
598 |
|
|
exit(1); |
599 |
|
|
} |
600 |
|
|
for (x = 0; x < xmax; x++) { |
601 |
greg |
2.9 |
rg = chartndx(x, y, &i); |
602 |
|
|
if (rg == RG_CENT) { |
603 |
|
|
if (!(1L<<i & gmtflags) || (x+y)&07) |
604 |
|
|
copycolor(scan[x], mbRGB[i]); |
605 |
|
|
else |
606 |
|
|
copycolor(scan[x], blkcol); |
607 |
|
|
} else if (rg == RG_CORR) |
608 |
greg |
2.1 |
cvtcolor(scan[x], scan[x]); |
609 |
greg |
2.9 |
else if (rg != RG_ORIG) |
610 |
greg |
2.8 |
copycolor(scan[x], blkcol); |
611 |
greg |
2.1 |
} |
612 |
|
|
if (fwritescan(scan, xmax, debugfp) < 0) { |
613 |
|
|
fprintf(stderr, "%s: error writing debugging picture\n", |
614 |
|
|
progname); |
615 |
|
|
exit(1); |
616 |
|
|
} |
617 |
|
|
} |
618 |
greg |
2.7 |
/* clean up */ |
619 |
|
|
fclose(debugfp); |
620 |
|
|
free((char *)scan); |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
|
624 |
|
|
clrdebug() /* put out debug picture from color input */ |
625 |
|
|
{ |
626 |
|
|
static COLR blkclr = BLKCOLR; |
627 |
greg |
2.9 |
COLR mbclr[24], cvclr[24], orclr[24]; |
628 |
greg |
2.7 |
COLR *scan; |
629 |
|
|
COLOR ctmp; |
630 |
greg |
2.9 |
int y, i; |
631 |
|
|
register int x, rg; |
632 |
greg |
2.7 |
/* convert colors */ |
633 |
|
|
for (i = 0; i < 24; i++) { |
634 |
|
|
setcolr(mbclr[i], colval(mbRGB[i],RED), |
635 |
|
|
colval(mbRGB[i],GRN), colval(mbRGB[i],BLU)); |
636 |
|
|
if (inpflags & 1L<<i) { |
637 |
greg |
2.9 |
setcolr(orclr[i], colval(inpRGB[i],RED), |
638 |
|
|
colval(inpRGB[i],GRN), |
639 |
|
|
colval(inpRGB[i],BLU)); |
640 |
greg |
2.7 |
cvtcolor(ctmp, inpRGB[i]); |
641 |
|
|
setcolr(cvclr[i], colval(ctmp,RED), |
642 |
|
|
colval(ctmp,GRN), colval(ctmp,BLU)); |
643 |
|
|
} |
644 |
|
|
} |
645 |
|
|
/* allocate scanline */ |
646 |
|
|
scan = (COLR *)malloc(xmax*sizeof(COLR)); |
647 |
|
|
if (scan == NULL) { |
648 |
|
|
perror(progname); |
649 |
|
|
exit(1); |
650 |
|
|
} |
651 |
|
|
/* finish debug header */ |
652 |
|
|
fputformat(COLRFMT, debugfp); |
653 |
|
|
putc('\n', debugfp); |
654 |
|
|
fprtresolu(xmax, ymax, debugfp); |
655 |
|
|
/* write debug picture */ |
656 |
|
|
for (y = ymax-1; y >= 0; y--) { |
657 |
greg |
2.9 |
for (x = 0; x < xmax; x++) { |
658 |
|
|
rg = chartndx(x, y, &i); |
659 |
|
|
if (rg == RG_CENT) { |
660 |
greg |
2.8 |
if (!(1L<<i & gmtflags) || (x+y)&07) |
661 |
|
|
copycolr(scan[x], mbclr[i]); |
662 |
|
|
else |
663 |
|
|
copycolr(scan[x], blkclr); |
664 |
greg |
2.9 |
} else if (rg == RG_BORD || !(1L<<i & inpflags)) |
665 |
|
|
copycolr(scan[x], blkclr); |
666 |
|
|
else if (rg == RG_ORIG) |
667 |
|
|
copycolr(scan[x], orclr[i]); |
668 |
|
|
else /* rg == RG_CORR */ |
669 |
greg |
2.7 |
copycolr(scan[x], cvclr[i]); |
670 |
greg |
2.9 |
} |
671 |
greg |
2.7 |
if (fwritecolrs(scan, xmax, debugfp) < 0) { |
672 |
|
|
fprintf(stderr, "%s: error writing debugging picture\n", |
673 |
|
|
progname); |
674 |
|
|
exit(1); |
675 |
|
|
} |
676 |
|
|
} |
677 |
|
|
/* clean up */ |
678 |
|
|
fclose(debugfp); |
679 |
greg |
2.1 |
free((char *)scan); |
680 |
|
|
} |