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

133
LINES

< > BotCompany Repo | #1036036 // Image2BAsInts - image in 2 bit format (just black and white) using int[] instead of byte[] (way faster)

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

Transpiled version (11251L) is out of date.

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

Author comment

Began life as a copy of #1006576

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1036036
Snippet name: Image2BAsInts - image in 2 bit format (just black and white) using int[] instead of byte[] (way faster)
Eternal ID of this version: #1036036/5
Text MD5: aa1f00577078294c0ac6749b1dedeb6f
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-09-03 01:23:10
Source code size: 3275 bytes / 133 lines
Pitched / IR pitched: No / No
Views / Downloads: 185 / 236
Version history: 4 change(s)
Referenced in: [show references]