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

84
LINES

< > BotCompany Repo | #1035876 // PosterizeBufferedImageToHi15

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

Libraryless. Click here for Pure Java version (11094L/63K).

1  
// same brightness level count for each channel for now
2  
sclass PosterizeBufferedImageToHi15 {
3  
  BufferedImage img;
4  
  Hi15Image result;
5  
  IPosterizer posterizer;
6  
7  
  *(int brightnessLevels, BufferedImage *img) {
8  
    if (brightnessLevels >= 256 || img == null)
9  
      posterizer = new DummyPosterizer;
10  
    posterizer = SinglePixelPosterizer(brightnessLevels);
11  
  }
12  
  
13  
  *(IPosterizer *posterizer, BufferedImage *img) {}
14  
  
15  
  run {
16  
    if (img == null) ret;
17  
    if (posterizer cast DummyPosterizer)
18  
      ret with result = new Hi15Image(img);
19  
    
20  
    GrabbableRGBBytePixels gp = grabbableRGBBytePixels(img);
21  
    if (gp != null)
22  
      ret with run(gp);
23  
      
24  
    GrabbableIntPixels gp2 = grabbableIntPixels_fastOrSlow(img);
25  
    run(gp2);
26  
  }
27  
  
28  
  void run(GrabbableIntPixels gp) {
29  
    int w = img.getWidth(), h = img.getHeight(), n = w*h;
30  
    int[] pixels = gp.data;
31  
    short[] pixels2 = new[n];
32  
    byte[] cache = cachePosterizer();
33  
    int extraStride = gp.scanlineStride-w;
34  
35  
    int iOut = 0, iIn = gp.offset;
36  
    for y to h: {
37  
      for x to w: {
38  
        int rgb = pixels[iIn++];
39  
        pixels2[iOut++] = (short)
40  
          ((cache[(rgb >> 16) & 0xFF] << 10) |
41  
          (cache[(rgb >> 8) & 0xFF] << 5) |
42  
          cache[rgb & 0xFF]);
43  
      }
44  
      iIn += extraStride;
45  
    }
46  
  
47  
    result = new Hi15Image(w, h, pixels2);
48  
  }
49  
  
50  
  void run(GrabbableRGBBytePixels gp) {
51  
    int w = img.getWidth(), h = img.getHeight(), n = w*h;
52  
    byte[] pixels = gp.data;
53  
    short[] pixels2 = new[n];
54  
    byte[] cache = cachePosterizer();
55  
    int extraStride = gp.scanlineStride-w*3;
56  
57  
    int iOut = 0, iIn = gp.offset;
58  
    for y to h: {
59  
      for x to w: {
60  
        int r = pixels[iIn++] & 0xFF;
61  
        int g = pixels[iIn++] & 0xFF;
62  
        int b = pixels[iIn++] & 0xFF;
63  
        pixels2[iOut++] = (short)
64  
          ((cache[r] << 10) |
65  
          (cache[g] << 5) |
66  
          cache[b]);
67  
      }
68  
      iIn += extraStride;
69  
    }
70  
  
71  
    result = new Hi15Image(w, h, pixels2);
72  
  }
73  
  
74  
  public BufferedImage pixelPreservingSrcImage() { ret img; }
75  
  
76  
  Hi15Image get() { if (result == null) run(); ret result; }
77  
  
78  
  byte[] cachePosterizer() {
79  
    byte[] cache = new[256];
80  
    for i to 256:
81  
      cache[i] = (byte) (posterizer.get(i) >> 3);
82  
    ret cache;
83  
  }
84  
}

Author comment

Began life as a copy of #1035253

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): elmgxqgtpvxh, mqqgnosmbjvj, wnsclhtenguj

No comments. add comment

Snippet ID: #1035876
Snippet name: PosterizeBufferedImageToHi15
Eternal ID of this version: #1035876/14
Text MD5: 24dd85a01860394288a8f68430947ca4
Transpilation MD5: a79818a0f775bf5c398e2ceabe5fab05
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-08 19:12:33
Source code size: 2320 bytes / 84 lines
Pitched / IR pitched: No / No
Views / Downloads: 193 / 294
Version history: 13 change(s)
Referenced in: [show references]