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).

// same brightness level count for each channel for now
sclass PosterizeBufferedImage is PixelPreservingImageOpResult<BufferedImage> {
  BufferedImage img;
  BufferedImage result;
  IPosterizer posterizer;
  CachedPosterizer cachedPosterizer;
  
  *(int brightnessLevels, BufferedImage *img) {
    if (brightnessLevels >= 256 || img == null)
      posterizer = new DummyPosterizer;
    posterizer = SinglePixelPosterizer(brightnessLevels);
  }
  
  *(IPosterizer *posterizer, BufferedImage *img) {}
  
  run {
    if (img == null) ret;
    if (posterizer cast DummyPosterizer)
      ret with result = img;
    
    GrabbableIntPixels gp = grabbableIntPixels_fastOrSlow(img);
    int w = img.getWidth(), h = img.getHeight(), n = w*h;
    int[] pixels = gp.data;
    int[] pixels2 = new[n];
    cachePosterizer();
    var cachedPosterizer = this.cachedPosterizer;

    int iOut = 0, iIn = gp.offset;
    for y to h: {
      for x to w: {
        int rgb = pixels[iIn+x];
        pixels2[iOut++] = rgbInt(
          cachedPosterizer.get(rgbRed(rgb)),
          cachedPosterizer.get(rgbGreen(rgb)),
          cachedPosterizer.get(rgbBlue(rgb)));
      }
      iIn += gp.scanlineStride;
    }
  
    result = bufferedImageWithoutAlpha(w, h, pixels2);
  }
  
  public BufferedImage pixelPreservingSrcImage() { ret img; }
  
  BufferedImage get() { if (result == null) run(); ret result; }
  
  private void cachePosterizer {
    if (cachedPosterizer == null)
      cachedPosterizer = main cachePosterizer(posterizer);
  }
}

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: 162 / 277
Version history: 10 change(s)
Referenced in: #1003674 - Standard Classes + Interfaces (LIVE continued in #1034167)
#1035876 - PosterizeBufferedImageToHi15