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

1  
// result image is as large as the region's bounds
2  
// with black pixels inside and white pixels outside the region
3  
srecord noeq RegionToBWImage(IImageRegion region) {
4  
  settable Rect bounds;
5  
  BWImage image;
6  
  
7  
  settable int bgColor = 0xFF;
8  
  settable int color = 0x00;
9  
  
10  
  run {
11  
    bounds if null = region.bounds();
12  
    int x1 = bounds.x, y1 = bounds.y, w = bounds.w, h = bounds.h, i = 0;
13  
    byte[] pixels = new[w*h];
14  
    if (bgColor != 0) fillArray(pixels, (byte) bgColor);
15  
    for y to h:
16  
      for x to w: {
17  
        if (region.contains(x1+x, y1+y))
18  
          pixels[i] = (byte) color;
19  
        i++;
20  
      }
21  
      
22  
    image = new BWImage(w, h, pixels);
23  
  }
24  
  
25  
  BWImage get() {
26  
    if (image == null) run();
27  
    ret image;
28  
  }
29  
}

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: 179 / 339
Version history: 14 change(s)
Referenced in: [show references]