Debian Codegraph @

src/testpattern/testpatterny.y

raw
1 /*
2 *
3 * Test pattern generator for Gimp-Print
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
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <string.h>
28 #include <strings.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include "testpattern.h"
32
33 extern int mylineno;
34
35 extern int yylex(void);
36 char *quotestrip(const char *i);
37 char *endstrip(const char *i);
38
39 extern char* yytext;
40
41 static int yyerror( const char *s )
42 {
43 fprintf(stderr,"stdin:%d: %s before '%s'\n",mylineno,s,yytext);
44 return 0;
45 }
46
47 typedef struct
48 {
49 const char *name;
50 int channel;
51 } color_t;
52
53 static color_t color_map[] =
54 {
55 { "black", 0 },
56 { "cyan", 1 },
57 { "red", 1 },
58 { "magenta", 2 },
59 { "green", 2 },
60 { "yellow", 3 },
61 { "blue", 3 },
62 { "l_black", 4 },
63 { "l_cyan", 5 },
64 { "l_magenta", 6 },
65 { "d_yellow", 4 },
66 { "l_l_black", 7 },
67 { NULL, -1 }
68 };
69
70 static int current_index = 0;
71 static testpattern_t *current_testpattern;
72 extern FILE *yyin;
73
74 static int
75 find_color(const char *name)
76 {
77 int i = 0;
78 while (color_map[i].name)
79 {
80 if (strcasecmp(color_map[i].name, name) == 0)
81 return color_map[i].channel;
82 i++;
83 }
84 return -1;
85 }
86
87 %}
88
89 %token <ival> tINT
90 %token <dval> tDOUBLE
91 %token <sval> tSTRING
92 %token <sval> COLOR
93
94 %token GAMMA
95 %token LEVEL
96 %token STEPS
97 %token INK_LIMIT
98 %token PRINTER
99 %token PARAMETER
100 %token PARAMETER_INT
101 %token PARAMETER_BOOL
102 %token PARAMETER_FLOAT
103 %token PARAMETER_CURVE
104 %token DENSITY
105 %token TOP
106 %token LEFT
107 %token SIZE_MODE
108 %token RELATIVE
109 %token PT
110 %token IN
111 %token MM
112 %token HSIZE
113 %token VSIZE
114 %token BLACKLINE
115 %token COLORLINE
116 %token NOSCALE
117 %token PATTERN
118 %token XPATTERN
119 %token EXTENDED
120 %token IMAGE
121 %token GRID
122 %token SEMI
123 %token CHANNEL
124 %token CMYK
125 %token KCMY
126 %token RGB
127 %token CMY
128 %token GRAY
129 %token WHITE
130 %token MODE
131 %token PAGESIZE
132 %token ROUND
133 %token MESSAGE
134 %token OUTPUT
135 %token START_JOB
136 %token END_JOB
137 %token END
138
139 %type <dval> NUMBER
140
141 %start Thing
142
143 %%
144
145 NUMBER: tDOUBLE | tINT
146 {
147 }
148 ;
149
150 cmykspec: CMYK
151 {
152 if (getenv("STP_TESTPATTERN_DEBUG"))
153 fprintf(stderr, ">>>cmykspec\n");
154 global_image_type = "CMYK";
155 global_channel_depth = 4;
156 global_invert_data = 0;
157 }
158 ;
159
160 kcmyspec: KCMY
161 {
162 if (getenv("STP_TESTPATTERN_DEBUG"))
163 fprintf(stderr, ">>>kcmyspec\n");
164 global_image_type = "KCMY";
165 global_channel_depth = 4;
166 global_invert_data = 0;
167 }
168 ;
169
170 rgbspec: RGB
171 {
172 if (getenv("STP_TESTPATTERN_DEBUG"))
173 fprintf(stderr, ">>>rgbspec\n");
174 global_image_type = "RGB";
175 global_channel_depth = 3;
176 global_invert_data = 1;
177 }
178 ;
179
180 cmyspec: CMY
181 {
182 if (getenv("STP_TESTPATTERN_DEBUG"))
183 fprintf(stderr, ">>>cmyspec\n");
184 global_image_type = "CMY";
185 global_channel_depth = 3;
186 global_invert_data = 0;
187 }
188 ;
189
190 grayspec: GRAY
191 {
192 if (getenv("STP_TESTPATTERN_DEBUG"))
193 fprintf(stderr, ">>>grayspec\n");
194 global_image_type = "Grayscale";
195 global_channel_depth = 1;
196 global_invert_data = 0;
197 }
198 ;
199
200 whitespec: WHITE
201 {
202 if (getenv("STP_TESTPATTERN_DEBUG"))
203 fprintf(stderr, ">>>whitespec\n");
204 global_image_type = "Whitescale";
205 global_channel_depth = 1;
206 global_invert_data = 1;
207 }
208 ;
209
210 extendedspec: EXTENDED tINT
211 {
212 if (getenv("STP_TESTPATTERN_DEBUG"))
213 fprintf(stderr, ">>>extendedspec %d\n", $2);
214 global_image_type = "Raw";
215 global_invert_data = 0;
216 global_channel_depth = $2;
217 }
218 ;
219
220 modespec1: cmykspec | kcmyspec | rgbspec | cmyspec | grayspec | whitespec | extendedspec
221 ;
222
223 modespec2: modespec1 tINT
224 {
225 if (getenv("STP_TESTPATTERN_DEBUG"))
226 fprintf(stderr, ">>>modespec2 %d\n", $2);
227 if ($2 == 8 || $2 == 16)
228 global_bit_depth = $2;
229 }
230
231 modespec: modespec1 | modespec2
232 ;
233
234 round: ROUND
235 {
236 if (getenv("STP_TESTPATTERN_DEBUG"))
237 fprintf(stderr, ">>>round\n");
238 global_round_size = 1;
239 }
240
241 inputspec: MODE modespec
242 ;
243
244 level: LEVEL COLOR NUMBER
245 {
246 int channel = find_color($2);
247 if (getenv("STP_TESTPATTERN_DEBUG"))
248 fprintf(stderr, ">>>level %s %f\n", $2, $3);
249 if (channel >= 0)
250 global_levels[channel] = $3;
251 }
252 ;
253
254 channel_level: LEVEL tINT NUMBER
255 {
256 if (getenv("STP_TESTPATTERN_DEBUG"))
257 fprintf(stderr, ">>>channel_level %d %f\n", $2, $3);
258 if ($2 >= 0 && $2 <= STP_CHANNEL_LIMIT)
259 global_levels[$2] = $3;
260 }
261 ;
262
263 gamma: GAMMA COLOR NUMBER
264 {
265 int channel = find_color($2);
266 if (getenv("STP_TESTPATTERN_DEBUG"))
267 fprintf(stderr, ">>>gamma %s %f\n", $2, $3);
268 if (channel >= 0)
269 global_gammas[channel] = $3;
270 }
271 ;
272
273 channel_gamma: GAMMA tINT NUMBER
274 {
275 if (getenv("STP_TESTPATTERN_DEBUG"))
276 fprintf(stderr, ">>>channel_gamma %d %f\n", $2, $3);
277 if ($2 >= 0 && $2 <= STP_CHANNEL_LIMIT)
278 global_gammas[$2] = $3;
279 }
280 ;
281
282 global_gamma: GAMMA NUMBER
283 {
284 if (getenv("STP_TESTPATTERN_DEBUG"))
285 fprintf(stderr, ">>>global_gamma %f\n", $2);
286 global_gamma = $2;
287 }
288 ;
289 steps: STEPS tINT
290 {
291 if (getenv("STP_TESTPATTERN_DEBUG"))
292 fprintf(stderr, ">>>steps %d\n", $2);
293 global_steps = $2;
294 }
295 ;
296 ink_limit: INK_LIMIT NUMBER
297 {
298 if (getenv("STP_TESTPATTERN_DEBUG"))
299 fprintf(stderr, ">>>ink_limit %f\n", $2);
300 global_ink_limit = $2;
301 }
302 ;
303 printer: PRINTER tSTRING
304 {
305 if (getenv("STP_TESTPATTERN_DEBUG"))
306 fprintf(stderr, ">>>printer %s\n", $2);
307 global_printer = stp_strdup($2);
308 free($2);
309 }
310 ;
311
312 page_size_name: PAGESIZE tSTRING
313 {
314 if (getenv("STP_TESTPATTERN_DEBUG"))
315 fprintf(stderr, ">>>page_size_name %s\n", $2);
316 stp_set_string_parameter(global_vars, "PageSize", $2);
317 free($2);
318 }
319 ;
320
321 page_size_custom: PAGESIZE tINT tINT
322 {
323 if (getenv("STP_TESTPATTERN_DEBUG"))
324 fprintf(stderr, ">>>page_size_custom %d %d\n", $2, $3);
325 stp_set_page_width(global_vars, $2);
326 stp_set_page_height(global_vars, $3);
327 }
328 ;
329
330 page_size: page_size_name | page_size_custom
331 ;
332
333 parameter_string: PARAMETER tSTRING tSTRING
334 {
335 if (getenv("STP_TESTPATTERN_DEBUG"))
336 fprintf(stderr, ">>>parameter_string %s %s\n", $2, $3);
337 stp_set_string_parameter(global_vars, $2, $3);
338 free($2);
339 free($3);
340 }
341 ;
342
343 parameter_int: PARAMETER_INT tSTRING tINT
344 {
345 if (getenv("STP_TESTPATTERN_DEBUG"))
346 fprintf(stderr, ">>>parameter_int %s %d\n", $2, $3);
347 stp_set_int_parameter(global_vars, $2, $3);
348 free($2);
349 }
350 ;
351
352 parameter_bool: PARAMETER_BOOL tSTRING tINT
353 {
354 if (getenv("STP_TESTPATTERN_DEBUG"))
355 fprintf(stderr, ">>>parameter_bool %s %d\n", $2, $3);
356 stp_set_boolean_parameter(global_vars, $2, $3);
357 free($2);
358 }
359 ;
360
361 parameter_float: PARAMETER_FLOAT tSTRING NUMBER
362 {
363 if (getenv("STP_TESTPATTERN_DEBUG"))
364 fprintf(stderr, ">>>parameter_float %s %f\n", $2, $3);
365 stp_set_float_parameter(global_vars, $2, $3);
366 free($2);
367 }
368 ;
369
370 parameter_curve: PARAMETER_CURVE tSTRING tSTRING
371 {
372 stp_curve_t *curve = stp_curve_create_from_string($3);
373 if (getenv("STP_TESTPATTERN_DEBUG"))
374 fprintf(stderr, ">>>parameter_curve %s %s\n", $2, $3);
375 if (curve)
376 {
377 stp_set_curve_parameter(global_vars, $2, curve);
378 stp_curve_destroy(curve);
379 }
380 free($2);
381 }
382 ;
383
384 parameter: parameter_string | parameter_int | parameter_float | parameter_curve | parameter_bool
385 ;
386 density: DENSITY NUMBER
387 {
388 if (getenv("STP_TESTPATTERN_DEBUG"))
389 fprintf(stderr, ">>>density %f\n", $2);
390 global_density = $2;
391 }
392 ;
393 top: TOP NUMBER
394 {
395 if (getenv("STP_TESTPATTERN_DEBUG"))
396 fprintf(stderr, ">>>top %f\n", $2);
397 global_xtop = $2;
398 }
399 ;
400 left: LEFT NUMBER
401 {
402 if (getenv("STP_TESTPATTERN_DEBUG"))
403 fprintf(stderr, ">>>left %f\n", $2);
404 global_xleft = $2;
405 }
406 ;
407 size_relative: RELATIVE
408 {
409 if (getenv("STP_TESTPATTERN_DEBUG"))
410 fprintf(stderr, ">>>relative size\n");
411 global_size_mode = SIZE_RELATIVE;
412 }
413 ;
414 size_in: IN
415 {
416 if (getenv("STP_TESTPATTERN_DEBUG"))
417 fprintf(stderr, ">>>size inches\n");
418 global_size_mode = SIZE_IN;
419 }
420 ;
421 size_pt: PT
422 {
423 if (getenv("STP_TESTPATTERN_DEBUG"))
424 fprintf(stderr, ">>>size pt\n");
425 global_size_mode = SIZE_PT;
426 }
427 ;
428 size_mm: MM
429 {
430 if (getenv("STP_TESTPATTERN_DEBUG"))
431 fprintf(stderr, ">>>size mm\n");
432 global_size_mode = SIZE_MM;
433 }
434 ;
435 size_mode_1: size_relative | size_pt | size_in | size_mm
436 ;
437 size_mode: SIZE_MODE size_mode_1
438 ;
439 hsize: HSIZE NUMBER
440 {
441 if (getenv("STP_TESTPATTERN_DEBUG"))
442 fprintf(stderr, ">>>hsize %f\n", $2);
443 global_hsize = $2;
444 }
445 ;
446 vsize: VSIZE NUMBER
447 {
448 if (getenv("STP_TESTPATTERN_DEBUG"))
449 fprintf(stderr, ">>>vsize %f\n", $2);
450 global_vsize = $2;
451 }
452 ;
453 blackline: BLACKLINE tINT
454 {
455 if (getenv("STP_TESTPATTERN_DEBUG"))
456 fprintf(stderr, ">>>blackline %d\n", $2);
457 global_noblackline = !($2);
458 }
459 ;
460
461 colorline: COLORLINE tINT
462 {
463 if (getenv("STP_TESTPATTERN_DEBUG"))
464 fprintf(stderr, ">>>colorline %d\n", $2);
465 global_colorline = ($2 != 0);
466 }
467 ;
468
469 noscale: NOSCALE tINT
470 {
471 if (getenv("STP_TESTPATTERN_DEBUG"))
472 fprintf(stderr, ">>>noscale %d\n", $2);
473 global_noscale = $2;
474 }
475 ;
476
477 color_block1: NUMBER NUMBER NUMBER
478 {
479 if (getenv("STP_TESTPATTERN_DEBUG"))
480 fprintf(stderr, ">>>color_block1 %f %f %f (%d)\n", $1, $2, $3,
481 current_index);
482 if (current_index < STP_CHANNEL_LIMIT)
483 {
484 current_testpattern->d.pattern.mins[current_index] = $1;
485 current_testpattern->d.pattern.vals[current_index] = $2;
486 current_testpattern->d.pattern.gammas[current_index] = $3;
487 current_index++;
488 }
489 }
490 ;
491
492 color_blocks1a: color_block1 | color_blocks1a color_block1
493 ;
494
495 color_blocks1b: /* empty */ | color_blocks1a
496 ;
497
498 color_blocks1: color_block1 color_blocks1b
499 ;
500
501 color_block2a: COLOR NUMBER NUMBER NUMBER
502 {
503 int channel = find_color($1);
504 if (getenv("STP_TESTPATTERN_DEBUG"))
505 fprintf(stderr, ">>>color_block2a %s %f %f %f\n", $1, $2, $3, $4);
506 if (channel >= 0 && channel < STP_CHANNEL_LIMIT)
507 {
508 current_testpattern->d.pattern.mins[channel] = $2;
509 current_testpattern->d.pattern.vals[channel] = $3;
510 current_testpattern->d.pattern.gammas[channel] = $4;
511 }
512 }
513 ;
514
515 color_block2b: CHANNEL tINT NUMBER NUMBER NUMBER
516 {
517 if (getenv("STP_TESTPATTERN_DEBUG"))
518 fprintf(stderr, ">>>color_block2b %d %f %f %f\n", $2, $3, $4, $5);
519 if ($2 >= 0 && $2 < STP_CHANNEL_LIMIT)
520 {
521 current_testpattern->d.pattern.mins[$2] = $3;
522 current_testpattern->d.pattern.vals[$2] = $4;
523 current_testpattern->d.pattern.gammas[$2] = $5;
524 }
525 }
526 ;
527
528 color_block2: color_block2a | color_block2b
529 ;
530
531 color_blocks2a: color_block2 | color_blocks2a color_block2
532 ;
533
534 color_blocks2: /* empty */ | color_blocks2a
535 ;
536
537 color_blocks: color_blocks1 | color_blocks2
538 ;
539
540 patvars: NUMBER NUMBER NUMBER NUMBER NUMBER
541 {
542 if (getenv("STP_TESTPATTERN_DEBUG"))
543 fprintf(stderr, ">>>patvars %f %f %f %f %f\n", $1, $2, $3, $4, $5);
544 current_testpattern->type = E_PATTERN;
545 current_testpattern->d.pattern.lower = $1;
546 current_testpattern->d.pattern.upper = $2;
547 current_testpattern->d.pattern.levels[1] = $3;
548 current_testpattern->d.pattern.levels[2] = $4;
549 current_testpattern->d.pattern.levels[3] = $5;
550 current_testpattern = get_next_testpattern();
551 current_index = 0;
552 }
553 ;
554
555 pattern: PATTERN patvars color_blocks
556 ;
557
558 xpattern: XPATTERN color_blocks
559 {
560 if (getenv("STP_TESTPATTERN_DEBUG"))
561 fprintf(stderr, ">>>xpattern\n");
562 if (global_channel_depth == 0)
563 {
564 fprintf(stderr, "xpattern may only be used with extended color depth\n");
565 exit(1);
566 }
567 current_testpattern->type = E_XPATTERN;
568 current_testpattern = get_next_testpattern();
569 current_index = 0;
570 }
571 ;
572
573 grid: GRID tINT
574 {
575 if (getenv("STP_TESTPATTERN_DEBUG"))
576 fprintf(stderr, ">>>grid %d\n", $2);
577 current_testpattern->type = E_GRID;
578 current_testpattern->d.grid.ticks = $2;
579 current_testpattern = get_next_testpattern();
580 current_index = 0;
581 }
582 ;
583
584 image: IMAGE tINT tINT
585 {
586 if (getenv("STP_TESTPATTERN_DEBUG"))
587 fprintf(stderr, ">>>image %d %d\n", $2, $3);
588 current_testpattern->type = E_IMAGE;
589 current_testpattern->d.image.x = $2;
590 current_testpattern->d.image.y = $3;
591 if (current_testpattern->d.image.x <= 0 ||
592 current_testpattern->d.image.y <= 0)
593 {
594 fprintf(stderr, "image width and height must be greater than zero\n");
595 exit(1);
596 }
597 return 0;
598 }
599 ;
600
601 Message: tSTRING
602 {
603 fprintf(stderr,"%s",$1);
604 free($1);
605 }
606 ;
607
608 Messages: /* empty */ | Messages Message
609 ;
610
611 message: MESSAGE Messages
612 ;
613
614 Output0: OUTPUT
615 {
616 close_output();
617 if (global_output)
618 free(global_output);
619 global_output = NULL;
620 output = stdout;
621 }
622 ;
623
624 Output1: OUTPUT tSTRING
625 {
626 global_output = $2;
627 }
628 ;
629
630 output: Output0 | Output1
631 ;
632
633 start_job: START_JOB
634 { start_job = 1; }
635 ;
636
637 end_job: END_JOB
638 { end_job = 1; }
639 ;
640
641 A_Rule: gamma | channel_gamma | level | channel_level | global_gamma | steps
642 | ink_limit | printer | parameter | density | top | left | hsize
643 | vsize | blackline | noscale | inputspec | page_size | message
644 | output | start_job | end_job | size_mode | round | colorline
645 ;
646
647 Rule: A_Rule SEMI
648 { global_did_something = 1; }
649 ;
650
651 A_Pattern: pattern | xpattern | grid | message
652 ;
653
654 Pattern: A_Pattern SEMI
655 { global_did_something = 1; }
656 ;
657
658 Patterns: /* empty */ | Patterns Pattern
659 ;
660
661 Image: image
662 { global_did_something = 1; }
663 ;
664
665 Rules: /* empty */ | Rules Rule
666 ;
667
668 Print: Patterns | Image
669 ;
670
671 EOF: /* empty */ | END SEMI
672 { return 0; }
673 ;
674
675 Thing: Rules
676 {
677 current_testpattern = get_next_testpattern();
678 }
679 Print EOF
680 ;
681
682 %%