| 1 | /* |
| 2 | * |
| 3 | * Super simple color calibration program for the Common UNIX |
| 4 | * Printing System. |
| 5 | * |
| 6 | * Copyright 1993-2000 by Easy Software Products. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License as published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, but |
| 14 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 15 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 16 | * for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License |
| 19 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 | * |
| 21 | * Contents: |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #ifdef HAVE_CONFIG_H |
| 26 | #include <config.h> |
| 27 | #endif |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <ctype.h> |
| 32 | #include <math.h> |
| 33 | #include <unistd.h> |
| 34 | #include <sys/types.h> |
| 35 | |
| 36 | /* |
| 37 | * min/max/abs macros... |
| 38 | */ |
| 39 | |
| 40 | #ifndef max |
| 41 | # define max(a,b) ((a) > (b) ? (a) : (b)) |
| 42 | #endif /* !max */ |
| 43 | #ifndef min |
| 44 | # define min(a,b) ((a) < (b) ? (a) : (b)) |
| 45 | #endif /* !min */ |
| 46 | #ifndef abs |
| 47 | # define abs(a) ((a) < 0 ? -(a) : (a)) |
| 48 | #endif /* !abs */ |
| 49 | |
| 50 | |
| 51 | /* |
| 52 | * Prototypes... |
| 53 | */ |
| 54 | |
| 55 | float get_calibration(const char *prompt, float minval, float maxval); |
| 56 | char *safe_gets(const char *prompt, char *s, int size); |
| 57 | int safe_geti(const char *prompt, int defval); |
| 58 | void send_prolog(FILE *fp); |
| 59 | void send_pass1(FILE *fp); |
| 60 | void send_pass2(FILE *fp, float kd, float rd, float yellow); |
| 61 | void send_pass3(FILE *fp, float kd, float rd, float g, float yellow); |
| 62 | void send_pass4(FILE *fp, float red, float green, float blue, const char *p); |
| 63 | |
| 64 | |
| 65 | /* |
| 66 | * 'main()' - Main entry for calibration program. |
| 67 | */ |
| 68 | |
| 69 | int |
| 70 | main(int argc, |
| 71 | char *argv[]) |
| 72 | { |
| 73 | char printer[255]; |
| 74 | char resolution[255]; |
| 75 | char mediatype[255]; |
| 76 | char *profile; |
| 77 | char cupsProfile[1024]; |
| 78 | char command[1024]; |
| 79 | char lpoptionscommand[1024]; |
| 80 | char line[255]; |
| 81 | char junk[255]; |
| 82 | FILE *fp; |
| 83 | float kd, rd, g; |
| 84 | float color; |
| 85 | float red, green, blue; |
| 86 | float cyan, magenta, yellow; |
| 87 | float m[3][3]; |
| 88 | |
| 89 | (void)argc; |
| 90 | (void)argv; |
| 91 | |
| 92 | puts("ESP Printer Calibration Tool v1.0"); |
| 93 | puts("Copyright 1999-2000 by Easy Software Products, All Rights Reserved."); |
| 94 | puts(""); |
| 95 | puts("This program allows you to calibrate the color output of printers"); |
| 96 | puts("using the Gutenprint CUPS or ESP Print Pro drivers."); |
| 97 | puts(""); |
| 98 | puts("Please note that this program ONLY works with the Gutenprint CUPS"); |
| 99 | puts("driver."); |
| 100 | puts(""); |
| 101 | puts("These drivers by the text \"CUPS+Gutenprint\""); |
| 102 | puts("the model description displayed by the CUPS web interface or"); |
| 103 | puts("similar tool."); |
| 104 | puts(""); |
| 105 | puts("If you are not using the correct driver, press CTRL+C now and"); |
| 106 | puts("reinstall your printer queue with the appropriate driver first."); |
| 107 | puts(""); |
| 108 | puts("To make a calibration profile for all users, run this program as"); |
| 109 | puts("the \"root\" user."); |
| 110 | puts(""); |
| 111 | puts(""); |
| 112 | |
| 113 | safe_gets("Printer name [default]?", printer, sizeof(printer)); |
| 114 | safe_gets("Resolution [default]?", resolution, sizeof(resolution)); |
| 115 | safe_gets("Media type [default]?", mediatype, sizeof(mediatype)); |
| 116 | |
| 117 | strcpy(command, "lp -s"); |
| 118 | if (printer[0]) |
| 119 | { |
| 120 | strcat(command, " -d "); |
| 121 | strcat(command, printer); |
| 122 | } |
| 123 | if (resolution[0]) |
| 124 | { |
| 125 | strcat(command, " -o resolution="); |
| 126 | strcat(command, resolution); |
| 127 | } |
| 128 | if (mediatype[0]) |
| 129 | { |
| 130 | strcat(command, " -o media="); |
| 131 | strcat(command, mediatype); |
| 132 | } |
| 133 | |
| 134 | strcat(command, " -o profile="); |
| 135 | |
| 136 | profile = command + strlen(command); |
| 137 | |
| 138 | strcpy(profile, "1000,1000,1000,0,0,0,1000,0,0,0,1000"); |
| 139 | |
| 140 | safe_gets("Press ENTER to print pass #1 or N to skip...", junk, sizeof(junk)); |
| 141 | |
| 142 | if (!junk[0]) |
| 143 | { |
| 144 | puts("Sending calibration pass #1 for density/saturation levels..."); |
| 145 | |
| 146 | fp = popen(command, "w"); |
| 147 | send_prolog(fp); |
| 148 | send_pass1(fp); |
| 149 | fputs("showpage\n", fp); |
| 150 | pclose(fp); |
| 151 | |
| 152 | puts("Calibration pass #1 sent."); |
| 153 | } |
| 154 | |
| 155 | puts(""); |
| 156 | puts("Please select the character that corresponds to the black block that"); |
| 157 | puts("is 100% saturated (dark) while not bleeding through the paper. If"); |
| 158 | puts("the saturation point appears to occur between two characters, enter"); |
| 159 | puts("both characters."); |
| 160 | puts(""); |
| 161 | |
| 162 | kd = get_calibration("Black density?", 0.25f, 1.0f); |
| 163 | |
| 164 | puts(""); |
| 165 | puts("Now select the character that corresponds to the yellow block that is"); |
| 166 | puts("100% saturated (dark) while not bleeding through the paper. If the"); |
| 167 | puts("saturation point appears to occur between two characters, enter both"); |
| 168 | puts("characters."); |
| 169 | puts(""); |
| 170 | |
| 171 | cyan = kd; |
| 172 | magenta = kd; |
| 173 | yellow = get_calibration("Yellow density?", 0.25f, 1.0f); |
| 174 | |
| 175 | puts(""); |
| 176 | puts("Now select the character that corresponds to the red block that is"); |
| 177 | puts("100% saturated (dark) while not bleeding through the paper. If the"); |
| 178 | puts("saturation point appears to occur between two characters, enter both"); |
| 179 | puts("characters."); |
| 180 | puts(""); |
| 181 | |
| 182 | rd = get_calibration("Red density?", 0.5f, 2.0f); |
| 183 | |
| 184 | puts(""); |
| 185 | puts("Thank you. Now insert the page back into the printer and press the"); |
| 186 | puts("ENTER key to print calibration pass #2."); |
| 187 | puts(""); |
| 188 | |
| 189 | safe_gets("Press ENTER to print pass #2 or N to skip...", junk, sizeof(junk)); |
| 190 | |
| 191 | if (!junk[0]) |
| 192 | { |
| 193 | puts("Sending calibration pass #2 for gamma levels..."); |
| 194 | |
| 195 | fp = popen(command, "w"); |
| 196 | send_prolog(fp); |
| 197 | send_pass2(fp, kd, rd, yellow); |
| 198 | fputs("showpage\n", fp); |
| 199 | pclose(fp); |
| 200 | |
| 201 | puts("Calibration pass #2 sent."); |
| 202 | } |
| 203 | |
| 204 | puts(""); |
| 205 | puts("Please select the character that corresponds to the column of gray"); |
| 206 | puts("blocks that appear to be 1/2 and 1/4 as dark as the black blocks,"); |
| 207 | puts("respectively. If the transition point appears to occur between two"); |
| 208 | puts("characters, enter both characters."); |
| 209 | puts(""); |
| 210 | |
| 211 | g = get_calibration("Gamma?", 1.0f, 4.0f); |
| 212 | |
| 213 | puts(""); |
| 214 | puts("Thank you. Now insert the page back into the printer and press the"); |
| 215 | puts("ENTER key to print calibration pass #3."); |
| 216 | puts(""); |
| 217 | |
| 218 | safe_gets("Press ENTER to print pass #3 or N to skip...", junk, sizeof(junk)); |
| 219 | |
| 220 | if (!junk[0]) |
| 221 | { |
| 222 | puts("Sending calibration pass #3 for red, green, and blue adjustment..."); |
| 223 | |
| 224 | fp = popen(command, "w"); |
| 225 | send_prolog(fp); |
| 226 | send_pass3(fp, kd, rd, g, yellow); |
| 227 | fputs("showpage\n", fp); |
| 228 | pclose(fp); |
| 229 | |
| 230 | puts("Calibration pass #3 sent."); |
| 231 | } |
| 232 | |
| 233 | puts(""); |
| 234 | puts("Please select the character that corresponds to the correct red,"); |
| 235 | puts("green, and blue colors. If the transition point appears to occur"); |
| 236 | puts("between two characters, enter both characters."); |
| 237 | puts(""); |
| 238 | |
| 239 | red = get_calibration("Red color?", 0.35f, -0.40f); |
| 240 | green = get_calibration("Green color?", 0.35f, -0.40f); |
| 241 | blue = get_calibration("Blue color?", 0.35f, -0.40f); |
| 242 | |
| 243 | color = 0.5f * rd / kd - kd; |
| 244 | |
| 245 | cyan /= kd; |
| 246 | magenta /= kd; |
| 247 | yellow /= kd; |
| 248 | |
| 249 | m[0][0] = cyan; /* C */ |
| 250 | m[0][1] = cyan * (color + blue); /* C + M (blue) */ |
| 251 | m[0][2] = cyan * (color - green); /* C + Y (green) */ |
| 252 | m[1][0] = magenta * (color - blue); /* M + C (blue) */ |
| 253 | m[1][1] = magenta; /* M */ |
| 254 | m[1][2] = magenta * (color + red); /* M + Y (red) */ |
| 255 | m[2][0] = yellow * (color + green); /* Y + C (green) */ |
| 256 | m[2][1] = yellow * (color - red); /* Y + M (red) */ |
| 257 | m[2][2] = yellow; /* Y */ |
| 258 | |
| 259 | if (m[0][1] > 0.0f) |
| 260 | { |
| 261 | m[1][0] -= m[0][1]; |
| 262 | m[0][1] = 0.0f; |
| 263 | } |
| 264 | else if (m[1][0] > 0.0f) |
| 265 | { |
| 266 | m[0][1] -= m[1][0]; |
| 267 | m[1][0] = 0.0f; |
| 268 | } |
| 269 | |
| 270 | if (m[0][2] > 0.0f) |
| 271 | { |
| 272 | m[2][0] -= m[0][2]; |
| 273 | m[0][2] = 0.0f; |
| 274 | } |
| 275 | else if (m[2][0] > 0.0f) |
| 276 | { |
| 277 | m[0][2] -= m[2][0]; |
| 278 | m[2][0] = 0.0f; |
| 279 | } |
| 280 | |
| 281 | if (m[1][2] > 0.0f) |
| 282 | { |
| 283 | m[2][1] -= m[1][2]; |
| 284 | m[1][2] = 0.0f; |
| 285 | } |
| 286 | else if (m[2][1] > 0.0f) |
| 287 | { |
| 288 | m[1][2] -= m[2][1]; |
| 289 | m[2][1] = 0.0f; |
| 290 | } |
| 291 | |
| 292 | sprintf(profile, "%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f,%.0f", |
| 293 | kd * 1000.0f, g * 1000.0f, |
| 294 | m[0][0] * 1000.0f, m[0][1] * 1000.0f, m[0][2] * 1000.0f, |
| 295 | m[1][0] * 1000.0f, m[1][1] * 1000.0f, m[1][2] * 1000.0f, |
| 296 | m[2][0] * 1000.0f, m[2][1] * 1000.0f, m[2][2] * 1000.0f); |
| 297 | |
| 298 | sprintf(cupsProfile, " *cupsColorProfile %s/%s: \"%.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f %.3f\"\n", |
| 299 | resolution[0] ? resolution : "-", mediatype[0] ? mediatype : "-", |
| 300 | kd, g, m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2], |
| 301 | m[2][0], m[2][1], m[2][2]); |
| 302 | |
| 303 | puts(""); |
| 304 | puts("Thank you. Now insert the page back into the printer and press the"); |
| 305 | puts("ENTER key to print the final calibration pass."); |
| 306 | puts(""); |
| 307 | |
| 308 | safe_gets("Press ENTER to continue...", junk, sizeof(junk)); |
| 309 | |
| 310 | puts("Sending calibration pass #4 for visual confirmation..."); |
| 311 | |
| 312 | fp = popen(command, "w"); |
| 313 | send_prolog(fp); |
| 314 | send_pass4(fp, red, green, blue, cupsProfile); |
| 315 | fputs("showpage\n", fp); |
| 316 | pclose(fp); |
| 317 | |
| 318 | puts("Calibration pass #4 sent."); |
| 319 | |
| 320 | puts(""); |
| 321 | puts("The basic color profile for these values is:"); |
| 322 | puts(""); |
| 323 | printf(" %s\n", cupsProfile); |
| 324 | puts(""); |
| 325 | puts("You can add this to the PPD file for this printer to make this change"); |
| 326 | puts("permanent, or use the following option with a printing command:"); |
| 327 | puts(""); |
| 328 | printf(" -o profile=%s\n", profile); |
| 329 | puts(""); |
| 330 | puts("to use the profile for this job only."); |
| 331 | puts(""); |
| 332 | puts("Calibration is complete."); |
| 333 | puts(""); |
| 334 | |
| 335 | #if defined(HAVE_GETUID) |
| 336 | if (getuid() == 0) |
| 337 | { |
| 338 | do |
| 339 | safe_gets("Would you like to save the profile as a system-wide default (y/n)? ", |
| 340 | line, sizeof(line)); |
| 341 | while (tolower(line[0]) != 'n' && tolower(line[0]) != 'y'); |
| 342 | } |
| 343 | else |
| 344 | #endif |
| 345 | line[0] = 'n'; |
| 346 | |
| 347 | if (line[0] == 'n') |
| 348 | { |
| 349 | do |
| 350 | safe_gets("Would you like to save the profile as a personal default (y/n)? ", |
| 351 | line, sizeof(line)); |
| 352 | while (tolower(line[0]) != 'n' && tolower(line[0]) != 'y'); |
| 353 | } |
| 354 | |
| 355 | puts(""); |
| 356 | if (tolower(line[0]) == 'n') |
| 357 | { |
| 358 | puts("Calibration profile NOT saved."); |
| 359 | return (0); |
| 360 | } |
| 361 | |
| 362 | strcpy(lpoptionscommand, "lpoptions"); |
| 363 | if (printer[0]) |
| 364 | { |
| 365 | strcat(lpoptionscommand, " -p "); |
| 366 | strcat(lpoptionscommand, printer); |
| 367 | } |
| 368 | |
| 369 | strcat(lpoptionscommand, " -o profile="); |
| 370 | |
| 371 | strcat(lpoptionscommand, profile); |
| 372 | |
| 373 | if (system(lpoptionscommand) == 0) |
| 374 | puts("Calibration profile successfully saved."); |
| 375 | else |
| 376 | puts("An error occurred while saving the calibration profile."); |
| 377 | |
| 378 | return (0); |
| 379 | } |
| 380 | |
| 381 | |
| 382 | float |
| 383 | get_calibration(const char *prompt, |
| 384 | float minval, |
| 385 | float maxval) |
| 386 | { |
| 387 | char line[4]; /* Input from user */ |
| 388 | int val1, val2; /* Calibration values */ |
| 389 | |
| 390 | |
| 391 | do |
| 392 | { |
| 393 | if (safe_gets(prompt, line, sizeof(line)) == NULL) |
| 394 | return (minval); |
| 395 | } |
| 396 | while (!isxdigit(line[0]) || (line[1] && !isxdigit(line[1]))); |
| 397 | |
| 398 | if (isdigit(line[0])) |
| 399 | val1 = line[0] - '0'; |
| 400 | else |
| 401 | val1 = tolower(line[0]) - 'a' + 10; |
| 402 | |
| 403 | if (!line[1]) |
| 404 | val2 = val1; |
| 405 | else if (isdigit(line[1])) |
| 406 | val2 = line[1] - '0'; |
| 407 | else |
| 408 | val2 = tolower(line[1]) - 'a' + 10; |
| 409 | |
| 410 | return (minval + (maxval - minval) * (val1 + val2) / 30.0f); |
| 411 | } |
| 412 | |
| 413 | |
| 414 | int |
| 415 | safe_geti(const char *prompt, |
| 416 | int defval) |
| 417 | { |
| 418 | char line[255]; |
| 419 | |
| 420 | |
| 421 | do |
| 422 | { |
| 423 | safe_gets(prompt, line, sizeof(line)); |
| 424 | |
| 425 | if (defval && !line[0]) |
| 426 | return (defval); |
| 427 | } |
| 428 | while (!line[0] || !isdigit(line[0])); |
| 429 | |
| 430 | return (atoi(line)); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* |
| 435 | * 'safe_gets()' - Get a string from the user. |
| 436 | */ |
| 437 | |
| 438 | char * |
| 439 | safe_gets(const char *prompt, |
| 440 | char *s, |
| 441 | int size) |
| 442 | { |
| 443 | printf("%s ", prompt); |
| 444 | |
| 445 | if (fgets(s, size, stdin) == NULL) |
| 446 | return (NULL); |
| 447 | |
| 448 | if (s[0]) |
| 449 | s[strlen(s) - 1] = '\0'; |
| 450 | |
| 451 | return (s); |
| 452 | } |
| 453 | |
| 454 | |
| 455 | void |
| 456 | send_prolog(FILE *fp) |
| 457 | { |
| 458 | fputs("%!\n", fp); |
| 459 | fputs("/Helvetica findfont 12 scalefont setfont\n", fp); |
| 460 | fputs("/BLOCK {\n" |
| 461 | " 14.4 mul neg 720 add exch\n" |
| 462 | " 14.4 mul 72 add exch\n" |
| 463 | " 14.4 14.4 rectfill\n" |
| 464 | "} bind def\n", fp); |
| 465 | fputs("/DIAMOND {\n" |
| 466 | " 14.4 mul neg 720 add 7.2 add exch\n" |
| 467 | " 14.4 mul 72 add exch\n" |
| 468 | " newpath\n" |
| 469 | " moveto\n" |
| 470 | " 7.2 7.2 rlineto\n" |
| 471 | " 7.2 -7.2 rlineto\n" |
| 472 | " -7.2 -7.2 rlineto\n" |
| 473 | " closepath\n" |
| 474 | " 0 ne { fill } { stroke } ifelse\n" |
| 475 | "} bind def\n", fp); |
| 476 | fputs("/PLUS {\n" |
| 477 | " 14.4 mul neg 720 add 7.2 add exch\n" |
| 478 | " 14.4 mul 72 add exch\n" |
| 479 | " newpath\n" |
| 480 | " moveto\n" |
| 481 | " 14.4 0 rlineto\n" |
| 482 | " -7.2 -7.2 rmoveto\n" |
| 483 | " 0 14.4 rlineto\n" |
| 484 | " closepath\n" |
| 485 | " fill\n" |
| 486 | "} bind def\n", fp); |
| 487 | fputs("/TEXT {\n" |
| 488 | " 14.4 mul neg 720 add 4 add exch\n" |
| 489 | " 14.4 mul 72 add 7.2 add exch\n" |
| 490 | " moveto\n" |
| 491 | " dup stringwidth pop 0.5 mul neg 0 rmoveto\n" |
| 492 | " show\n" |
| 493 | "} bind def\n", fp); |
| 494 | } |
| 495 | |
| 496 | |
| 497 | void |
| 498 | send_pass1(FILE *fp) |
| 499 | { |
| 500 | int i; |
| 501 | float p; |
| 502 | static const char *hex = "FEDCBA9876543210"; |
| 503 | |
| 504 | |
| 505 | fputs("0 setgray", fp); |
| 506 | fputs("(K) 0 1 TEXT\n", fp); |
| 507 | fputs("(Y) 0 2 TEXT\n", fp); |
| 508 | fputs("(R) 0 4 TEXT\n", fp); |
| 509 | |
| 510 | for (i = 0; i < 16; i ++) |
| 511 | { |
| 512 | fprintf(fp, "(%d) %d 0 TEXT\n", 100 - i * 5, i * 2 + 2); |
| 513 | fprintf(fp, "(%c) %d 3 TEXT\n", hex[i], i * 2 + 2); |
| 514 | fprintf(fp, "(%d) %d 5 TEXT\n", 200 - i * 10, i * 2 + 2); |
| 515 | } |
| 516 | |
| 517 | for (i = 0; i < 16; i ++) |
| 518 | { |
| 519 | p = 0.01f * (100 - i * 5); |
| 520 | |
| 521 | fprintf(fp, "%.2f setgray %d 1 BLOCK\n", 1.0 - p, i * 2 + 2); |
| 522 | fprintf(fp, "0 0 %.2f 0 setcmykcolor %d 2 BLOCK\n", p, i * 2 + 2); |
| 523 | fprintf(fp, "0 %.2f %.2f 0 setcmykcolor %d 4 BLOCK\n", p, p, i * 2 + 2); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | |
| 528 | void |
| 529 | send_pass2(FILE *fp, |
| 530 | float kd, |
| 531 | float rd, |
| 532 | float yellow) |
| 533 | { |
| 534 | int i; |
| 535 | float p; |
| 536 | float g; |
| 537 | static const char *hex = "FEDCBA9876543210"; |
| 538 | |
| 539 | |
| 540 | rd *= 0.5f; |
| 541 | |
| 542 | fprintf(fp, "0 0 %.2f 0 setcmykcolor\n", yellow); |
| 543 | fprintf(fp, "1 %.2f 6 DIAMOND\n", 2.0f + 30.0f * (1.0f - yellow) / 0.75f); |
| 544 | |
| 545 | fprintf(fp, "0 %.2f %.2f 0 setcmykcolor\n", rd, rd); |
| 546 | fprintf(fp, "%d %.2f 6 DIAMOND\n", rd != yellow, |
| 547 | 2.0f + 30.0f * (1.0f - rd) / 0.75f); |
| 548 | |
| 549 | p = 1.0f - kd; |
| 550 | fprintf(fp, "%.2f setgray\n", p); |
| 551 | |
| 552 | if (kd == rd && kd == yellow) |
| 553 | fprintf(fp, "%.2f 6 PLUS\n", 2.0f + 30.0f * (1.0f - kd) / 0.75f); |
| 554 | else |
| 555 | fprintf(fp, "%d %.2f 6 DIAMOND\n", kd != yellow && kd != rd, |
| 556 | 2.0f + 30.0f * (1.0f - kd) / 0.75f); |
| 557 | |
| 558 | fputs("(100%) 0 9 TEXT\n", fp); |
| 559 | fputs("(50%) 0 10 TEXT\n", fp); |
| 560 | fputs("(25%) 0 11 TEXT\n", fp); |
| 561 | |
| 562 | for (i = 0; i < 16; i ++) |
| 563 | { |
| 564 | fprintf(fp, "(%.1f) %d 8 TEXT\n", 0.2f * (20 - i), i * 2 + 2); |
| 565 | fprintf(fp, "(%c) %d 12 TEXT\n", hex[i], i * 2 + 2); |
| 566 | } |
| 567 | |
| 568 | for (i = 0; i < 16; i ++) |
| 569 | { |
| 570 | g = 0.2f * (20 - i); |
| 571 | |
| 572 | p = 1.0f - kd * (float)pow(1.0f, g); |
| 573 | fprintf(fp, "%.2f setgray %d 9 BLOCK\n", p, i * 2 + 2); |
| 574 | |
| 575 | p = 1.0f - kd * (float)pow(0.5f, g); |
| 576 | fprintf(fp, "%.2f setgray %d 10 BLOCK\n", p, i * 2 + 2); |
| 577 | |
| 578 | p = 1.0f - kd * (float)pow(0.25f, g); |
| 579 | fprintf(fp, "%.2f setgray %d 11 BLOCK\n", p, i * 2 + 2); |
| 580 | } |
| 581 | } |
| 582 | |
| 583 | |
| 584 | void |
| 585 | send_pass3(FILE *fp, |
| 586 | float kd, |
| 587 | float rd, |
| 588 | float g, |
| 589 | float yellow) |
| 590 | { |
| 591 | int i; |
| 592 | float p; |
| 593 | float color; |
| 594 | float c, m, y; |
| 595 | float adj; |
| 596 | static const char *hex = "FEDCBA9876543210"; |
| 597 | |
| 598 | |
| 599 | p = 1.0f - kd; |
| 600 | fprintf(fp, "%.2f setgray\n", p); |
| 601 | fprintf(fp, "1 %.2f 13 DIAMOND\n", 2.0f + 30.0f * (4.0f - g) / 3.0f); |
| 602 | |
| 603 | color = 0.5f * rd / kd - kd; |
| 604 | yellow /= kd; |
| 605 | |
| 606 | fputs("(R) 2 16 TEXT\n", fp); |
| 607 | fputs("(G) 2 17 TEXT\n", fp); |
| 608 | fputs("(B) 2 18 TEXT\n", fp); |
| 609 | |
| 610 | for (i = 1; i < 16; i ++) |
| 611 | { |
| 612 | fprintf(fp, "(%+d) %d 15 TEXT\n", i * 5 - 40, i * 2 + 2); |
| 613 | fprintf(fp, "(%c) %d 19 TEXT\n", hex[i], i * 2 + 2); |
| 614 | } |
| 615 | |
| 616 | for (i = 1; i < 16; i ++) |
| 617 | { |
| 618 | /* Adjustment value */ |
| 619 | adj = i * 0.05f - 0.40f; |
| 620 | |
| 621 | /* RED */ |
| 622 | c = 0.0f; |
| 623 | m = color + adj; |
| 624 | y = color - adj; |
| 625 | if (m > 0) |
| 626 | { |
| 627 | y -= m; |
| 628 | m = 0; |
| 629 | } |
| 630 | else if (y > 0) |
| 631 | { |
| 632 | m -= y; |
| 633 | y = 0; |
| 634 | } |
| 635 | |
| 636 | m += 1.0f; |
| 637 | y = yellow * (1.0f + y); |
| 638 | |
| 639 | if (c <= 0.0f) |
| 640 | c = 0.0f; |
| 641 | else if (c >= 1.0f) |
| 642 | c = 1.0f; |
| 643 | else |
| 644 | c = (float)pow(c, g); |
| 645 | |
| 646 | if (m <= 0.0f) |
| 647 | m = 0.0f; |
| 648 | else if (m >= 1.0f) |
| 649 | m = 1.0f; |
| 650 | else |
| 651 | m = (float)pow(m, g); |
| 652 | |
| 653 | if (y <= 0.0f) |
| 654 | y = 0.0f; |
| 655 | else if (y >= 1.0f) |
| 656 | y = 1.0f; |
| 657 | else |
| 658 | y = (float)pow(y, g); |
| 659 | |
| 660 | fprintf(fp, "%.2f %.2f %.2f 0 setcmykcolor %d 16 BLOCK\n", |
| 661 | c * kd, m * kd, y * kd, i * 2 + 2); |
| 662 | |
| 663 | /* GREEN */ |
| 664 | c = color - adj; |
| 665 | m = 0.0f; |
| 666 | y = color + adj; |
| 667 | |
| 668 | if (c > 0) |
| 669 | { |
| 670 | y -= c; |
| 671 | c = 0; |
| 672 | } |
| 673 | else if (y > 0) |
| 674 | { |
| 675 | c -= y; |
| 676 | y = 0; |
| 677 | } |
| 678 | |
| 679 | c += 1.0f; |
| 680 | y = yellow * (1.0f + y); |
| 681 | |
| 682 | if (c <= 0.0f) |
| 683 | c = 0.0f; |
| 684 | else if (c >= 1.0f) |
| 685 | c = 1.0f; |
| 686 | else |
| 687 | c = (float)pow(c, g); |
| 688 | |
| 689 | if (m <= 0.0f) |
| 690 | m = 0.0f; |
| 691 | else if (m >= 1.0f) |
| 692 | m = 1.0f; |
| 693 | else |
| 694 | m = (float)pow(m, g); |
| 695 | |
| 696 | if (y <= 0.0f) |
| 697 | y = 0.0f; |
| 698 | else if (y >= 1.0f) |
| 699 | y = 1.0f; |
| 700 | else |
| 701 | y = (float)pow(y, g); |
| 702 | |
| 703 | fprintf(fp, "%.2f %.2f %.2f 0 setcmykcolor %d 17 BLOCK\n", |
| 704 | c * kd, m * kd, y * kd, i * 2 + 2); |
| 705 | |
| 706 | /* BLUE */ |
| 707 | c = color + adj; |
| 708 | m = color - adj; |
| 709 | y = 0.0f; |
| 710 | |
| 711 | if (c > 0) |
| 712 | { |
| 713 | m -= c; |
| 714 | c = 0; |
| 715 | } |
| 716 | else if (m > 0) |
| 717 | { |
| 718 | c -= m; |
| 719 | m = 0; |
| 720 | } |
| 721 | |
| 722 | c += 1.0f; |
| 723 | m += 1.0f; |
| 724 | |
| 725 | if (c <= 0.0f) |
| 726 | c = 0.0f; |
| 727 | else if (c >= 1.0f) |
| 728 | c = 1.0f; |
| 729 | else |
| 730 | c = (float)pow(c, g); |
| 731 | |
| 732 | if (m <= 0.0f) |
| 733 | m = 0.0f; |
| 734 | else if (m >= 1.0f) |
| 735 | m = 1.0f; |
| 736 | else |
| 737 | m = (float)pow(m, g); |
| 738 | |
| 739 | if (y <= 0.0f) |
| 740 | y = 0.0f; |
| 741 | else if (y >= 1.0f) |
| 742 | y = 1.0f; |
| 743 | else |
| 744 | y = (float)pow(y, g); |
| 745 | |
| 746 | fprintf(fp, "%.2f %.2f %.2f 0 setcmykcolor %d 18 BLOCK\n", |
| 747 | c * kd, m * kd, y * kd, i * 2 + 2); |
| 748 | } |
| 749 | } |
| 750 | |
| 751 | |
| 752 | void |
| 753 | send_pass4(FILE *fp, |
| 754 | float red, |
| 755 | float green, |
| 756 | float blue, |
| 757 | const char *profile) |
| 758 | { |
| 759 | FILE *ppm; |
| 760 | int x, y, col, width, height; |
| 761 | int r, g, b; |
| 762 | char line[255]; |
| 763 | |
| 764 | |
| 765 | fprintf(fp, "0 0 1 setrgbcolor 1 %.2f 20 DIAMOND\n", /* BLUE */ |
| 766 | 2.0f + 30.0f * (0.40f + blue) / 0.75f); |
| 767 | fprintf(fp, "1 0 0 setrgbcolor %d %.2f 20 DIAMOND\n", /* RED */ |
| 768 | red != blue, 2.0f + 30.0f * (0.40f + red) / 0.75f); |
| 769 | if (green == red && green == blue) |
| 770 | fprintf(fp, "0 1 0 setrgbcolor %.2f 20 PLUS\n", /* GREEN */ |
| 771 | 2.0f + 30.0f * (0.40f + green) / 0.75f); |
| 772 | else |
| 773 | fprintf(fp, "0 1 0 setrgbcolor %d %.2f 20 DIAMOND\n", /* GREEN */ |
| 774 | green != red && green != blue, |
| 775 | 2.0f + 30.0f * (0.40f + green) / 0.75f); |
| 776 | |
| 777 | fputs("0 setgray\n", fp); |
| 778 | fputs("currentfont 0.8 scalefont setfont\n", fp); |
| 779 | |
| 780 | fprintf(fp, "(%s) 16 22 TEXT\n", profile); |
| 781 | |
| 782 | if ((ppm = fopen(CUPS_DATADIR "/calibrate.ppm", "rb")) == NULL) |
| 783 | if ((ppm = fopen("calibrate.ppm", "rb")) == NULL) |
| 784 | return; |
| 785 | |
| 786 | fgets(line, sizeof(line), ppm); |
| 787 | while (fgets(line, sizeof(line), ppm)) |
| 788 | if (line[0] != '#') |
| 789 | break; |
| 790 | |
| 791 | sscanf(line, "%d%d", &width, &height); |
| 792 | |
| 793 | fgets(line, sizeof(line), ppm); |
| 794 | |
| 795 | fputs("gsave\n", fp); |
| 796 | fprintf(fp, "72 %d translate\n", 504 * height / width + 72); |
| 797 | fprintf(fp, "504 -%d scale\n", 504 * height / width); |
| 798 | |
| 799 | fprintf(fp, "/scanline %d string def\n", width * 3); |
| 800 | |
| 801 | fprintf(fp, "%d %d 8\n", width, height); |
| 802 | fprintf(fp, "[%d 0 0 %d 0 0]\n", width, height); |
| 803 | fputs("{ currentfile scanline readhexstring pop } false 3 colorimage\n", fp); |
| 804 | |
| 805 | for (y = 0, col = 0; y < height; y ++) |
| 806 | { |
| 807 | printf("Sending scanline %d of %d...\r", y + 1, height); |
| 808 | fflush(stdout); |
| 809 | |
| 810 | for (x = 0; x < width; x ++) |
| 811 | { |
| 812 | r = getc(ppm); |
| 813 | g = getc(ppm); |
| 814 | b = getc(ppm); |
| 815 | |
| 816 | fprintf(fp, "%02X%02X%02X", r, g, b); |
| 817 | col ++; |
| 818 | if (col >= 12) |
| 819 | { |
| 820 | col = 0; |
| 821 | putc('\n', fp); |
| 822 | } |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | if (col) |
| 827 | putc('\n', fp); |
| 828 | |
| 829 | printf(" \r"); |
| 830 | |
| 831 | fclose(ppm); |
| 832 | |
| 833 | fputs("grestore\n", fp); |
| 834 | } |