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

56
LINES

< > BotCompany Repo | #1030535 // BWIntegralImage_twoParts

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

Libraryless. Click here for Pure Java version (6782L/42K).

// integral image split in 2 vertical stripes so creation is 2-parallel
final sclass BWIntegralImage_twoParts implements MakesBufferedImage, IBWIntegralImage {
  int w, h, th;
  IBWIntegralImage top, bottom;

  *() {}
  *(File f) { this(loadImage2(f)); }
  
  *(MakesBufferedImage img) { this(toBufferedImage(img)); }
  
  *(BufferedImage image) {
    this(image, (img, idx) -> BWIntegralImage(img));
  }
  
  *(BufferedImage image, IF2<BufferedImage, Int, IBWIntegralImage> makeSubImage) {
    this(null, image, makeSubImage);
  }
  
  *(ThreadPoolExecutor executor, BufferedImage image, IF2<BufferedImage, Int, IBWIntegralImage> makeSubImage) ctex {
    w = image.getWidth();
    h = image.getHeight();
    th = (h+1)/2;
    L<IBWIntegralImage> l = parallelEval(executor,
      () -> makeSubImage.get(virtualClipBufferedImage(image, 0, 0, w, th), 0),
      () -> h <= th ? null : makeSubImage.get(virtualClipBufferedImage(image, 0, th, w, h-th), 1)
    );
    top = first(l);
    bottom = second(l);
  }
    
  public int getIIValue(int x, int y) {
    ret y < th ? top.getIIValue(x, y)
      : top.getIIValue(x, th-1) + bottom.getIIValue(x, y-th);
  }
  
  // optimization over default method
  public int getPixelSum(int x1, int y1, int x2, int y2) {
    if (y2 <= th)
      ret top.getPixelSum(x1, y1, x2, y2);
    if (y1 >= th)
      ret bottom.getPixelSum(x1, y1-th, x2, y2-th);
      
    // rectangle straddles top and bottom stripe
    //ret bwIntegralImage_sumRect(this, x1, y1, x2, y2);
    int topLeft     = top.getIIValue(x1-1, y1-1);
    int topRight    = top.getIIValue(x2-1, y1-1);
    int mediumLeft  = top.getIIValue(x1-1, th-1);
    int mediumRight = top.getIIValue(x2-1, th-1);
    int bottomLeft  = bottom.getIIValue(x1-1, y2-th-1);
    int bottomRight = bottom.getIIValue(x2-1, y2-th-1);
    ret bottomRight-bottomLeft+mediumRight-mediumLeft+topLeft-topRight;
  }
  
  public int getWidth() { ret w; }
  public int getHeight() { ret h; }
}

Author comment

Began life as a copy of #1019595

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, vouqrxazstgt

No comments. add comment

Snippet ID: #1030535
Snippet name: BWIntegralImage_twoParts
Eternal ID of this version: #1030535/28
Text MD5: 8deaef4c85b9b9f18141dd8655740077
Transpilation MD5: 3ed531335193367e5fcd9e4ba1171367
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-01-07 03:04:17
Source code size: 2017 bytes / 56 lines
Pitched / IR pitched: No / No
Views / Downloads: 188 / 461
Version history: 27 change(s)
Referenced in: [show references]