| 1 | /* |
| 2 | * Test/profiling program for dithering code. |
| 3 | * |
| 4 | * Copyright 1997-2000 Michael Sweet ([email protected]) |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify it |
| 7 | * under the terms of the GNU General Public License as published by the Free |
| 8 | * Software Foundation; either version 2 of the License, or (at your option) |
| 9 | * any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, but |
| 12 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 13 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #ifdef HAVE_CONFIG_H |
| 21 | #include <config.h> |
| 22 | #endif |
| 23 | #include <gutenprint/gutenprint.h> |
| 24 | #define STPI_TESTDITHER |
| 25 | |
| 26 | #include "../src/main/gutenprint-internal.h" |
| 27 | #include <stdio.h> |
| 28 | #include <sys/time.h> |
| 29 | #include <unistd.h> |
| 30 | #include <string.h> |
| 31 | |
| 32 | /* |
| 33 | * Definitions for dither test... |
| 34 | */ |
| 35 | |
| 36 | #define MAX_IMAGE_WIDTH 5760 /* 8in * 720dpi */ |
| 37 | #define MAX_IMAGE_HEIGHT 2880 /* 4in * 720dpi */ |
| 38 | #define BUFFER_SIZE MAX_IMAGE_WIDTH |
| 39 | |
| 40 | #define IMAGE_MIXED 0 /* Mix of line types */ |
| 41 | #define IMAGE_WHITE 1 /* All white image */ |
| 42 | #define IMAGE_BLACK 2 /* All black image */ |
| 43 | #define IMAGE_COLOR 3 /* All color image */ |
| 44 | #define IMAGE_RANDOM 4 /* All random image */ |
| 45 | |
| 46 | #define DITHER_GRAY 0 /* Dither grayscale pixels */ |
| 47 | #define DITHER_COLOR 1 /* Dither color pixels */ |
| 48 | #define DITHER_PHOTO 2 /* Dither photo pixels */ |
| 49 | #define DITHER_CMYK 3 /* Dither photo pixels */ |
| 50 | #define DITHER_PHOTO_CMYK 4 /* Dither photo pixels */ |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * Globals... |
| 55 | */ |
| 56 | |
| 57 | unsigned int image_type = IMAGE_MIXED; |
| 58 | unsigned int dither_type = DITHER_COLOR; |
| 59 | const char *dither_name = NULL; |
| 60 | int dither_bits = 1; |
| 61 | int write_image = 1; |
| 62 | int quiet = 0; |
| 63 | int dont_regenerate_input = 0; |
| 64 | int dimage_width = MAX_IMAGE_WIDTH; |
| 65 | int dimage_height = MAX_IMAGE_HEIGHT; |
| 66 | unsigned short white_line[MAX_IMAGE_WIDTH * 6], |
| 67 | black_line[MAX_IMAGE_WIDTH * 6], |
| 68 | color_line[MAX_IMAGE_WIDTH * 6], |
| 69 | random_line[MAX_IMAGE_WIDTH * 6]; |
| 70 | |
| 71 | static const char *dither_types[] = /* Different dithering modes */ |
| 72 | { |
| 73 | "gray", |
| 74 | "color", |
| 75 | "photo", |
| 76 | "cmyk", |
| 77 | "photocmyk" |
| 78 | }; |
| 79 | static const char *image_types[] = /* Different image types */ |
| 80 | { |
| 81 | "mixed", |
| 82 | "white", |
| 83 | "black", |
| 84 | "colorimage", |
| 85 | "random" |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | #define SHADE(density, name) \ |
| 90 | { density, sizeof(name)/sizeof(stp_dotsize_t), name } |
| 91 | |
| 92 | static const stp_dotsize_t single_dotsize[] = |
| 93 | { |
| 94 | { 0x1, 1.0 } |
| 95 | }; |
| 96 | |
| 97 | static const stp_dotsize_t variable_dotsizes[] = |
| 98 | { |
| 99 | { 0x1, 0.28 }, |
| 100 | { 0x2, 0.58 }, |
| 101 | { 0x3, 1.0 } |
| 102 | }; |
| 103 | |
| 104 | static const stp_shade_t normal_1bit_shades[] = |
| 105 | { |
| 106 | SHADE(1.0, single_dotsize) |
| 107 | }; |
| 108 | |
| 109 | static const stp_shade_t photo_1bit_shades[] = |
| 110 | { |
| 111 | SHADE(0.33, single_dotsize), |
| 112 | SHADE(1.0, single_dotsize) |
| 113 | }; |
| 114 | |
| 115 | static const stp_shade_t normal_2bit_shades[] = |
| 116 | { |
| 117 | SHADE(1.0, variable_dotsizes) |
| 118 | }; |
| 119 | |
| 120 | static const stp_shade_t photo_2bit_shades[] = |
| 121 | { |
| 122 | SHADE(0.33, variable_dotsizes), |
| 123 | SHADE(1.0, variable_dotsizes) |
| 124 | }; |
| 125 | |
| 126 | |
| 127 | double compute_interval(struct timeval *tv1, struct timeval *tv2); |
| 128 | void image_init(void); |
| 129 | void image_get_row(unsigned short *data, int row); |
| 130 | void write_gray(FILE *fp, unsigned char *black); |
| 131 | void write_color(FILE *fp, unsigned char *cyan, unsigned char *magenta, |
| 132 | unsigned char *yellow, unsigned char *black); |
| 133 | void write_photo(FILE *fp, unsigned char *cyan, unsigned char *lcyan, |
| 134 | unsigned char *magenta, unsigned char *lmagenta, |
| 135 | unsigned char *yellow, unsigned char *black); |
| 136 | |
| 137 | |
| 138 | double |
| 139 | compute_interval(struct timeval *tv1, struct timeval *tv2) |
| 140 | { |
| 141 | return ((double) tv2->tv_sec + (double) tv2->tv_usec / 1000000.) - |
| 142 | ((double) tv1->tv_sec + (double) tv1->tv_usec / 1000000.); |
| 143 | } |
| 144 | |
| 145 | static void |
| 146 | writefunc(void *file, const char *buf, size_t bytes) |
| 147 | { |
| 148 | FILE *prn = (FILE *)file; |
| 149 | fwrite(buf, 1, bytes, prn); |
| 150 | } |
| 151 | |
| 152 | static int |
| 153 | image_width(stp_image_t *image) |
| 154 | { |
| 155 | (void)image; |
| 156 | return dimage_width; |
| 157 | } |
| 158 | |
| 159 | static stp_image_t theImage = |
| 160 | { |
| 161 | .width = image_width, |
| 162 | }; |
| 163 | |
| 164 | /* |
| 165 | * 'main()' - Test dithering code for performance measurement. |
| 166 | */ |
| 167 | |
| 168 | static int |
| 169 | run_one_testdither(void) |
| 170 | { |
| 171 | int print_progress = 0; |
| 172 | int i; /* Looping vars */ |
| 173 | unsigned char black[BUFFER_SIZE], /* Black bitmap data */ |
| 174 | cyan[BUFFER_SIZE], /* Cyan bitmap data */ |
| 175 | magenta[BUFFER_SIZE], /* Magenta bitmap data */ |
| 176 | lcyan[BUFFER_SIZE], /* Light cyan bitmap data */ |
| 177 | lmagenta[BUFFER_SIZE], /* Light magenta bitmap data */ |
| 178 | yellow[BUFFER_SIZE]; /* Yellow bitmap data */ |
| 179 | unsigned short rgb[MAX_IMAGE_WIDTH * 6], /* RGB buffer */ |
| 180 | gray[MAX_IMAGE_WIDTH]; /* Grayscale buffer */ |
| 181 | FILE *fp = NULL; /* PPM/PGM output file */ |
| 182 | char filename[1024]; /* Name of file */ |
| 183 | stp_vars_t *v; /* Dither variables */ |
| 184 | stp_parameter_t desc; |
| 185 | struct timeval tv1, tv2; |
| 186 | |
| 187 | /* |
| 188 | * Initialise libgutenprint |
| 189 | */ |
| 190 | |
| 191 | stp_init(); |
| 192 | v = stp_vars_create(); |
| 193 | stp_set_driver(v, "escp2-ex"); |
| 194 | stp_describe_parameter(v, "DitherAlgorithm", &desc); |
| 195 | |
| 196 | /* |
| 197 | * Setup the image and color functions... |
| 198 | */ |
| 199 | |
| 200 | image_init(); |
| 201 | stp_set_outfunc(v, writefunc); |
| 202 | stp_set_errfunc(v, writefunc); |
| 203 | stp_set_outdata(v, stdout); |
| 204 | stp_set_errdata(v, stderr); |
| 205 | |
| 206 | /* |
| 207 | * Output the page... |
| 208 | */ |
| 209 | |
| 210 | if (dither_name) |
| 211 | stp_set_string_parameter(v, "DitherAlgorithm", dither_name); |
| 212 | |
| 213 | stp_set_string_parameter(v, "ChannelBitDepth", "8"); |
| 214 | switch (dither_type) |
| 215 | { |
| 216 | case DITHER_GRAY: |
| 217 | stp_set_string_parameter(v, "PrintingMode", "BW"); |
| 218 | stp_set_string_parameter(v, "InputImageType", "Grayscale"); |
| 219 | break; |
| 220 | case DITHER_COLOR: |
| 221 | case DITHER_PHOTO: |
| 222 | stp_set_string_parameter(v, "PrintingMode", "Color"); |
| 223 | stp_set_string_parameter(v, "InputImageType", "RGB"); |
| 224 | break; |
| 225 | case DITHER_CMYK: |
| 226 | case DITHER_PHOTO_CMYK: |
| 227 | stp_set_string_parameter(v, "PrintingMode", "Color"); |
| 228 | stp_set_string_parameter(v, "InputImageType", "CMYK"); |
| 229 | break; |
| 230 | } |
| 231 | |
| 232 | stp_dither_init(v, &theImage, dimage_width, 1, 1); |
| 233 | |
| 234 | /* |
| 235 | * Now dither the "page"... |
| 236 | */ |
| 237 | |
| 238 | switch (dither_type) |
| 239 | { |
| 240 | case DITHER_PHOTO: |
| 241 | stp_dither_add_channel(v, lcyan, STP_ECOLOR_C, 1); |
| 242 | stp_dither_add_channel(v, lmagenta, STP_ECOLOR_M, 1); |
| 243 | /* FALLTHROUGH */ |
| 244 | case DITHER_COLOR: |
| 245 | stp_dither_add_channel(v, cyan, STP_ECOLOR_C, 0); |
| 246 | stp_dither_add_channel(v, magenta, STP_ECOLOR_M, 0); |
| 247 | stp_dither_add_channel(v, yellow, STP_ECOLOR_Y, 0); |
| 248 | break; |
| 249 | case DITHER_PHOTO_CMYK : |
| 250 | stp_dither_add_channel(v, lcyan, STP_ECOLOR_C, 1); |
| 251 | stp_dither_add_channel(v, lmagenta, STP_ECOLOR_M, 1); |
| 252 | /* FALLTHROUGH */ |
| 253 | case DITHER_CMYK : |
| 254 | stp_dither_add_channel(v, cyan, STP_ECOLOR_C, 0); |
| 255 | stp_dither_add_channel(v, magenta, STP_ECOLOR_M, 0); |
| 256 | stp_dither_add_channel(v, yellow, STP_ECOLOR_Y, 0); |
| 257 | /* FALLTHROUGH */ |
| 258 | case DITHER_GRAY: |
| 259 | stp_dither_add_channel(v, black, STP_ECOLOR_K, 0); |
| 260 | } |
| 261 | |
| 262 | if (dither_type == DITHER_PHOTO) |
| 263 | stp_set_float_parameter(v, "GCRLower", 0.4 / dither_bits + 0.1); |
| 264 | else |
| 265 | stp_set_float_parameter(v, "GCRLower", 0.25 / dither_bits); |
| 266 | |
| 267 | stp_set_float_parameter(v, "GCRUpper", .5); |
| 268 | |
| 269 | switch (dither_type) |
| 270 | { |
| 271 | case DITHER_GRAY : |
| 272 | switch (dither_bits) |
| 273 | { |
| 274 | case 1 : |
| 275 | stp_dither_set_inks_full(v, STP_ECOLOR_K, 1, normal_1bit_shades, 1.0, 1.0); |
| 276 | break; |
| 277 | case 2 : |
| 278 | stp_dither_set_transition(v, 0.5); |
| 279 | stp_dither_set_inks_full(v, STP_ECOLOR_K, 1, normal_2bit_shades, 1.0, 1.0); |
| 280 | break; |
| 281 | } |
| 282 | break; |
| 283 | case DITHER_COLOR : |
| 284 | switch (dither_bits) |
| 285 | { |
| 286 | case 1 : |
| 287 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 1, normal_1bit_shades, 1.0, 0.65); |
| 288 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 1, normal_1bit_shades, 1.0, 0.6); |
| 289 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_1bit_shades, 1.0, 0.08); |
| 290 | break; |
| 291 | case 2 : |
| 292 | stp_dither_set_transition(v, 0.5); |
| 293 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 1, normal_2bit_shades, 1.0, 0.65); |
| 294 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 1, normal_2bit_shades, 1.0, 0.6); |
| 295 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_2bit_shades, 1.0, 0.08); |
| 296 | break; |
| 297 | } |
| 298 | break; |
| 299 | case DITHER_CMYK : |
| 300 | switch (dither_bits) |
| 301 | { |
| 302 | case 1 : |
| 303 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 1, normal_1bit_shades, 1.0, 0.65); |
| 304 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 1, normal_1bit_shades, 1.0, 0.6); |
| 305 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_1bit_shades, 1.0, 0.08); |
| 306 | stp_dither_set_inks_full(v, STP_ECOLOR_K, 1, normal_1bit_shades, 1.0, 1.0); |
| 307 | break; |
| 308 | case 2 : |
| 309 | stp_dither_set_transition(v, 0.5); |
| 310 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 1, normal_2bit_shades, 1.0, 0.65); |
| 311 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 1, normal_2bit_shades, 1.0, 0.6); |
| 312 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_2bit_shades, 1.0, 0.08); |
| 313 | stp_dither_set_inks_full(v, STP_ECOLOR_K, 1, normal_2bit_shades, 1.0, 1.0); |
| 314 | break; |
| 315 | } |
| 316 | break; |
| 317 | case DITHER_PHOTO : |
| 318 | switch (dither_bits) |
| 319 | { |
| 320 | case 1 : |
| 321 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 2, photo_1bit_shades, 1.0, 0.65); |
| 322 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 2, photo_1bit_shades, 1.0, 0.6); |
| 323 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_1bit_shades, 1.0, 0.08); |
| 324 | break; |
| 325 | case 2 : |
| 326 | stp_dither_set_transition(v, 0.7); |
| 327 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 2, photo_2bit_shades, 1.0, 0.65); |
| 328 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 2, photo_2bit_shades, 1.0, 0.6); |
| 329 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_2bit_shades, 1.0, 0.08); |
| 330 | break; |
| 331 | } |
| 332 | break; |
| 333 | case DITHER_PHOTO_CMYK : |
| 334 | switch (dither_bits) |
| 335 | { |
| 336 | case 1 : |
| 337 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 2, photo_1bit_shades, 1.0, 0.65); |
| 338 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 2, photo_1bit_shades, 1.0, 0.6); |
| 339 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_1bit_shades, 1.0, 0.08); |
| 340 | stp_dither_set_inks_full(v, STP_ECOLOR_K, 1, normal_1bit_shades, 1.0, 1.0); |
| 341 | break; |
| 342 | case 2 : |
| 343 | stp_dither_set_transition(v, 0.7); |
| 344 | stp_dither_set_inks_full(v, STP_ECOLOR_C, 2, photo_2bit_shades, 1.0, 0.65); |
| 345 | stp_dither_set_inks_full(v, STP_ECOLOR_M, 2, photo_2bit_shades, 1.0, 0.6); |
| 346 | stp_dither_set_inks_full(v, STP_ECOLOR_Y, 1, normal_2bit_shades, 1.0, 0.08); |
| 347 | stp_dither_set_inks_full(v, STP_ECOLOR_K, 1, normal_2bit_shades, 1.0, 1.0); |
| 348 | break; |
| 349 | } |
| 350 | break; |
| 351 | } |
| 352 | |
| 353 | stp_dither_set_ink_spread(v, 12 + dither_bits); |
| 354 | |
| 355 | /* |
| 356 | * Open the PPM/PGM file... |
| 357 | */ |
| 358 | |
| 359 | |
| 360 | sprintf(filename, "%s-%s-%s-%dbit.%s", image_types[image_type], |
| 361 | dither_types[dither_type], |
| 362 | dither_name ? dither_name : desc.deflt.str, dither_bits, |
| 363 | (dither_type == DITHER_GRAY) ? "pgm" : "ppm"); |
| 364 | |
| 365 | stp_parameter_description_destroy(&desc); |
| 366 | |
| 367 | if (isatty(1)) |
| 368 | print_progress = 1; |
| 369 | |
| 370 | if (print_progress && !quiet) |
| 371 | printf("%s ", filename); |
| 372 | |
| 373 | if (write_image) |
| 374 | { |
| 375 | if ((fp = fopen(filename, "wb")) != NULL) |
| 376 | { |
| 377 | if (dither_type == DITHER_GRAY) |
| 378 | fputs("P5\n", fp); |
| 379 | else |
| 380 | fputs("P6\n", fp); |
| 381 | |
| 382 | fprintf(fp, "%d\n%d\n255\n", dimage_width, dimage_height); |
| 383 | } |
| 384 | else |
| 385 | perror("Create"); |
| 386 | } |
| 387 | |
| 388 | (void) gettimeofday(&tv1, NULL); |
| 389 | |
| 390 | for (i = 0; i < dimage_height; i ++) |
| 391 | { |
| 392 | if (print_progress && !quiet && (i & 63) == 0) |
| 393 | { |
| 394 | printf("\rProcessing row %d...", i); |
| 395 | fflush(stdout); |
| 396 | } |
| 397 | |
| 398 | switch (dither_type) |
| 399 | { |
| 400 | case DITHER_GRAY : |
| 401 | image_get_row(gray, i); |
| 402 | stp_dither_internal(v, i, gray, 0, 0, NULL); |
| 403 | if (fp) |
| 404 | write_gray(fp, black); |
| 405 | break; |
| 406 | case DITHER_COLOR : |
| 407 | case DITHER_CMYK : |
| 408 | image_get_row(rgb, i); |
| 409 | stp_dither_internal(v, i, rgb, 0, 0, NULL); |
| 410 | if (fp) |
| 411 | write_color(fp, cyan, magenta, yellow, black); |
| 412 | break; |
| 413 | case DITHER_PHOTO : |
| 414 | case DITHER_PHOTO_CMYK : |
| 415 | image_get_row(rgb, i); |
| 416 | stp_dither_internal(v, i, rgb, 0, 0, NULL); |
| 417 | if (fp) |
| 418 | write_photo(fp, cyan, lcyan, magenta, lmagenta, yellow, black); |
| 419 | break; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | (void) gettimeofday(&tv2, NULL); |
| 424 | |
| 425 | stp_vars_destroy(v); |
| 426 | |
| 427 | if (fp != NULL) |
| 428 | fclose(fp); |
| 429 | |
| 430 | if (!quiet) |
| 431 | { |
| 432 | if (print_progress) |
| 433 | fputc('\r', stdout); |
| 434 | printf("%-30s %d pix %.3f sec %.2f pix/sec\n", |
| 435 | filename, dimage_width * dimage_height, compute_interval(&tv1, &tv2), |
| 436 | (float)(dimage_width * dimage_height) / compute_interval(&tv1, &tv2)); |
| 437 | fflush(stdout); |
| 438 | } |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static int |
| 443 | run_testdither_from_cmdline(int argc, char **argv) |
| 444 | { |
| 445 | int i, j; |
| 446 | int status; |
| 447 | for (i = 1; i < argc; i ++) |
| 448 | { |
| 449 | if (strcmp(argv[i], "no-image") == 0) |
| 450 | { |
| 451 | write_image = 0; |
| 452 | continue; |
| 453 | } |
| 454 | |
| 455 | if (strcmp(argv[i], "quiet") == 0) |
| 456 | { |
| 457 | quiet = 1; |
| 458 | continue; |
| 459 | } |
| 460 | |
| 461 | if (strcmp(argv[i], "1-bit") == 0) |
| 462 | { |
| 463 | dither_bits = 1; |
| 464 | continue; |
| 465 | } |
| 466 | |
| 467 | if (strcmp(argv[i], "2-bit") == 0) |
| 468 | { |
| 469 | dither_bits = 2; |
| 470 | continue; |
| 471 | } |
| 472 | |
| 473 | if (strcmp(argv[i], "dont_regenerate_input") == 0) |
| 474 | { |
| 475 | dont_regenerate_input = 1; |
| 476 | continue; |
| 477 | } |
| 478 | |
| 479 | for (j = 0; j < 5; j ++) |
| 480 | if (strcmp(argv[i], dither_types[j]) == 0) |
| 481 | break; |
| 482 | |
| 483 | if (j < 5) |
| 484 | { |
| 485 | dither_type = j; |
| 486 | continue; |
| 487 | } |
| 488 | |
| 489 | for (j = 0; j < 5; j ++) |
| 490 | if (strcmp(argv[i], image_types[j]) == 0) |
| 491 | break; |
| 492 | |
| 493 | if (j < 5) |
| 494 | { |
| 495 | image_type = j; |
| 496 | continue; |
| 497 | } |
| 498 | |
| 499 | dither_name = argv[i]; |
| 500 | } |
| 501 | status = run_one_testdither(); |
| 502 | if (status) |
| 503 | return 1; |
| 504 | else |
| 505 | return 0; |
| 506 | } |
| 507 | |
| 508 | static int |
| 509 | run_standard_testdithers(void) |
| 510 | { |
| 511 | stp_vars_t *v = stp_vars_create(); |
| 512 | stp_parameter_t desc; |
| 513 | unsigned int j; |
| 514 | int failures = 0; |
| 515 | int status; |
| 516 | |
| 517 | stp_set_driver(v, "escp2-ex"); |
| 518 | stp_describe_parameter(v, "DitherAlgorithm", &desc); |
| 519 | |
| 520 | write_image = 0; |
| 521 | quiet = 1; |
| 522 | dont_regenerate_input = 1; |
| 523 | /* For automatic test purposes, this should suffice -- rlk 20171230 */ |
| 524 | dimage_width = dimage_width / 8; |
| 525 | dimage_height = dimage_height / 8; |
| 526 | for (j = 0; j < stp_string_list_count(desc.bounds.str); j ++) |
| 527 | { |
| 528 | dither_name = stp_string_list_param(desc.bounds.str, j)->name; |
| 529 | if (strcmp(dither_name, "None") == 0) |
| 530 | continue; |
| 531 | printf("%s", dither_name); |
| 532 | fflush(stdout); |
| 533 | for (dither_bits = 1; dither_bits <= 2; dither_bits++) |
| 534 | for (dither_type = 0; |
| 535 | dither_type < sizeof(dither_types) / sizeof(const char *); |
| 536 | dither_type++) |
| 537 | for (image_type = 0; |
| 538 | image_type < sizeof(image_types) / sizeof(const char *); |
| 539 | image_type++) |
| 540 | { |
| 541 | status = run_one_testdither(); |
| 542 | if (status) |
| 543 | { |
| 544 | printf("%s %d %s %s\n", dither_name, dither_bits, |
| 545 | dither_types[dither_type], |
| 546 | image_types[image_type]); |
| 547 | failures++; |
| 548 | } |
| 549 | else |
| 550 | printf("."); |
| 551 | fflush(stdout); |
| 552 | } |
| 553 | printf("\n"); |
| 554 | fflush(stdout); |
| 555 | } |
| 556 | stp_parameter_description_destroy(&desc); |
| 557 | stp_vars_destroy(v); |
| 558 | return (failures ? 1 : 0); |
| 559 | } |
| 560 | |
| 561 | int |
| 562 | main(int argc, char **argv) |
| 563 | { |
| 564 | stp_init(); |
| 565 | |
| 566 | if (argc == 1) |
| 567 | return run_standard_testdithers(); |
| 568 | else |
| 569 | return run_testdither_from_cmdline(argc, argv); |
| 570 | } |
| 571 | |
| 572 | |
| 573 | void |
| 574 | image_get_row(unsigned short *data, |
| 575 | int row) |
| 576 | { |
| 577 | unsigned short *src; |
| 578 | |
| 579 | switch (image_type) |
| 580 | { |
| 581 | case IMAGE_MIXED : |
| 582 | switch ((row / 100) & 3) |
| 583 | { |
| 584 | case 0 : |
| 585 | src = white_line; |
| 586 | break; |
| 587 | case 1 : |
| 588 | src = color_line; |
| 589 | break; |
| 590 | case 2 : |
| 591 | src = black_line; |
| 592 | break; |
| 593 | case 3 : |
| 594 | default: |
| 595 | src = random_line; |
| 596 | break; |
| 597 | } |
| 598 | break; |
| 599 | case IMAGE_WHITE : |
| 600 | src = white_line; |
| 601 | break; |
| 602 | case IMAGE_BLACK : |
| 603 | src = black_line; |
| 604 | break; |
| 605 | case IMAGE_COLOR : |
| 606 | src = color_line; |
| 607 | break; |
| 608 | case IMAGE_RANDOM : |
| 609 | default: |
| 610 | src = random_line; |
| 611 | break; |
| 612 | } |
| 613 | |
| 614 | switch (dither_type) |
| 615 | { |
| 616 | case DITHER_GRAY: |
| 617 | memcpy(data, src, dimage_width * 2); |
| 618 | break; |
| 619 | case DITHER_COLOR: |
| 620 | memcpy(data, src, dimage_width * 6); |
| 621 | break; |
| 622 | case DITHER_CMYK: |
| 623 | memcpy(data, src, dimage_width * 8); |
| 624 | break; |
| 625 | case DITHER_PHOTO: |
| 626 | memcpy(data, src, dimage_width * 10); |
| 627 | break; |
| 628 | case DITHER_PHOTO_CMYK: |
| 629 | memcpy(data, src, dimage_width * 12); |
| 630 | break; |
| 631 | } |
| 632 | } |
| 633 | |
| 634 | |
| 635 | /* |
| 636 | * Much faster than built-in rand(), and quite good enough. |
| 637 | * From rand(3), attributed to POSIX.1-2001. |
| 638 | */ |
| 639 | static unsigned long next = 1; |
| 640 | static unsigned |
| 641 | myrand(void) |
| 642 | { |
| 643 | next = next * 1103515245 + 12345; |
| 644 | return((unsigned)(next/65536) % 256); |
| 645 | } |
| 646 | |
| 647 | static int image_is_initialized = 0; |
| 648 | |
| 649 | void |
| 650 | image_init(void) |
| 651 | { |
| 652 | int i, j; |
| 653 | unsigned short *cptr, |
| 654 | *rptr; |
| 655 | |
| 656 | if (image_is_initialized && dont_regenerate_input) |
| 657 | return; |
| 658 | image_is_initialized = 1; |
| 659 | |
| 660 | /* |
| 661 | * Set the white and black line data... |
| 662 | */ |
| 663 | |
| 664 | memset(white_line, 0, sizeof(white_line)); |
| 665 | memset(black_line, 255, sizeof(black_line)); |
| 666 | |
| 667 | /* |
| 668 | * Fill in the color and random data... |
| 669 | */ |
| 670 | |
| 671 | for (i = dimage_width, cptr = color_line, rptr = random_line; i > 0; i --) |
| 672 | { |
| 673 | /* |
| 674 | * Do 64 color or grayscale blocks over the line... |
| 675 | */ |
| 676 | |
| 677 | j = i / (dimage_width / 64); |
| 678 | |
| 679 | switch (dither_type) |
| 680 | { |
| 681 | case DITHER_GRAY: |
| 682 | *cptr++ = 65535 * j / 63; |
| 683 | *rptr++ = 65535 * myrand() / 255; |
| 684 | break; |
| 685 | case DITHER_COLOR: |
| 686 | *cptr++ = 65535 * (j >> 4) / 3; |
| 687 | *cptr++ = 65535 * ((j >> 2) & 3) / 3; |
| 688 | *cptr++ = 65535 * (j & 3) / 3; |
| 689 | *rptr++ = 65535 * myrand() / 255; |
| 690 | *rptr++ = 65535 * myrand() / 255; |
| 691 | *rptr++ = 65535 * myrand() / 255; |
| 692 | break; |
| 693 | case DITHER_CMYK: |
| 694 | *cptr++ = 65535 * (j >> 4) / 3; |
| 695 | *cptr++ = 65535 * ((j >> 2) & 3) / 3; |
| 696 | *cptr++ = 65535 * (j & 3) / 3; |
| 697 | *cptr++ = 65535 * j / 63; |
| 698 | *rptr++ = 65535 * myrand() / 255; |
| 699 | *rptr++ = 65535 * myrand() / 255; |
| 700 | *rptr++ = 65535 * myrand() / 255; |
| 701 | *rptr++ = 65535 * myrand() / 255; |
| 702 | break; |
| 703 | case DITHER_PHOTO: |
| 704 | *cptr++ = 65535 * (j >> 4) / 3; |
| 705 | *cptr++ = 65535 * ((j >> 2) & 3) / 3; |
| 706 | *cptr++ = 65535 * (j & 3) / 3; |
| 707 | *cptr++ = 65535 * j / 63; |
| 708 | *cptr++ = 65535 * (j >> 4) / 3; |
| 709 | *rptr++ = 65535 * myrand() / 255; |
| 710 | *rptr++ = 65535 * myrand() / 255; |
| 711 | *rptr++ = 65535 * myrand() / 255; |
| 712 | *rptr++ = 65535 * myrand() / 255; |
| 713 | *rptr++ = 65535 * myrand() / 255; |
| 714 | break; |
| 715 | case DITHER_PHOTO_CMYK: |
| 716 | *cptr++ = 65535 * (j >> 4) / 3; |
| 717 | *cptr++ = 65535 * ((j >> 2) & 3) / 3; |
| 718 | *cptr++ = 65535 * (j & 3) / 3; |
| 719 | *cptr++ = 65535 * j / 63; |
| 720 | *cptr++ = 65535 * (j >> 4) / 3; |
| 721 | *cptr++ = 65535 * ((j >> 2) & 3) / 3; |
| 722 | *rptr++ = 65535 * myrand() / 255; |
| 723 | *rptr++ = 65535 * myrand() / 255; |
| 724 | *rptr++ = 65535 * myrand() / 255; |
| 725 | *rptr++ = 65535 * myrand() / 255; |
| 726 | *rptr++ = 65535 * myrand() / 255; |
| 727 | *rptr++ = 65535 * myrand() / 255; |
| 728 | break; |
| 729 | } |
| 730 | } |
| 731 | } |
| 732 | |
| 733 | |
| 734 | void |
| 735 | write_gray(FILE *fp, |
| 736 | unsigned char *black) |
| 737 | { |
| 738 | int count; |
| 739 | unsigned char byte, |
| 740 | bit, |
| 741 | shift; |
| 742 | |
| 743 | |
| 744 | if (dither_bits == 1) |
| 745 | { |
| 746 | for (count = dimage_width, byte = *black++, bit = 128; count > 0; count --) |
| 747 | { |
| 748 | if (byte & bit) |
| 749 | putc(0, fp); |
| 750 | else |
| 751 | putc(255, fp); |
| 752 | |
| 753 | if (bit > 1) |
| 754 | bit >>= 1; |
| 755 | else |
| 756 | { |
| 757 | byte = *black++; |
| 758 | bit = 128; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | else |
| 763 | { |
| 764 | unsigned char kb[BUFFER_SIZE]; |
| 765 | unsigned char *kbuf = kb; |
| 766 | stp_fold(black, dimage_width / 8, kbuf); |
| 767 | for (count = dimage_width, byte = *kbuf++, shift = 6; count > 0; count --) |
| 768 | { |
| 769 | putc(255 - 255 * ((byte >> shift) & 3) / 3, fp); |
| 770 | |
| 771 | if (shift > 0) |
| 772 | shift -= 2; |
| 773 | else |
| 774 | { |
| 775 | byte = *kbuf++; |
| 776 | shift = 6; |
| 777 | } |
| 778 | } |
| 779 | } |
| 780 | } |
| 781 | |
| 782 | |
| 783 | void |
| 784 | write_color(FILE *fp, |
| 785 | unsigned char *cyan, |
| 786 | unsigned char *magenta, |
| 787 | unsigned char *yellow, |
| 788 | unsigned char *black) |
| 789 | { |
| 790 | int count; |
| 791 | unsigned char cbyte, |
| 792 | mbyte, |
| 793 | ybyte, |
| 794 | kbyte, |
| 795 | bit, |
| 796 | shift; |
| 797 | int r, g, b, k; |
| 798 | |
| 799 | |
| 800 | if (dither_bits == 1) |
| 801 | { |
| 802 | for (count = dimage_width, cbyte = *cyan++, mbyte = *magenta++, |
| 803 | ybyte = *yellow++, kbyte = *black++, bit = 128; |
| 804 | count > 0; |
| 805 | count --) |
| 806 | { |
| 807 | if (kbyte & bit) |
| 808 | { |
| 809 | putc(0, fp); |
| 810 | putc(0, fp); |
| 811 | putc(0, fp); |
| 812 | } |
| 813 | else |
| 814 | { |
| 815 | if (cbyte & bit) |
| 816 | putc(0, fp); |
| 817 | else |
| 818 | putc(255, fp); |
| 819 | |
| 820 | if (mbyte & bit) |
| 821 | putc(0, fp); |
| 822 | else |
| 823 | putc(255, fp); |
| 824 | |
| 825 | if (ybyte & bit) |
| 826 | putc(0, fp); |
| 827 | else |
| 828 | putc(255, fp); |
| 829 | } |
| 830 | |
| 831 | if (bit > 1) |
| 832 | bit >>= 1; |
| 833 | else |
| 834 | { |
| 835 | cbyte = *cyan++; |
| 836 | mbyte = *magenta++; |
| 837 | ybyte = *yellow++; |
| 838 | kbyte = *black++; |
| 839 | bit = 128; |
| 840 | } |
| 841 | } |
| 842 | } |
| 843 | else |
| 844 | { |
| 845 | unsigned char kb[BUFFER_SIZE]; |
| 846 | unsigned char cb[BUFFER_SIZE]; |
| 847 | unsigned char mb[BUFFER_SIZE]; |
| 848 | unsigned char yb[BUFFER_SIZE]; |
| 849 | unsigned char *kbuf = kb; |
| 850 | unsigned char *cbuf = cb; |
| 851 | unsigned char *mbuf = mb; |
| 852 | unsigned char *ybuf = yb; |
| 853 | stp_fold(black, dimage_width / 8, kbuf); |
| 854 | stp_fold(cyan, dimage_width / 8, cbuf); |
| 855 | stp_fold(magenta, dimage_width / 8, mbuf); |
| 856 | stp_fold(yellow, dimage_width / 8, ybuf); |
| 857 | for (count = dimage_width, cbyte = *cbuf++, mbyte = *mbuf++, |
| 858 | ybyte = *ybuf++, kbyte = *kbuf++, shift = 6; |
| 859 | count > 0; |
| 860 | count --) |
| 861 | { |
| 862 | k = 255 * ((kbyte >> shift) & 3) / 3; |
| 863 | r = 255 - 255 * ((cbyte >> shift) & 3) / 3 - k; |
| 864 | g = 255 - 255 * ((mbyte >> shift) & 3) / 3 - k; |
| 865 | b = 255 - 255 * ((ybyte >> shift) & 3) / 3 - k; |
| 866 | |
| 867 | if (r < 0) |
| 868 | putc(0, fp); |
| 869 | else |
| 870 | putc(r, fp); |
| 871 | |
| 872 | if (g < 0) |
| 873 | putc(0, fp); |
| 874 | else |
| 875 | putc(g, fp); |
| 876 | |
| 877 | if (b < 0) |
| 878 | putc(0, fp); |
| 879 | else |
| 880 | putc(b, fp); |
| 881 | |
| 882 | if (shift > 0) |
| 883 | shift -= 2; |
| 884 | else |
| 885 | { |
| 886 | cbyte = *cbuf++; |
| 887 | mbyte = *mbuf++; |
| 888 | ybyte = *ybuf++; |
| 889 | kbyte = *kbuf++; |
| 890 | shift = 6; |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | } |
| 895 | |
| 896 | |
| 897 | void |
| 898 | write_photo(FILE *fp, |
| 899 | unsigned char *cyan, |
| 900 | unsigned char *lcyan, |
| 901 | unsigned char *magenta, |
| 902 | unsigned char *lmagenta, |
| 903 | unsigned char *yellow, |
| 904 | unsigned char *black) |
| 905 | { |
| 906 | int count; |
| 907 | unsigned char cbyte, |
| 908 | lcbyte, |
| 909 | mbyte, |
| 910 | lmbyte, |
| 911 | ybyte, |
| 912 | kbyte, |
| 913 | bit, |
| 914 | shift; |
| 915 | int r, g, b, k; |
| 916 | |
| 917 | |
| 918 | if (dither_bits == 1) |
| 919 | { |
| 920 | for (count = dimage_width, cbyte = *cyan++, lcbyte = *lcyan++, |
| 921 | mbyte = *magenta++, lmbyte = *lmagenta++, |
| 922 | ybyte = *yellow++, kbyte = *black++, bit = 128; |
| 923 | count > 0; |
| 924 | count --) |
| 925 | { |
| 926 | if (kbyte & bit) |
| 927 | { |
| 928 | putc(0, fp); |
| 929 | putc(0, fp); |
| 930 | putc(0, fp); |
| 931 | } |
| 932 | else |
| 933 | { |
| 934 | if (cbyte & bit) |
| 935 | putc(0, fp); |
| 936 | else if (lcbyte & bit) |
| 937 | putc(127, fp); |
| 938 | else |
| 939 | putc(255, fp); |
| 940 | |
| 941 | if (mbyte & bit) |
| 942 | putc(0, fp); |
| 943 | else if (lmbyte & bit) |
| 944 | putc(127, fp); |
| 945 | else |
| 946 | putc(255, fp); |
| 947 | |
| 948 | if (ybyte & bit) |
| 949 | putc(0, fp); |
| 950 | else |
| 951 | putc(255, fp); |
| 952 | } |
| 953 | |
| 954 | if (bit > 1) |
| 955 | bit >>= 1; |
| 956 | else |
| 957 | { |
| 958 | cbyte = *cyan++; |
| 959 | lcbyte = *lcyan++; |
| 960 | mbyte = *magenta++; |
| 961 | lmbyte = *lmagenta++; |
| 962 | ybyte = *yellow++; |
| 963 | kbyte = *black++; |
| 964 | bit = 128; |
| 965 | } |
| 966 | } |
| 967 | } |
| 968 | else |
| 969 | { |
| 970 | unsigned char kb[BUFFER_SIZE]; |
| 971 | unsigned char cb[BUFFER_SIZE]; |
| 972 | unsigned char mb[BUFFER_SIZE]; |
| 973 | unsigned char lcb[BUFFER_SIZE]; |
| 974 | unsigned char lmb[BUFFER_SIZE]; |
| 975 | unsigned char yb[BUFFER_SIZE]; |
| 976 | unsigned char *kbuf = kb; |
| 977 | unsigned char *cbuf = cb; |
| 978 | unsigned char *mbuf = mb; |
| 979 | unsigned char *lcbuf = lcb; |
| 980 | unsigned char *lmbuf = lmb; |
| 981 | unsigned char *ybuf = yb; |
| 982 | stp_fold(black, dimage_width / 8, kbuf); |
| 983 | stp_fold(cyan, dimage_width / 8, cbuf); |
| 984 | stp_fold(magenta, dimage_width / 8, mbuf); |
| 985 | stp_fold(yellow, dimage_width / 8, ybuf); |
| 986 | stp_fold(lcyan, dimage_width / 8, lcbuf); |
| 987 | stp_fold(lmagenta, dimage_width / 8, lmbuf); |
| 988 | for (count = dimage_width, cbyte = *cbuf++, mbyte = *mbuf++, |
| 989 | ybyte = *ybuf++, kbyte = *kbuf++, lmbyte = *lmbuf++, |
| 990 | lcbyte = *lcyan++, shift = 6; |
| 991 | count > 0; |
| 992 | count --) |
| 993 | { |
| 994 | k = 255 * ((kbyte >> shift) & 3) / 3; |
| 995 | r = 255 - 255 * ((cbyte >> shift) & 3) / 3 - |
| 996 | 127 * ((lcbyte >> shift) & 3) / 3 - k; |
| 997 | g = 255 - 255 * ((mbyte >> shift) & 3) / 3 - |
| 998 | 127 * ((lmbyte >> shift) & 3) / 3 - k; |
| 999 | b = 255 - 255 * ((ybyte >> shift) & 3) / 3 - k; |
| 1000 | |
| 1001 | if (r < 0) |
| 1002 | putc(0, fp); |
| 1003 | else |
| 1004 | putc(r, fp); |
| 1005 | |
| 1006 | if (g < 0) |
| 1007 | putc(0, fp); |
| 1008 | else |
| 1009 | putc(g, fp); |
| 1010 | |
| 1011 | if (b < 0) |
| 1012 | putc(0, fp); |
| 1013 | else |
| 1014 | putc(b, fp); |
| 1015 | |
| 1016 | if (shift > 0) |
| 1017 | shift -= 2; |
| 1018 | else |
| 1019 | { |
| 1020 | cbyte = *cbuf++; |
| 1021 | mbyte = *mbuf++; |
| 1022 | ybyte = *ybuf++; |
| 1023 | kbyte = *kbuf++; |
| 1024 | lmbyte = *lmbuf++; |
| 1025 | lcbyte = *lcbuf++; |
| 1026 | shift = 6; |
| 1027 | } |
| 1028 | } |
| 1029 | } |
| 1030 | } |