Bluebox - Chromakey etc
-
Hello I'm looking for a fast way to scan a buffer and check if a pixel color is between a color range, or not. Maybe some code explains it better: I have a buffer with 32Bits per Pixel (RGBA => R=8Bit, G=8Bit, B=8Bit, A=8Bit => 32Bit). This buffer is... let's say 200x200 pixels.
// Defining color ranges
char redUpper = 100;
char redLower = 50;
char greenUpper = 150;
char greenLower = 130;
char blueUpper = 20;
char blueLower = 10;//creating my pixel buffer
int* pBuffer = new int[200*200];
fillBufferWithData(pBuffer, 200*200); //some magic function the gets the image data into the buffer.//analysing
char* pCursor = (char*)pBuffer;
for (int i = 0; i < 200*200; i++)
{
//Every Pixel that is between the color range...
if ((pCursor[0] > redLower) && (pCursor[0] < redUpper)
&& (pCursor[1] > greenLower) && (pCursor[1] < greenUpper)
&& (pCursor[2] > blueLower) && (pCursor[2] < blueUpper))
{
pCursor[3] = 0;
}
else
{
pCursor[3] = 255
}pCursor += 4;
}
delete [] pBuffer;Okay. I hope that helps to understand what i want to do. I know thats not the fastest way (at least I hope so). Maybe someone knows a better way, perhaps with bit-shifting, or bit-masking? Thanks in advance, SnowProg
-
Hello I'm looking for a fast way to scan a buffer and check if a pixel color is between a color range, or not. Maybe some code explains it better: I have a buffer with 32Bits per Pixel (RGBA => R=8Bit, G=8Bit, B=8Bit, A=8Bit => 32Bit). This buffer is... let's say 200x200 pixels.
// Defining color ranges
char redUpper = 100;
char redLower = 50;
char greenUpper = 150;
char greenLower = 130;
char blueUpper = 20;
char blueLower = 10;//creating my pixel buffer
int* pBuffer = new int[200*200];
fillBufferWithData(pBuffer, 200*200); //some magic function the gets the image data into the buffer.//analysing
char* pCursor = (char*)pBuffer;
for (int i = 0; i < 200*200; i++)
{
//Every Pixel that is between the color range...
if ((pCursor[0] > redLower) && (pCursor[0] < redUpper)
&& (pCursor[1] > greenLower) && (pCursor[1] < greenUpper)
&& (pCursor[2] > blueLower) && (pCursor[2] < blueUpper))
{
pCursor[3] = 0;
}
else
{
pCursor[3] = 255
}pCursor += 4;
}
delete [] pBuffer;Okay. I hope that helps to understand what i want to do. I know thats not the fastest way (at least I hope so). Maybe someone knows a better way, perhaps with bit-shifting, or bit-masking? Thanks in advance, SnowProg
Due to short-circuit of the boolean operations inside the
if
statement, the code is reasonably fast. Maybe using three look-up tables will improve the code speed, but I'm not an expert. :)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles]