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

83
LINES

< > BotCompany Repo | #1032003 // IntegralImage simplified for JS conversion

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

Libraryless. Click here for Pure Java version (117L/1K).

set flag LeanMode.
set flag l_withoutReflection.
set flag fail_dontUseFailClass.
set flag iround_noGeneric.
set flag Reparse.

final sclass IntegralImage {
  final int w, h;
  final int[] data; // 3 ints per pixel
  
  /* *(BufferedImage img) {
    w = img.getWidth();
    h = img.getHeight();
    if (longMul(w, h) > 8000000) fail("Image too big: " + w + "*" + h);
    int[] pixels = pixelsOfBufferedImage(img);
  */
  
  *(int *w, int *h, int[] pixels) {
    data = new int[w*h*3];
    int i = 0, j = 0, sumR = 0, sumG = 0, sumB = 0;
    for x to w: {
      int rgb = pixels[j++];
      data[i++] = (sumR += (rgb >> 16) & 0xFF);
      data[i++] = (sumG += (rgb >> 8) & 0xFF);
      data[i++] = (sumB += rgb & 0xFF);
    }
    for (int y = 1; y < h; y++) {
      sumR = sumG = sumB = 0;
      for x to w: {
        int rgb = pixels[j++];
        sumR += (rgb >> 16) & 0xFF;
        sumG += (rgb >> 8) & 0xFF;
        sumB += rgb & 0xFF;
        data[i] = sumR + data[i-w*3];
        data[i+1] = sumG + data[i-w*3+1];
        data[i+2] = sumB + data[i-w*3+2];
        i += 3;
      }
    }
  }
  
  // gets the integral value at x/y for given RGB channel
  public double getIntegralValue(int x, int y, int channel) {
    ret x < 0 || y < 0 || x >= w || y >= h ? 0
      : data[(y*w+x)*3+channel];
  }
  
  // gets sum of the 3 channels
  public double getIntegralValue(int x, int y) {
    if (x < 0 || y < 0 || x >= w || y >= h) ret 0;
    int i = (y*w+x)*3;
    ret data[i]+data[i+1]+data[i+2];
  }
  
  double averageBrightness() {
    ret doubleRatio(getIntegralValue(w-1, h-1), w*h*3*255.0);
  }
  
  toString {
    ret "IntegralImage " + w + "*" + h + ", brightness: " + this.averageBrightness();
  }
  
  //public BufferedImage getBufferedImage() { ret integralImageToBufferedImage(this); }
  
  public int getWidth() { ret w; }
  public int getHeight() { ret h; }
    
  double rectSum(int x1, int y1, int x2, int y2, int channel) {
    double bottomLeft  = getIntegralValue(x1-1, y2-1, channel);
    double bottomRight = getIntegralValue(x2-1, y2-1, channel);
    double topLeft     = getIntegralValue(x1-1, y1-1, channel);
    double topRight    = getIntegralValue(x2-1, y1-1, channel);
    ret bottomRight+topLeft-topRight-bottomLeft;
  }
  
  double rectSum(int x1, int y1, int x2, int y2) {
    double bottomLeft  = getIntegralValue(x1-1, y2-1);
    double bottomRight = getIntegralValue(x2-1, y2-1);
    double topLeft     = getIntegralValue(x1-1, y1-1);
    double topRight    = getIntegralValue(x2-1, y1-1);
    ret bottomRight+topLeft-topRight-bottomLeft;
  }
}

Author comment

Began life as a copy of #1019588

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx

No comments. add comment

Snippet ID: #1032003
Snippet name: IntegralImage simplified for JS conversion
Eternal ID of this version: #1032003/15
Text MD5: 242df3e63452dd2917367d9d4344e582
Transpilation MD5: 07b2c33f0ae6365ed47f97f06e72aa3c
Author: stefan
Category: javax / gui
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-20 16:39:52
Source code size: 2659 bytes / 83 lines
Pitched / IR pitched: No / No
Views / Downloads: 223 / 345
Version history: 14 change(s)
Referenced in: #1032535 - IntegralImage in JavaScript!