I am writing an application where I look for a white pixel by testing
if all the R,G,B values are 255 i.e. I use
if(RGB[0] == 255 && RGB[1] == 255 && RGB[2] == 255) (assuming RGB is a
pointer to unsigned char)
This statement gets executed for all the pixels in a page, so if I can
find a better way to do this, I could potentially save a lot of cpu
cycles.
I'm going to be flamed to death for posting non-portable code to comp.lang.c,
but here's something you could try. You may need to adjust it slightly for
better performance if your target architecture doesn't use 4-byte words.
Have fun.
#include <stdint.h> /* any C99-compliant compiler should provide this */
/* define BYTE_ORDER_VAX for little endian, BYTE_ORDER_NETWORK for big endian */
#define BYTE_ORDER_VAX 1
/*
* find_white --
*
* returns the index of the first white pixel in 'data', an array of
* packed 'npixels' pixels, or -1 if we couldn't find one.
*
* a 'pixel' is a group of three bytes; it's white when all three bytes are
* set to 0xff.
*/
int
find_white(const uint8_t *data, int npixels)
{
const uint32_t *p = (uint32_t *)data;
const uint8_t *q;
int n = (3*npixels) / 12; /* # of groups of 4 pixels (3 words) */
int r = npixels - n*4; /* # of remaining pixels */
int i;
for (i = 0; i < n; i++) {
#if BYTE_ORDER_NETWORK /* UNTESTED */
if ((p[0] & 0xffffff00) == 0xffffff00)
return i*4;
if (((p[0] & 0x000000ff) == 0x000000ff) &&
(p[1] & 0xffff0000) == 0xffff0000)
return i*4 + 1;
if (((p[1] & 0x0000ffff) == 0x0000ffff) &&
(p[2] & 0xff000000) == 0xff000000)
return i*4 + 2;
if ((p[3] & 0x0000ffff) == 0x0000ffff)
return i*4 + 3;
#elif BYTE_ORDER_VAX
if ((p[0] & 0x00ffffff) == 0x00ffffff)
return i*4;
if (((p[0] & 0xff000000) == 0xff000000) &&
(p[1] & 0x0000ffff) == 0x0000ffff)
return i*4 + 1;
if (((p[1] & 0xffff0000) == 0xffff0000) &&
(p[2] & 0x000000ff) == 0x000000ff)
return i*4 + 2;
if ((p[3] & 0xffff0000) == 0xffff0000)
return i*4 + 3;
#else
#error "define either BYTE_ORDER_NETWORK or BYTE_ORDER_VAX"
#endif
p += 3;
}
q = (uint8_t *)p;
for (i = 0; i < r; i++) {
if (q[0] == 0xff && q[1] == 0xff && q[2] == 0xff)
return n*4 + i;
q += 3;
}
return -1;
}