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

131
LINES

< > BotCompany Repo | #1006576 // Image2B - image in 2 bit format (just black and white)

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

Transpiled version (10946L) is out of date.

1  
persistable sclass Image2B is IBinaryImage {
2  
  int w, h;
3  
  byte[] pixels;
4  
  
5  
  *(int *w, int *h, byte[] *pixels) { cleanPixelArray(); }
6  
  
7  
  *(Image2B img) {
8  
    w = img.getWidth();
9  
    h = img.getHeight();
10  
    pixels = cloneByteArray(img.pixels);
11  
  }
12  
  
13  
  *(RGBImage img) {
14  
    w = img.getWidth();
15  
    h = img.getHeight();
16  
    pixels = new byte[(w*h+7)/8];
17  
    for y to h: for x to w:
18  
      if (img.getPixel(x, y).getBrightness() >= 0.5f) {
19  
        int i = y*w+x;
20  
        pixels[i/8] |= 1 << (i & 7);
21  
      }
22  
  }
23  
  
24  
  *(BWImage img) {
25  
    this(img, 128);
26  
  }
27  
  
28  
  // >= threshold
29  
  *(BWImage img, int threshold) {
30  
    w = img.w(); h = img.h();
31  
    int n = w*h;
32  
    int nOut = (n+7)/8;
33  
    byte[] pixels = this.pixels = new byte[nOut];
34  
    byte[] bwPixels = img.pixels;
35  
    int iIn = 0;
36  
    
37  
    // do the bulk
38  
    for (int iOut = 0; iOut < nOut-1; iOut++) {
39  
      int value = 0;
40  
      for bit to 8: {
41  
        value >>= 1;
42  
        if (ubyteToInt(bwPixels[iIn++]) >= threshold)
43  
          value |= 0x80;
44  
      }
45  
      pixels[iOut] = (byte) value;
46  
    }
47  
    
48  
    // do last (up to 7) bits
49  
    for (; iIn < n; iIn++)
50  
      if (ubyteToInt(bwPixels[iIn]) >= threshold)
51  
        pixels[nOut-1] |= 1 << (iIn & 7);
52  
  }
53  
  
54  
  *(BufferedImage img) {
55  
    this(img, 128);
56  
  }
57  
  
58  
  *(BufferedImage img, int threshold) {
59  
    this(BWImage(img), threshold);
60  
  }
61  
  
62  
  // initializes with black
63  
  *(int *w, int *h) {
64  
    pixels = new byte[(w*h+7)/8];
65  
  }
66  
  
67  
  RGBImage toRGB() {
68  
    RGBImage img = new RGBImage(w, h, Color.black);
69  
    for y to h: for x to w: {
70  
      int i = y*w+x;
71  
      if ((pixels[i/8] & (1 << (i & 7))) != 0)
72  
        img.setPixel(x, y, Color.white);
73  
    }
74  
    ret img;
75  
  }
76  
  
77  
  BWImage toBW() {
78  
    BWImage img = new BWImage(w, h, 0f);
79  
    for y to h: for x to w: {
80  
      int i = y*w+x;
81  
      if ((pixels[i/8] & (1 << (i & 7))) != 0)
82  
        img.setPixel(x, y, 1f);
83  
    }
84  
    ret img;
85  
  }
86  
  
87  
  public BufferedImage getBufferedImage() { ret toBW().getBufferedImage(); }
88  
  
89  
  // x and y must be inside the image
90  
  public bool getPixel aka getBoolPixel(int x, int y) {
91  
    int i = y*w+x;
92  
    ret (pixels[i/8] & (1 << (i & 7))) != 0;
93  
  }
94  
  
95  
  // defaultColor is color outside of image
96  
  public bool getPixel aka getBoolPixel(int x, int y, bool defaultColor) {
97  
    if (x < 0 || y < 0 || x >= w || y >= h) ret defaultColor;
98  
    ret getPixel(x, y);
99  
  }
100  
  
101  
  public void setPixel(int x, int y, bool b) {
102  
    int i = y*w+x;
103  
    byte val = pixels[i/8], shifted = (byte) (1 << (i & 7));
104  
    val = (byte) (b ? val | shifted : val & ~shifted);
105  
    pixels[i/8] = val;
106  
  }
107  
  
108  
  void setPixel(int x, int y) {
109  
    int i = y*w+x;
110  
    pixels[i/8] |= 1 << (i & 7);
111  
  }
112  
  
113  
  public int getWidth() { ret w; }
114  
  public int getHeight() { ret h; }
115  
  
116  
  toString {
117  
    ret "Image2B " + str_px(w, h);
118  
  }
119  
  
120  
  // clear unused bits in pixel array after we received
121  
  // a possibly dirty array
122  
  void cleanPixelArray {
123  
    int n = w*h;
124  
    if ((n & 7) != 0)
125  
      pixels[n/8] &= (1 << (n & 7))-1;
126  
  }
127  
  
128  
  public double averageBrightness() {
129  
    ret doubleRatio(bitCount(pixels), w*h);
130  
  }
131  
}

Author comment

Began life as a copy of #1005378

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006576
Snippet name: Image2B - image in 2 bit format (just black and white)
Eternal ID of this version: #1006576/37
Text MD5: 2efb245e48159fe641a9b0d3fe99b439
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-08 23:05:10
Source code size: 3191 bytes / 131 lines
Pitched / IR pitched: No / No
Views / Downloads: 598 / 1744
Version history: 36 change(s)
Referenced in: [show references]