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

85
LINES

< > BotCompany Repo | #1035219 // FastDownscale Include (old/slower, use v2) - downscales each scaleX*scaleY block of a color BufferedImage to one pixel (with scaleX and scaleY being powers of two)

JavaX fragment (include)

  // Need external definition of: scaleX, scaleY
  
  // We compress horizontally first, then vertically
  // which I think may be the fastest way.
  //
  // Up to scaleX-1 pixel columns and scaleY pixel rows may be discarded
  // (those not fitting in a scaleX*scaleY block). The discarded pixels are
  // along the right and bottom border of the image, respectively.

  int w, h, w2, h2;
  int[] horizontallyScaledPixels, finalPixels;
  
  void scaleHorizontally(BufferedImage img) {
    scaleHorizontally(grabbableIntPixels(img));
  }
    
  void scaleHorizontally(GrabbableIntPixels gp) {
    w = gp.w;
    int h = this.h = gp.h;
    int w2 = this.w2 = w/scaleX, h2 = this.h2 = h/scaleY;
    
    int[] newPixels = horizontallyScaledPixels = new int[w2*h];
  
    int iRow = gp.offset, iOut = 0;
    int[] pixels = gp.data;
    
    for (int y = 0; y < h; y++) {
      int iIn = iRow;
    
      for x to w2: {
        int r = 0, g = 0, b = 0;
    
        for subX to scaleX: {
          int rgb = pixels[iIn+subX];
          r += (rgb >> 16) & 0xFF;
          g += (rgb >> 8) & 0xFF;
          b += rgb & 0xFF;
        }
    
        iIn += scaleX;
        newPixels[iOut++] = rgbIntFullAlpha(r/scaleX, g/scaleX, b/scaleX);
      }
    
      iRow += gp.scanlineStride;
    } // for y
  }
  
  void scaleVertically {
    int[] pixels = horizontallyScaledPixels;
    int[] newPixels = finalPixels = new int[w2*h2];
  
    int iRow = 0, iOut = 0;
    
    for (int y = 0; y < h2; y++) {
      int iIn = iRow;
      
      for x to w2: {
        int r = 0, g = 0, b = 0;
        int iIn2 = iIn;
        
        for subY to scaleY: {
          int rgb = pixels[iIn2];
          iIn2 += w2;
          r += (rgb >> 16) & 0xFF;
          g += (rgb >> 8) & 0xFF;
          b += rgb & 0xFF;
        }
        
        iIn++;
        newPixels[iOut++] = rgbIntFullAlpha(r/scaleX, g/scaleX, b/scaleX);
      }
      
      iRow += w2*scaleY;
    } // for y
  }
  
  BufferedImage get(BufferedImage img) {
    scaleHorizontally(img);
    scaleVertically();
    ret get();
  }
  
  BufferedImage get() {
    ret bufferedImage(w2, h2, finalPixels);
  }

Author comment

Began life as a copy of #1035216

download  show line numbers  debug dex  old transpilations   

Travelled to 3 computer(s): bhatertpkbcr, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035219
Snippet name: FastDownscale Include (old/slower, use v2) - downscales each scaleX*scaleY block of a color BufferedImage to one pixel (with scaleX and scaleY being powers of two)
Eternal ID of this version: #1035219/3
Text MD5: 90c26491e3eb6e625af30dbb93cfcf9c
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-04-15 18:52:28
Source code size: 2229 bytes / 85 lines
Pitched / IR pitched: No / No
Views / Downloads: 114 / 135
Version history: 2 change(s)
Referenced in: #1035221 - FastDownscale Include v2 - do it all in one conversion. faster!