We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
16 bit color to rgb conversion (Read 902 times)
16 bit color to rgb conversion
Oct 30th, 2005, 10:44am
 
hi,

after reading about the performance issues with processings video library (well, quicktime for java seems to be blamed for that) i decided to write a my own motiondetector using jmf.
which works great with 24bit rgb data (where each color has its own byte).
i'm having big trouble trying to convert 16 bit color data to 3 individual rgb-channels (and back) can anyone point me to an example or hint how to do that?

this is what i came up with so far ...
Code:
  
int r,g,b,c;
c |= inData[i] & 0xFF - Byte.MIN_VALUE;
c <<= 8;
c |= inData[i - 1] & 0xFF - Byte.MIN_VALUE;

r = (c & 0xF800) >> 11;
g = (c & 0x07E0) >> 5;
b = c & 0x1F;

is this correct or am i doing something wrong when trying to build the simple greyscale value and inserting it back into the buffer?

Code:

int bw;
bw = (int) ((r + g + b) / (double) 3.0);
outData[i - 1] = (byte) (bw & 0xFF + Byte.MIN_VALUE);
bw >>= 8;
outData[i] = (byte) (bw & 0xFF + Byte.MIN_VALUE);


thanks a lot!
horst
Re: 16 bit color to rgb conversion
Reply #1 - Oct 31st, 2005, 11:36pm
 
you'll need to put the grayscale bytes back the same way that you've unpacked them (assuming that unpacking is working for you).

grayscale just means that r = g = b, so set them to something that's equal and pack things back up and you should be set.
Re: 16 bit color to rgb conversion
Reply #2 - Nov 1st, 2005, 10:17am
 
after many hours staring at the binary code representation of these color values, i found a solution that works

Code:

// 1. convert your byte data to an int
byte[] data; // some byte array e.g. a JMF buffer

// endianness determines which one is the high or low byte
// little endian here:
int highbyte = data [x];
int lowbyte = data [x+1];
int rgb16 = ((highbyte & 0xff) << 8) | (lowbyte & 0xff);

// 2. split that int into 3 ints representing 3 color channels

int red = rgb16 >> 7 & 0xff;
int green = rgb16 >> 2 & 0xff;
int blue = rgb16 << 3 & 0xff;

// 3. merge these 3 channels back into one int
int result = (red >> 3) << 10 | (green >> 3) << 5 | blue >> 3;

// 4. convert the resulting int back to two bytes
byte highbyte = (byte) (number & 0xFF);
byte lowbyte = (byte) (number >> 8 & 0xFF);


this was quite tricky to figure out for me, so maybe this saves someone from a headache or two Wink
Page Index Toggle Pages: 1