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)

import java.awt.geom.*;

static Path2D.Float alphaToShape(BufferedImage img) {
  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 && !alphaToShape_isInteresting(alphaToShape_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 = alphaToShape_getQuadrant(alpha, x, y);
    
    Pt next = null;
    for (Pt p : neighborhood8(x, y)) 
      if (!seen.contains(p) && alphaToShape_isInteresting(alphaToShape_getQuadrant(alpha, p.x, p.y))) {
      next = p;
      break;
    }
    
    if (next == null) break;
      
    x = next.x;
    y = next.y;
    path.lineTo(x+1, y+1);
  }
  
  ret path;
}

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

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

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

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: 343 / 384
Version history: 4 change(s)
Referenced in: [show references]