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).

!7

import java.awt.geom.*;

p {
  BufferedImage img = loadBufferedImage(#1006973);
  BWImage bw = getAlphaChannel(img);
  Image2B alpha = new Image2B(bw);
  
  int w = alpha.w(), h = alpha.h();
  
  Path2D.Float path = new Path2D.Float();
  
  int x = w/2;
  int y = -1;
  while (y < h && !isInteresting(getQuadrant(alpha, x, y))) ++y;
  
  int x1 = x, y1 = y;
  path.moveTo(x, y);
  
  new HashSet<Pt> seen;
  while (seen.add(new Pt(x, y))) {
    int q = getQuadrant(alpha, x, y);
    
    Pt next = null;
    for (Pt p : neighborhood(x, y)) 
      if (!seen.contains(p) && isInteresting(getQuadrant(alpha, p.x, p.y))) {
      next = p;
      break;
    }
    
    if (next == null) break;
      
    x = next.x;
    y = next.y;
    path.lineTo(x, y);
  }
  
  print("Have " + n(seen, "points"));

  showImage(alpha);
  showImage(renderShape(path, w, h));
}

sbool isInteresting(int q) {
  ret q != 0 && q != 0x1111;
}

static int getQuadrant(Image2B img, int x, int y) {
  ret (getQuadrant_getPixel(img, x, y) ? 0x1000 : 0) +
    (getQuadrant_getPixel(img, x+1, y) ? 0x0100 : 0) +
    (getQuadrant_getPixel(img, x, y+1) ? 0x0010 : 0) +
    (getQuadrant_getPixel(img, x+1, y+1) ? 0x0001 : 0);
}

static bool getQuadrant_getPixel(Image2B img, int x, int y) {
  ret x >= 0 && x < img.w && y >= 0 && y < img.h && img.getPixel(x, y);
}

static L<Pt> neighborhood(int x, int y) {
  ret ll(new Pt(x-1, y-1), new Pt(x, y-1), new Pt(x+1, y-1),
    new Pt(x-1, y), new Pt(x+1, y),
    new Pt(x-1, y+1), new Pt(x, y+1), new Pt(x+1, y+1));
}

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: 371 / 500
Version history: 15 change(s)
Referenced in: [show references]