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)

1  
  // Need external definition of: scaleX, scaleY
2  
  
3  
  // We compress horizontally first, then vertically
4  
  // which I think may be the fastest way.
5  
  //
6  
  // Up to scaleX-1 pixel columns and scaleY pixel rows may be discarded
7  
  // (those not fitting in a scaleX*scaleY block). The discarded pixels are
8  
  // along the right and bottom border of the image, respectively.
9  
10  
  int w, h, w2, h2;
11  
  int[] horizontallyScaledPixels, finalPixels;
12  
  
13  
  void scaleHorizontally(BufferedImage img) {
14  
    scaleHorizontally(grabbableIntPixels(img));
15  
  }
16  
    
17  
  void scaleHorizontally(GrabbableIntPixels gp) {
18  
    w = gp.w;
19  
    int h = this.h = gp.h;
20  
    int w2 = this.w2 = w/scaleX, h2 = this.h2 = h/scaleY;
21  
    
22  
    int[] newPixels = horizontallyScaledPixels = new int[w2*h];
23  
  
24  
    int iRow = gp.offset, iOut = 0;
25  
    int[] pixels = gp.data;
26  
    
27  
    for (int y = 0; y < h; y++) {
28  
      int iIn = iRow;
29  
    
30  
      for x to w2: {
31  
        int r = 0, g = 0, b = 0;
32  
    
33  
        for subX to scaleX: {
34  
          int rgb = pixels[iIn+subX];
35  
          r += (rgb >> 16) & 0xFF;
36  
          g += (rgb >> 8) & 0xFF;
37  
          b += rgb & 0xFF;
38  
        }
39  
    
40  
        iIn += scaleX;
41  
        newPixels[iOut++] = rgbIntFullAlpha(r/scaleX, g/scaleX, b/scaleX);
42  
      }
43  
    
44  
      iRow += gp.scanlineStride;
45  
    } // for y
46  
  }
47  
  
48  
  void scaleVertically {
49  
    int[] pixels = horizontallyScaledPixels;
50  
    int[] newPixels = finalPixels = new int[w2*h2];
51  
  
52  
    int iRow = 0, iOut = 0;
53  
    
54  
    for (int y = 0; y < h2; y++) {
55  
      int iIn = iRow;
56  
      
57  
      for x to w2: {
58  
        int r = 0, g = 0, b = 0;
59  
        int iIn2 = iIn;
60  
        
61  
        for subY to scaleY: {
62  
          int rgb = pixels[iIn2];
63  
          iIn2 += w2;
64  
          r += (rgb >> 16) & 0xFF;
65  
          g += (rgb >> 8) & 0xFF;
66  
          b += rgb & 0xFF;
67  
        }
68  
        
69  
        iIn++;
70  
        newPixels[iOut++] = rgbIntFullAlpha(r/scaleX, g/scaleX, b/scaleX);
71  
      }
72  
      
73  
      iRow += w2*scaleY;
74  
    } // for y
75  
  }
76  
  
77  
  BufferedImage get(BufferedImage img) {
78  
    scaleHorizontally(img);
79  
    scaleVertically();
80  
    ret get();
81  
  }
82  
  
83  
  BufferedImage get() {
84  
    ret bufferedImage(w2, h2, finalPixels);
85  
  }

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: 115 / 136
Version history: 2 change(s)
Referenced in: [show references]