1 | srecord noeq G22RegionThinner_v2<Img extends WidthAndHeight>(IImageRegion<Img> originalRegion) is Steppable { |
2 | Rect bounds; |
3 | IImageRegion<Img> region; |
4 | |
5 | // 0 = outside of region |
6 | // 1 = inner pixel |
7 | // 2 = border pixel |
8 | // index is in bounds coordinates |
9 | byte[] pixels; |
10 | |
11 | int idx(int x, int y) { |
12 | ret (y-bounds.y)*bounds.w+x-bounds.x; |
13 | } |
14 | int idx(Pt p) { ret idx(p.x, p.y); } |
15 | |
16 | Pt idxToPt(int idx) { |
17 | ret pt(bounds.x+(idx % bounds.w), bounds.y+idx/bounds.w); |
18 | } |
19 | |
20 | byte getPixel(Pt p) { |
21 | ret !containsPt(bounds, p) ? 0 : pixels[idx(p)]; |
22 | } |
23 | |
24 | void init { |
25 | if (bounds != null) ret; |
26 | bounds = originalRegion.bounds(); |
27 | pixels = new byte[area(bounds)]; |
28 | for (Pt p : originalRegion.pixelIterator()) |
29 | pixels[idx(p.x, p.y)] = 1; |
30 | |
31 | region = new ThinnedRegion; |
32 | } |
33 | |
34 | class ThinnedRegion is IImageRegion<Img> { |
35 | public Img image() { ret originalRegion.image(); } |
36 | public Rect bounds() { ret bounds; } |
37 | |
38 | public bool contains(int x, int y) { |
39 | ret containsPt(bounds, x, y) && pixels[idx(x, y)] > 0; |
40 | } |
41 | |
42 | public ItIt<Pt> pixelIterator() { |
43 | ret iff_null(new IF0<Pt> { |
44 | int idx = 0; |
45 | |
46 | public Pt get() { |
47 | for (; idx < pixels.length; idx++) |
48 | if (pixels[idx] > 0) |
49 | ret idxToPt(idx++); |
50 | null; |
51 | } |
52 | }); |
53 | } |
54 | } |
55 | |
56 | public bool step() { |
57 | init(); |
58 | |
59 | L<PtBuffer> traces = g22_allBorderTraces_withDiagonals(region); |
60 | for (points : traces) |
61 | for (p : points) |
62 | pixels[idx(p)] = 2; |
63 | |
64 | new PtBuffer toDelete; |
65 | |
66 | for (points : traces) |
67 | for ping (p : points) { |
68 | if (deletableBorderPoint(p)) |
69 | toDelete.add(p); |
70 | } |
71 | |
72 | for (p : toDelete) |
73 | pixels[idx(p)] = 0; |
74 | |
75 | ret nempty(toDelete); |
76 | } |
77 | |
78 | bool deletableBorderPoint(Pt p) { |
79 | int surroundingBorderPixels = 0, surroundingInnerPixels = 0; |
80 | for (int dir = 1; dir <= 8; dir++) { |
81 | Pt p2 = ptPlus(p, onePathDirection(dir)); |
82 | byte value = getPixel(p2); |
83 | if (value == 2) |
84 | surroundingBorderPixels++; |
85 | else if (value == 1) |
86 | surroundingInnerPixels++; |
87 | } |
88 | |
89 | bool deletable = surroundingInnerPixels > 0 && surroundingBorderPixels <= 2; |
90 | |
91 | printVars ifdef G22RegionThinner_debug(+p, +surroundingInnerPixels, +surroundingBorderPixels, +deletable); |
92 | ret deletable; |
93 | } |
94 | |
95 | IImageRegion region aka get() { ret or(region, originalRegion); } |
96 | } |
Began life as a copy of #1034983
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj
No comments. add comment
Snippet ID: | #1034988 |
Snippet name: | G22RegionThinner_v2 - thin a region [backup] |
Eternal ID of this version: | #1034988/1 |
Text MD5: | 97578565acee40c7f1fe507d3d49e59a |
Author: | stefan |
Category: | javax |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2022-03-21 13:50:55 |
Source code size: | 2538 bytes / 96 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 119 / 133 |
Referenced in: | [show references] |