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

64
LINES

< > BotCompany Repo | #1006978 // Make Shape from image's alpha channel (for drawing transparent windows) [dev.]

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (5768L/40K/141K).

1  
!7
2  
3  
import java.awt.geom.*;
4  
5  
p {
6  
  BufferedImage img = loadBufferedImage(#1006973);
7  
  BWImage bw = getAlphaChannel(img);
8  
  Image2B alpha = new Image2B(bw);
9  
  
10  
  int w = alpha.w(), h = alpha.h();
11  
  
12  
  Path2D.Float path = new Path2D.Float();
13  
  
14  
  int x = w/2;
15  
  int y = -1;
16  
  while (y < h && !isInteresting(getQuadrant(alpha, x, y))) ++y;
17  
  
18  
  int x1 = x, y1 = y;
19  
  path.moveTo(x, y);
20  
  
21  
  new HashSet<Pt> seen;
22  
  while (seen.add(new Pt(x, y))) {
23  
    int q = getQuadrant(alpha, x, y);
24  
    
25  
    Pt next = null;
26  
    for (Pt p : neighborhood(x, y)) 
27  
      if (!seen.contains(p) && isInteresting(getQuadrant(alpha, p.x, p.y))) {
28  
      next = p;
29  
      break;
30  
    }
31  
    
32  
    if (next == null) break;
33  
      
34  
    x = next.x;
35  
    y = next.y;
36  
    path.lineTo(x, y);
37  
  }
38  
  
39  
  print("Have " + n(seen, "points"));
40  
41  
  showImage(alpha);
42  
  showImage(renderShape(path, w, h));
43  
}
44  
45  
sbool isInteresting(int q) {
46  
  ret q != 0 && q != 0x1111;
47  
}
48  
49  
static int getQuadrant(Image2B img, int x, int y) {
50  
  ret (getQuadrant_getPixel(img, x, y) ? 0x1000 : 0) +
51  
    (getQuadrant_getPixel(img, x+1, y) ? 0x0100 : 0) +
52  
    (getQuadrant_getPixel(img, x, y+1) ? 0x0010 : 0) +
53  
    (getQuadrant_getPixel(img, x+1, y+1) ? 0x0001 : 0);
54  
}
55  
56  
static bool getQuadrant_getPixel(Image2B img, int x, int y) {
57  
  ret x >= 0 && x < img.w && y >= 0 && y < img.h && img.getPixel(x, y);
58  
}
59  
60  
static L<Pt> neighborhood(int x, int y) {
61  
  ret ll(new Pt(x-1, y-1), new Pt(x, y-1), new Pt(x+1, y-1),
62  
    new Pt(x-1, y), new Pt(x+1, y),
63  
    new Pt(x-1, y+1), new Pt(x, y+1), new Pt(x+1, y+1));
64  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006978
Snippet name: Make Shape from image's alpha channel (for drawing transparent windows) [dev.]
Eternal ID of this version: #1006978/16
Text MD5: 5d5f1f0cccdbc20da642cda7e6cbd19f
Transpilation MD5: 7986b747dbf706d4faa7d1b3494e55b9
Author: stefan
Category: javax / imaging
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-02-21 19:55:30
Source code size: 1595 bytes / 64 lines
Pitched / IR pitched: No / No
Views / Downloads: 374 / 505
Version history: 15 change(s)
Referenced in: [show references]