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

62
LINES

< > BotCompany Repo | #1035585 // Hi15Image - HiColor image (15 bit / 32,768 colors)

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

Transpiled version (10459L) is out of date.

1  
sclass Hi15Image extends WAndHImpl is MakesBufferedImage {
2  
  short[] pixels;
3  
  
4  
  *() {} // for persistence
5  
  *(int *w, int *h, short[] *pixels) {}
6  
  
7  
  // TODO: optimize!
8  
  *(BufferedImage img) { this(RGBImage(img)); }
9  
  *(RGBImage img) {
10  
    w = img.getWidth();
11  
    h = img.getHeight();
12  
    pixels = new short[w*h];
13  
    for y to h: for x to w:
14  
      pixels[y*w+x] = rgbIntToHi15(img.getInt(x, y));
15  
  }
16  
  
17  
  *(MakesBufferedImage img) { this(toBufferedImage(img)); }
18  
  
19  
  RGBImage toRGB() {
20  
    RGBImage img = new RGBImage(w, h);
21  
    int i = 0, w = this.w, h = this.h;
22  
    for y to h: for x to w:
23  
      img.setPixel(x, y, hi15ToRGBInt(pixels[i++]));
24  
    ret img;
25  
  }
26  
  
27  
  public BufferedImage getBufferedImage() {
28  
    ret toRGB().getBufferedImage();
29  
  }
30  
  
31  
  // 0 to 32767
32  
  short getHi15Pixel aka getHi15Color(int x, int y) {
33  
    ret pixels[y*w+x];
34  
  }
35  
  
36  
  // idx = index in pixel array
37  
  public short getHi15Pixel_noRangeCheck(int idx) {
38  
    ret pixels[idx];
39  
  }
40  
  
41  
  static int perChannelBitCountMask(int bitCount) {
42  
    int mask = ((~0x1F) >> bitCount) & 0x1F;
43  
    int mask2 = (mask << 10) | (mask << 5) | mask;
44  
    ret mask2;
45  
  }
46  
  
47  
  void reducePerChannelBitCount(int bitCount) {
48  
    if (bitCount >= 5) ret;
49  
    int mask = perChannelBitCountMask(bitCount);
50  
    short[] pixels = this.pixels;
51  
    for (int i = 0; i < pixels.length; i++)
52  
      pixels[i] = (short) (pixels[i] & mask);
53  
  }
54  
  
55  
  void reduceColorCount(int numColors) {
56  
    assertPowerOfTwo(numColors);
57  
    int bits = dualLog(numColors);
58  
    assertDivisibleBy(3, bits);
59  
    int bitsPerChannel = bits/3;
60  
    reducePerChannelBitCount(bitsPerChannel);
61  
  }
62  
}

Author comment

Began life as a copy of #1005378

download  show line numbers  debug dex  old transpilations   

Travelled to 2 computer(s): elmgxqgtpvxh, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035585
Snippet name: Hi15Image - HiColor image (15 bit / 32,768 colors)
Eternal ID of this version: #1035585/19
Text MD5: 6dba2c9db048e8ce88481e122f26e462
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-08-08 15:51:24
Source code size: 1681 bytes / 62 lines
Pitched / IR pitched: No / No
Views / Downloads: 216 / 344
Version history: 18 change(s)
Referenced in: [show references]