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

51
LINES

< > BotCompany Repo | #1035253 // PosterizeBufferedImage

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

Libraryless. Click here for Pure Java version (13454L/76K).

1  
// same brightness level count for each channel for now
2  
sclass PosterizeBufferedImage is PixelPreservingImageOpResult<BufferedImage> {
3  
  BufferedImage img;
4  
  BufferedImage result;
5  
  IPosterizer posterizer;
6  
  CachedPosterizer cachedPosterizer;
7  
  
8  
  *(int brightnessLevels, BufferedImage *img) {
9  
    if (brightnessLevels >= 256 || img == null)
10  
      posterizer = new DummyPosterizer;
11  
    posterizer = SinglePixelPosterizer(brightnessLevels);
12  
  }
13  
  
14  
  *(IPosterizer *posterizer, BufferedImage *img) {}
15  
  
16  
  run {
17  
    if (img == null) ret;
18  
    if (posterizer cast DummyPosterizer)
19  
      ret with result = img;
20  
    
21  
    GrabbableIntPixels gp = grabbableIntPixels_fastOrSlow(img);
22  
    int w = img.getWidth(), h = img.getHeight(), n = w*h;
23  
    int[] pixels = gp.data;
24  
    int[] pixels2 = new[n];
25  
    cachePosterizer();
26  
    var cachedPosterizer = this.cachedPosterizer;
27  
28  
    int iOut = 0, iIn = gp.offset;
29  
    for y to h: {
30  
      for x to w: {
31  
        int rgb = pixels[iIn+x];
32  
        pixels2[iOut++] = rgbInt(
33  
          cachedPosterizer.get(rgbRed(rgb)),
34  
          cachedPosterizer.get(rgbGreen(rgb)),
35  
          cachedPosterizer.get(rgbBlue(rgb)));
36  
      }
37  
      iIn += gp.scanlineStride;
38  
    }
39  
  
40  
    result = bufferedImageWithoutAlpha(w, h, pixels2);
41  
  }
42  
  
43  
  public BufferedImage pixelPreservingSrcImage() { ret img; }
44  
  
45  
  BufferedImage get() { if (result == null) run(); ret result; }
46  
  
47  
  private void cachePosterizer {
48  
    if (cachedPosterizer == null)
49  
      cachedPosterizer = main cachePosterizer(posterizer);
50  
  }
51  
}

Author comment

Began life as a copy of #1033935

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1035253
Snippet name: PosterizeBufferedImage
Eternal ID of this version: #1035253/11
Text MD5: 5ecd7852ce73f224903be420806d5328
Transpilation MD5: fef8498edafe81b6073850d34c81efa8
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-07-17 22:19:29
Source code size: 1567 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 163 / 279
Version history: 10 change(s)
Referenced in: [show references]