PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

Extract RGB from buf.puixels

How can i extract the blue value for example from the RGB of a specific pixel.

//After fetching the pixel
uint16_t p = lockInfo.pixels[y * nPitch + x];

// What should i do here?

Thanks Maoz.
when you have the 16 bit pixel value,
as in your code, you probably have a 565 format meaning 5 bits red, 6 bits green, 5 bits blue,
so do a bitwise 'and = &' operation on the pixel to remove the R & G component

eg: uint16_t pix = buffer[...];
int blueval = pix & 0x1F; // 0x1F = binary 11111
There are #define's already set in pocket frog in defs.h

Code:
const Pixel RED_MASK = 0x1F << 11;
const Pixel GREEN_MASK = 0x3F << 5;
const Pixel BLUE_MASK = 0x1F;
const int RED_SHIFT = 11;
const int GREEN_SHIFT = 5;
const int BLUE_SHIFT = 0;
const int RED_BITS = 5;
const int GREEN_BITS = 6;
const int BLUE_BITS = 5;


So, for your case, you can use:
int blueval = pix & BLUE_MASK;

But for red and green, you'd probably want to do the shift as well...

int redval = (pix & RED_MASK) >> RED_SHIFT;
int greenval = (pix & GREEN_MASK) >> GREEN_SHIFT;