No 16 bit data type?

edited November 2015 in Programming Questions

I am trying to use processing to read in an image with 24 bit color. Then I want to reduce the color to 16 bit, compatible with an AdaFruit Hx tft display. But I just noticed that there appears to be no 16-bit data type that I can use for my 16-bit pixel colors. I suppose I can poke the colors into 2 bytes, but that is a bit more messy.

Is there, by chance, an undocumented 16-bit type. I've tried a few possibilities, with no luck.

Thanks in advance for any advice.

Answers

  • edited October 2015
    • Java got 2 16-bit (2-byte) primitive datatypes: short & char.
    • short is signed while char in the only Java datatype which is unsigned by default.
    • For color values, unsigned is more adequate. So I'd choose char for it. B-)
      https://Processing.org/reference/char.html

    P.S.: Use map() in order to reduce default 0-255 8-bit range of a RGB attribute to 0-31 5-bit range:
    https://Processing.org/reference/map_.html

  • edited October 2015

    A better idea: Instead of map(), use >>> 3 for each RGB attribute. *-:)
    In this approach, the 3 least significant bits of each RGB are simply discarded. (:|

  • Ok, never thought of char as a 16-bit type, but this my first journey into Java. That will quickly solve my problem. This odd color scheme has 5 bits R, 6 bits G, 5 bits B. The idea of the extra bit for G appears to be that our eyes are more sensitive to green.

  • edited October 2015

    For R & B, use >> 3. For G, >> 2. :ar!

  • edited October 2015
    // forum.Processing.org/two/discussion/13254/no-16-bit-data-type
    // GoToLoop (2015-Oct-25)
    
    static final char argb32_to_rgb16(color c) {
      color r = c>>023 & 0x1f;
      color g = c>>012 & 0x3f;
      color b = c>>3 & 0x1f;
    
      return (char) (r<<11 | g<<5 | b);
    }
    
    static final color rgb16_to_argb32(char c) {
      color r = c>>010 | 7;
      color g = c>>3 & 0xfc | 3;
      color b = c<<3 & 0xf8 | 7;
    
      return r<<020 | g<<010 | b | #000000;
    }
    
    static final char from_rgb16_array(byte... arr) {
      return (char) (arr[0]<<11 | arr[1]<<5 | arr[2]);
    }
    
    static final byte[] to_rgb16_array(char c) {
      return to_rgb16_array(c, null);
    }
    
    static final byte[] to_rgb16_array(char c, byte... arr) {
      byte r = (byte) (c>>11);
      byte g = (byte) (c>>5 & 0x3f);
      byte b = (byte) (c & 0x1f);
    
      if (arr == null || arr.length < 3)  return new byte[] {
        r, g, b
      };
    
      arr[0] = r;
      arr[1] = g;
      arr[2] = b;
      return arr;
    }
    
    void setup() {
      final byte[] rgb16 = new byte[3];
    
      for (int i = 0; i++ != 5; println()) {
        color c = (color) random(#000000);
        char r = argb32_to_rgb16(c);
        println(hex(c, 6), hex(rgb16_to_argb32(r), 6), hex(r), binary(r));
    
        boolean g = true;
        for (byte b : to_rgb16_array(r, rgb16))
          println(nf(b, 2), hex(b), binary(b, (g ^= true)? 6 : 5));
      }
    
      exit();
    }
    
Sign In or Register to comment.