Board index » cppbuilder » Fastest way to retrieve RGB values from a pixel.

Fastest way to retrieve RGB values from a pixel.


2004-01-22 04:24:07 AM
cppbuilder11
Hi,
I was using ScanLine to get these values. Something like this:
Byte *ptr
ptr = (Byte *)pBitmap->ScanLine[y];
// RGB values are stored here:
ptr[3*x+0]
ptr[3*x+1]
ptr[3*x+2]
However, I need something faster. I've read that you can use DIB to do that.
Would it be faster? Anyone has any example to do the same using DIBs?
Thanks
 
 

Re:Fastest way to retrieve RGB values from a pixel.

Star wrote:
Quote
// RGB values are stored here:
ptr[3*x+0]
ptr[3*x+1]
ptr[3*x+2]
It is a pitty that you did not show the whole x loop.
What we see here are three multiplications. Multiplications take
much longer than additions.
You could begin with bringing back the multiplications to one.
But yet better get rid of all multiplications.
int X = 3*x-1;
now the RGB values are stored in:
ptr[++X]
ptr[++X]
ptr[++X]
Hans.
 

Re:Fastest way to retrieve RGB values from a pixel.

Quote
What we see here are three multiplications. Multiplications take
much longer than additions.
Hans, you're right. I should change that.
Here is the whole iteration (without your changes):
This code processes the whole image.
for (int y = 0; y < pBitmap->Height; y++)
{
ptr = (Byte *)pBitmap->ScanLine[y];
x=0;
while ( x< pBitmap->Width )
{
nRGB1 = ptr[3*x+0] ;
nRGB2 = ptr[3*x+1] ;
nRGB3 = ptr[3*x+2] ;
x++;
}
}
What I would like to know if using DIBs would be faster than this method.
Thanks
 

{smallsort}

Re:Fastest way to retrieve RGB values from a pixel.

Star wrote:
Quote
What I would like to know if using DIBs would be faster than this method.
Sorry, I have no experience with doing that with DIBs.
Hans.
 

Re:Fastest way to retrieve RGB values from a pixel.

Quote
Sorry, I have no experience with doing that with DIBs.
It's alright, Hans. Thanks for all your help.
 

Re:Fastest way to retrieve RGB values from a pixel.

Star wrote:
Quote
I've read that you can use DIB to do that.
Would it be faster? Anyone has any example to do the same using DIBs?
I've an article about bitmap rotation and one of the methods I explain
is using DIBs. I start converting a bitmap to a DIB, process with the
individual bits and then convert the DIB back to a bitmap. You can see
that article here:
www.leunen.com/cbuilder/rotbmp.html
HTH
Michel
--
----------------------------------------
Michel Leunen
mailto: XXXX@XXXXX.COM
C++Builder, C++BuilderX, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:Fastest way to retrieve RGB values from a pixel.

Thanks, Michel! I'll take a look.
 

Re:Fastest way to retrieve RGB values from a pixel.

Indeed I don't think DIB will be much faster than the following code unless
DIB is supported by the hardware.
first of all don't use such an access (pBitmap->Width) inside a loop.
pBitmap->PixelFormat = pf24bit;
int lastpixel;
int height = pBitmap->Height;
int width = pBitmap->Width;
int width3 = width*3;
unsigned char * buffer;
for (int y = 0; y<height; y++)
{
buffer = (unsigned char *)pBitmap->ScanLine[y];
lastpixel = buffer + width3;
while ( buffer<lastpixel )
{
r = buffer++;
g = buffer++;
b = buffer++;
}
}
Cagatay
"Star" < XXXX@XXXXX.COM >wrote in message
Quote
>What we see here are three multiplications. Multiplications take
>much longer than additions.

Hans, you're right. I should change that.

Here is the whole iteration (without your changes):
This code processes the whole image.

for (int y = 0; y < pBitmap->Height; y++)
{
ptr = (Byte *)pBitmap->ScanLine[y];
x=0;
while ( x< pBitmap->Width )
{
nRGB1 = ptr[3*x+0] ;
nRGB2 = ptr[3*x+1] ;
nRGB3 = ptr[3*x+2] ;
x++;
}
}

What I would like to know if using DIBs would be faster than this method.

Thanks


 

Re:Fastest way to retrieve RGB values from a pixel.

Quote
Byte *ptr
ptr = (Byte *)pBitmap->ScanLine[y];

// RGB values are stored here:
ptr[3*x+0]
ptr[3*x+1]
ptr[3*x+2]
In addition to what other people have suggested, I would like recommend
that you use the RGBTRIPLE structure instead of peforming byte math.
for(int y=0; y<height; ++y)
{
RGBTRIPLE *rgb = (RGBTRIPLE *)pBitmap->ScanLine[y];
for (int x=0; x<width; ++x)
{
rgb[x].rgbtRed = ...
rgb[x].rgbtGreen = ...
rgb[x].rgbtBlue = ...
}
}
or
for(int y=0; y<height; ++y)
{
RGBTRIPLE *rgb = (RGBTRIPLE *)pBitmap->ScanLine[y];
for (int x=0; x<width; ++x, ++rgb)
{
rgb->rgbtRed = ...
rgb->rgbtGreen = ...
rgb->rgbtBlue = ...
}
}
One advantage of the RGBTRIPLE structure is that you can copy an entire
pixel easily.
rgb[0] = rgb[1];
h^2