-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnpiet.c
2787 lines (2309 loc) · 65.5 KB
/
npiet.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* npiet.c: May 2004
* (schoenfr@web.de) Aug 2016
*
* npiet is an interperter for the piet programming language.
*
*
* about the piet programming language see:
*
* http://www.dangermouse.net/esoteric/piet.html
*
* more about the piet programming language and the npiet interpreter see:
*
* http://www.bertnase.de/npiet/
*
*
* compile:
* cc -o npiet npiet.c
* use:
* ./piet hi.ppm
*
*
* if compiled with gd-lib support, graphical trace output can be
* created - great fun ;-)
*
* cc -DHAVE_GD_H -o npiet npiet.c -lgd
* ./npiet -tpic hi.ppm
*
* reading png is supported if -DHAVE_PNP_H is set
* and reading gif is supported if -DHAVE_GIF_LIB_H is set.
*
*
* but all this is automagically handled by running
*
* ./configure
*
*
* Copyright (C) 2004 Erik Schoenfelder (schoenfr@web.de)
*
* This file is part of npiet.
*
* npiet is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation version 2.
*
* npiet is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with npiet; see the file COPYING. If not, write to the Free
* Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
char *version = "v1.3e";
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
void
usage (int rc)
{
fprintf (stderr, "npiet %s (with", version);
#ifdef HAVE_GD_H
fprintf (stderr, " ");
#else
fprintf (stderr, "out ");
#endif
fprintf (stderr, "GD support, with");
#ifdef HAVE_GIF_LIB_H
fprintf (stderr, " ");
#else
fprintf (stderr, "out ");
#endif
fprintf (stderr, "GIF support, with");
#ifdef HAVE_PNG_H
fprintf (stderr, " ");
#else
fprintf (stderr, "out ");
#endif
fprintf (stderr, "PNG support)\n\n");
fprintf (stderr, "use: npiet [<options>] <filename>\n");
fprintf (stderr, "options:\n");
fprintf (stderr, "\t-v - be verbose (default: off)\n");
fprintf (stderr, "\t-q - be quiet (default: off)\n");
fprintf (stderr, "\t-e <n> - execution steps (default: unlimited)\n");
fprintf (stderr, "\t-t - trace (default: off)\n");
fprintf (stderr, "\t-ub - unknown colors are black "
"(default: white)\n");
fprintf (stderr, "\t-uu - unknown colors give error "
"(default: white)\n");
fprintf (stderr, "\t-cs <n> - codelsize of the input (default: guess)\n");
#ifndef HAVE_GD_H
fprintf (stderr, "\t-tpic - create trace picture (not compiled in)\n");
fprintf (stderr, "\t-tpf <n> - trace pixelzoom (not compiled in)\n");
fprintf (stderr, "\t-tps - simple trace pic w/o dp/cc info (not compiled in)\n");
#else
fprintf (stderr, "\t-tpic - create trace picture (default: do not)\n");
fprintf (stderr, "\t-tpf <n> - trace pixelzoom (default: 48 or so)\n");
fprintf (stderr, "\t-tps - simple trace pic w/o dp/cc info (default: sho dp/cc info)\n");
#endif
fprintf (stderr, "\t-ts <n> - graphic trace start (default: 0)\n");
fprintf (stderr, "\t-te <n> - graphic trace end (default: unlimited)\n");
fprintf (stderr, "\t-n-str <s> - nase stuff: string-to-command\n");
fprintf (stderr, "\t-d - debug (default: off)\n");
fprintf (stderr, "\t-dpbug - model the perl piet interpreter (default: off)\n");
fprintf (stderr, "\t-v11 - model the npiet v1.1 interpreter (default: off)\n");
exit (rc);
}
/* be somewhat verbose: */
int verbose = 0;
/* be quiet - actually only suppresses the prompt when doing input: */
int quiet = 0;
/* show program execution information: */
int trace = 0;
/* maximum number of execution steps (0 == unlimited): */
unsigned max_exec_step = 0;
/* print debugging stuff: */
int debug = 0;
/* filename to work for: */
char *input_filename = 0;
/* unknown colors are treated as white, black or error (default 1 is white): */
int unknown_color = 1;
/* with gd2 lib linked we try to save trace output: */
int do_gdtrace = 0;
char *gd_trace_filename = "npiet-trace.png";
int gd_trace_simple = 0;
unsigned gd_trace_start = 0;
unsigned gd_trace_end = 1 << 31; /* lot's to print */
/* pixelsize when painting graphical trace output: */
int c_xy = 32;
/* codelsize of the input. -1 means, we try to guess it from the input: */
int codel_size = -1;
/* trace codelsize threshold for pixel numbers, not tiny strings: */
int pp_size = 49;
/* work around broken source (wrong assumption about toggle of dp and cc): */
int toggle_bug = 0;
/* fun-stuff: create commands to print a string: */
char *do_n_str = 0;
/* fall back to npiet v1.1 behavior (fixed white codel crossing but
* without trace info:
*/
int version_11 = 0;
/* helper: */
#define dprintf if (debug) printf
#define d2printf if (debug > 1) printf
#define tprintf if (trace \
&& exec_step >= gd_trace_start \
&& exec_step <= gd_trace_end) printf
#define t2printf if (trace > 1) printf
#define vprintf if (verbose) printf
int
parse_args (int argc, char **argv)
{
while (--argc > 0) {
argv++;
if (! strcmp (argv [0], "-v")) {
verbose++;
vprintf ("info: verbose set to %d\n", verbose);
} else if (! strcmp (argv [0], "-q")) {
quiet++;
} else if (! strcmp (argv [0], "-t")) {
trace++;
vprintf ("info: trace set to %d\n", trace);
} else if (! strcmp (argv [0], "-d")) {
debug++;
dprintf ("info: debug set to %d\n", debug);
} else if (! strcmp (argv [0], "-uu")) {
unknown_color = -1;
vprintf ("info: unknown color set to error\n");
} else if (! strcmp (argv [0], "-ub")) {
unknown_color = 0;
vprintf ("info: unknown color set to black\n");
} else if (! strcmp (argv [0], "-dpbug")) {
/* just a tbd (how to follow wrong behavior ;-) */
toggle_bug = 1;
vprintf ("info: setting toggle bug and white bug behavior\n");
} else if (! strcmp (argv [0], "-v11")) {
version_11 = 1;
vprintf ("info: setting npiet version 1.1 behavior\n");
} else if (! strcmp (argv [0], "-tpic")) {
#ifndef HAVE_GD_H
printf ("note: no GD support compiled in. the graphical trace "
"feature is not avail\n");
#else
do_gdtrace = 1;
vprintf ("info: save trace output to %s\n", gd_trace_filename);
#endif
} else if (argc > 0 && ! strcmp (argv [0], "-tpf")) {
argc--, argv++; /* shift */
#ifndef HAVE_GD_H
printf ("note: no GD support compiled in. the graphical trace "
"feature is not avail\n");
#else
if ((c_xy = atoi (argv [0])) < 1) {
fprintf (stderr, "warning: trace pixelzoom %d is invalid\n",
c_xy);
c_xy = 32;
}
vprintf ("info: trace pixelzoom set to %d\n", c_xy);
do_gdtrace = 1;
#endif
} else if (! strcmp (argv [0], "-tps")) {
#ifndef HAVE_GD_H
printf ("note: no GD support compiled in. the graphical trace "
"feature is not avail\n");
#else
do_gdtrace = 1;
gd_trace_simple++;
#endif
} else if (argc > 0 && ! strcmp (argv [0], "-e")) {
argc--, argv++; /* shift */
max_exec_step = atoi (argv [0]);
vprintf ("info: number of execution steps set to %u\n", max_exec_step);
} else if (argc > 0 && ! strcmp (argv [0], "-ts")) {
argc--, argv++; /* shift */
#ifndef HAVE_GD_H
printf ("note: no GD support compiled in. the graphical trace "
"feature is not avail\n");
#else
gd_trace_start = atoi (argv [0]);
vprintf ("info: graphical trace start set to %d\n", gd_trace_start);
/* enable graphical tracing anyway: */
do_gdtrace = 1;
#endif
} else if (argc > 0 && ! strcmp (argv [0], "-te")) {
argc--, argv++; /* shift */
#ifndef HAVE_GD_H
printf ("note: no GD support compiled in. the graphical trace "
"feature is not avail\n");
#else
gd_trace_end = atoi (argv [0]);
vprintf ("info: graphical trace end set to %d\n", gd_trace_end);
/* enable graphical tracing anyway: */
do_gdtrace = 1;
#endif
} else if (argc > 0 && ! strcmp (argv [0], "-n-str")) {
argc--, argv++; /* shift */
do_n_str = argv [0];
} else if (argc > 0 && ! strcmp (argv [0], "-cs")) {
argc--, argv++; /* shift */
if ((codel_size = atoi (argv [0])) < 1) {
fprintf (stderr, "warning: codelsize %d is invalid\n",
codel_size);
codel_size = 1;
}
vprintf ("info: codelsize set to %d\n", codel_size);
} else if (argv [0][0] == '-' && argv [0][1]) {
usage (-1);
} else if (! input_filename) {
input_filename = argv [0];
vprintf ("info: using file %s\n", input_filename);
} else {
usage (-1);
}
}
return 0;
}
extern void alloc_cells (int n_width, int n_height);
/*
* picture storage:
*/
static int *cells = 0;
static int width = 0, height = 0;
/*
* color and hue values:
*
* we order the colors linear:
*
* idx 0: light red
* [...]
* idx 15: dark margenta
* idx 16: white
* idx 17: black
*/
#define n_hue 6 /* 4 colors */
#define n_light 3 /* 4 shades */
#define c_white (n_hue * n_light)
#define c_black (c_white + 1)
#define n_colors (c_black + 1)
/* internal used index for filling areas: */
#define c_mark_index 9999
#define adv_col(c, h, l) (((((c) % 6) + (h)) % 6) \
+ (6 * ((((c) / 6) + (l)) % 3)))
static struct c_color {
int col; /* rgb color */
char *l_name; /* long color name */
char *s_name; /* short color name */
int c_idx; /* our internal color index */
} c_colors [] = {
{ 0xFFC0C0, "light red", "lR", 0 },
{ 0xFFFFC0, "light yellow", "lY", 1 },
{ 0xC0FFC0, "light green", "lG", 2 },
{ 0xC0FFFF, "light cyan", "lC", 3 },
{ 0xC0C0FF, "light blue", "lB", 4 },
{ 0xFFC0FF, "light magenta", "lM", 5 },
{ 0xFF0000, "red", "nR", 6 },
{ 0xFFFF00, "yellow", "nY", 7 },
{ 0x00FF00, "green", "nG", 8 },
{ 0x00FFFF, "cyan", "nC", 9 },
{ 0x0000FF, "blue", "nB", 10 },
{ 0xFF00FF, "magenta", "nM", 11 },
{ 0xC00000, "dark red", "dR", 12 },
{ 0xC0C000, "dark yellow", "dY", 13 },
{ 0x00C000, "dark green", "dG", 14 },
{ 0x00C0C0, "dark cyan", "dC", 15 },
{ 0x0000C0, "dark blue", "dB", 16 },
{ 0xC000C0, "dark magenta", "dM", 17 },
{ 0xFFFFFF, "white", "WW", c_white },
{ 0x000000, "black", "BB", c_black }
};
/*
* execution states:
*/
int p_dir_pointer; /* DP: p_{left, right, up, down} */
int p_codel_chooser; /* CC: p_left or p_right */
int p_xpos, p_ypos; /* execution position */
#define p_left 'l'
#define p_right 'r'
#define p_up 'u'
#define p_down 'd'
#define toggle_cc(cc) ((cc) == 'r' ? 'l' : 'r')
#define turn_dp(dp) ((dp) == 'r' ? 'd' : ((dp) == 'd' ? 'l' : \
((dp) == 'l' ? 'u' : 'r')))
#define turn_dp_inv(dp) ((dp) == 'r' ? 'u' : ((dp) == 'u' ? 'l' : \
((dp) == 'l' ? 'd' : 'r')))
#define dp_dx(dp) ((dp) == 'l' ? -1 : ((dp) == 'r' ? 1 : 0))
#define dp_dy(dp) ((dp) == 'u' ? -1 : ((dp) == 'd' ? 1 : 0))
/* informal step counter: */
unsigned exec_step = 0;
/*
* stack space for runtime action:
*/
long *stack = 0; /* stack space */
int num_stack = 0; /* current number of values on stack */
int max_stack = 0; /* max size of stack allocated */
void
alloc_stack_space (int val)
{
if (val <= max_stack) {
return;
} else if (! stack) {
max_stack = val;
stack = (long *) calloc (val, sizeof (long));
} else {
long *new_stack = (long *) calloc (val, sizeof (long));
memcpy (new_stack, stack, num_stack * sizeof (long));
free (stack);
max_stack = val;
stack = new_stack;
}
dprintf ("deb: stack extended to %d entries (num_stack is %d)\n",
max_stack, num_stack);
}
void
tdump_stack ()
{
int i;
if (num_stack == 0) {
tprintf ("trace: stack is empty");
} else {
tprintf ("trace: stack (%d values):", num_stack);
}
for (i = 0; i < num_stack; i++) {
tprintf (" %ld", stack [num_stack - i - 1]);
}
tprintf ("\n");
}
/*
* extract color component:
*/
int
get_hue (int val)
{
if (val < (n_hue * n_light)) {
return val % n_hue;
}
if (val == c_black || val == c_white) {
return val;
}
fprintf (stderr, "no such color: col %d\n", val);
exit (-99);
}
/*
* extract lightness component:
*/
int
get_light (int val)
{
if (val < c_black) {
return val / n_hue;
}
fprintf (stderr, "no such color: col %d\n", val);
exit (-99);
}
int
get_color_idx (int col)
{
int i;
for (i = 0; i < n_colors; i++) {
if (col == c_colors [i].col) {
return c_colors [i].c_idx;
}
}
return -1;
}
/*
*
*
*/
char *
cell2str (int idx)
{
int i;
/* internal special index w/o real color: */
if (idx == c_mark_index) {
return "II";
}
for (i = 0; i < n_colors; i++) {
if (idx == c_colors [i].c_idx) {
return c_colors [i].s_name;
}
}
fprintf (stderr, "unknown color index %d\n", idx);
return "??";
}
#ifdef DEBUG
/*
* slower call for nicer debugging:
*/
int
cell_idx (int x, int y)
{
if (x < 0 || x >= width || y < 0 || y >= height) {
return -1;
}
return y * width + x;
}
#else
# define cell_idx(x, y) (((x) < 0 || (x) >= width \
|| (y) < 0 || (y) >= height) ? -1 : \
(y) * width + (x))
#endif
int
get_cell (int x, int y)
{
int c_idx = cell_idx (x, y);
if (c_idx < 0) {
if (debug > 1) printf ("deb: bad index for x=%d, y=%d\n", x, y);
return -1;
}
return cells [c_idx];
}
void
set_cell (int x, int y, int val)
{
int c_idx;
c_idx = cell_idx (x, y);
if (c_idx < 0) {
alloc_cells (x >= width ? x + 1 : width, y >= height ? y + 1 : height);
}
if ((c_idx = cell_idx (x, y)) < 0) {
exit (-99); /* internal error */
}
cells [c_idx] = val;
}
void
alloc_cells (int n_width, int n_height)
{
int i, j;
int *n_cells = (int *) malloc (n_width * n_height * sizeof(int));
for (j = 0; j < n_height; j++) {
for (i = 0; i < n_width; i++) {
n_cells [j * n_width + i] = c_black;
}
}
if (! n_cells) {
fprintf (stderr, "out of memory: cannot allocate %d * %d cells\n",
n_height, n_width);
exit (-99);
}
if (cells) {
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
n_cells [j * n_width + i] = cells [j * width + i];
}
}
free (cells);
}
cells = n_cells;
width = n_width;
height = n_height;
}
void
dump_cells ()
{
int i, j;
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
int idx = get_cell (i, j);
printf ("%3s", cell2str (idx));
}
printf ("\n");
}
}
/*
* graphical trace output:
*
* this is extra fun, but requires libgd and libpng.
*/
#ifndef HAVE_GD_H
/*
* without support, make dummy substitutions avail:
*/
#define gd_init()
#define gd_save()
#define gd_trace()
#define gd_try_init()
#define gd_try_step(a1,a2,a3,a4,a5,a6)
#define gd_action(a1,a2,a3,a4,a5,a6,a7)
#else
#include <gd.h>
#include <gdfonts.h>
#include <gdfontt.h>
#define i_sign(x) ((x) < 0 ? -1 : ((x) > 0 ? 1 : 0))
#define i_abs(x) ((x) < 0 ? -(x) : (x))
#define i_max(x, y) ((x) > (y) ? (x) : (y))
gdImagePtr im;
int gd_col [n_colors];
int gd_white;
int gd_black;
int gd_nase;
int gd_step;
int gd_grey [8]; /* grey indices */
gdFontPtr gdft, gdfs;
/*
* if a ongoing try hits the same cell, we will change the print offset
* to get at least a clue with the overwritten strings:
*/
int gd_try_xoff = 0;
int gd_try_yoff = 0;
int gd_try_dcol = 0;
void
gd_try_init ()
{
gd_try_xoff = 0;
gd_try_yoff = 0;
gd_try_dcol = 0;
}
void
gd_arrow_pp (int x1, int y1, int dp, int gd_col)
{
int i;
for (i = 0; i < 3; i++) {
gdImageLine (im, x1 - i * dp_dx(dp) + i * dp_dy(dp),
y1 + i * dp_dx(dp) - i * dp_dy(dp),
x1 - i * dp_dx(dp) - i * dp_dy(dp),
y1 - i * dp_dx(dp) - i * dp_dy(dp), gd_col);
}
}
void
gd_arrow (int x1, int y1, int x2, int y2, int dp, int gd_col)
{
#if 0
int i;
gdImageLine (im, x1, y1, x2, y2, gd_col);
for (i = 0; i < 3; i++) {
gdImageLine (im, x2 - i * dp_dx(dp) + i * dp_dy(dp),
y2 + i * dp_dx(dp) - i * dp_dy(dp),
x2 - i * dp_dx(dp) - i * dp_dy(dp),
y2 - i * dp_dx(dp) - i * dp_dy(dp), gd_col);
}
/* gdImageLine (im, x1 - 2, y1 + 2, x2 + 2, y2 + 2, gd_col); */
#else
gdPoint pts [3];
/* base line: */
gdImageLine (im, x1, y1, x2, y2, gd_col);
pts [0].x = x2;
pts [0].y = y2;
pts [1].x = (x2 + x1) / 2 - 2 * dp_dy(dp);
pts [1].y = (y2 + y1) / 2 - 2 * dp_dx(dp);
pts [2].x = (x2 + x1) / 2 + 2 * dp_dy(dp);
pts [2].y = (y2 + y1) / 2 + 2 * dp_dx(dp);
/* arrow head: */
gdImageFilledPolygon (im, pts, 3, gd_col);
#endif
}
/*
* ok, here is another try:
*
* we put the chars for dp and cc ourself pixel per pixel
* together and don't use the gd's tiny font.
* same for the trace and try numbers.
*/
gdPoint r_pts [4] = { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 0 } };
gdPoint l_pts [4] = { { 0, 0 }, { 0, 1 }, { 0, 2 }, { 1, 2 } };
gdPoint d_pts [6] = { { 0, 2 }, { 0, 3 }, { 1, 0 }, { 1, 1 },
{ 1, 2 }, { 1, 3 } };
gdPoint u_pts [7] = { { 0, 0 }, { 0, 1 }, { 0, 2 },
{ 1, 2 }, { 2, 0 }, { 2, 1 }, { 2, 2 } };
gdPoint num_pts [10][15] = {
{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 },
{ 1, 0 }, { 1, 4 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, },
{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 } },
{ { 0, 0 }, { 0, 2 }, { 0, 3 }, { 0, 4 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 4 }, },
{ { 0, 0 }, { 0, 2 }, { 0, 4 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, },
{ { 0, 2 }, { 0, 3 }, { 1, 1 }, { 1, 3 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, },
{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 4 },
{ 1, 0 }, { 1, 2 }, { 1, 3 }, { 1, 4 }, },
{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 },
{ 1, 2 }, { 1, 4 },
{ 2, 2 }, { 2, 3 }, { 2, 4 }, },
{ { 0, 0 }, { 0, 3 }, { 0, 4 },
{ 1, 0 }, { 1, 1 }, { 1, 2 }, },
{ { 0, 0 }, { 0, 1 }, { 0, 2 }, { 0, 3 }, { 0, 4 },
{ 1, 0 }, { 1, 2 }, { 1, 4 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, },
{ { 0, 0}, { 0, 1 }, { 0, 2 }, { 1, 0 }, { 1, 2 },
{ 2, 0 }, { 2, 1 }, { 2, 2 }, { 2, 3 }, { 2, 4 }, } };
int num_pts_plen [10] = { 12, 5, 8, 8, 9, 8, 10, 6, 13, 11 };
#define gd_ch_pts(c) (c == 'r' ? r_pts : (c == 'l' ? l_pts : \
(c == 'd' ? d_pts : (c == 'u' ? u_pts : \
(c >= 0 && c <= 9 ? num_pts [c] : 0)))))
#define gd_ch_plen(c) (c == 'r' ? 4 : (c == 'l' ? 4 : (c == 'd' ? 6 : \
c == 'u' ? 7 : (c >= 0 && c <= 9 ? num_pts_plen[c] : 0))))
#define gd_ch_pw(c) (c == 0 || c == 4 || c == 6 || c > 7 ? 3 : \
(c == 1 ? 1 : 2))
void
gd_paint_ch (int x1, int y1, int ch, int gd_col)
{
gdPoint *pts;
int i, n_pts;
pts = gd_ch_pts (ch);
n_pts = gd_ch_plen (ch);
for (i = 0; i < n_pts; i++) {
gdImageSetPixel (im, x1 + pts [i].x, y1 + pts [i].y, gd_col);
}
}
void
gd_paint_num (int x1, int y1, int num, int gd_col)
{
gdPoint *pts;
int len, div, n, i, w, n_pts;
div = 1;
for (n = num, len = 0; (! len && ! n) || n > 0; len++) {
n = n / 10;
div = div * 10;
}
while (len-- > 0) {
div = div / 10;
n = num / div;
num = num - n * div;
pts = gd_ch_pts (n);
n_pts = gd_ch_plen (n);
w = gd_ch_pw(n);
for (i = 0; i < n_pts; i++) {
gdImageSetPixel (im, x1 + pts [i].x, y1 + pts [i].y, gd_col);
}
x1 += w + 1;
}
}
void
gd_step_num_pp (int x, int y)
{
if (c_xy <= pp_size) {
/* only pixl numbers if we are low on space: */
gd_paint_num (x, y, exec_step, gd_step);
}
}
void
gd_alloc_piet_colors (gdImagePtr im, int *col)
{
int i;
for (i = 0; i < n_colors; i++) {
int r = c_colors [i].col >> 16;
int g = (c_colors [i].col >> 8) & 0xff;
int b = c_colors [i].col & 0xff;
col [i] = gdImageColorAllocate (im, r, g, b);
}
}
void
gd_init ()
{
int i, j;
im = gdImageCreate (width * c_xy, height * c_xy);
/* background color: */
gdImageColorAllocate (im, 255, 255, 255);
gd_alloc_piet_colors (im, gd_col);
gd_black = gd_col [c_black];
gd_white = gd_col [c_white];
gd_step = gdImageColorAllocate (im, 180, 180, 180);
gd_nase = gdImageColorAllocate (im, 85, 75, 255);
gdft = gdFontTiny;
gdfs = gdFontSmall;
/* create some grey values: maybe helpful... */
for (i = 0; i < 8; i++) {
int c = 72 + (48 / 8) * i;
gd_grey [i] = gdImageColorAllocate (im, c, c, c);
}
for (j = 0; j < height; j++) {
for (i = 0; i < width; i++) {
gdImageFilledRectangle (im, i * c_xy, j * c_xy, (i + 1) * c_xy - 1,
(j + 1) * c_xy - 1, gd_col [get_cell (i, j)]);
}
}
/* start circle: */
if (gd_trace_end > 0) {
/* set start dot only if something to trace is wanted: */
gdImageArc (im, c_xy / 2, c_xy / 2, c_xy / 7, c_xy / 7, 0, 360, gd_black);
}
}
void
gd_save ()
{
FILE *pngout;
if (! (pngout = fopen (gd_trace_filename, "wb"))) {
fprintf (stderr, "cannot open %s for writing; reason: %s\n",
gd_trace_filename, strerror (errno));
do_gdtrace = 0;
return;
}
gdImagePng (im, pngout);
fclose (pngout);
}
/*
* like gd_try_step but with p(ixel)p(ainting) for smaller output size:
*/
void
gd_try_step_pp (int try, int n_x, int n_y, int dp, int cc)
{
int x1 = n_x * c_xy;
int y1 = n_y * c_xy;
int x2, y2, x3, y3;
if (dp_dx(dp) < 0) {
/* left: */
y1 += c_xy - 3;
if (cc == 'r') {
y1 -= 6;
}
x3 = x1 + 4;
y3 = y1 - 3;
x2 = x3 + 6;
y2 = y3;
} else if (dp_dx(dp) > 0) {
/* right: */
x1 += c_xy - 1;
y1 += 3;
if (cc == 'r') {
y1 += 6;
}
x3 = x1 - 8;
y3 = y1 - 2;
x2 = x3 - 4;
y2 = y3;
} else if (dp_dy(dp) < 0) {
/* up: */
x1 += 3;
if (cc == 'r') {
x1 += 6;
}
x3 = x1 - 2;
y3 = y1 + 4;
x2 = x1 - 1;
y2 = y3 + 5;
} else {
/* down: */
x1 += c_xy - 1;
y1 += c_xy - 1;
x1 -= 3;
if (cc == 'r') {
x1 -= 6;
}
x3 = x1 - 2;
y3 = y1 - 8;
x2 = x1;
y2 = y3 - 6;
}
gd_arrow_pp (x1 + gd_try_xoff, y1 + gd_try_yoff, dp, gd_grey [gd_try_dcol]);
gd_paint_ch (x3, y3, dp, gd_grey [gd_try_dcol]);
gd_paint_ch (x3 + 3 + (dp == 'u' ? 1 : 0), y3 + 2, cc,
gd_grey [gd_try_dcol]);
gd_paint_num (x2, y2, try, gd_grey [gd_try_dcol]);
/* experimental: add some increment: */
/** gd_try_xoff += 3;
gd_try_yoff += 5;
**/
/* experimental: increment color value: */
gd_try_dcol = (gd_try_dcol + 1) % 8;
}
/*
* paint trace info about this try:
*/
void
gd_try_step (int exec_step, int tries, int n_x, int n_y,
int dp, int cc)
{
char tmp [128];
int a_len = i_max((c_xy * 1) / 4, 6);
int x1 = (n_x * c_xy) + c_xy / 2 + dp_dx(dp) * a_len;
int y1 = (n_y * c_xy) + c_xy / 2 + dp_dy(dp) * a_len;
int x2 = (n_x * c_xy) + c_xy / 2 + dp_dx(dp) * (c_xy / 2 - 2);
int y2 = (n_y * c_xy) + c_xy / 2 + dp_dy(dp) * (c_xy / 2 - 2);
int x3, y3;
if (c_xy < pp_size) {
/* try pixel painting: */
gd_try_step_pp (tries, n_x, n_y, dp, cc);
return;
}
sprintf (tmp, "%d.%d", exec_step, tries);
if (dp_dx(dp) < 0) {
/* left: */
if (cc == 'r') {
y1 -= gdft->h * 2 + 1;
y2 -= gdft->h * 2 + 1;
}
y1 += (c_xy / 2) - 5;
y2 += (c_xy / 2) - 5;
x3 = x2;
y3 = y2 - 2 * gdft->h;
} else if (dp_dx(dp) > 0) {
/* right: */
if (cc == 'r') {
y1 += gdft->h * 2 + 1;
y2 += gdft->h * 2 + 1;
}
y1 -= (c_xy / 2) - 5;
y2 -= (c_xy / 2) - 5;
x3 = x2 - strlen (tmp) * gdft->w;
y3 = y1 + 2;
} else if (dp_dy(dp) < 0) {
/* up: */
if (cc == 'r') {
x1 += gdft->w * strlen (tmp) + 3;