S
Steve Leach
I am writing an application where I look for a white pixel by
Use XOR rather than a comparison. Much faster (or at least it was back
in the days of the 386 and 486... I assume it still is).
If short int is 2 bytes on your hardware (check, but it probably is) do
the first XOR on a short int at RGB[0] and the second on a char at RGB[2].
This last part might not matter depending on how smart the compiler is
but if you are going sequentially through the entire array then dont use
an array+offset but rather set a pointer to the base before you enter
the loop and increment it.
char test[1024*768*3]; // Just guessing at an image size
unsigned short int * pointer;
unsigned char * limit;
pointer=&test;
limit=pointer+1024*768*3;
do {
if(!((unsigned short int )*pointer^0xffff)
&& !((unsigned char )*(pointer+2)^0xff))
printf("white\n");
pointer+=3;
} } while(pointer<limit);
Other than some warnings from the compiler about wrong pointer type, I
think this should work... check my logic on this as I'm more than a
little rusty to put it lightly.
Oh, and I don't know if this is still true or not with modern processors,
but you might see a benefit from always loading the word (16 bits) from
an even address... if you check the pointer first and then do (if
pointer is even) short int, char, char, short int for each loop (
checking two pixles for each loop) then you will always load a word from
an even address rather than odd. if the base pointer is odd, then start
with char. I don't even know if this still matters though It used to be
that the x86's would load a word from an odd base in two fetches and
from an even in one. Anyone know if this still applies to Pentium and
newer processors?
After all of this if things are STILL too slow, about your only other
option would be to reduce the number of loops by repeating the same
instructions several times in the loop... ie check pixel one, then two,
then three, then four, THEN loop. that way there are 1/4 as many jumps
back to the beginning of the loop.
............................................................
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)
Use XOR rather than a comparison. Much faster (or at least it was back
in the days of the 386 and 486... I assume it still is).
If short int is 2 bytes on your hardware (check, but it probably is) do
the first XOR on a short int at RGB[0] and the second on a char at RGB[2].
This last part might not matter depending on how smart the compiler is
but if you are going sequentially through the entire array then dont use
an array+offset but rather set a pointer to the base before you enter
the loop and increment it.
char test[1024*768*3]; // Just guessing at an image size
unsigned short int * pointer;
unsigned char * limit;
pointer=&test;
limit=pointer+1024*768*3;
do {
if(!((unsigned short int )*pointer^0xffff)
&& !((unsigned char )*(pointer+2)^0xff))
printf("white\n");
pointer+=3;
} } while(pointer<limit);
Other than some warnings from the compiler about wrong pointer type, I
think this should work... check my logic on this as I'm more than a
little rusty to put it lightly.
Oh, and I don't know if this is still true or not with modern processors,
but you might see a benefit from always loading the word (16 bits) from
an even address... if you check the pointer first and then do (if
pointer is even) short int, char, char, short int for each loop (
checking two pixles for each loop) then you will always load a word from
an even address rather than odd. if the base pointer is odd, then start
with char. I don't even know if this still matters though It used to be
that the x86's would load a word from an odd base in two fetches and
from an even in one. Anyone know if this still applies to Pentium and
newer processors?
After all of this if things are STILL too slow, about your only other
option would be to reduce the number of loops by repeating the same
instructions several times in the loop... ie check pixel one, then two,
then three, then four, THEN loop. that way there are 1/4 as many jumps
back to the beginning of the loop.
............................................................