-
Notifications
You must be signed in to change notification settings - Fork 0
/
expand.c
726 lines (599 loc) · 12.7 KB
/
expand.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
/*
expand.c
This is part of pcb2g - pcb bitmap to G code converter
Copyright (C) 2011- 2015 Peter Popovec, [email protected]
This program 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, either version 3 of the License, or
(at your option) any later version.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
PCB copper expansion routines
*/
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>
#include "pcb2g.h"
//#define SPEED_UP1
static unsigned char
get_mask (struct image *image, int i, int border)
{
unsigned char mask = 0;
if (*(image->data + i) <= border)
mask |= 1 << 3;
if (*(image->data + i + 1) <= border)
mask |= 1 << 2;
if (*(image->data + i + 2) <= border)
mask |= 1 << 1;
if (*(image->data + i + 2 + image->x) <= border)
mask |= 1 << 0;
if (*(image->data + i + image->x) <= border)
mask |= 1 << 4;
if (*(image->data + i + image->x + image->x) <= border)
mask |= 1 << 5;
if (*(image->data + i + 1 + image->x + image->x) <= border)
mask |= 1 << 6;
if (*(image->data + i + 2 + image->x + image->x) <= border)
mask |= 1 << 7;
return mask;
}
static int
local_end (struct image *image, int fill_data, int border)
{
int count = 0;
int i;
unsigned char mask;
for (i = 0; i < image->size; i++)
{
if (*(image->data + i + 1 + image->x) <= border)
continue;
mask = get_mask (image, i, border);
switch (mask) // o ->x if match mask
{
case 0xa7: // a7
case 0xcb: // .xx x.x xx. x.x x.. xxx xxx ..x
case 0xbc: // .ox .ox xo. xo. .ox .ox xo. xo.
case 0x7a: // x.x .xx x.x xx. xxx x.. ..x xxx
case 0xe9:
case 0x2f:
case 0x9e:
case 0xf2:
case 0xbe: // be fa af eb
case 0xfa: // xxx x.x xxx x.x
case 0xaf: // xo. xo. .ox .ox
case 0xeb: // x.x xxx x.x xxx
case 0xe0: // e0 0e 83 38
case 0x0e: // ... xxx ..x x..
case 0x83: // .o. .o. .ox xo.
case 0x38: // xxx ... ..x x..
case 0x06: // 06 0c 30 18 c0 60 81 03
case 0x0c: // .xx xx. ... x.. ... ... ... ..x
case 0x30: // .o. .o. xo. xo. .o. .o. .ox .ox
case 0x18: // ... ... x.. ... .xx xx. ..x ...
case 0xc0:
case 0x60:
case 0x81:
case 0x03:
*(image->data + i + 1 + image->x) = fill_data;
count++;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
//printf ("end filter %d\n", count);
return count;
}
static int
local_T1 (struct image *image, int fill_data, int border)
{
int count = 0;
int i;
unsigned char mask;
for (i = 0; i < image->size; i++)
{
if (*(image->data + i + 1 + image->x) <= border)
continue;
mask = get_mask (image, i, border);
switch (mask) // o ->x if match mask x = copper
{
case 0x2b: // 2b a9 a6 ac 9a b2 6a ca
case 0xa9: // x.x x.. .xx xx. x.x ..x x.x x.x
case 0xa6: // .ox .ox .o. .o. xo. xo. .o. .o.
case 0xac: // x.. x.x x.x x.x ..x x.x xx. .xx
case 0x9a:
case 0xb2:
case 0x6a:
case 0xca:
case 0xae: // ae ea ab ba
case 0xea: // xxx x.x x.x x.x
case 0xab: // .o. .o. .ox xo.
case 0xba: // x.x xxx x.x x.x
*(image->data + i + 1 + image->x) = fill_data;
count++;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
//printf ("end filter T1 %d\n", count);
return count;
}
void
recolor (struct image *image, int fill_color, int level)
{
int i;
for (i = 0; i < image->x * image->y; i++)
if (*(image->data + i) == level)
*(image->data + i) = fill_color;
return;
}
static void bit_e (struct image *image);
int
expand_copper (struct image *image)
{
int ret, i1, i2, flag;
bit_e (image);
for (i1 = 0, i2 = image->x; i1 < image->size; i1++, i2++)
if (image->mask_data[i1] & 0x10)
if (image->data[i2] != 0)
image->data[i2] = 1;
//printf ("all ok\n");
for (flag = 1; flag;)
{
flag = 0;
for (;;)
{
ret = local_T1 (image, 70, 90);
if (!ret)
break;
flag = 1;
printf ("local_t1 found\n");
}
for (;;)
{
ret = local_end (image, 70, 90);
if (!ret)
break;
flag = 1;
printf ("local_end found\n");
}
}
recolor (image, 160, 70);
recolor (image, 150, C_HOLE);
recolor (image, 190, C_HOLE_ALIGNED);
recolor (image, 200, 1);
recolor (image, 255, 0);
recolor (image, 0, 100);
/*
for (sy = 0, sx = 0; sx < image->x * image->y; sx++)
if (*(image->data + sx) < 64)
sy++;
printf ("track=%d\n", sy);
*/
return 0;
}
#define SET_OR_MASK count++;\
image->or_mask[i + 1] |= 0x10; \
image->or_mask[i - 1] |= 0x01; \
image->or_mask[i - 1 - image->x] |= 0x80; \
image->or_mask[i - image->x] |= 0x40; \
image->or_mask[i + 1 - image->x] |= 0x20; \
image->or_mask[i - 1 + image->x] |= 0x02; \
image->or_mask[i + image->x] |= 0x04; \
image->or_mask[i + 1 + image->x] |= 0x08;
//convert 4 to 8 continuous
/* 34 85 58 43 16 0D D0 16
321 .x. .x. x.. ..x .xx xx.
4 0 x.. ..x x.. ..x x.. ..x
567 x.. ..x .x. .x. ... ...
*/
static int
l_4to8 (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x34:
case 0x85:
case 0x58:
case 0x43:
case 0x16:
case 0x0d:
case 0x61:
case 0xd0:
SET_OR_MASK;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
printf ("4to8 %d\n", count);
return count;
}
//convert diagonal
/* 14 05 50 41
321 .x. .x. ... ...
4 0 x.. ..x x.. ..x
567 ... ... .x. .x.
*/
static int
l_diag (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x14:
case 0x05:
case 0x50:
case 0x41:
SET_OR_MASK;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
printf ("diag %d\n", count);
return count;
}
//diag_fill
/* 97 D3 3D 79 5e 4f f4 e5
321 .xx ..x xx. x.. xxx xxx .x. .x.
4 0 x.x x.x x.x x.x x.. ..x x.. ..x
567 ..x .xx x.. xx. .x. .x. xxx xxx
*/
static int
l_diag_fill (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x97:
case 0xd3:
case 0x3d:
case 0x79:
case 0x5e:
case 0xe5:
case 0x4f:
case 0xf4:
SET_OR_MASK;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
printf ("diag_fill %d\n", count);
return count;
}
static int
l_noexpand (struct image *image)
{
int i, count = 0;
for (i = image->x + 1; i < image->size;)
{
if ((image->or_mask[i - 1] & 1))
{
i++;
continue;
}
switch (image->or_mask[i])
{
//x.x
//x.x
//xxx
case 0xfb:
SET_OR_MASK;
if (i - image->x > 0)
i -= image->x;
else
i++;
break;
//xxx
//x.x
//x.x
case 0xbf:
SET_OR_MASK;
if (i + image->x < image->size)
i += image->x;
else
i++;
break;
//xxx
//..x
//xxx
case 0xef:
SET_OR_MASK;
i--;
break;
/* f1 f9 f3 1f 3f 9f c7 cf e7 fd f7 df 7f
321 ... x.. ..x xxx xxx xxx .xx XXX .XX xx. .xx xxx xxx
4 0 x.x x.x x.x x.x x.x x.x ..x ..X ..X x.x x.x x.x x.x
567 xxx xxx xxx ... x.. ..x .xx .XX XXX xxx xxx .xx xx.
*/
case 0xf1:
case 0xf9:
case 0xf3:
case 0x1f:
case 0x3f:
case 0x9f:
case 0xc7:
case 0xcf:
case 0xe7:
case 0xfd:
case 0xf7:
case 0xdf:
case 0x7f:
SET_OR_MASK;
i += 2;
break;
/* 7c fe 7e fc
321 xx. xxx XXX xx.
4 0 x.. x.. x.. x..
567 xx. xxx XX. xxx
*/
case 0x7c:
case 0xfe:
case 0x7e:
case 0xfc:
SET_OR_MASK;
default:
i++;
break;
} //switch
} //for
// printf ("noexpand run with %d points \n", count);
return count;
}
/* e0 e1 f0
321 ... ... ...
4 0 ... ..x x..
567 xxx xxx xxx
x = copper
*/
static int
l_up (struct image *image)
{
int count = 0;
int i = 0;
/*
if ((image->mask_data[1] & 0x10))
switch (image->mask_data[0])
{
case 0xe0:
case 0xe1:
case 0xf0:
SET_OR_MASK;
}
*/
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0xe0:
case 0xe1:
case 0xf0:
SET_OR_MASK;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
//printf ("up %d\n", count);
return count;
}
/* 0e 1e 0f
321 XXX xxx xxx
4 0 ... x.. ..x
567 ... ... ...
*/
static int
l_down (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x0e:
case 0x1e:
case 0x0f:
SET_OR_MASK;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
//printf ("down %d\n", count);
return count;
}
/* 83 87 c3
321 ..x .xx ..x
4 0 ..x ..x ..x
567 ..x ..x .xx
*/
static int
l_left (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x83:
case 0x87:
case 0xc3:
SET_OR_MASK;
i++;
}
}
//printf ("left %d\n", count);
return count;
}
/* 38 3c 78
321 x.. xx. x..
4 0 x.. x.. x..
567 x.. x.. xx.
*/
static int
l_right (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x38:
case 0x3c:
case 0x78:
SET_OR_MASK;
}
}
//printf ("right %d\n", count);
return count;
}
/* 1c 70 07 c1
321 xx. ... .xx ...
4 0 x.. x.. ..x ..x
567 ... xx. ... .xx
*/
/* 3e 8f f8 e3
321 XXX xxx x.. ..x
4 0 X.. ..x x.. ..x
567 X.. ..x xxx xxx
*/
static int
l_rozok (struct image *image)
{
int count = 0;
int i;
for (i = image->x + 1; i < image->size; i++)
{
if ((image->mask_data[i - 1] & 1))
continue;
switch (image->mask_data[i])
{
case 0x1c:
case 0x70:
case 0x07:
case 0xc1:
case 0x3e:
case 0x8f:
case 0xf8:
case 0xe3:
SET_OR_MASK;
}
#ifdef SPEED_UP1
i += (mask & 1);
#endif
}
//printf ("rozok %d\n", count);
return count;
}
static void
debug_dump_bit_image (struct image *image)
{
FILE *f;
int i;
f = fopen ("bdebug.pgm", "w");
if (!f)
return;
fprintf (f, "P5\n%d %d\n255\n", image->x, image->y);
for (i = 1; i < image->x * image->y; i++)
{
if (image->or_mask[i] & 0x10)
fputc (255, f);
else
fputc (0, f);
}
fputc (255, f);
fclose (f);
}
static void
bit_e (struct image *image)
{
int flag = 0;
int i, j, k, ret, allret;
struct timespec ts, ts_old, ts_diff;
image->size = image->x * (image->y - 2) - 2;
clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts_old);
image->mask_data =
calloc (image->x * (image->y + 2), sizeof (unsigned char));
image->or_mask = calloc (image->x * (image->y + 2), sizeof (unsigned char));
for (i = 0; i < image->size; i++)
image->or_mask[i] = get_mask (image, i, 70);
memcpy (image->mask_data, image->or_mask, image->size);
l_4to8 (image);
for (j = 0;; j++)
{
allret = 0;
for (k = 0, ret = 1; ret; k++)
{
ret = l_noexpand (image);
memcpy (image->mask_data, image->or_mask, image->size);
allret += ret;
}
if (!flag)
{
ret += l_diag (image);
memcpy (image->mask_data, image->or_mask, image->size);
flag++;
ret += l_diag_fill (image);
memcpy (image->mask_data, image->or_mask, image->size);
}
ret = l_up (image);
memcpy (image->mask_data, image->or_mask, image->size);
ret += l_down (image);
memcpy (image->mask_data, image->or_mask, image->size);
ret += l_left (image);
memcpy (image->mask_data, image->or_mask, image->size);
ret += l_right (image);
memcpy (image->mask_data, image->or_mask, image->size);
ret += l_rozok (image);
memcpy (image->mask_data, image->or_mask, image->size);
allret += ret;
if (!allret)
break;
clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &ts);
ts_diff = tsx_diff (ts_old, ts);
printf ("................... iteration %d, time %ld.%09ld", j,
ts_diff.tv_sec, ts_diff.tv_nsec);
ts_diff = tsx_diff (image->ts, ts);
printf (" sum: %ld.%09ld\n", ts_diff.tv_sec, ts_diff.tv_nsec);
ts_old.tv_sec = ts.tv_sec;
ts_old.tv_nsec = ts.tv_nsec;
}
printf ("all ok\n");
if (image->debug_files)
debug_dump_bit_image (image);
}