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

1  
set flag LeanMode.
2  
set flag l_withoutReflection.
3  
set flag fail_dontUseFailClass.
4  
set flag iround_noGeneric.
5  
set flag Reparse.
6  
7  
final sclass IntegralImage {
8  
  final int w, h;
9  
  final int[] data; // 3 ints per pixel
10  
  
11  
  /* *(BufferedImage img) {
12  
    w = img.getWidth();
13  
    h = img.getHeight();
14  
    if (longMul(w, h) > 8000000) fail("Image too big: " + w + "*" + h);
15  
    int[] pixels = pixelsOfBufferedImage(img);
16  
  */
17  
  
18  
  *(int *w, int *h, int[] pixels) {
19  
    data = new int[w*h*3];
20  
    int i = 0, j = 0, sumR = 0, sumG = 0, sumB = 0;
21  
    for x to w: {
22  
      int rgb = pixels[j++];
23  
      data[i++] = (sumR += (rgb >> 16) & 0xFF);
24  
      data[i++] = (sumG += (rgb >> 8) & 0xFF);
25  
      data[i++] = (sumB += rgb & 0xFF);
26  
    }
27  
    for (int y = 1; y < h; y++) {
28  
      sumR = sumG = sumB = 0;
29  
      for x to w: {
30  
        int rgb = pixels[j++];
31  
        sumR += (rgb >> 16) & 0xFF;
32  
        sumG += (rgb >> 8) & 0xFF;
33  
        sumB += rgb & 0xFF;
34  
        data[i] = sumR + data[i-w*3];
35  
        data[i+1] = sumG + data[i-w*3+1];
36  
        data[i+2] = sumB + data[i-w*3+2];
37  
        i += 3;
38  
      }
39  
    }
40  
  }
41  
  
42  
  // gets the integral value at x/y for given RGB channel
43  
  public double getIntegralValue(int x, int y, int channel) {
44  
    ret x < 0 || y < 0 || x >= w || y >= h ? 0
45  
      : data[(y*w+x)*3+channel];
46  
  }
47  
  
48  
  // gets sum of the 3 channels
49  
  public double getIntegralValue(int x, int y) {
50  
    if (x < 0 || y < 0 || x >= w || y >= h) ret 0;
51  
    int i = (y*w+x)*3;
52  
    ret data[i]+data[i+1]+data[i+2];
53  
  }
54  
  
55  
  double averageBrightness() {
56  
    ret doubleRatio(getIntegralValue(w-1, h-1), w*h*3*255.0);
57  
  }
58  
  
59  
  toString {
60  
    ret "IntegralImage " + w + "*" + h + ", brightness: " + this.averageBrightness();
61  
  }
62  
  
63  
  //public BufferedImage getBufferedImage() { ret integralImageToBufferedImage(this); }
64  
  
65  
  public int getWidth() { ret w; }
66  
  public int getHeight() { ret h; }
67  
    
68  
  double rectSum(int x1, int y1, int x2, int y2, int channel) {
69  
    double bottomLeft  = getIntegralValue(x1-1, y2-1, channel);
70  
    double bottomRight = getIntegralValue(x2-1, y2-1, channel);
71  
    double topLeft     = getIntegralValue(x1-1, y1-1, channel);
72  
    double topRight    = getIntegralValue(x2-1, y1-1, channel);
73  
    ret bottomRight+topLeft-topRight-bottomLeft;
74  
  }
75  
  
76  
  double rectSum(int x1, int y1, int x2, int y2) {
77  
    double bottomLeft  = getIntegralValue(x1-1, y2-1);
78  
    double bottomRight = getIntegralValue(x2-1, y2-1);
79  
    double topLeft     = getIntegralValue(x1-1, y1-1);
80  
    double topRight    = getIntegralValue(x2-1, y1-1);
81  
    ret bottomRight+topLeft-topRight-bottomLeft;
82  
  }
83  
}

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