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

29
LINES

< > BotCompany Repo | #1034574 // RegionToBWImage

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (12568L/73K).

// result image is as large as the region's bounds
// with black pixels inside and white pixels outside the region
srecord noeq RegionToBWImage(IImageRegion region) {
  settable Rect bounds;
  BWImage image;
  
  settable int bgColor = 0xFF;
  settable int color = 0x00;
  
  run {
    bounds if null = region.bounds();
    int x1 = bounds.x, y1 = bounds.y, w = bounds.w, h = bounds.h, i = 0;
    byte[] pixels = new[w*h];
    if (bgColor != 0) fillArray(pixels, (byte) bgColor);
    for y to h:
      for x to w: {
        if (region.contains(x1+x, y1+y))
          pixels[i] = (byte) color;
        i++;
      }
      
    image = new BWImage(w, h, pixels);
  }
  
  BWImage get() {
    if (image == null) run();
    ret image;
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1034574
Snippet name: RegionToBWImage
Eternal ID of this version: #1034574/15
Text MD5: bbed297c1f3822144f7441ba55508b91
Transpilation MD5: b27c9e60d9cd2765bd0f5b1e2f3e1577
Author: stefan
Category: javax / gazelle 22
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-06-24 09:53:01
Source code size: 763 bytes / 29 lines
Pitched / IR pitched: No / No
Views / Downloads: 177 / 336
Version history: 14 change(s)
Referenced in: [show references]