18 |
|
#include "resolu.h" |
19 |
|
#include "pmap.h" |
20 |
|
|
21 |
< |
/* MacBeth colors (CIE 1931, absolute white) */ |
22 |
< |
/* computed from spectral measurements */ |
21 |
> |
/* 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 |
|
float mbxyY[24][3] = { |
49 |
|
{0.462, 0.3769, 0.0932961}, /* DarkSkin */ |
50 |
|
{0.4108, 0.3542, 0.410348}, /* LightSkin */ |
75 |
|
COLOR mbRGB[24]; /* MacBeth RGB values */ |
76 |
|
|
77 |
|
#define NMBNEU 6 /* Number of MacBeth neutral colors */ |
78 |
< |
short mbneu[NMBNEU] = {23,22,21,20,19,18}; |
54 |
< |
#define NMBMOD 3 /* Number of MacBeth moderate colors */ |
55 |
< |
short mbmod[NMBMOD] = {1,3,2}; |
56 |
< |
#define NMBSAT 6 /* Number of MacBeth saturated colors */ |
57 |
< |
short mbsat[NMBSAT] = {14,12,13,15,16,17}; |
78 |
> |
short mbneu[NMBNEU] = {Black,Neutral35,Neutral5,Neutral65,Neutral8,White}; |
79 |
|
|
80 |
+ |
#define NEUFLGS (1L<<White|1L<<Neutral8|1L<<Neutral65| \ |
81 |
+ |
1L<<Neutral5|1L<<Neutral35|1L<<Black) |
82 |
+ |
|
83 |
+ |
#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 |
+ |
|
87 |
+ |
#define UNSFLGS (1L<<DarkSkin|1L<<LightSkin|1L<<BlueSky|1L<<Foliage| \ |
88 |
+ |
1L<<BluishGreen|1L<<ModerateRed) |
89 |
+ |
|
90 |
+ |
#define REQFLGS NEUFLGS /* need these colors */ |
91 |
+ |
#define MODFLGS (NEUFLGS|UNSFLGS) /* should be in gamut */ |
92 |
+ |
|
93 |
+ |
#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 |
+ |
|
98 |
|
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 |
< |
COLOR picRGB[24]; /* picture colors */ |
102 |
> |
COLOR inpRGB[24]; /* measured or scanned input colors */ |
103 |
> |
long inpflags = 0; /* flags of which colors were input */ |
104 |
> |
long gmtflags = 0; /* flags of out-of-gamut colors */ |
105 |
|
|
106 |
< |
double bramp[NMBNEU][2]; /* brightness ramp */ |
106 |
> |
COLOR bramp[NMBNEU][2]; /* brightness ramp (per primary) */ |
107 |
|
double solmat[3][3]; /* color mapping matrix */ |
108 |
|
|
109 |
< |
FILE *debugfp = NULL; |
109 |
> |
FILE *debugfp = NULL; /* debug output picture */ |
110 |
|
char *progname; |
111 |
|
|
112 |
|
extern char *malloc(); |
116 |
|
int argc; |
117 |
|
char **argv; |
118 |
|
{ |
119 |
+ |
int inpispic = 1; |
120 |
|
int i; |
121 |
|
|
122 |
|
progname = argv[0]; |
123 |
< |
if (argc > 2 && !strcmp(argv[1], "-d")) { /* debug output */ |
124 |
< |
if ((debugfp = fopen(argv[2], "w")) == NULL) { |
125 |
< |
perror(argv[2]); |
126 |
< |
exit(1); |
127 |
< |
} |
123 |
> |
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 |
|
#ifdef MSDOS |
134 |
< |
setmode(fileno(debugfp), O_BINARY); |
134 |
> |
setmode(fileno(debugfp), O_BINARY); |
135 |
|
#endif |
136 |
< |
newheader("RADIANCE", debugfp); |
137 |
< |
printargs(argc, argv, debugfp); |
138 |
< |
argv += 2; |
139 |
< |
argc -= 2; |
140 |
< |
} |
141 |
< |
if (argc != 3 && argc != 11) |
142 |
< |
goto userr; |
143 |
< |
if (strcmp(argv[1], "-") && freopen(argv[1], "r", stdin) == NULL) { |
136 |
> |
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 |
|
perror(argv[1]); |
161 |
|
exit(1); |
162 |
|
} |
163 |
< |
if (strcmp(argv[2], "-") && freopen(argv[2], "w", stdout) == NULL) { |
163 |
> |
if (i+1 < argc && freopen(argv[i+1], "w", stdout) == NULL) { |
164 |
|
perror(argv[2]); |
165 |
|
exit(1); |
166 |
|
} |
167 |
+ |
if (inpispic) { /* load input picture header */ |
168 |
|
#ifdef MSDOS |
169 |
< |
setmode(fileno(stdin), O_BINARY); |
169 |
> |
setmode(fileno(stdin), O_BINARY); |
170 |
|
#endif |
171 |
< |
if (checkheader(stdin, COLRFMT, NULL) < 0 || |
172 |
< |
fgetresolu(&xmax, &ymax, stdin) < 0) { |
173 |
< |
fprintf(stderr, "%s: bad input picture\n", progname); |
174 |
< |
exit(1); |
111 |
< |
} |
112 |
< |
/* get chart boundaries */ |
113 |
< |
if (argc == 11) { |
114 |
< |
for (i = 0; i < 4; i++) { |
115 |
< |
if (!isint(argv[2*i+3]) | !isint(argv[2*i+4])) |
116 |
< |
goto userr; |
117 |
< |
bounds[i][0] = atoi(argv[2*i+3]); |
118 |
< |
bounds[i][1] = atoi(argv[2*i+4]); |
171 |
> |
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 { |
176 |
> |
} else { /* else set default xmax and ymax */ |
177 |
> |
xmax = 512; |
178 |
> |
ymax = 2*512/3; |
179 |
> |
} |
180 |
> |
if (inpispic != 2) { /* use default boundaries */ |
181 |
|
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 |
< |
getcolors(); /* get picture colors */ |
187 |
> |
if (inpispic) /* get picture colors */ |
188 |
> |
getpicture(); |
189 |
> |
else |
190 |
> |
getcolors(); |
191 |
> |
compute(); /* compute color mapping */ |
192 |
> |
/* print comment */ |
193 |
> |
printf("{ Color correction file computed by:\n\t"); |
194 |
> |
printargs(argc, argv, stdout); |
195 |
> |
printf("}\n"); |
196 |
|
putmapping(); /* put out color mapping */ |
197 |
< |
putdebug(); /* put out debug picture */ |
197 |
> |
if (debugfp != NULL) /* put out debug picture */ |
198 |
> |
if (inpispic) |
199 |
> |
picdebug(); |
200 |
> |
else |
201 |
> |
clrdebug(); |
202 |
|
exit(0); |
203 |
|
userr: |
204 |
< |
fprintf(stderr, "Usage: %s [-d dbg.pic] input.pic output.cal [xul yul xur yur xll yll xlr ylr]\n", |
204 |
> |
fprintf(stderr, |
205 |
> |
"Usage: %s [-d dbg.pic][-p xul yul xur yur xll yll xlr ylr] input.pic [output.cal]\n", |
206 |
|
progname); |
207 |
+ |
fprintf(stderr, " or: %s [-d dbg.pic] -c [xyY.dat [output.cal]]\n", |
208 |
+ |
progname); |
209 |
|
exit(1); |
210 |
|
} |
211 |
|
|
213 |
|
init() /* initialize */ |
214 |
|
{ |
215 |
|
double quad[4][2]; |
216 |
< |
COLOR ctmp; |
142 |
< |
double d; |
143 |
< |
int i; |
216 |
> |
register int i; |
217 |
|
/* make coordinate transformation */ |
218 |
|
quad[0][0] = bounds[0][0]; |
219 |
|
quad[0][1] = bounds[0][1]; |
229 |
|
exit(1); |
230 |
|
} |
231 |
|
/* map MacBeth colors to RGB space */ |
232 |
< |
for (i = 0; i < 24; i++) { |
233 |
< |
d = mbxyY[i][2] / mbxyY[i][1]; |
161 |
< |
ctmp[0] = mbxyY[i][0] * d; |
162 |
< |
ctmp[1] = mbxyY[i][2]; |
163 |
< |
ctmp[2] = (1. - mbxyY[i][0] - mbxyY[i][1]) * d; |
164 |
< |
cie_rgb(mbRGB[i], ctmp); |
165 |
< |
} |
232 |
> |
for (i = 0; i < 24; i++) |
233 |
> |
xyY2RGB(mbRGB[i], mbxyY[i]); |
234 |
|
} |
235 |
|
|
236 |
|
|
237 |
|
int |
238 |
< |
chartndx(x, y) /* find color number for position */ |
238 |
> |
chartndx(x, y, np) /* find color number for position */ |
239 |
|
int x, y; |
240 |
+ |
int *np; |
241 |
|
{ |
242 |
|
double ipos[3], cpos[3]; |
243 |
|
int ix, iy; |
250 |
|
cpos[0] /= cpos[2]; |
251 |
|
cpos[1] /= cpos[2]; |
252 |
|
if (cpos[0] < 0. || cpos[0] >= 6. || cpos[1] < 0. || cpos[1] >= 4.) |
253 |
< |
return(-1); |
253 |
> |
return(RG_BORD); |
254 |
|
ix = cpos[0]; |
255 |
|
iy = cpos[1]; |
256 |
|
fx = cpos[0] - ix; |
257 |
|
fy = cpos[1] - iy; |
258 |
< |
if (fx < .35 || fx >= .65 || fy < .35 || fy >= .65) |
259 |
< |
return(-1); |
260 |
< |
return(iy*6 + ix); |
258 |
> |
*np = iy*6 + ix; |
259 |
> |
if (fx >= 0.35 && fx < 0.65 && fy >= 0.35 && fy < 0.65) |
260 |
> |
return(RG_CENT); |
261 |
> |
if (fx < 0.05 || fx >= 0.95 || fy < 0.05 || fy >= 0.95) |
262 |
> |
return(RG_BORD); |
263 |
> |
if (fx >= 0.5) /* right side is corrected */ |
264 |
> |
return(RG_CORR); |
265 |
> |
return(RG_ORIG); /* left side is original */ |
266 |
|
} |
267 |
|
|
268 |
|
|
269 |
< |
getcolors() /* load in picture colors */ |
269 |
> |
getpicture() /* load in picture colors */ |
270 |
|
{ |
271 |
|
COLR *scanln; |
272 |
|
COLOR pval; |
273 |
|
int ccount[24]; |
274 |
|
double d; |
275 |
< |
int y; |
276 |
< |
register int x, i; |
275 |
> |
int y, i; |
276 |
> |
register int x; |
277 |
|
|
278 |
|
scanln = (COLR *)malloc(xmax*sizeof(COLR)); |
279 |
|
if (scanln == NULL) { |
281 |
|
exit(1); |
282 |
|
} |
283 |
|
for (i = 0; i < 24; i++) { |
284 |
< |
setcolor(picRGB[i], 0., 0., 0.); |
284 |
> |
setcolor(inpRGB[i], 0., 0., 0.); |
285 |
|
ccount[i] = 0; |
286 |
|
} |
287 |
|
for (y = ymax-1; y >= 0; y--) { |
290 |
|
progname); |
291 |
|
exit(1); |
292 |
|
} |
293 |
< |
for (x = 0; x < xmax; x++) { |
294 |
< |
i = chartndx(x, y); |
221 |
< |
if (i >= 0) { |
293 |
> |
for (x = 0; x < xmax; x++) |
294 |
> |
if (chartndx(x, y, &i) == RG_CENT) { |
295 |
|
colr_color(pval, scanln[x]); |
296 |
< |
addcolor(picRGB[i], pval); |
296 |
> |
addcolor(inpRGB[i], pval); |
297 |
|
ccount[i]++; |
298 |
|
} |
299 |
+ |
} |
300 |
+ |
for (i = 0; i < 24; i++) { /* compute averages */ |
301 |
+ |
if (ccount[i] == 0) |
302 |
+ |
continue; |
303 |
+ |
d = 1./ccount[i]; |
304 |
+ |
scalecolor(inpRGB[i], d); |
305 |
+ |
inpflags |= 1L<<i; |
306 |
+ |
} |
307 |
+ |
free((char *)scanln); |
308 |
+ |
} |
309 |
+ |
|
310 |
+ |
|
311 |
+ |
getcolors() /* get xyY colors from standard input */ |
312 |
+ |
{ |
313 |
+ |
int gotwhite = 0; |
314 |
+ |
COLOR whiteclr; |
315 |
+ |
int n; |
316 |
+ |
float xyYin[3]; |
317 |
+ |
|
318 |
+ |
while (fgetval(stdin, 'i', &n) == 1) { /* read colors */ |
319 |
+ |
if (n < 0 | n > 24 || |
320 |
+ |
fgetval(stdin, 'f', &xyYin[0]) != 1 || |
321 |
+ |
fgetval(stdin, 'f', &xyYin[1]) != 1 || |
322 |
+ |
fgetval(stdin, 'f', &xyYin[2]) != 1 || |
323 |
+ |
xyYin[0] < 0. | xyYin[0] > 1. | |
324 |
+ |
xyYin[1] < 0. | xyYin[1] > 1.) { |
325 |
+ |
fprintf(stderr, "%s: bad color input data\n", |
326 |
+ |
progname); |
327 |
+ |
exit(1); |
328 |
|
} |
329 |
+ |
if (n == 0) { /* calibration white */ |
330 |
+ |
xyY2RGB(whiteclr, xyYin); |
331 |
+ |
gotwhite++; |
332 |
+ |
} else { /* standard color */ |
333 |
+ |
n--; |
334 |
+ |
xyY2RGB(inpRGB[n], xyYin); |
335 |
+ |
inpflags |= 1L<<n; |
336 |
+ |
} |
337 |
|
} |
338 |
< |
for (i = 0; i < 24; i++) { |
339 |
< |
if (ccount[i] == 0) { |
340 |
< |
fprintf(stderr, "%s: bad chart boundaries\n", |
338 |
> |
/* normalize colors */ |
339 |
> |
if (!gotwhite) { |
340 |
> |
if (!(inpflags & 1L<<White)) { |
341 |
> |
fprintf(stderr, "%s: missing input for White\n", |
342 |
|
progname); |
343 |
|
exit(1); |
344 |
|
} |
345 |
< |
d = 1.0/ccount[i]; |
346 |
< |
scalecolor(picRGB[i], d); |
345 |
> |
setcolor(whiteclr, |
346 |
> |
colval(inpRGB[White],RED)/colval(mbRGB[White],RED), |
347 |
> |
colval(inpRGB[White],GRN)/colval(mbRGB[White],GRN), |
348 |
> |
colval(inpRGB[White],BLU)/colval(mbRGB[White],BLU)); |
349 |
|
} |
350 |
< |
free((char *)scanln); |
350 |
> |
for (n = 0; n < 24; n++) |
351 |
> |
if (inpflags & 1L<<n) |
352 |
> |
setcolor(inpRGB[n], |
353 |
> |
colval(inpRGB[n],RED)/colval(whiteclr,RED), |
354 |
> |
colval(inpRGB[n],GRN)/colval(whiteclr,GRN), |
355 |
> |
colval(inpRGB[n],BLU)/colval(whiteclr,BLU)); |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
< |
double |
360 |
< |
bresp(x) /* piecewise linear interpolation of brightness */ |
243 |
< |
double x; |
359 |
> |
bresp(y, x) /* piecewise linear interpolation of primaries */ |
360 |
> |
COLOR y, x; |
361 |
|
{ |
362 |
< |
register int n = NMBNEU; |
362 |
> |
register int i, n; |
363 |
|
|
364 |
< |
while (n > 0 && x < bramp[--n][0]) |
365 |
< |
; |
366 |
< |
return( ((bramp[n+1][0] - x)*bramp[n][1] + |
367 |
< |
(x - bramp[n][0])*bramp[n+1][1]) / |
368 |
< |
(bramp[n+1][0] - bramp[n][0]) ); |
364 |
> |
for (i = 0; i < 3; i++) { |
365 |
> |
for (n = 0; n < NMBNEU-2; n++) |
366 |
> |
if (colval(x,i) < colval(bramp[n+1][0],i)) |
367 |
> |
break; |
368 |
> |
colval(y,i) = ((colval(bramp[n+1][0],i) - colval(x,i)) * |
369 |
> |
colval(bramp[n][1],i) + |
370 |
> |
(colval(x,i) - colval(bramp[n][0],i)) * |
371 |
> |
colval(bramp[n+1][1],i)) / |
372 |
> |
(colval(bramp[n+1][0],i) - colval(bramp[n][0],i)); |
373 |
> |
} |
374 |
|
} |
375 |
|
|
376 |
|
|
377 |
< |
putmapping() /* compute and print mapping for pcomb -f */ |
377 |
> |
compute() /* compute color mapping */ |
378 |
|
{ |
379 |
< |
float clrin[NMBMOD][3], clrout[NMBMOD][3]; |
380 |
< |
register int i, j; |
379 |
> |
COLOR clrin[24], clrout[24]; |
380 |
> |
long cflags; |
381 |
> |
COLOR ctmp; |
382 |
> |
register int i, j, n; |
383 |
> |
/* did we get what we need? */ |
384 |
> |
if ((inpflags & REQFLGS) != REQFLGS) { |
385 |
> |
fprintf(stderr, "%s: missing required input colors\n", |
386 |
> |
progname); |
387 |
> |
exit(1); |
388 |
> |
} |
389 |
|
/* compute piecewise luminance curve */ |
390 |
|
for (i = 0; i < NMBNEU; i++) { |
391 |
< |
bramp[i][0] = bright(picRGB[mbneu[i]]); |
392 |
< |
bramp[i][1] = bright(mbRGB[mbneu[i]]); |
391 |
> |
copycolor(bramp[i][0], inpRGB[mbneu[i]]); |
392 |
> |
copycolor(bramp[i][1], mbRGB[mbneu[i]]); |
393 |
|
} |
394 |
< |
/* compute color matrix */ |
395 |
< |
for (i = 0; i < NMBMOD; i++) |
396 |
< |
for (j = 0; j < 3; j++) { |
397 |
< |
clrin[i][j] = bresp(picRGB[mbmod[i]][j]); |
398 |
< |
clrout[i][j] = mbRGB[mbmod[i]][j]; |
399 |
< |
} |
400 |
< |
compsoln(clrin, clrout, NMBMOD); |
394 |
> |
/* compute color mapping */ |
395 |
> |
do { |
396 |
> |
cflags = inpflags & ~gmtflags; |
397 |
> |
n = 0; /* compute transform matrix */ |
398 |
> |
for (i = 0; i < 24; i++) |
399 |
> |
if (cflags & 1L<<i) { |
400 |
> |
bresp(clrin[n], inpRGB[i]); |
401 |
> |
copycolor(clrout[n], mbRGB[i]); |
402 |
> |
n++; |
403 |
> |
} |
404 |
> |
compsoln(clrin, clrout, n); |
405 |
> |
/* check out-of-gamut colors */ |
406 |
> |
for (i = 0; i < 24; i++) |
407 |
> |
if (cflags & 1L<<i) { |
408 |
> |
cresp(ctmp, mbRGB[i]); |
409 |
> |
for (j = 0; j < 3; j++) |
410 |
> |
if (colval(ctmp,j) <= 0. || |
411 |
> |
colval(ctmp,j) >= 1.) { |
412 |
> |
gmtflags |= 1L<<i; |
413 |
> |
break; |
414 |
> |
} |
415 |
> |
} |
416 |
> |
} while (cflags & gmtflags); |
417 |
> |
if (gmtflags & MODFLGS) |
418 |
> |
fprintf(stderr, |
419 |
> |
"%s: warning - some moderate colors are out of gamut\n", |
420 |
> |
progname); |
421 |
> |
} |
422 |
> |
|
423 |
> |
|
424 |
> |
putmapping() /* put out color mapping for pcomb -f */ |
425 |
> |
{ |
426 |
> |
static char cchar[3] = {'r', 'g', 'b'}; |
427 |
> |
register int i, j; |
428 |
|
/* print brightness mapping */ |
429 |
< |
printf("xval(i) : select(i, 0"); |
430 |
< |
for (i = 0; i < NMBNEU; i++) |
431 |
< |
printf(", %g", bramp[i][0]); |
432 |
< |
printf(");\n"); |
433 |
< |
printf("yval(i) : select(i, 0"); |
434 |
< |
for (i = 0; i < NMBNEU; i++) |
435 |
< |
printf(", %g", bramp[i][1]); |
436 |
< |
printf(");\n"); |
437 |
< |
printf("ifind(x,f,n) : if(1.5-n, 1, if(x-f(n), n, ifind(x,f,n-1)));\n"); |
438 |
< |
printf("binterp(i,x) : ((xval(i+1)-x)*yval(i) + (x-xval(i))*yval(i+1))/\n"); |
439 |
< |
printf("\t\t(xval(i+1) - xval(i));\n"); |
440 |
< |
printf("bresp(x) : binterp(ifind(x,xval,xval(0)-1), x);\n"); |
441 |
< |
printf("nred = bresp(ri(1));\n"); |
442 |
< |
printf("ngrn = bresp(gi(1));\n"); |
443 |
< |
printf("nblu = bresp(bi(1));\n"); |
429 |
> |
for (j = 0; j < 3; j++) { |
430 |
> |
printf("%cxa(i) : select(i", cchar[j]); |
431 |
> |
for (i = 0; i < NMBNEU; i++) |
432 |
> |
printf(",%g", colval(bramp[i][0],j)); |
433 |
> |
printf(");\n"); |
434 |
> |
printf("%cya(i) : select(i", cchar[j]); |
435 |
> |
for (i = 0; i < NMBNEU; i++) |
436 |
> |
printf(",%g", colval(bramp[i][1],j)); |
437 |
> |
printf(");\n"); |
438 |
> |
printf("%c = %ci(1);\n", cchar[j], cchar[j]); |
439 |
> |
printf("%cfi(n) = if(n-%g, %d, if(%cxa(n+1)-%c, n, %cfi(n+1)));\n", |
440 |
> |
cchar[j], NMBNEU-1.5, NMBNEU-1, cchar[j], |
441 |
> |
cchar[j], cchar[j]); |
442 |
> |
printf("%cndx = %cfi(1);\n", cchar[j], cchar[j]); |
443 |
> |
printf("%cn = ((%cxa(%cndx+1)-%c)*%cya(%cndx) + ", |
444 |
> |
cchar[j], cchar[j], cchar[j], |
445 |
> |
cchar[j], cchar[j], cchar[j]); |
446 |
> |
printf("(%c-%cxa(%cndx))*%cya(%cndx+1)) /\n", |
447 |
> |
cchar[j], cchar[j], cchar[j], |
448 |
> |
cchar[j], cchar[j]); |
449 |
> |
printf("\t\t(%cxa(%cndx+1) - %cxa(%cndx)) ;\n", |
450 |
> |
cchar[j], cchar[j], cchar[j], cchar[j]); |
451 |
> |
} |
452 |
|
/* print color mapping */ |
453 |
< |
printf("ro = %g*nred + %g*ngrn + %g*nblu\n", |
454 |
< |
solmat[0][0], solmat[1][0], solmat[2][0]); |
455 |
< |
printf("go = %g*nred + %g*ngrn + %g*nblu\n", |
456 |
< |
solmat[0][1], solmat[1][1], solmat[2][1]); |
457 |
< |
printf("bo = %g*nred + %g*ngrn + %g*nblu\n", |
458 |
< |
solmat[0][2], solmat[1][2], solmat[2][2]); |
453 |
> |
printf("ro = %g*rn + %g*gn + %g*bn ;\n", |
454 |
> |
solmat[0][0], solmat[0][1], solmat[0][2]); |
455 |
> |
printf("go = %g*rn + %g*gn + %g*bn ;\n", |
456 |
> |
solmat[1][0], solmat[1][1], solmat[1][2]); |
457 |
> |
printf("bo = %g*rn + %g*gn + %g*bn ;\n", |
458 |
> |
solmat[2][0], solmat[2][1], solmat[2][2]); |
459 |
|
} |
460 |
|
|
461 |
|
|
462 |
< |
compsoln(cin, cout, n) /* solve 3x3 system */ |
463 |
< |
float cin[][3], cout[][3]; |
462 |
> |
compsoln(cin, cout, n) /* solve 3xN system using least-squares */ |
463 |
> |
COLOR cin[], cout[]; |
464 |
|
int n; |
465 |
|
{ |
466 |
|
extern double mx3d_adjoint(), fabs(); |
467 |
|
double mat[3][3], invmat[3][3]; |
468 |
|
double det; |
469 |
|
double colv[3], rowv[3]; |
470 |
< |
register int i, j; |
470 |
> |
register int i, j, k; |
471 |
|
|
472 |
< |
if (n != 3) { |
473 |
< |
fprintf(stderr, "%s: inconsistent code!\n", progname); |
472 |
> |
if (n < 3) { |
473 |
> |
fprintf(stderr, "%s: too few colors to match!\n", progname); |
474 |
|
exit(1); |
475 |
|
} |
476 |
< |
for (i = 0; i < 3; i++) |
477 |
< |
for (j = 0; j < 3; j++) |
478 |
< |
mat[i][j] = cin[j][i]; |
476 |
> |
if (n == 3) |
477 |
> |
for (i = 0; i < 3; i++) |
478 |
> |
for (j = 0; j < 3; j++) |
479 |
> |
mat[i][j] = colval(cin[j],i); |
480 |
> |
else { /* compute A^t A */ |
481 |
> |
for (i = 0; i < 3; i++) |
482 |
> |
for (j = i; j < 3; j++) { |
483 |
> |
mat[i][j] = 0.; |
484 |
> |
for (k = 0; k < n; k++) |
485 |
> |
mat[i][j] += colval(cin[k],i) * |
486 |
> |
colval(cin[k],j); |
487 |
> |
} |
488 |
> |
for (i = 1; i < 3; i++) /* using symmetry */ |
489 |
> |
for (j = 0; j < i; j++) |
490 |
> |
mat[i][j] = mat[j][i]; |
491 |
> |
} |
492 |
|
det = mx3d_adjoint(mat, invmat); |
493 |
|
if (fabs(det) < 1e-4) { |
494 |
|
fprintf(stderr, "%s: cannot compute color mapping\n", |
502 |
|
for (j = 0; j < 3; j++) |
503 |
|
invmat[i][j] /= det; |
504 |
|
for (i = 0; i < 3; i++) { |
505 |
+ |
if (n == 3) |
506 |
+ |
for (j = 0; j < 3; j++) |
507 |
+ |
colv[j] = colval(cout[j],i); |
508 |
+ |
else |
509 |
+ |
for (j = 0; j < 3; j++) { |
510 |
+ |
colv[j] = 0.; |
511 |
+ |
for (k = 0; k < n; k++) |
512 |
+ |
colv[j] += colval(cout[k],i) * |
513 |
+ |
colval(cin[k],j); |
514 |
+ |
} |
515 |
+ |
mx3d_transform(colv, invmat, rowv); |
516 |
|
for (j = 0; j < 3; j++) |
517 |
< |
rowv[j] = cout[j][i]; |
329 |
< |
mx3d_transform(rowv, invmat, colv); |
330 |
< |
for (j = 0; j < 3; j++) |
331 |
< |
solmat[j][i] = colv[j]; |
517 |
> |
solmat[i][j] = rowv[j]; |
518 |
|
} |
519 |
|
} |
520 |
|
|
522 |
|
cvtcolor(cout, cin) /* convert color according to our mapping */ |
523 |
|
COLOR cout, cin; |
524 |
|
{ |
525 |
+ |
COLOR ctmp; |
526 |
+ |
|
527 |
+ |
bresp(ctmp, cin); |
528 |
+ |
cresp(cout, ctmp); |
529 |
+ |
if (colval(cout,RED) < 0.) |
530 |
+ |
colval(cout,RED) = 0.; |
531 |
+ |
if (colval(cout,GRN) < 0.) |
532 |
+ |
colval(cout,GRN) = 0.; |
533 |
+ |
if (colval(cout,BLU) < 0.) |
534 |
+ |
colval(cout,BLU) = 0.; |
535 |
+ |
} |
536 |
+ |
|
537 |
+ |
|
538 |
+ |
cresp(cout, cin) /* transform color according to matrix */ |
539 |
+ |
COLOR cout, cin; |
540 |
+ |
{ |
541 |
|
double r, g, b; |
340 |
– |
double r1, g1, b1; |
542 |
|
|
543 |
< |
r = bresp(colval(cin,RED)); |
544 |
< |
g = bresp(colval(cin,GRN)); |
545 |
< |
b = bresp(colval(cin,BLU)); |
546 |
< |
r1 = r*solmat[0][0] + g*solmat[1][0] + b*solmat[2][0]; |
547 |
< |
if (r1 < 0) r1 = 0; |
548 |
< |
g1 = r*solmat[0][1] + g*solmat[1][1] + b*solmat[2][1]; |
549 |
< |
if (g1 < 0) g1 = 0; |
349 |
< |
b1 = r*solmat[0][2] + g*solmat[1][2] + b*solmat[2][2]; |
350 |
< |
if (b1 < 0) b1 = 0; |
351 |
< |
setcolor(cout, r1, g1, b1); |
543 |
> |
r = colval(cin,0)*solmat[0][0] + colval(cin,1)*solmat[0][1] |
544 |
> |
+ colval(cin,2)*solmat[0][2]; |
545 |
> |
g = colval(cin,0)*solmat[1][0] + colval(cin,1)*solmat[1][1] |
546 |
> |
+ colval(cin,2)*solmat[1][2]; |
547 |
> |
b = colval(cin,0)*solmat[2][0] + colval(cin,1)*solmat[2][1] |
548 |
> |
+ colval(cin,2)*solmat[2][2]; |
549 |
> |
setcolor(cout, r, g, b); |
550 |
|
} |
551 |
|
|
552 |
|
|
553 |
< |
putdebug() /* put out debugging picture */ |
553 |
> |
xyY2RGB(rgbout, xyYin) /* convert xyY to RGB */ |
554 |
> |
COLOR rgbout; |
555 |
> |
register float xyYin[3]; |
556 |
|
{ |
557 |
+ |
COLOR ctmp; |
558 |
+ |
double d; |
559 |
+ |
|
560 |
+ |
d = xyYin[2] / xyYin[1]; |
561 |
+ |
ctmp[0] = xyYin[0] * d; |
562 |
+ |
ctmp[1] = xyYin[2]; |
563 |
+ |
ctmp[2] = (1. - xyYin[0] - xyYin[1]) * d; |
564 |
+ |
cie_rgb(rgbout, ctmp); |
565 |
+ |
} |
566 |
+ |
|
567 |
+ |
|
568 |
+ |
picdebug() /* put out debugging picture */ |
569 |
+ |
{ |
570 |
+ |
static COLOR blkcol = BLKCOLOR; |
571 |
|
COLOR *scan; |
572 |
< |
int y; |
573 |
< |
register int x, i; |
572 |
> |
int y, i; |
573 |
> |
register int x, rg; |
574 |
|
|
361 |
– |
if (debugfp == NULL) |
362 |
– |
return; |
575 |
|
if (fseek(stdin, 0L, 0) == EOF) { |
576 |
|
fprintf(stderr, "%s: cannot seek on input picture\n", progname); |
577 |
|
exit(1); |
585 |
|
exit(1); |
586 |
|
} |
587 |
|
/* finish debug header */ |
588 |
+ |
fputformat(COLRFMT, debugfp); |
589 |
|
putc('\n', debugfp); |
590 |
|
fprtresolu(xmax, ymax, debugfp); |
591 |
+ |
/* write debug picture */ |
592 |
|
for (y = ymax-1; y >= 0; y--) { |
593 |
|
if (freadscan(scan, xmax, stdin) < 0) { |
594 |
|
fprintf(stderr, "%s: error rereading input picture\n", |
596 |
|
exit(1); |
597 |
|
} |
598 |
|
for (x = 0; x < xmax; x++) { |
599 |
< |
i = chartndx(x, y); |
600 |
< |
if (i < 0) |
599 |
> |
rg = chartndx(x, y, &i); |
600 |
> |
if (rg == RG_CENT) { |
601 |
> |
if (!(1L<<i & gmtflags) || (x+y)&07) |
602 |
> |
copycolor(scan[x], mbRGB[i]); |
603 |
> |
else |
604 |
> |
copycolor(scan[x], blkcol); |
605 |
> |
} else if (rg == RG_CORR) |
606 |
|
cvtcolor(scan[x], scan[x]); |
607 |
< |
else |
608 |
< |
copycolor(scan[x], mbRGB[i]); |
607 |
> |
else if (rg != RG_ORIG) |
608 |
> |
copycolor(scan[x], blkcol); |
609 |
|
} |
610 |
|
if (fwritescan(scan, xmax, debugfp) < 0) { |
611 |
|
fprintf(stderr, "%s: error writing debugging picture\n", |
613 |
|
exit(1); |
614 |
|
} |
615 |
|
} |
616 |
+ |
/* clean up */ |
617 |
+ |
fclose(debugfp); |
618 |
+ |
free((char *)scan); |
619 |
+ |
} |
620 |
+ |
|
621 |
+ |
|
622 |
+ |
clrdebug() /* put out debug picture from color input */ |
623 |
+ |
{ |
624 |
+ |
static COLR blkclr = BLKCOLR; |
625 |
+ |
COLR mbclr[24], cvclr[24], orclr[24]; |
626 |
+ |
COLR *scan; |
627 |
+ |
COLOR ctmp; |
628 |
+ |
int y, i; |
629 |
+ |
register int x, rg; |
630 |
+ |
/* convert colors */ |
631 |
+ |
for (i = 0; i < 24; i++) { |
632 |
+ |
setcolr(mbclr[i], colval(mbRGB[i],RED), |
633 |
+ |
colval(mbRGB[i],GRN), colval(mbRGB[i],BLU)); |
634 |
+ |
if (inpflags & 1L<<i) { |
635 |
+ |
setcolr(orclr[i], colval(inpRGB[i],RED), |
636 |
+ |
colval(inpRGB[i],GRN), |
637 |
+ |
colval(inpRGB[i],BLU)); |
638 |
+ |
cvtcolor(ctmp, inpRGB[i]); |
639 |
+ |
setcolr(cvclr[i], colval(ctmp,RED), |
640 |
+ |
colval(ctmp,GRN), colval(ctmp,BLU)); |
641 |
+ |
} |
642 |
+ |
} |
643 |
+ |
/* allocate scanline */ |
644 |
+ |
scan = (COLR *)malloc(xmax*sizeof(COLR)); |
645 |
+ |
if (scan == NULL) { |
646 |
+ |
perror(progname); |
647 |
+ |
exit(1); |
648 |
+ |
} |
649 |
+ |
/* finish debug header */ |
650 |
+ |
fputformat(COLRFMT, debugfp); |
651 |
+ |
putc('\n', debugfp); |
652 |
+ |
fprtresolu(xmax, ymax, debugfp); |
653 |
+ |
/* write debug picture */ |
654 |
+ |
for (y = ymax-1; y >= 0; y--) { |
655 |
+ |
for (x = 0; x < xmax; x++) { |
656 |
+ |
rg = chartndx(x, y, &i); |
657 |
+ |
if (rg == RG_CENT) { |
658 |
+ |
if (!(1L<<i & gmtflags) || (x+y)&07) |
659 |
+ |
copycolr(scan[x], mbclr[i]); |
660 |
+ |
else |
661 |
+ |
copycolr(scan[x], blkclr); |
662 |
+ |
} else if (rg == RG_BORD || !(1L<<i & inpflags)) |
663 |
+ |
copycolr(scan[x], blkclr); |
664 |
+ |
else if (rg == RG_ORIG) |
665 |
+ |
copycolr(scan[x], orclr[i]); |
666 |
+ |
else /* rg == RG_CORR */ |
667 |
+ |
copycolr(scan[x], cvclr[i]); |
668 |
+ |
} |
669 |
+ |
if (fwritecolrs(scan, xmax, debugfp) < 0) { |
670 |
+ |
fprintf(stderr, "%s: error writing debugging picture\n", |
671 |
+ |
progname); |
672 |
+ |
exit(1); |
673 |
+ |
} |
674 |
+ |
} |
675 |
+ |
/* clean up */ |
676 |
+ |
fclose(debugfp); |
677 |
|
free((char *)scan); |
678 |
|
} |