Debian Codegraph @

src/testpattern/testpattern.c

raw symbols SCIP
1 /*
2 *
3 * Test pattern generator for Gutenprint
4 *
5 * Copyright 2001 Robert Krawitz <[email protected]>
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the Free
9 * Software Foundation; either version 2 of the License, or (at your option)
10 * any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 * for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
19 */
20
21 /*
22 * This sample program may be used to generate test patterns. It also
23 * serves as an example of how to use the Gutenprint API.
24 *
25 * As the purpose of this program is to allow fine grained control over
26 * the output, it uses the raw CMYK output type. This feeds 16 bits each
27 * of CMYK to the driver. This mode performs no correction on the data;
28 * it passes it directly to the dither engine, performing no color,
29 * density, gamma, etc. correction. Most programs will use one of the
30 * other modes (RGB, density and gamma corrected 8-bit CMYK, grayscale, or
31 * black and white).
32 */
33
34 #include <stdlib.h>
35 #include <unistd.h>
36 #include <stdio.h>
37 #include <math.h>
38 #include <string.h>
39 #include "testpattern.h"
40 #include <gutenprint/gutenprint-intl.h>
41 #include <errno.h>
42
43 #pragma GCC diagnostic ignored "-Wredundant-decls"
44
45 extern int yyparse(void);
46
47 static const char *Image_get_appname(stp_image_t *image);
48 static void Image_conclude(stp_image_t *image);
49 static void Image_init(stp_image_t *image);
50 static stp_image_status_t Image_get_row(stp_image_t *image,
51 unsigned char *data,
52 size_t byte_limit, int row);
53 static int Image_height(stp_image_t *image);
54 static void Image_reset(stp_image_t *image);
55 static int Image_width(stp_image_t *image);
56 static int Image_is_valid = 0;
57 static stp_image_t theImage =
58 {
59 Image_init,
60 Image_reset,
61 Image_width,
62 Image_height,
63 Image_get_row,
64 Image_get_appname,
65 Image_conclude,
66 NULL
67 };
68 stp_vars_t *global_vars = NULL;
69
70 double global_levels[STP_CHANNEL_LIMIT];
71 double global_gammas[STP_CHANNEL_LIMIT];
72 double global_gamma;
73 int global_steps;
74 int global_halt_on_error = 0;
75 double global_ink_limit;
76 int global_noblackline;
77 int global_colorline;
78 int global_printer_width;
79 int global_printer_height;
80 int global_band_height;
81 int global_n_testpatterns;
82 char *global_printer = NULL;
83 double global_density;
84 double global_xtop;
85 double global_xleft;
86 size_mode_t global_size_mode;
87 double global_hsize;
88 double global_vsize;
89 const char *global_image_type;
90 int global_bit_depth;
91 int global_channel_depth;
92 int global_invert_data = 0;
93 int global_use_raw_cmyk;
94 int global_did_something;
95 int global_noscale = 0;
96 int global_suppress_output = 0;
97 int global_quiet = 0;
98 int global_fail_verify_ok = 0;
99 int global_round_size = 0;
100 char *global_output = NULL;
101 FILE *output = NULL;
102 int write_to_process = 0;
103 int start_job = 0;
104 int end_job = 0;
105 int passes = 0;
106 int failures = 0;
107 int skipped = 0;
108 size_t bytes_written = 0;
109
110 static testpattern_t *static_testpatterns;
111
112 static size_t
113 c_strlen(const char *s)
114 {
115 return strlen(s);
116 }
117
118 static double
119 roundto(double n)
120 {
121 if (global_round_size)
122 return (double) ((int) n);
123 else
124 return n;
125 }
126
127 static char *
128 c_strndup(const char *s, int n)
129 {
130 char *ret;
131 if (!s || n < 0)
132 {
133 ret = stp_malloc(1);
134 ret[0] = 0;
135 return ret;
136 }
137 else
138 {
139 ret = stp_malloc(n + 1);
140 memcpy(ret, s, n);
141 ret[n] = 0;
142 return ret;
143 }
144 }
145
146 char *
147 c_strdup(const char *s)
148 {
149 char *ret;
150 if (!s)
151 {
152 ret = stp_malloc(1);
153 ret[0] = 0;
154 return ret;
155 }
156 else
157 return c_strndup(s, c_strlen(s));
158 }
159
160 static void
161 clear_testpattern(testpattern_t *p)
162 {
163 int i;
164 for (i = 0; i < STP_CHANNEL_LIMIT; i++)
165 {
166 p->d.pattern.mins[i] = 0;
167 p->d.pattern.vals[i] = 0;
168 p->d.pattern.gammas[i] = 1;
169 p->d.pattern.levels[i] = 0;
170 }
171 }
172
173
174 testpattern_t *
175 get_next_testpattern(void)
176 {
177 static int internal_n_testpatterns = 0;
178 if (global_n_testpatterns == -1)
179 {
180 static_testpatterns = stp_malloc(sizeof(testpattern_t));
181 global_n_testpatterns = 0;
182 internal_n_testpatterns = 1;
183 clear_testpattern(&(static_testpatterns[0]));
184 return &(static_testpatterns[0]);
185 }
186 else if (global_n_testpatterns + 1 >= internal_n_testpatterns)
187 {
188 internal_n_testpatterns *= 2;
189 static_testpatterns =
190 stp_realloc(static_testpatterns,
191 internal_n_testpatterns * sizeof(testpattern_t));
192 }
193 global_n_testpatterns++;
194 clear_testpattern(&(static_testpatterns[global_n_testpatterns]));
195 return &(static_testpatterns[global_n_testpatterns]);
196 }
197
198 static void
199 writefunc(void *file, const char *buf, size_t bytes)
200 {
201 FILE *prn = (FILE *)file;
202 bytes_written += bytes;
203 if (! file)
204 return;
205 if (file == stderr)
206 {
207 if (!global_quiet)
208 fwrite(buf, 1, bytes, prn);
209 }
210 else if (!global_suppress_output)
211 {
212 fwrite(buf, 1, bytes, prn);
213 }
214 }
215
216 void
217 close_output(void)
218 {
219 if (output && output != stdout)
220 {
221 if (write_to_process)
222 (void) pclose(output);
223 else
224 (void) fclose(output);
225 output = NULL;
226 }
227 }
228
229 static void
230 initialize_global_parameters(void)
231 {
232 int i;
233 for (i = 0; i < STP_CHANNEL_LIMIT; i++)
234 {
235 global_levels[i] = 1.0;
236 global_gammas[i] = 1.0;
237 }
238 global_gamma = 1.0;
239 global_steps = 256;
240 global_ink_limit = 1.0;
241 global_noblackline = 0;
242 global_printer_width = 0;
243 global_printer_height = 0;
244 global_band_height = 0;
245 global_n_testpatterns = -1;
246 if (global_printer)
247 free(global_printer); /* Allocated with strdup() */
248 global_printer = NULL;
249 global_density = 1.0;
250 global_xtop = 0;
251 global_xleft = 0;
252 global_hsize = 1.0;
253 global_vsize = 1.0;
254 global_bit_depth = 16;
255 global_channel_depth = 0;
256 global_image_type = "CMYK";
257 global_use_raw_cmyk = 0;
258 global_did_something = 0;
259 global_invert_data = 0;
260 if (static_testpatterns)
261 stp_free(static_testpatterns);
262 static_testpatterns = NULL;
263 if (global_vars)
264 stp_vars_destroy(global_vars);
265 global_vars = NULL;
266 if (global_printer)
267 free(global_printer); /* Allocated with strdup() */
268 global_printer = NULL;
269 start_job = 0;
270 end_job = 0;
271 }
272
273 static int
274 do_print(void)
275 {
276 int status = 0;
277 stp_vars_t *v;
278 const stp_printer_t *the_printer;
279 stp_dimension_t left, right, top, bottom;
280 stp_resolution_t x, y;
281 stp_dimension_t width, height;
282 int retval;
283 stp_parameter_list_t params;
284 int count;
285 int i;
286 char tmp[32];
287
288 initialize_global_parameters();
289 global_vars = stp_vars_create();
290 stp_set_outfunc(global_vars, writefunc);
291 stp_set_errfunc(global_vars, writefunc);
292 stp_set_outdata(global_vars, stdout);
293 stp_set_errdata(global_vars, stderr);
294
295 setlocale(LC_ALL, "C");
296 retval = yyparse();
297 setlocale(LC_ALL, "");
298 if (retval)
299 return retval + 1;
300
301 if (!global_did_something)
302 return 1;
303
304 v = stp_vars_create();
305 the_printer = stp_get_printer_by_driver(global_printer);
306 if (!the_printer)
307 {
308 int j;
309 fprintf(stderr, "Unknown printer %s\nValid printers are:\n",
310 global_printer);
311 for (j = 0; j < stp_printer_model_count(); j++)
312 {
313 the_printer = stp_get_printer_by_index(j);
314 fprintf(stderr, "%-16s%s\n", stp_printer_get_driver(the_printer),
315 stp_printer_get_long_name(the_printer));
316 }
317 return 2;
318 }
319 bytes_written = 0;
320 if (global_output)
321 {
322 if (strcmp(global_output, "-") == 0)
323 output = stdout;
324 else if (strcmp(global_output, "") == 0)
325 output = NULL;
326 else if (global_output[0] == '|')
327 {
328 close_output();
329 write_to_process = 1;
330 output = popen(global_output+1, "w");
331 if (! output)
332 {
333 fprintf(stderr, "popen '%s' failed: %s\n", global_output, strerror(errno));
334 output = NULL;
335 }
336 free(global_output);
337 global_output = NULL;
338 }
339 else
340 {
341 close_output();
342 output = fopen(global_output, "wb");
343 if (! output)
344 {
345 fprintf(stderr, "Create %s failed: %s\n", global_output, strerror(errno));
346 output = NULL;
347 }
348 free(global_output);
349 global_output = NULL;
350 }
351 }
352 stp_set_printer_defaults(v, the_printer);
353 stp_set_outfunc(v, writefunc);
354 stp_set_errfunc(v, writefunc);
355 stp_set_outdata(v, output);
356 stp_set_errdata(v, stderr);
357 stp_set_string_parameter(v, "InputImageType", global_image_type);
358 sprintf(tmp, "%d", global_bit_depth);
359 stp_set_string_parameter(v, "ChannelBitDepth", tmp);
360 if (strcmp(global_image_type, "Raw") == 0)
361 {
362 sprintf(tmp, "%d", global_channel_depth);
363 stp_set_string_parameter(v, "RawChannels", tmp);
364 }
365 stp_set_float_parameter(v, "Density", global_density);
366 stp_set_string_parameter(v, "Quality", "None");
367 stp_set_string_parameter(v, "ImageType", "None");
368
369 params = stp_get_parameter_list(v);
370 count = stp_parameter_list_count(params);
371 for (i = 0; i < count; i++)
372 {
373 const stp_parameter_t *p = stp_parameter_list_param(params, i);
374 if (p->p_type == STP_PARAMETER_TYPE_STRING_LIST)
375 {
376 const char *val = stp_get_string_parameter(global_vars, p->name);
377 if (val && strlen(val) > 0)
378 {
379 stp_set_string_parameter(v, p->name, val);
380 }
381 }
382 else if (p->p_type == STP_PARAMETER_TYPE_INT &&
383 stp_check_int_parameter(global_vars, p->name, STP_PARAMETER_ACTIVE))
384 {
385 int val = stp_get_int_parameter(global_vars, p->name);
386 stp_set_int_parameter(v, p->name, val);
387 }
388 else if (p->p_type == STP_PARAMETER_TYPE_BOOLEAN &&
389 stp_check_boolean_parameter(global_vars, p->name, STP_PARAMETER_ACTIVE))
390 {
391 int val = stp_get_boolean_parameter(global_vars, p->name);
392 stp_set_boolean_parameter(v, p->name, val);
393 }
394 else if (p->p_type == STP_PARAMETER_TYPE_CURVE &&
395 stp_check_curve_parameter(global_vars, p->name, STP_PARAMETER_ACTIVE))
396 {
397 const stp_curve_t *val = stp_get_curve_parameter(global_vars, p->name);
398 stp_set_curve_parameter(v, p->name, val);
399 }
400 else if (p->p_type == STP_PARAMETER_TYPE_DOUBLE &&
401 stp_check_float_parameter(global_vars, p->name, STP_PARAMETER_ACTIVE))
402 {
403 double val = stp_get_float_parameter(global_vars, p->name);
404 stp_set_float_parameter(v, p->name, val);
405 }
406 }
407 stp_set_page_width(v, stp_get_page_width(global_vars));
408 stp_set_page_height(v, stp_get_page_height(global_vars));
409 stp_parameter_list_destroy(params);
410 if (stp_check_string_parameter(v, "PageSize", STP_PARAMETER_ACTIVE) &&
411 !strcmp(stp_get_string_parameter(v, "PageSize"), "Auto"))
412 {
413 stp_parameter_t desc;
414 stp_describe_parameter(v, "PageSize", &desc);
415 if (desc.p_type == STP_PARAMETER_TYPE_STRING_LIST)
416 stp_set_string_parameter(v, "PageSize", desc.deflt.str);
417 stp_parameter_description_destroy(&desc);
418 }
419 stp_set_printer_defaults_soft(v, the_printer);
420
421 stp_get_imageable_area(v, &left, &right, &bottom, &top);
422 left = roundto(left);
423 right = roundto(right);
424 bottom = roundto(bottom);
425 top = roundto(top);
426 stp_describe_resolution(v, &x, &y);
427 if (x < 0)
428 x = 300;
429 if (y < 0)
430 y = 300;
431
432 width = roundto(right - left);
433 height = roundto(bottom - top);
434
435 switch (global_size_mode)
436 {
437 case SIZE_PT:
438 top += roundto((int) (global_xtop + .5));
439 left += roundto((int) (global_xleft + .5));
440 width = roundto((int) (global_hsize + .5));
441 height = roundto((int) (global_vsize + .5));
442 break;
443 case SIZE_IN:
444 top += roundto((int) ((global_xtop * 72) + .5));
445 left += roundto((int) ((global_xleft * 72) + .5));
446 width = roundto((int) ((global_hsize * 72) + .5));
447 height = roundto((int) ((global_vsize * 72) + .5));
448 break;
449 case SIZE_MM:
450 top += roundto((int) ((global_xtop * 72 / 25.4) + .5));
451 left += roundto((int) ((global_xleft * 72 / 25.4) + .5));
452 width = roundto((int) ((global_hsize * 72 / 25.4) + .5));
453 height = roundto((int) ((global_vsize * 72 / 25.4) + .5));
454 break;
455 case SIZE_RELATIVE:
456 default:
457 top = roundto(top + (height * global_xtop));
458 left = roundto(left + (width * global_xleft));
459 width = roundto(width * global_hsize);
460 height = roundto(height * global_vsize);
461 break;
462 }
463 if (width < 1)
464 width = 1;
465 if (height < 1)
466 height = 1;
467
468 stp_set_width(v, width);
469 stp_set_height(v, height);
470 #if 0
471 width = (width / global_steps) * global_steps;
472 height = (height / global_n_testpatterns) * global_n_testpatterns;
473 #endif
474 if (global_steps > width)
475 global_steps = width;
476
477 global_printer_width = width * x / 72;
478 global_printer_height = height * y / 72;
479
480 global_band_height = global_printer_height / global_n_testpatterns;
481 if (global_band_height == 0)
482 global_band_height = 1;
483 stp_set_left(v, left);
484 stp_set_top(v, top);
485
486 stp_merge_printvars(v, stp_printer_get_defaults(the_printer));
487 if (stp_verify(v))
488 {
489 bytes_written = 0;
490 if (start_job)
491 {
492 stp_start_job(v, &theImage);
493 start_job = 0;
494 }
495 if (stp_print(v, &theImage) != 1)
496 {
497 if (!global_quiet)
498 fputs("FAILED", stderr);
499 failures++;
500 status = 2;
501 if (global_halt_on_error)
502 return status;
503 }
504 else if (bytes_written == 0)
505 {
506 if (!global_quiet)
507 fputs("FAILED: No output", stderr);
508 failures++;
509 status = 2;
510 if (global_halt_on_error)
511 return status;
512 }
513 else
514 passes++;
515 if (end_job)
516 {
517 stp_end_job(v, &theImage);
518 end_job = 0;
519 }
520 }
521 else
522 {
523 if (! global_fail_verify_ok)
524 {
525 if (!global_quiet)
526 fputs("FAILED", stderr);
527 failures++;
528 status = 2;
529 if (global_halt_on_error)
530 return status;
531 }
532 else
533 {
534 if (!global_quiet)
535 fputs("(skipped)", stderr);
536 skipped++;
537 }
538 }
539 if (!global_quiet)
540 fputc('\n', stderr);
541 stp_vars_destroy(v);
542 stp_free(static_testpatterns);
543 static_testpatterns = NULL;
544 return status;
545 }
546
547 int
548 main(int argc, char **argv)
549 {
550 int c;
551 int status;
552 int global_status = 0;
553 while (1)
554 {
555 c = getopt(argc, argv, "nqyH");
556 if (c == -1)
557 break;
558 switch (c)
559 {
560 case 'n':
561 global_suppress_output = 1;
562 break;
563 case 'q':
564 global_quiet = 1;
565 break;
566 case 'y':
567 global_fail_verify_ok = 1;
568 break;
569 case 'H':
570 global_halt_on_error = 1;
571 break;
572 default:
573 break;
574 }
575 }
576
577 stp_init();
578 output = stdout;
579 while (1)
580 {
581 status = do_print();
582 if (status == 1)
583 break;
584 else if (status != 0)
585 global_status = 1;
586 }
587 close_output();
588 if (passes + failures + skipped > 1)
589 fprintf(stderr, "%d pass, %d fail, %d skipped\n", passes, failures, skipped);
590 return global_status;
591 }
592
593 static void
594 invert_data(unsigned char *data, size_t byte_depth)
595 {
596 size_t i;
597 size_t total_bytes;
598 total_bytes = global_printer_width * global_channel_depth * byte_depth;
599 for (i = 0; i < total_bytes; i++)
600 data[i] = 0xff ^ data[i];
601 }
602
603 /*
604 * Emulate templates with macros -- rlk 20031014
605 */
606
607 #define FILL_CHANNELS_FUNCTION(T, bits) \
608 static void \
609 fill_channels_##bits(unsigned char *data, size_t len, size_t scount) \
610 { \
611 size_t i; \
612 size_t c; \
613 scount = global_channel_depth; \
614 T *s_data = (T *) data; \
615 unsigned black_val = global_ink_limit * ((1 << bits) - 1); \
616 unsigned blocksize = len / scount; \
617 unsigned blocks = blocksize * scount; \
618 unsigned extra = len - blocks; \
619 memset(s_data, 0, sizeof(T) * len * scount); \
620 for (c = 0; c < scount; c++) \
621 { \
622 for (i = 0; i < blocksize; i++) \
623 { \
624 s_data[c] = black_val; \
625 s_data += global_channel_depth; \
626 } \
627 } \
628 memset(s_data, 0xff, sizeof(T) * extra * \
629 global_channel_depth); \
630 }
631
632 FILL_CHANNELS_FUNCTION(unsigned short, 16)
633 FILL_CHANNELS_FUNCTION(unsigned char, 8)
634
635 static void
636 fill_channels(unsigned char *data, size_t len, size_t scount, size_t bytes)
637 {
638 switch (bytes)
639 {
640 case 1:
641 fill_channels_8(data, len, scount);
642 break;
643 case 2:
644 fill_channels_16(data, len, scount);
645 break;
646 }
647 if (global_invert_data)
648 invert_data(data, bytes);
649 }
650
651 #define FILL_BLACK_FUNCTION(T, bits) \
652 static void \
653 fill_black_##bits(unsigned char *data, size_t len, size_t scount) \
654 { \
655 size_t i; \
656 (void)scount; \
657 T *s_data = (T *) data; \
658 unsigned black_val = global_ink_limit * ((1 << bits) - 1); \
659 unsigned blocks = (len / scount) * scount; \
660 unsigned extra = len - blocks; \
661 if (strcmp(global_image_type, "Raw") == 0) \
662 { \
663 for (i = 0; i < (len / scount) * scount; i++) \
664 { \
665 memset(s_data, 0, sizeof(T) * global_channel_depth); \
666 s_data[0] = black_val; \
667 if (global_channel_depth == 3) \
668 { \
669 s_data[1] = black_val; \
670 s_data[2] = black_val; \
671 } \
672 else if (global_channel_depth == 5) \
673 { \
674 s_data[2] = black_val; \
675 s_data[4] = black_val; \
676 } \
677 s_data += global_channel_depth; \
678 } \
679 memset(s_data, 0xff, sizeof(T) * extra * \
680 global_channel_depth); \
681 } \
682 else if (strcmp(global_image_type, "RGB") == 0) \
683 { \
684 for (i = 0; i < (len / scount) * scount; i++) \
685 { \
686 memset(s_data, 0, sizeof(T) * 3); \
687 s_data += 3; \
688 } \
689 memset(s_data, 0, sizeof(T) * extra * 3); \
690 } \
691 else if (strcmp(global_image_type, "CMY") == 0) \
692 { \
693 for (i = 0; i < (len / scount) * scount; i++) \
694 { \
695 s_data[0] = black_val; \
696 s_data[1] = black_val; \
697 s_data[2] = black_val; \
698 s_data += 3; \
699 } \
700 memset(s_data, 0, sizeof(T) * extra * 3); \
701 } \
702 else if (strcmp(global_image_type, "CMYK") == 0) \
703 { \
704 for (i = 0; i < (len / scount) * scount; i++) \
705 { \
706 memset(s_data, 0, sizeof(T) * 4); \
707 s_data[3] = black_val; \
708 s_data += 4; \
709 } \
710 memset(s_data, 0, sizeof(T) * extra * 4); \
711 } \
712 else if (strcmp(global_image_type, "KCMY") == 0) \
713 { \
714 for (i = 0; i < (len / scount) * scount; i++) \
715 { \
716 memset(s_data, 0, sizeof(T) * 4); \
717 s_data[0] = black_val; \
718 s_data += 4; \
719 } \
720 memset(s_data, 0, sizeof(T) * extra * 4); \
721 } \
722 else if (strcmp(global_image_type, "Grayscale") == 0) \
723 { \
724 for (i = 0; i < (len / scount) * scount; i++) \
725 { \
726 memset(s_data, 0, sizeof(T) * 1); \
727 s_data[0] = black_val; \
728 s_data += 1; \
729 } \
730 memset(s_data, 0, sizeof(T) * extra); \
731 } \
732 else if (strcmp(global_image_type, "Whitescale") == 0) \
733 { \
734 for (i = 0; i < (len / scount) * scount; i++) \
735 { \
736 memset(s_data, 0, sizeof(T) * 1); \
737 s_data += 1; \
738 } \
739 memset(s_data, 0xff, sizeof(T) * extra); \
740 } \
741 }
742
743 FILL_BLACK_FUNCTION(unsigned short, 16)
744 FILL_BLACK_FUNCTION(unsigned char, 8)
745
746 static void
747 fill_black(unsigned char *data, size_t len, size_t scount, size_t bytes)
748 {
749 if (global_colorline)
750 fill_channels(data, len, global_channel_depth, bytes);
751 else
752 {
753 switch (bytes)
754 {
755 case 1:
756 fill_black_8(data, len, scount);
757 break;
758 case 2:
759 fill_black_16(data, len, scount);
760 break;
761 }
762 if (global_invert_data)
763 invert_data(data, bytes);
764 }
765 }
766
767 #define FILL_WHITE_FUNCTION(T, bits) \
768 static void \
769 fill_white_##bits(unsigned char *data, size_t len, size_t scount) \
770 { \
771 size_t i; \
772 (void)scount; \
773 T *s_data = (T *) data; \
774 if (strcmp(global_image_type, "Raw") == 0) \
775 { \
776 for (i = 0; i < len; i++) \
777 { \
778 memset(s_data, 0, sizeof(T) * global_channel_depth); \
779 s_data += global_channel_depth; \
780 } \
781 } \
782 else if (strcmp(global_image_type, "RGB") == 0) \
783 { \
784 for (i = 0; i < len; i++) \
785 { \
786 memset(s_data, 0xff, sizeof(T) * 3); \
787 s_data += 3; \
788 } \
789 } \
790 else if (strcmp(global_image_type, "CMY") == 0) \
791 { \
792 for (i = 0; i < len; i++) \
793 { \
794 memset(s_data, 0, sizeof(T) * 3); \
795 s_data += 3; \
796 } \
797 } \
798 else if (strcmp(global_image_type, "CMYK") == 0) \
799 { \
800 for (i = 0; i < len; i++) \
801 { \
802 memset(s_data, 0, sizeof(T) * 4); \
803 s_data += 4; \
804 } \
805 } \
806 else if (strcmp(global_image_type, "KCMY") == 0) \
807 { \
808 for (i = 0; i < len; i++) \
809 { \
810 memset(s_data, 0, sizeof(T) * 4); \
811 s_data += 4; \
812 } \
813 } \
814 else if (strcmp(global_image_type, "Grayscale") == 0) \
815 { \
816 for (i = 0; i < len; i++) \
817 { \
818 memset(s_data, 0, sizeof(T) * 1); \
819 s_data += 1; \
820 } \
821 } \
822 else if (strcmp(global_image_type, "Whitescale") == 0) \
823 { \
824 for (i = 0; i < len; i++) \
825 { \
826 memset(s_data, 0xff, sizeof(T) * 1); \
827 s_data += 1; \
828 } \
829 } \
830 }
831
832 FILL_WHITE_FUNCTION(unsigned short, 16)
833 FILL_WHITE_FUNCTION(unsigned char, 8)
834
835 static void
836 fill_white(unsigned char *data, size_t len, size_t scount, size_t bytes)
837 {
838 switch (bytes)
839 {
840 case 1:
841 fill_white_8(data, len, scount);
842 break;
843 case 2:
844 fill_white_16(data, len, scount);
845 break;
846 }
847 if (global_invert_data)
848 invert_data(data, bytes);
849 }
850
851 #define FILL_GRID_FUNCTION(T, bits) \
852 static void \
853 fill_grid_##bits(unsigned char *data, size_t len, size_t scount, \
854 testpattern_t *p) \
855 { \
856 int i; \
857 int xlen = (len / scount) * scount; \
858 int errdiv = (p->d.grid.ticks) / (xlen - 1); \
859 int errmod = (p->d.grid.ticks) % (xlen - 1); \
860 int errval = 0; \
861 int errlast = -1; \
862 int errline = 0; \
863 T *s_data = (T *) data; \
864 unsigned multiplier = (1 << bits) - 1; \
865 \
866 for (i = 0; i < xlen; i++) \
867 { \
868 if (errline != errlast) \
869 { \
870 errlast = errline; \
871 s_data[0] = multiplier; \
872 } \
873 else \
874 s_data[0] = 0; \
875 errval += errmod; \
876 errline += errdiv; \
877 if (errval >= xlen - 1) \
878 { \
879 errval -= xlen - 1; \
880 errline++; \
881 } \
882 s_data += global_channel_depth; \
883 } \
884 }
885
886 FILL_GRID_FUNCTION(unsigned short, 16)
887 FILL_GRID_FUNCTION(unsigned char, 8)
888
889 static void
890 fill_grid(unsigned char *data, size_t len, size_t scount,
891 testpattern_t *p, size_t bytes)
892 {
893 switch (bytes)
894 {
895 case 1:
896 fill_grid_8(data, len, scount, p);
897 break;
898 case 2:
899 fill_grid_16(data, len, scount, p);
900 break;
901 }
902 }
903
904 #define FILL_COLORS_EXTENDED_FUNCTION(T, bits) \
905 static void \
906 fill_colors_extended_##bits(unsigned char *data, size_t len, \
907 size_t scount, testpattern_t *p) \
908 { \
909 double mins[STP_CHANNEL_LIMIT]; \
910 double vals[STP_CHANNEL_LIMIT]; \
911 double gammas[STP_CHANNEL_LIMIT]; \
912 size_t i; \
913 int j; \
914 int k; \
915 int pixels; \
916 int channel_limit = \
917 global_channel_depth <= 7 ? 7 : global_channel_depth; \
918 T *s_data = (T *) data; \
919 unsigned multiplier = global_noscale ? 1 : (1 << bits) - 1; \
920 \
921 for (j = 0; j < channel_limit; j++) \
922 { \
923 mins[j] = p->d.pattern.mins[j] == -2 ? \
924 global_levels[j] : p->d.pattern.mins[j]; \
925 vals[j] = p->d.pattern.vals[j] == -2 ? \
926 global_levels[j] : p->d.pattern.vals[j]; \
927 gammas[j] = \
928 p->d.pattern.gammas[j] * global_gamma * global_gammas[j]; \
929 vals[j] -= mins[j]; \
930 } \
931 if (scount > len) \
932 scount = len; \
933 pixels = len / scount; \
934 for (i = 0; i < scount; i++) \
935 { \
936 double where = (double) i / ((double) scount - 1); \
937 double val = where; \
938 double xvals[STP_CHANNEL_LIMIT]; \
939 \
940 for (j = 0; j < channel_limit; j++) \
941 { \
942 xvals[j] = mins[j] + val * vals[j]; \
943 xvals[j] = pow(xvals[j], gammas[j]); \
944 xvals[j] *= global_ink_limit * multiplier; \
945 xvals[j] += 0.9; \
946 } \
947 for (k = 0; k < pixels; k++) \
948 { \
949 switch (global_channel_depth) \
950 { \
951 case 1: \
952 s_data[0] = xvals[0]; \
953 break; \
954 case 2: \
955 s_data[0] = xvals[0]; \
956 s_data[1] = xvals[4]; \
957 break; \
958 case 3: \
959 s_data[0] = xvals[1]; \
960 s_data[1] = xvals[2]; \
961 s_data[2] = xvals[3]; \
962 break; \
963 case 4: \
964 s_data[0] = xvals[0]; \
965 s_data[1] = xvals[1]; \
966 s_data[2] = xvals[2]; \
967 s_data[3] = xvals[3]; \
968 break; \
969 case 5: \
970 s_data[0] = xvals[1]; \
971 s_data[1] = xvals[5]; \
972 s_data[2] = xvals[2]; \
973 s_data[3] = xvals[6]; \
974 s_data[4] = xvals[3]; \
975 break; \
976 case 6: \
977 s_data[0] = xvals[0]; \
978 s_data[1] = xvals[1]; \
979 s_data[2] = xvals[5]; \
980 s_data[3] = xvals[2]; \
981 s_data[4] = xvals[6]; \
982 s_data[5] = xvals[3]; \
983 break; \
984 case 7: \
985 s_data[0] = xvals[0]; \
986 s_data[1] = xvals[4]; \
987 s_data[2] = xvals[1]; \
988 s_data[3] = xvals[5]; \
989 s_data[4] = xvals[2]; \
990 s_data[5] = xvals[6]; \
991 s_data[6] = xvals[3]; \
992 break; \
993 default: \
994 for (j = 0; j < global_channel_depth; j++) \
995 s_data[j] = xvals[j]; \
996 } \
997 s_data += global_channel_depth; \
998 } \
999 } \
1000 }
1001
1002 FILL_COLORS_EXTENDED_FUNCTION(unsigned short, 16)
1003 FILL_COLORS_EXTENDED_FUNCTION(unsigned char, 8)
1004
1005 static void
1006 fill_colors_extended(unsigned char *data, size_t len, size_t scount,
1007 testpattern_t *p, size_t bytes)
1008 {
1009 switch (bytes)
1010 {
1011 case 1:
1012 fill_colors_extended_8(data, len, scount, p);
1013 break;
1014 case 2:
1015 fill_colors_extended_16(data, len, scount, p);
1016 break;
1017 }
1018 }
1019
1020 #define FILL_COLORS_FUNCTION(T, bits) \
1021 static void \
1022 fill_colors_##bits(unsigned char *data, size_t len, size_t scount, \
1023 testpattern_t *p) \
1024 { \
1025 double mins[4]; \
1026 double vals[4]; \
1027 double gammas[4]; \
1028 double levels[4]; \
1029 double lower = p->d.pattern.lower; \
1030 double upper = p->d.pattern.upper; \
1031 size_t i; \
1032 size_t j; \
1033 int pixels; \
1034 T *s_data = (T *) data; \
1035 unsigned multiplier = global_noscale ? 1 : (1 << bits) - 1; \
1036 \
1037 vals[0] = p->d.pattern.vals[0]; \
1038 mins[0] = p->d.pattern.mins[0]; \
1039 \
1040 for (j = 1; j < 4; j++) \
1041 { \
1042 vals[j] = p->d.pattern.vals[j] == -2 ? global_levels[j] : \
1043 p->d.pattern.vals[j]; \
1044 mins[j] = p->d.pattern.mins[j] == -2 ? global_levels[j] : \
1045 p->d.pattern.mins[j]; \
1046 levels[j] = p->d.pattern.levels[j] == \
1047 -2 ? global_levels[j] : p->d.pattern.levels[j]; \
1048 } \
1049 for (j = 0; j < 4; j++) \
1050 { \
1051 gammas[j] = \
1052 p->d.pattern.gammas[j] * global_gamma * global_gammas[j]; \
1053 vals[j] -= mins[j]; \
1054 } \
1055 \
1056 if (scount > len) \
1057 scount = len; \
1058 pixels = len / scount; \
1059 for (i = 0; i < scount; i++) \
1060 { \
1061 int k; \
1062 double where = (double) i / ((double) scount - 1); \
1063 double cmyv; \
1064 double kv; \
1065 double val = where; \
1066 double xvals[4]; \
1067 for (j = 0; j < 4; j++) \
1068 { \
1069 if (j > 0) \
1070 xvals[j] = mins[j] + val * vals[j]; \
1071 else \
1072 xvals[j] = mins[j] + vals[j]; \
1073 xvals[j] = pow(xvals[j], gammas[j]); \
1074 } \
1075 \
1076 if (where <= lower) \
1077 kv = 0; \
1078 else if (where > upper) \
1079 kv = where; \
1080 else \
1081 kv = (where - lower) * upper / (upper - lower); \
1082 cmyv = vals[0] * (where - kv); \
1083 xvals[0] *= kv; \
1084 for (j = 1; j < 4; j++) \
1085 xvals[j] += cmyv * levels[j]; \
1086 for (j = 0; j < 4; j++) \
1087 { \
1088 if (xvals[j] > 1) \
1089 xvals[j] = 1; \
1090 xvals[j] *= global_ink_limit * multiplier; \
1091 xvals[j] += 0.9; \
1092 } \
1093 for (k = 0; k < pixels; k++) \
1094 { \
1095 switch (global_channel_depth) \
1096 { \
1097 case 0: \
1098 for (j = 0; j < 4; j++) \
1099 s_data[j] = xvals[(j + 1) % 4]; \
1100 s_data += 4; \
1101 break; \
1102 case 1: \
1103 s_data[0] = xvals[0]; \
1104 break; \
1105 case 2: \
1106 s_data[0] = xvals[0]; \
1107 s_data[1] = 0; \
1108 break; \
1109 case 3: \
1110 for (j = 1; j < 4; j++) \
1111 s_data[j - 1] = xvals[j]; \
1112 break; \
1113 case 4: \
1114 for (j = 0; j < 4; j++) \
1115 s_data[j] = xvals[j]; \
1116 break; \
1117 case 6: \
1118 s_data[0] = xvals[0]; \
1119 s_data[1] = xvals[1]; \
1120 s_data[2] = 0; \
1121 s_data[3] = xvals[2]; \
1122 s_data[4] = 0; \
1123 s_data[5] = xvals[3]; \
1124 break; \
1125 case 7: \
1126 for (j = 0; j < 4; j++) \
1127 s_data[j * 2] = xvals[j]; \
1128 for (j = 1; j < 6; j += 2) \
1129 s_data[j] = 0; \
1130 break; \
1131 } \
1132 s_data += global_channel_depth; \
1133 } \
1134 } \
1135 }
1136
1137 FILL_COLORS_FUNCTION(unsigned short, 16)
1138 FILL_COLORS_FUNCTION(unsigned char, 8)
1139
1140 static void
1141 fill_colors(unsigned char *data, size_t len, size_t scount,
1142 testpattern_t *p, size_t bytes)
1143 {
1144 if (strcmp(global_image_type, "RGB") == 0)
1145 fill_colors_extended(data, len, scount, p, bytes);
1146 else
1147 {
1148 switch (bytes)
1149 {
1150 case 1:
1151 fill_colors_8(data, len, scount, p);
1152 break;
1153 case 2:
1154 fill_colors_16(data, len, scount, p);
1155 break;
1156 }
1157 }
1158 }
1159
1160 extern FILE *yyin;
1161
1162 static void
1163 fill_pattern(testpattern_t *p, unsigned char *data, size_t width,
1164 size_t s_count, size_t image_depth, size_t byte_depth)
1165 {
1166 memset(data, 0, global_printer_width * image_depth * byte_depth);
1167 switch (p->type)
1168 {
1169 case E_PATTERN:
1170 fill_colors(data, width, s_count, p, byte_depth);
1171 break;
1172 case E_XPATTERN:
1173 fill_colors_extended(data, width, s_count, p, byte_depth);
1174 break;
1175 case E_GRID:
1176 fill_grid(data, width, s_count, p, byte_depth);
1177 break;
1178 default:
1179 break;
1180 }
1181 }
1182
1183
1184 static stp_image_status_t
1185 Image_get_row(stp_image_t *image, unsigned char *data,
1186 size_t byte_limit, int row)
1187 {
1188 (void)image;
1189 (void)byte_limit;
1190 int depth = global_channel_depth;
1191 if (! Image_is_valid)
1192 {
1193 fputs("Calling Image_get_row with invalid image!\n", stderr);
1194 abort();
1195 }
1196 if (static_testpatterns[0].type == E_IMAGE)
1197 {
1198 testpattern_t *t = &(static_testpatterns[0]);
1199 int total_read = fread(data, 1, t->d.image.x * depth * global_bit_depth / 8,
1200 yyin);
1201 if (total_read != t->d.image.x * depth * global_bit_depth / 8)
1202 {
1203 fputs("Read failed!\n", stderr);
1204 return STP_IMAGE_STATUS_ABORT;
1205 }
1206 if (!global_quiet)
1207 fputc('.', stderr);
1208 }
1209 else
1210 {
1211 static int previous_band = -1;
1212 static int printed_blackline = 0;
1213 int band = row / global_band_height;
1214 if (row == global_printer_height - 1 && ! global_noblackline)
1215 fill_black(data, global_printer_width, global_steps,
1216 global_bit_depth / 8);
1217 else if (band >= global_n_testpatterns)
1218 fill_white(data, global_printer_width, global_steps,
1219 global_bit_depth / 8);
1220 else
1221 {
1222 if (band != previous_band)
1223 {
1224 if (! global_noblackline && printed_blackline == 0)
1225 {
1226 fill_black(data, global_printer_width, global_steps,
1227 global_bit_depth / 8);
1228 printed_blackline = 1;
1229 return STP_IMAGE_STATUS_OK;
1230 }
1231 else
1232 {
1233 previous_band = band;
1234 printed_blackline = 0;
1235 if (! global_quiet)
1236 fputc('.', stderr);
1237 }
1238 }
1239 fill_pattern(&(static_testpatterns[band]), data,
1240 global_printer_width, global_steps, depth,
1241 global_bit_depth / 8);
1242 }
1243 }
1244 return STP_IMAGE_STATUS_OK;
1245 }
1246
1247 static void
1248 check_valid_image(const char *s)
1249 {
1250 if (! Image_is_valid)
1251 {
1252 fputs(s, stderr);
1253 abort();
1254 }
1255 }
1256
1257 static int
1258 Image_width(stp_image_t *image)
1259 {
1260 (void)image;
1261 check_valid_image("Calling Image_width with invalid image!\n");
1262 if (static_testpatterns[0].type == E_IMAGE)
1263 return static_testpatterns[0].d.image.x;
1264 else
1265 return global_printer_width;
1266 }
1267
1268 static int
1269 Image_height(stp_image_t *image)
1270 {
1271 (void)image;
1272 check_valid_image("Calling Image_height with invalid image!\n");
1273 if (static_testpatterns[0].type == E_IMAGE)
1274 return static_testpatterns[0].d.image.y;
1275 else
1276 return global_printer_height;
1277 }
1278
1279 static void
1280 Image_init(stp_image_t *image)
1281 {
1282 (void)image;
1283 if (Image_is_valid)
1284 {
1285 fputs("Calling Image_init with already valid image!\n", stderr);
1286 abort();
1287 }
1288 Image_is_valid = 1;
1289 /* dummy function */
1290 }
1291
1292 static void
1293 Image_reset(stp_image_t *image)
1294 {
1295 (void)image;
1296 check_valid_image("Calling Image_reset with invalid image!\n");
1297 /* dummy function */
1298 }
1299
1300 static void
1301 Image_conclude(stp_image_t *image)
1302 {
1303 (void)image;
1304 check_valid_image("Calling Image_conclude with invalid image!\n");
1305 Image_is_valid = 0;
1306 }
1307
1308 static const char *
1309 Image_get_appname(stp_image_t *image)
1310 {
1311 (void)image;
1312 check_valid_image("Calling Image_get_appname with invalid image!\n");
1313 return "Test Pattern";
1314 }