Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

52
LINES

< > BotCompany Repo | #1006981 // alphaToShape - quite experimental shape generation from alpha channel

JavaX fragment (include)

1  
import java.awt.geom.*;
2  
3  
static Path2D.Float alphaToShape(BufferedImage img) {
4  
  BWImage bw = getAlphaChannel(img);
5  
  Image2B alpha = new Image2B(bw);
6  
  
7  
  int w = alpha.w(), h = alpha.h();
8  
  
9  
  Path2D.Float path = new Path2D.Float();
10  
  
11  
  int x = w/2;
12  
  int y = -1;
13  
  while (y < h && !alphaToShape_isInteresting(alphaToShape_getQuadrant(alpha, x, y))) ++y;
14  
  
15  
  int x1 = x, y1 = y;
16  
  path.moveTo(x, y);
17  
  
18  
  new HashSet<Pt> seen;
19  
  while (seen.add(new Pt(x, y))) {
20  
    int q = alphaToShape_getQuadrant(alpha, x, y);
21  
    
22  
    Pt next = null;
23  
    for (Pt p : neighborhood8(x, y)) 
24  
      if (!seen.contains(p) && alphaToShape_isInteresting(alphaToShape_getQuadrant(alpha, p.x, p.y))) {
25  
      next = p;
26  
      break;
27  
    }
28  
    
29  
    if (next == null) break;
30  
      
31  
    x = next.x;
32  
    y = next.y;
33  
    path.lineTo(x+1, y+1);
34  
  }
35  
  
36  
  ret path;
37  
}
38  
39  
sbool alphaToShape_isInteresting(int q) {
40  
  ret q != 0 && q != 0x1111;
41  
}
42  
43  
static int alphaToShape_getQuadrant(Image2B img, int x, int y) {
44  
  ret (alphaToShape_getQuadrant_getPixel(img, x, y) ? 0x1000 : 0) +
45  
    (alphaToShape_getQuadrant_getPixel(img, x+1, y) ? 0x0100 : 0) +
46  
    (alphaToShape_getQuadrant_getPixel(img, x, y+1) ? 0x0010 : 0) +
47  
    (alphaToShape_getQuadrant_getPixel(img, x+1, y+1) ? 0x0001 : 0);
48  
}
49  
50  
static bool alphaToShape_getQuadrant_getPixel(Image2B img, int x, int y) {
51  
  ret x >= 0 && x < img.w && y >= 0 && y < img.h && img.getPixel(x, y);
52  
}

Author comment

Began life as a copy of #1006978

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ekrmjmnbrukm, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006981
Snippet name: alphaToShape - quite experimental shape generation from alpha channel
Eternal ID of this version: #1006981/5
Text MD5: 6ed9de05f6dbc6d10347bb7cd7038f90
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-26 19:41:21
Source code size: 1457 bytes / 52 lines
Pitched / IR pitched: No / No
Views / Downloads: 416 / 460
Version history: 4 change(s)
Referenced in: [show references]