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

87
LINES

< > BotCompany Repo | #1035218 // FastDownscale8x8 (working backup) - downscales each 8x8 block of a color BufferedImage to one pixel

JavaX fragment (include)

1  
// We compress horizontally first, then vertically
2  
// which I think may be the fastest way.
3  
//
4  
// Up to 7 pixel columns and 7 pixel rows may be discarded
5  
// (those not fitting in an 8x8 block). The discarded pixels are
6  
// along the right and bottom border of the image, respectively.
7  
8  
sclass FastDownscale8x8 {
9  
  int w, h, w2, h2;
10  
  int[] horizontallyScaledPixels, finalPixels;
11  
  
12  
  void scaleHorizontally(BufferedImage img) {
13  
    scaleHorizontally(grabbableIntPixels(img));
14  
  }
15  
    
16  
  void scaleHorizontally(GrabbableIntPixels gp) {
17  
    int scaleX = 8, scaleY = scaleX;
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 8: {
34  
          int rgb = pixels[iIn+subX];
35  
          r += (rgb >> 16) & 0xFF;
36  
          g += (rgb >> 8) & 0xFF;
37  
          b += rgb & 0xFF;
38  
        }
39  
    
40  
        iIn += 8;
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 scaleX = 8, scaleY = scaleX;
50  
    int[] pixels = horizontallyScaledPixels;
51  
    int[] newPixels = finalPixels = new int[w2*h2];
52  
  
53  
    int iRow = 0, iOut = 0;
54  
    
55  
    for (int y = 0; y < h2; y++) {
56  
      int iIn = iRow;
57  
      
58  
      for x to w2: {
59  
        int r = 0, g = 0, b = 0;
60  
        int iIn2 = iIn;
61  
        
62  
        for subY to 8: {
63  
          int rgb = pixels[iIn2];
64  
          iIn2 += w2;
65  
          r += (rgb >> 16) & 0xFF;
66  
          g += (rgb >> 8) & 0xFF;
67  
          b += rgb & 0xFF;
68  
        }
69  
        
70  
        iIn++;
71  
        newPixels[iOut++] = rgbIntFullAlpha(r/scaleX, g/scaleX, b/scaleX);
72  
      }
73  
      
74  
      iRow += w2*scaleY;
75  
    } // for y
76  
  }
77  
  
78  
  BufferedImage get(BufferedImage img) {
79  
    scaleHorizontally(img);
80  
    scaleVertically();
81  
    ret get();
82  
  }
83  
  
84  
  BufferedImage get() {
85  
    ret bufferedImage(w2, h2, finalPixels);
86  
  }
87  
}

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: #1035218
Snippet name: FastDownscale8x8 (working backup) - downscales each 8x8 block of a color BufferedImage to one pixel
Eternal ID of this version: #1035218/1
Text MD5: 7f49a33074b81b2abcbb9d9a3ec1bb50
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:14:52
Source code size: 2233 bytes / 87 lines
Pitched / IR pitched: No / No
Views / Downloads: 143 / 157
Referenced in: [show references]