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

253
LINES

< > BotCompany Repo | #1004247 - BWImage (8 bit)

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

Libraryless. Click here for Pure Java version (4886L/28K).

1  
static final class BWImage implements MakesBufferedImage, IBWImage {
2  
  int width, height;
3  
  byte[] pixels;
4  
5  
  // color returned when getPixel is called with a position outside the actual image
6  
  float borderColor = 0.0f;
7  
  
8  
  // for unstructure()
9  
  *() {}
10  
11  
  // BLACK!
12  
  *(int *width, int *height) {
13  
    pixels = new byte[width*height];
14  
  }
15  
16  
  *(int *width, int *height, float brightness) {
17  
    pixels = new byte[width*height];
18  
    fillArrayUnlessZero(pixels, _toByte(brightness));
19  
  }
20  
  
21  
  *(int *width, int *height, float[] pixels) {
22  
    this.pixels = new byte[pixels.length];
23  
    for (int i = 0; i < pixels.length; i++)
24  
      this.pixels[i] = _toByte(pixels[i]);
25  
  }
26  
27  
  public BWImage(int width, int height, byte[] pixels) {
28  
    this.height = height;
29  
    this.width = width;
30  
    this.pixels = pixels;
31  
  }
32  
33  
  public BWImage(BWImage image) {
34  
    width = image.getWidth();
35  
    height = image.getHeight();
36  
    byte[] pixels = this.pixels = new byte[width*height];
37  
    for (int y = 0; y < height; y++)
38  
      for (int x = 0; x < width; x++)
39  
        pixels[y*width+x] = image.getByte(x, y);
40  
  }
41  
42  
  // TODO: optimize!
43  
  *(RGBImage image) {
44  
    width = image.getWidth();
45  
    height = image.getHeight();
46  
    byte[] pixels = this.pixels = new byte[height*width];
47  
    for (int y = 0; y < height; y++)
48  
      for (int x = 0; x < width; x++) {
49  
        RGB rgb = image.getRGB(x, y);
50  
        pixels[y*width+x] = BWImage._toByte(rgb.getBrightness());
51  
      }
52  
  }
53  
54  
  /*public BWImage(BufferedImage image) {
55  
    this(new RGBImage(image));
56  
  }*/
57  
58  
  *(BufferedImage image) ctex {
59  
    width = image.getWidth();
60  
    height = image.getHeight();
61  
    int[] pixels = new int[width*height];
62  
    byte[] bytePixels = this.pixels = new byte[width*height];
63  
    PixelGrabber pixelGrabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
64  
    if (!pixelGrabber.grabPixels())
65  
      fail("Could not grab pixels");
66  
    int n = width*height;
67  
    
68  
    for (int i = 0; i < n; i++) {
69  
      //bytePixels[i] = pixelToByte(pixels[i]);
70  
      int packed = pixels[i];
71  
      /*float r = ((packed >> 16) & 0xFF)/255f;
72  
      float g = ((packed >> 8) & 0xFF)/255f;
73  
      float b = (packed & 0xFF)/255f;
74  
      bytePixels[i] = (byte) iround((r+g+b)/3.0f*255f);*/
75  
      int r = ((packed >> 16) & 0xFF);
76  
      int g = ((packed >> 8) & 0xFF);
77  
      int b = (packed & 0xFF);
78  
      bytePixels[i] = (byte) ((r+g+b+1)/3);
79  
    }
80  
  }
81  
  
82  
  // TODO: does it exactly match the other method? (asRGB+getBrightness+_toByte)
83  
  static byte pixelToByte(int packed) {
84  
    /*int r = (packed >> 16) & 0xFF;
85  
    int g = (packed >> 8) & 0xFF;
86  
    int b = packed & 0xFF;
87  
    ret (byte) ((r+g+b)/3.0f);*/
88  
    float r = ((packed >> 16) & 0xFF)/255f;
89  
    float g = ((packed >> 8) & 0xFF)/255f;
90  
    float b = (packed & 0xFF)/255f;
91  
    ret (byte) ((r+g+b)/3.0f*255f);
92  
  }
93  
94  
  public byte getByte(int x, int y) {
95  
    return inRange(x, y) ? getByte_noRangeCheck(x, y) : _toByte(borderColor);
96  
  }
97  
  
98  
  int getInt(int x, int y) {
99  
    ret ubyteToInt(getByte(x, y));
100  
  }
101  
102  
  public double averageBrightness() {
103  
    double sum = 0;
104  
    for (int y = 0; y < height; y++)
105  
      for (int x = 0; x < width; x++)
106  
        sum += getPixel(x, y);
107  
    return (sum/(double) (height*width));
108  
  }
109  
110  
  public float minimumBrightness() {
111  
    float min = 1;
112  
    for (int y = 0; y < height; y++)
113  
      for (int x = 0; x < width; x++)
114  
        min = Math.min(min, getPixel(x, y));
115  
    return min;
116  
  }
117  
118  
  public float maximumBrightness() {
119  
    float max = 0;
120  
    for (int y = 0; y < height; y++)
121  
      for (int x = 0; x < width; x++)
122  
        max = Math.max(max, getPixel(x, y));
123  
    return max;
124  
  }
125  
126  
  float getPixel(int x, int y) {
127  
    return inRange(x, y) ? _toFloat(getByte(x,y )) : borderColor;
128  
  }
129  
  
130  
  public float getFloatPixel(int x, int y) { ret getPixel(x, y); }
131  
  
132  
  float getPixel(Pt p) { ret getPixel(p.x, p.y); }
133  
134  
  static byte _toByte(float pixel) {
135  
    return (byte) (pixel*255f);
136  
  }
137  
138  
  static float _toFloat(byte pixel) {
139  
    return (((int) pixel) & 255)/255f;
140  
  }
141  
142  
  private boolean inRange(int x, int y) {
143  
    return x >= 0 && x < width && y >= 0 && y < height;
144  
  }
145  
146  
  public int getWidth() { return width; }
147  
  int w() { return width; }
148  
149  
  public int getHeight() { return height; }
150  
  int h() { return height; }
151  
152  
  public RGBImage toRGB() {
153  
    int[] rgbs = new int[width*height];
154  
    for (int y = 0; y < height; y++)
155  
      for (int x = 0; x < width; x++) {
156  
        int b = getByte(x, y) & 0xFF;
157  
        rgbs[y*width+x] = 0xFF000000 | b*0x010101;
158  
      }
159  
    return new RGBImage(width, height, rgbs);
160  
  }
161  
  
162  
  public RGBImage toRGB_slow() {
163  
    RGB[] rgbs = new RGB[width*height];
164  
    for (int y = 0; y < height; y++)
165  
      for (int x = 0; x < width; x++) {
166  
        float p = getPixel(x, y);
167  
        rgbs[y*width+x] = new RGB(p, p, p);
168  
      }
169  
    return new RGBImage(width, height, rgbs);
170  
  }
171  
172  
173  
  public BWImage clip(int x, int y, int w, int h) {
174  
    return clip(new Rectangle(x, y, w, h));
175  
  }
176  
177  
  private Rectangle fixClipRect(Rectangle r) {
178  
    return r.intersection(new Rectangle(0, 0, width, height));
179  
  }
180  
181  
  BWImage clip(Rect r) {
182  
    ret clip(r.getRectangle());
183  
  }
184  
  
185  
  /** this should be multithread-safe */
186  
  public BWImage clip(Rectangle r) {
187  
    r = fixClipRect(r);
188  
    byte[] newPixels = new byte[r.height*r.width];
189  
    for (int y = 0; y < r.height; y++)
190  
      for (int x = 0; x < r.width; x++)
191  
        newPixels[y*r.width+x] = getByte(r.x+x, r.y+y);
192  
    return new BWImage(r.width, r.height, newPixels);
193  
  }
194  
195  
  public void setPixel(int x, int y, float brightness) {
196  
    setByte(x, y, _toByte(fixPixel(brightness)));
197  
  }
198  
  
199  
  // i = 0 to 255
200  
  public void setInt(int x, int y, int i) {
201  
    setByte(x, y, (byte) limitToUByte(i));
202  
  }
203  
  
204  
  public void setByte(int x, int y, byte b) {
205  
    if (x >= 0 && x < width && y >= 0 && y < height)
206  
      pixels[y*width+x] = b;
207  
  }
208  
209  
  byte getByte_noRangeCheck(int x, int y) {
210  
    return pixels[y*width+x];
211  
  }
212  
213  
  public void setByte(int x, int y, int brightness) {
214  
    setByte(x, y, (byte) brightness);
215  
  }
216  
217  
  private float fixPixel(float pixel) {
218  
    return Math.max(0, Math.min(1, pixel));
219  
  }
220  
221  
  public float getBorderColor() {
222  
    return borderColor;
223  
  }
224  
225  
  public void setBorderColor(float borderColor) {
226  
    this.borderColor = borderColor;
227  
  }
228  
229  
  public boolean anyPixelBrighterThan(double threshold) {
230  
    for (int y = 0; y < height; y++)
231  
      for (int x = 0; x < width; x++)
232  
        if (getPixel(x, y) > threshold)
233  
          return true;
234  
    return false;
235  
  }
236  
  
237  
  public BufferedImage getBufferedImage() {
238  
    //ret toRGB().getBufferedImage();
239  
    
240  
    // TYPE_BYTE_GRAY is buggy - see #1015235
241  
    BufferedImage bufferedImage = new BufferedImage(width, height, /*BufferedImage.TYPE_BYTE_GRAY*/BufferedImage.TYPE_INT_RGB);
242  
    for (int y = 0; y < height; y++)
243  
      for (int x = 0; x < width; x++) {
244  
        int b = ((int) getByte(x, y) & 0xFF);
245  
        bufferedImage.setRGB(x, y, b*0x010101);
246  
      }
247  
    return bufferedImage; 
248  
  }
249  
  
250  
  byte[] getBytes() {
251  
    ret pixels;
252  
  }
253  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 20 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, ofpaelxlmzfo, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv

No comments. add comment

Snippet ID: #1004247
Snippet name: BWImage (8 bit)
Eternal ID of this version: #1004247/27
Text MD5: 1fc59f2e87a2955c455ea7e0fa48c344
Transpilation MD5: f6ce8864afd59e94e3e8b06850c7ad72
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2021-09-11 16:29:20
Source code size: 7216 bytes / 253 lines
Pitched / IR pitched: No / No
Views / Downloads: 788 / 4961
Version history: 26 change(s)
Referenced in: [show references]