Debian Codegraph @

icc/mcheck.c

raw symbols SCIP
1
2 /*
3 * International Color Consortium Format Library (icclib)
4 * Check the device chanel to PCS monotonicity.
5 *
6 * Author: Graeme W. Gill
7 * Date: 2000/12/11
8 * Version: 2.15
9 *
10 * Copyright 2000 - 2012 Graeme W. Gill
11 *
12 * This material is licensed with an "MIT" free use license:-
13 * see the License4.txt file in this directory for licensing details.
14 */
15
16 /* TTBD:
17 *
18 * Make general device input, not just CMYK
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <stdarg.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <math.h>
27 #include "vrml.h"
28 #include "icc.h"
29
30 void error(char *fmt, ...), warning(char *fmt, ...);
31
32 void usage(void) {
33 fprintf(stderr,"Check device to PCS monotonicity of a CMYK ICC file, V%s\n",ICCLIB_VERSION_STR);
34 fprintf(stderr,"Author: Graeme W. Gill\n");
35 fprintf(stderr,"usage: mcheck [-v] [-w] infile\n");
36 fprintf(stderr," -v verbose\n");
37 fprintf(stderr," -c Check just Cyan monotonicity\n");
38 fprintf(stderr," -m Check just Magenta monotonicity\n");
39 fprintf(stderr," -y Check just Yellow monotonicity\n");
40 fprintf(stderr," -k Check just Black monotonicity\n");
41 fprintf(stderr," -w create %s visualisation\n",vrml_format());
42 fprintf(stderr," -x use %s axes\n",vrml_format());
43 exit(1);
44 }
45
46 #define MGR 50 /* Maximum grid resolution handled */
47
48 int
49 main(
50 int argc,
51 char *argv[]
52 ) {
53 int fa,nfa; /* argument we're looking at */
54 icmErr e = { 0, { '\000'} };
55 int verb = 0;
56 int cchan = -1; /* default all */
57 int dovrml = 0;
58 int doaxes = 0;
59 static char in_name[MAXNAMEL+1];
60 static char out_name[MAXNAMEL+1], *xl;
61 icmFile *rd_fp;
62 icc *wr_icco, *rd_icco; /* Keep object separate */
63 int rv = 0;
64
65 /* Check variables */
66 icmLuBase *luo;
67 icmLuLut *luluto; /* Lookup xLut type object */
68 int gres; /* Grid resolution */
69 icColorSpaceSignature ins, outs; /* Type of input and output spaces */
70 int inn; /* Number of input chanels */
71 icmLuAlgType alg;
72 vrml *wrl;
73 int dx[4]; /* Device index mapping */
74 int chan, cs, ce;
75
76 if (argc < 2)
77 usage();
78
79 /* Process the arguments */
80 for(fa = 1;fa < argc;fa++) {
81 nfa = fa; /* skip to nfa if next argument is used */
82 if (argv[fa][0] == '-') { /* Look for any flags */
83 char *na = NULL; /* next argument after flag, null if none */
84
85 if (argv[fa][2] != '\000')
86 na = &argv[fa][2]; /* next is directly after flag */
87 else {
88 if ((fa+1) < argc) {
89 if (argv[fa+1][0] != '-') {
90 nfa = fa + 1;
91 na = argv[nfa]; /* next is seperate non-flag argument */
92 }
93 }
94 }
95
96 /* Verbosity */
97 if (argv[fa][1] == 'v' || argv[fa][1] == 'V') {
98 verb = 1;
99 }
100 /* VRML/X3D */
101 else if (argv[fa][1] == 'w' || argv[fa][1] == 'W') {
102 dovrml = 1;
103 }
104 /* Cyan */
105 else if (argv[fa][1] == 'c' || argv[fa][1] == 'C') {
106 cchan = 0;
107 }
108 /* Magenta */
109 else if (argv[fa][1] == 'm' || argv[fa][1] == 'M') {
110 cchan = 1;
111 }
112 /* Yellow */
113 else if (argv[fa][1] == 'y' || argv[fa][1] == 'Y') {
114 cchan = 2;
115 }
116 /* Black */
117 else if (argv[fa][1] == 'k' || argv[fa][1] == 'K') {
118 cchan = 3;
119 }
120 else if (argv[fa][1] == 'x') {
121 doaxes = 1;
122 }
123 else if (argv[fa][1] == '?')
124 usage();
125 else
126 usage();
127 }
128 else
129 break;
130 }
131
132 if (fa >= argc || argv[fa][0] == '-') usage();
133 strncpy(in_name,argv[fa++],MAXNAMEL); in_name[MAXNAMEL] = '\000';
134
135 strcpy(out_name, in_name);
136 if ((xl = strrchr(out_name, '.')) == NULL) /* Figure where extention is */
137 xl = out_name + strlen(out_name);
138 xl[0] = '\000'; /* Remove extension */
139
140 icm_err_clear_e(&e);
141
142 /* Open up the file for reading */
143 if ((rd_fp = new_icmFileStd_name(&e, in_name,"r")) == NULL)
144 error ("Read: Can't open file '%s', failed with 0x%x, '%s'",in_name, e.c, e.m);
145
146 if ((rd_icco = new_icc(&e)) == NULL)
147 error ("Read: Creation of ICC object failed with 0x%x, '%s'", e.c, e.m);
148
149 /* Read the header and tag list */
150 if ((rv = rd_icco->read(rd_icco,rd_fp,0)) != 0)
151 error ("Read: %d, %s",rv,rd_icco->e.m);
152
153 /* Get a Device to PCS conversion object */
154 if ((luo = rd_icco->get_luobj(rd_icco, icmFwd, icRelativeColorimetric, icSigLabData, icmLuOrdNorm)) == NULL) {
155 if ((luo = rd_icco->get_luobj(rd_icco, icmFwd, icmDefaultIntent, icSigLabData, icmLuOrdNorm)) == NULL)
156 error ("%d, %s",rd_icco->e.c, rd_icco->e.m);
157 }
158 /* Get details of conversion */
159 luo->spaces(luo, &ins, &inn, &outs, NULL, &alg, NULL, NULL, NULL, NULL);
160
161 if (alg != icmLutType) {
162 error("Expecting Lut based profile");
163 }
164
165 if (ins != icSigCmykData) {
166 error("Expecting CMYK device");
167 }
168
169 if (outs != icSigLabData) {
170 error("Expecting Lab PCS");
171 }
172
173 luluto = (icmLuLut *)luo; /* Lookup xLut type object */
174
175 gres = luluto->lut->clutPoints;
176 if (gres > MGR) {
177 error("Can't handle grid resolution greater than %d\n",MGR);
178 }
179
180 if (dovrml) {
181 wrl = new_vrml(out_name, doaxes, vrml_lab);
182 if (wrl == NULL)
183 error("new_vrml for '%s%s' failed",out_name,vrml_ext());
184 wrl->start_line_set(wrl, 0);
185 }
186
187 /* For all the device chanels chosen */
188 if (cchan < 0) {
189 cs = 0;
190 ce = inn;
191 } else {
192 cs = cchan;
193 ce = cs + 1;
194 }
195 for (chan = cs; chan < ce; chan++) {
196
197 /* Check the monotonicity of the output for a given device input */
198 int co[4];
199 if (chan == 0) {
200 dx[0] = 1;
201 dx[1] = 2;
202 dx[2] = 3;
203 dx[3] = 0; /* Cyan is variable */
204 } else if (chan == 1) {
205 dx[0] = 0;
206 dx[1] = 2;
207 dx[2] = 3;
208 dx[3] = 1; /* Magenta is variable */
209 } else if (chan == 2) {
210 dx[0] = 0;
211 dx[1] = 1;
212 dx[2] = 3;
213 dx[3] = 2; /* Yellow is variable */
214 } else if (chan == 3) {
215 dx[0] = 0;
216 dx[1] = 1;
217 dx[2] = 2;
218 dx[3] = 3; /* Black is variable */
219 }
220
221 /* Itterate throught the CMY clut grid points */
222 for (co[0] = 0; co[0] < gres; co[0]++) {
223 for (co[1] = 0; co[1] < gres; co[1]++) {
224 for (co[2] = 0; co[2] < gres; co[2]++) {
225 int j, k, ck, nm;
226 double dev[MGR][4];
227 double pcs[MGR][3];
228 double apcs[3], ss;
229
230 /* Run up the variable axis */
231 for (ck = 0; ck < gres; ck++) {
232
233 dev[ck][dx[0]] = co[0]/(gres-1.0);
234 dev[ck][dx[1]] = co[1]/(gres-1.0);
235 dev[ck][dx[2]] = co[2]/(gres-1.0);
236 dev[ck][dx[3]] = ck/(gres-1.0);
237
238 /* Device to PCS */
239 if ((rv = luluto->clut(luluto, pcs[ck], dev[ck])) > 1)
240 error ("%d, %s",rd_icco->e.c,rd_icco->e.m);
241
242 // if (dovrml)
243 // wrl->add_vertex(wrl, 0, pcs[ck]);
244 }
245
246 /* Compute average vector direction */
247 for (ss = 0.0, k = 0; k < 3; k++) {
248 double tt;
249 tt = pcs[gres-1][k] - pcs[0][k];
250 ss += tt * tt;
251 apcs[k] = tt;
252 }
253 for (k = 0; k < 3; k++)
254 apcs[k] /= ss;
255
256 /* Now compute the dot product for each vector, */
257 /* and check for reversals. */
258 j = 0;
259 //printf("Checking CMYK %f %f %f %f Lab %f %f %f\n",
260 // dev[j][0], dev[j][1], dev[j][2], dev[j][3],
261 // pcs[j][0], pcs[j][1], pcs[j][2]);
262 for (nm = 0, j = 1; j < gres; j++) {
263 for (ss = 0.0, k = 0; k < 3; k++) /* Dot product */
264 ss += (pcs[j][k] - pcs[j-1][k]) * apcs[k];
265
266 //printf("Checking %f CMYK %f %f %f %f Lab %f %f %f\n",
267 // ss, dev[j][0], dev[j][1], dev[j][2], dev[j][3],
268 // pcs[j][0], pcs[j][1], pcs[j][2]);
269
270 if (ss <= 0.0) {
271 nm = 1;
272 printf("NonMon %f at CMYK %f %f %f %f Lab %f %f %f\n",
273 ss, dev[j][0], dev[j][1], dev[j][2], dev[j][3],
274 pcs[j][0], pcs[j][1], pcs[j][2]);
275 }
276 }
277 //printf("\n");
278
279 /* Display just the non mono threads */
280 if (nm && dovrml) {
281 for (j = 0; j < gres; j++)
282 wrl->add_vertex(wrl, 0, pcs[j]);
283 }
284 if (verb) {
285 printf("."); fflush(stdout);
286 }
287 }
288 }
289 }
290 }
291
292 if (dovrml) {
293 wrl->make_lines(wrl, 0, gres);
294 wrl->del(wrl);
295 }
296
297 /* Done with lookup object */
298 luo->del(luo);
299
300 rd_icco->del(rd_icco);
301 rd_fp->del(rd_fp);
302
303 return 0;
304 }
305
306 /* ------------------------------------------------ */
307 /* Basic printf type error() and warning() routines */
308
309 void
310 error(char *fmt, ...)
311 {
312 va_list args;
313
314 fprintf(stderr,"icctest: Error - ");
315 va_start(args, fmt);
316 vfprintf(stderr, fmt, args);
317 va_end(args);
318 fprintf(stderr, "\n");
319 exit (-1);
320 }
321
322 void
323 warning(char *fmt, ...)
324 {
325 va_list args;
326
327 fprintf(stderr,"icctest: Warning - ");
328 va_start(args, fmt);
329 vfprintf(stderr, fmt, args);
330 va_end(args);
331 fprintf(stderr, "\n");
332 }