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

205
LINES

< > BotCompany Repo | #1035058 // BWIntegralImage [backup]

JavaX fragment (include)

1  
// grayscale, actually
2  
sclass BWIntegralImage > Meta implements IBWIntegralImage, IIntegralImage {
3  
  int w, h;
4  
  int[] data; // 1 entry per pixel
5  
  ifdef BWIntegralImage_CountAccesses
6  
    long accesses;
7  
  endifdef
8  
  
9  
  // constructors
10  
  
11  
  *() {}
12  
  *(File f) { this(loadImage2(f)); }
13  
  *(MakesBufferedImage img) { this(toBufferedImage(img)); }
14  
  *(BufferedImage image) { grab(image); }
15  
  *(BufferedImage image, Decolorizer decolorizer) { grab(image, decolorizer); }
16  
  *(GrabbableIntPixels gp) { grab(gp); }
17  
  *(GrabbableGrayPixels gp) { grab(gp); }
18  
  
19  
  *(BWImage img) {
20  
    grab(new GrabbableGrayPixels(img.pixels, img.width, img.height,
21  
      0, img.width);
22  
  }
23  
  
24  
  // grab functions
25  
  
26  
  void grab(BufferedImage image, Decolorizer decolorizer default null) {
27  
    if (image.getType() == BufferedImage.TYPE_BYTE_GRAY)
28  
      grab(grabbableGrayPixels(image));
29  
    else // rgb image
30  
      grab(grabbableIntPixels_fastOrSlow(image), decolorizer);
31  
  }
32  
33  
  void grab(GrabbableIntPixels gp, Decolorizer decolorizer) {
34  
    if (isDefaultDecolorizer(decolorizer))
35  
      ret with grab(gp);
36  
    
37  
    alloc(gp.w, gp.h);
38  
    int w = this.w, h = this.h;
39  
    ifdef BWIntegralImage_debug
40  
      print("BWIntegralImage grabbing: " + gp);
41  
    endifdef
42  
    int offset = gp.offset, sum = 0;
43  
    int[] image = gp.data;
44  
    for x to w: {
45  
      int packed = image[offset+x];
46  
      int brightness = decolorizer.toGrayScale(packed);
47  
      data[x] = (sum += brightness);
48  
    }
49  
    
50  
    var ping = pingSource();
51  
    int scanlineExtra = gp.scanlineStride-w;
52  
    int iImage = offset+gp.scanlineStride, i = w;
53  
    for (int y = 1; y < h; y++) {
54  
      sum = 0;
55  
      for x to w: {
56  
        int packed = image[iImage];
57  
        int brightness = decolorizer.toGrayScale(packed);
58  
        sum += brightness;
59  
        data[i] = sum + data[i-w];
60  
        iImage++; i++;
61  
      }
62  
63  
      iImage += scanlineExtra;
64  
      ping?!;
65  
    } // for y
66  
  }
67  
    
68  
  void grab(GrabbableIntPixels gp) {
69  
    alloc(gp.w, gp.h);
70  
    int w = this.w, h = this.h;
71  
    ifdef BWIntegralImage_debug
72  
      print("BWIntegralImage grabbing: " + gp);
73  
    endifdef
74  
    int offset = gp.offset, sum = 0;
75  
    int[] image = gp.data;
76  
    for x to w: {
77  
      int packed = image[offset+x];
78  
      int brightness = packedToBrightness(packed);
79  
      data[x] = (sum += brightness);
80  
    }
81  
    
82  
    var ping = pingSource();
83  
    int scanlineExtra = gp.scanlineStride-w;
84  
    int iImage = offset+gp.scanlineStride, i = w;
85  
    for (int y = 1; y < h; y++) {
86  
      sum = 0;
87  
      for x to w: {
88  
        int packed = image[iImage];
89  
        int brightness = packedToBrightness(packed);
90  
        sum += brightness;
91  
        data[i] = sum + data[i-w];
92  
        iImage++; i++;
93  
      }
94  
95  
      iImage += scanlineExtra;
96  
      ping?!;
97  
    } // for y
98  
  }
99  
  
100  
  void grab(GrabbableGrayPixels gp) {
101  
    alloc(gp.w, gp.h);
102  
    ifdef BWIntegralImage_debug
103  
      print("BWIntegralImage grabbing: " + gp);
104  
    endifdef
105  
    int offset = gp.offset, sum = 0;
106  
    byte[] image = gp.data;
107  
    for x to w: {
108  
      int brightness = image[offset+x];
109  
      data[x] = (sum += brightness);
110  
    }
111  
    
112  
    var ping = pingSource();
113  
    int scanlineExtra = gp.scanlineStride-w;
114  
    int iImage = offset+gp.scanlineStride, i = w;
115  
    for (int y = 1; y < h; y++) {
116  
      sum = 0;
117  
      for x to w: {
118  
        int brightness = image[iImage];
119  
        sum += brightness;
120  
        data[i] = sum + data[i-w];
121  
        iImage++; i++;
122  
      }
123  
124  
      iImage += scanlineExtra;
125  
      ping?!;
126  
    } // for y
127  
  }
128  
    
129  
  private void alloc(int w, int h) {
130  
    this.w = w;
131  
    this.h = h;
132  
    if (w*h > 8*1024*1024)
133  
      fail("Image too large (more than 8 MP): " + w + "*" + h);
134  
    data = new int[w*h];
135  
  }
136  
  
137  
  // pixels outside of image are considered black
138  
  int get(int x, int y) {
139  
    ifdef BWIntegralImage_CountAccesses
140  
      ++accesses;
141  
    endifdef
142  
    ret x < 0 || y < 0 ? 0
143  
      : data[min(y, h-1)*w+min(x, w-1)];
144  
  }
145  
146  
  // precise version [TO TEST!]
147  
  public double getIIValue(double x, double y) {
148  
    int xFloor = ifloor(x), yFloor = ifloor(y);
149  
    double val = getIIValue(xFloor, yFloor);
150  
    
151  
    // at integer coordinate?
152  
    if (xFloor == x && yFloor == y)
153  
      ret val;
154  
      
155  
    // at non-integer coordinate, perform subpixel calculation
156  
    double val2 = getIIValue(xFloor+1, yFloor);
157  
    double val3 = getIIValue(xFloor  , yFloor+1);
158  
    double val4 = getIIValue(xFloor+1, yFloor+1);
159  
    
160  
    ret blend2D(val, val2, val3, val4, x-xFloor, y-yFloor);
161  
  }
162  
  
163  
  public int getIIValue(int x, int y) { ret get(x, y); }
164  
  public double getIntegralValue(int x, int y, int channel) {
165  
    ret get(x, y);
166  
  }
167  
  
168  
  public double getPixelAverage(int x1, int y1, int x2, int y2) {
169  
    int area = (x2-x1)*(y2-y1);
170  
    ret doubleRatio(bwIntegralImage_sumRect(this, x1, y1, x2, y2), area);
171  
  }
172  
  
173  
  public int getWidth() { ret w; }
174  
  public int getHeight() { ret h; }
175  
  
176  
  int packedToBrightness(int packed) {
177  
    int b = (packed & 0xFF);
178  
    ifdef BWIntegralImage_brightnessCheat
179  
      ret b;
180  
    endifdef
181  
    ifndef BWIntegralImage_brightnessCheat
182  
      int r = ((packed >> 16) & 0xFF);
183  
      int g = ((packed >> 8) & 0xFF);
184  
      ret (r+g+b+1)/3;
185  
    endifndef
186  
  }
187  
  
188  
  // get brightness of pixel
189  
  public int getInt(int x, int y) {
190  
    ret iround(rectSum(x, y, x+1, y+1, 0));
191  
  }
192  
  
193  
  // returns RGB pixel without alpha
194  
  public int getPixel(int x, int y) {
195  
    ret rgbIntFromGrayscale(getInt(x, y));
196  
  }
197  
  
198  
  public BufferedImage getBufferedImage() {
199  
    //ret scaleDownUsingIntegralImageBW(this, w).getBufferedImage();
200  
    O src = metaGet src(this);
201  
    if (src cast BufferedImage) ret src;
202  
    
203  
    ret grayImageFromIBWIntegralImage(this);
204  
  }
205  
}

Author comment

Began life as a copy of #1019595

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035058
Snippet name: BWIntegralImage [backup]
Eternal ID of this version: #1035058/1
Text MD5: bbb55b3ccb3c09dfceaf2c2739cac0bb
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-03-26 03:37:55
Source code size: 5816 bytes / 205 lines
Pitched / IR pitched: No / No
Views / Downloads: 175 / 154
Referenced in: [show references]