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

75
LINES

< > BotCompany Repo | #1032487 // FloatBWImage - grayscale image where each pixel is a real float (allowing bigger range, precision, infinity etc)

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

Libraryless. Click here for Pure Java version (10849L/62K).

1  
// pixels may contain any float number (e.g. for a distance map)
2  
static final class FloatBWImage is IBWImage {
3  
  int width, height;
4  
  float[] pixels; // 0 to 1 usually
5  
6  
  // color returned when getPixel is called with a position outside the actual image
7  
  float borderColor = 0.0f;
8  
  
9  
  // for unstructure()
10  
  *() {}
11  
12  
  // BLACK!
13  
  *(int *width, int *height) {
14  
    pixels = new float[width*height];
15  
  }
16  
17  
  *(int *width, int *height, float brightness) {
18  
    pixels = new float[width*height];
19  
    fillArrayUnlessZero(pixels, brightness);
20  
  }
21  
  
22  
  *(int *width, int *height, float[] *pixels) {}
23  
24  
  public float getPixel aka getFloatPixel(int x, int y) {
25  
    ret inRange(x, y) ? getPixel_noRangeCheck(x, y) : borderColor;
26  
  }
27  
  
28  
  public float getFloatPixel(int index) {
29  
    ret pixels[index];
30  
  }
31  
  
32  
  float getPixel(Pt p) { ret getPixel(p.x, p.y); }
33  
34  
  private boolean inRange(int x, int y) {
35  
    return x >= 0 && x < width && y >= 0 && y < height;
36  
  }
37  
38  
  public int getWidth() { return width; }
39  
  public int getHeight() { return height; }
40  
41  
  public void setPixel(int x, int y, float brightness) {
42  
    pixels[y*width+x] = brightness;
43  
  }
44  
  
45  
  float getPixel_noRangeCheck(int x, int y) {
46  
    return pixels[y*width+x];
47  
  }
48  
  
49  
  public byte getByte(int x, int y) {
50  
    return inRange(x, y) ? getByte_noRangeCheck(x, y) : _toByte(borderColor);
51  
  }
52  
  
53  
  byte getByte_noRangeCheck(int x, int y) {
54  
    ret _toByte(pixels[y*width+x]);
55  
  }
56  
  
57  
  static byte _toByte(float pixel) {
58  
    return (byte) (Math.max(0, Math.min(1, pixel))*255f);
59  
  }
60  
61  
  // TODO: optimize
62  
  public BufferedImage getBufferedImage() {
63  
    BufferedImage img = bufferedImage(width, height);
64  
    for (int y = 0; y < height; y++)
65  
      for (int x = 0; x < width; x++) {
66  
        int b = ((int) getByte(x, y) & 0xFF);
67  
        img.setRGB(x, y, b*0x010101);
68  
      }
69  
    ret img; 
70  
  }
71  
  
72  
  public double averageBrightness() {
73  
    ret floatSumAsDouble(pixels)/(width*height);
74  
  }
75  
}

Author comment

Began life as a copy of #1004247

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1032487
Snippet name: FloatBWImage - grayscale image where each pixel is a real float (allowing bigger range, precision, infinity etc)
Eternal ID of this version: #1032487/12
Text MD5: 284166705098acf2115e1e82eaa580ae
Transpilation MD5: 7ae03e83dc0315035dc22c0ea33f196e
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-05 18:59:32
Source code size: 2006 bytes / 75 lines
Pitched / IR pitched: No / No
Views / Downloads: 155 / 350
Version history: 11 change(s)
Referenced in: [show references]