1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
|
/*
* Argyll Color Correction System
* Create a linear display calibration file.
*
* Author: Graeme W. Gill
* Date: 15/11/2005
*
* Copyright 1996 - 2007 Graeme W. Gill
* All rights reserved.
*
* This material is licenced under the GNU AFFERO GENERAL PUBLIC LICENSE Version 3 :-
* see the License.txt file for licencing details.
*/
#undef DEBUG
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <sys/types.h>
#include <time.h>
#include <string.h>
#include "copyright.h"
#include "aconfig.h"
#include "numlib.h"
#include "cgats.h"
#include "xicc.h"
#include "sort.h"
#include <stdarg.h>
#if defined (NT)
#include <conio.h>
#endif
void
usage(int level) {
int i;
fprintf(stderr,"Create a synthetic calibration file, Version %s\n",ARGYLL_VERSION_STR);
fprintf(stderr,"Author: Graeme W. Gill, licensed under the AGPL Version 3\n");
fprintf(stderr,"usage: synthcal [-options] outfile\n");
fprintf(stderr," -r res Set the calibration resolution (default 256)\n");
fprintf(stderr," -t N i = input, o = output, d = display (default)\n");
fprintf(stderr," -d col_comb choose colorant combination from the following (default 3):\n");
for (i = 0; ; i++) {
char *desc;
if (icx_enum_colorant_comb(i, &desc) == 0)
break;
fprintf(stderr," %d: %s\n",i,desc);
}
fprintf(stderr," -D colorant Add or delete colorant from combination:\n");
if (level == 0)
fprintf(stderr," (Use -?? to list known colorants)\n");
else {
fprintf(stderr," %d: %s\n",0,"Additive");
for (i = 0; ; i++) {
char *desc;
if (icx_enum_colorant(i, &desc) == 0)
break;
fprintf(stderr," %d: %s\n",i+1,desc);
}
}
fprintf(stderr," -o o1,o2,o3, Set non-linear curve offset (default 0.0)\n");
fprintf(stderr," -s s1,s2,s3, Set non-linear curve scale (default 1.0)\n");
fprintf(stderr," -p p1,p2,p3, Set non-linear curve powers (default 1.0)\n");
fprintf(stderr," -E description Set the profile dEscription string\n");
fprintf(stderr," outfile Base name for output .cal file\n");
exit(1);
}
int main(int argc, char *argv[])
{
int fa,nfa; /* current argument we're looking at */
int j;
int verb = 0;
char outname[MAXNAMEL+1] = { 0 }; /* Output cgats file base name */
char *profDesc = NULL; /* Description */
int devtype = 2; /* debice type, 0 = in, 1 = out, 2 = display */
inkmask devmask = 0; /* ICX ink mask of device space */
int calres = 256; /* Resolution of resulting file */
int devchan; /* Number of chanels in device space */
char *ident; /* Ink combination identifier (includes possible leading 'i') */
char *bident; /* Base ink combination identifier */
double off[MAX_CHAN]; /* Output offset */
double sca[MAX_CHAN]; /* Output scale */
double gam[MAX_CHAN]; /* Gamma applied */
for (j = 0; j < MAX_CHAN; j++)
off[j] = 0.0, sca[j] = gam[j] = 1.0;
error_program = "synthcal";
if (argc <= 1)
usage(0);
/* Process the arguments */
for(fa = 1;fa < argc;fa++) {
nfa = fa; /* skip to nfa if next argument is used */
if (argv[fa][0] == '-') /* Look for any flags */
{
char *na = NULL; /* next argument after flag, null if none */
if (argv[fa][2] != '\000')
na = &argv[fa][2]; /* next is directly after flag */
else {
if ((fa+1) < argc) {
if (argv[fa+1][0] != '-')
{
nfa = fa + 1;
na = argv[nfa]; /* next is seperate non-flag argument */
}
}
}
if (argv[fa][1] == '?' || argv[fa][1] == '-') {
if (argv[fa][2] == '?' || argv[fa][2] == '-')
usage(1);
usage(0);
}
else if (argv[fa][1] == 'v')
verb = 1;
/* Calibration file resolution */
else if (argv[fa][1] == 'r') {
fa = nfa;
if (na == NULL) usage(0);
calres = atoi(na);
if (calres < 2 || calres > MAX_CAL_ENT)
usage(0);
}
/* Select the device type */
else if (argv[fa][1] == 't') {
fa = nfa;
if (na == NULL) usage(0);
if (na[0] == 'i' || na[0] == 'I')
devtype = 0;
else if (na[0] == 'o' || na[0] == 'O')
devtype = 1;
else if (na[0] == 'd' || na[0] == 'D')
devtype = 2;
else
usage(0);
}
/* Select the ink enumeration */
else if (argv[fa][1] == 'd') {
int ix;
fa = nfa;
if (na == NULL) usage(0);
ix = atoi(na);
if (ix == 0 && na[0] != '0')
usage(0);
if ((devmask = icx_enum_colorant_comb(ix, NULL)) == 0)
usage(0);
}
/* Toggle the colorant in ink combination */
else if (argv[fa][1] == 'D') {
int ix, tmask;
fa = nfa;
if (na == NULL) usage(0);
ix = atoi(na);
if (ix == 0 && na[0] != '0')
usage(0);
if (ix == 0)
tmask = ICX_ADDITIVE;
else
if ((tmask = icx_enum_colorant(ix-1, NULL)) == 0)
usage(0);
devmask ^= tmask;
}
/* curve offset */
else if (argv[fa][1] == 'o' || argv[fa][1] == 'O') {
fa = nfa;
if (na == NULL) usage(0);
for (j = 0; j < MAX_CHAN; j++) {
int i;
for (i = 0; ; i++) {
if (na[i] == ','){
na[i] = '\000';
break;
}
if (na[i] == '\000') {
i = 0;
break;
}
}
off[j] = atof(na);
if (i == 0)
break;
na += i+1;
}
}
/* curve scale */
else if (argv[fa][1] == 's' || argv[fa][1] == 'S') {
fa = nfa;
if (na == NULL) usage(0);
for (j = 0; j < MAX_CHAN; j++) {
int i;
for (i = 0; ; i++) {
if (na[i] == ','){
na[i] = '\000';
break;
}
if (na[i] == '\000') {
i = 0;
break;
}
}
sca[j] = atof(na);
if (i == 0)
break;
na += i+1;
}
}
/* curve power */
else if (argv[fa][1] == 'p' || argv[fa][1] == 'P') {
fa = nfa;
if (na == NULL) usage(0);
for (j = 0; j < MAX_CHAN; j++) {
int i;
for (i = 0; ; i++) {
if (na[i] == ','){
na[i] = '\000';
break;
}
if (na[i] == '\000') {
i = 0;
break;
}
}
gam[j] = atof(na);
if (i == 0)
break;
na += i+1;
}
}
/* Profile Description */
else if (argv[fa][1] == 'E') {
fa = nfa;
if (na == NULL) usage(0);
profDesc = na;
}
else
usage(0);
} else
break;
}
/* Get the file name argument */
if (fa >= argc || argv[fa][0] == '-') usage(0);
strcpy(outname,argv[fa]);
if (strlen(outname) < 4 || strcmp(".cal",outname + strlen(outname)-4) != 0)
strcat(outname,".cal");
/* Implement defaults */
if (devmask == 0) {
if (devtype == 0 || devtype == 2)
devmask = ICX_RGB;
else
devmask = ICX_CMYK;
}
ident = icx_inkmask2char(devmask, 1);
bident = icx_inkmask2char(devmask, 0);
devchan = icx_noofinks(devmask);
if (verb) {
if (devtype == 0)
printf("Device type: input\n");
else if (devtype == 1)
printf("Device type: output\n");
else
printf("Device type: display\n");
printf("Colorspace: %s\n", ident);
printf("Curve parameters:\n");
for (j = 0; j < devchan; j++)
printf("off[%d] = %f, sca[%d] = %f, gam[%d] = %f\n",j,off[j],j,sca[j],j, gam[j]);
}
/* Write out the resulting calibration file */
{
int i, j;
cgats *ocg; /* output cgats structure */
time_t clk = time(0);
struct tm *tsp = localtime(&clk);
char *atm = asctime(tsp); /* Ascii time */
cgats_set_elem *setel; /* Array of set value elements */
int nsetel = 0;
char buf[200];
ocg = new_cgats(); /* Create a CGATS structure */
ocg->add_other(ocg, "CAL"); /* our special type is Calibration file */
ocg->add_table(ocg, tt_other, 0); /* Add a table for RAMDAC values */
ocg->add_kword(ocg, 0, "DESCRIPTOR", "Argyll Device Calibration Curves",NULL);
ocg->add_kword(ocg, 0, "ORIGINATOR", "Argyll synthcal", NULL);
atm[strlen(atm)-1] = '\000'; /* Remove \n from end */
ocg->add_kword(ocg, 0, "CREATED",atm, NULL);
if (devtype == 0)
ocg->add_kword(ocg, 0, "DEVICE_CLASS","INPUT", NULL);
else if (devtype == 1)
ocg->add_kword(ocg, 0, "DEVICE_CLASS","OUTPUT", NULL);
else
ocg->add_kword(ocg, 0, "DEVICE_CLASS","DISPLAY", NULL);
ocg->add_kword(ocg, 0, "COLOR_REP", ident, NULL);
if (profDesc != NULL)
ocg->add_kword(ocg, 0, "DESCRIPTION", profDesc, NULL);
sprintf(buf, "%s_I",bident);
ocg->add_field(ocg, 0, buf, r_t);
nsetel++;
for (j = 0; j < devchan; j++) {
inkmask imask = icx_index2ink(devmask, j);
sprintf(buf, "%s_%s",bident,icx_ink2char(imask));
ocg->add_field(ocg, 0, buf, r_t);
nsetel++;
}
if ((setel = (cgats_set_elem *)malloc(sizeof(cgats_set_elem) * nsetel)) == NULL)
error("Malloc failed!");
for (i = 0; i < calres; i++) {
double vv = i/(calres-1.0);
setel[0].d = vv;
for (j = 0; j < devchan; j++) {
setel[j+1].d = off[j] + sca[j] * pow(vv, gam[j]);
if (setel[j+1].d < 0.0)
setel[j+1].d = 0.0;
else if (setel[j+1].d > 1.0)
setel[j+1].d = 1.0;
}
ocg->add_setarr(ocg, 0, setel);
}
free(setel);
if (ocg->write_name(ocg, outname))
error("Write error : %s",ocg->err);
ocg->del(ocg); /* Clean up */
}
return 0;
}
|