Board index » cppbuilder » Tbitmap speed issue.

Tbitmap speed issue.


2006-11-10 05:23:25 AM
cppbuilder0
Hi there I dont quite understand this; i have this class with the following
code.
ImageProcess::ImageProcess(String Filename)
{
fname = Filename;
tBitmap = new Graphics::TBitmap();
tBitmap->PixelFormat = pf24bit; //set pixel format to 24
tBitmap->LoadFromFile(Filename); //load bitmap into memory
/*Setup Image Data*/
Height = tBitmap->Height; //get bitmap Height top to bottom y
Width = tBitmap->Width; //get bitmap Width left to right x
pixelVal = new TRGBTriple();
ReturnData = new TRGBTriple();
}
/*=======================================*/
Graphics::TBitmap* ImageProcess::BinariseImage()
{
for(int i=0;i<Width;i++)
{
for(int j=0;j<Height;j++)
{
pixelVal = getPixel(i,j);
if(pixelVal->rgbtRed>128)
{
pixelVal->rgbtBlue = 0;
pixelVal->rgbtGreen = 0;
pixelVal->rgbtRed = 0;
}
else
{
pixelVal->rgbtBlue = 255;
pixelVal->rgbtGreen = 255;
pixelVal->rgbtRed = 255;
}
SetPixel(pixelVal,i,j);
}
}
return tBitmap;
}
TRGBTriple* __fastcall ImageProcess::getPixel(int row,int col)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[col];
ReturnData->rgbtRed = RowData[row].rgbtRed;
ReturnData->rgbtGreen = RowData[row].rgbtGreen;
ReturnData->rgbtBlue = RowData[row].rgbtBlue;
return ReturnData;
}
/*============================================================================*/
void __fastcall ImageProcess::SetPixel(TRGBTriple* val, int row, int col)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[col]; //Row data from
image
RowData[row].rgbtRed = val->rgbtRed;
RowData[row].rgbtGreen = val->rgbtGreen;
RowData[row].rgbtBlue = val->rgbtBlue;
}
/*==============================================================================*/
Now I have a second class that does exactly the same thing with the
following code.
*******************************************************************************************
ImageProcess::ImageProcess(String Filename)
{
fname = Filename;
tBitmap = new Graphics::TBitmap();
tBitmap->PixelFormat = pf24bit; //set pixel format to 24
tBitmap->LoadFromFile(Filename); //load bitmap into memory
Height = tBitmap->Height; //get bitmap Height top to bottom y
Width = tBitmap->Width; //get bitmap Width left to right x
/*Setup PixelData Array -- 2D*/
PixelData = new TRGBTriple*[Width];
for(int i=0;i<Width;i++)
{
PixelData[i] = new TRGBTriple[Height];
}
/*Copy tBitmap image data into Pixel Data*/
for(int i=0;i<Height;i++)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[i]; //Row data from
image
for(int j=0;j<Width;j++)
{
PixelData[j][i].rgbtRed = RowData[j].rgbtRed; //copy RowData
into image array
PixelData[j][i].rgbtGreen = RowData[j].rgbtGreen;
PixelData[j][i].rgbtBlue = RowData[j].rgbtBlue;
}
}
}
/*************************/
Graphics::TBitmap* ImageProcess::BinariseImage()
{
for(int i=0;i<Width;i++)
{
for(int j=0;j<Height;j++)
{
if(PixelData[i][j].rgbtRed>128)
{
PixelData[i][j].rgbtBlue = 0;
PixelData[i][j].rgbtGreen = 0;
PixelData[i][j].rgbtRed = 0;
}
else
{
PixelData[i][j].rgbtBlue = 255;
PixelData[i][j].rgbtGreen = 255;
PixelData[i][j].rgbtRed = 255;
}
}
}
CopyToRowData();
return tBitmap;
}
/*===========================================================================*/
void ImageProcess::CopyToRowData()
{
for(int i=0;i<Height;i++)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[i]; //Row data from
image
for(int j=0;j<Width;j++)
{
RowData[j] = PixelData[j][i];
}
}
}
The thing that im not sure of is why is the second class much faster than
the first class. I though creating the 2d arrays would slow the process down
and also calling CopyToRowData() should slow things down. Is there something
Im doing wrong with the first class. Also I thought it might be a problem
where u are trying to access pixels from the tbitmap and write pixels at the
same time so I created 2 tbimap objects; 1 for reading and 1 for writing and
still had the same problem. Im really sorry about all this code but if
anyone know what the solution I would be greatly appreciated. Thanks for
your time
 
 

Re:Tbitmap speed issue.

I should probably mention that the method i call in both class is
BinariseImage(), in the first class it is noticably slower than the second
class
"Moon2" < XXXX@XXXXX.COM >wrote in message
Quote
Hi there I dont quite understand this; i have this class with the
following code.

ImageProcess::ImageProcess(String Filename)
{
fname = Filename;

tBitmap = new Graphics::TBitmap();
tBitmap->PixelFormat = pf24bit; //set pixel format to 24
tBitmap->LoadFromFile(Filename); //load bitmap into memory

/*Setup Image Data*/
Height = tBitmap->Height; //get bitmap Height top to bottom y
Width = tBitmap->Width; //get bitmap Width left to right x

pixelVal = new TRGBTriple();
ReturnData = new TRGBTriple();

}
/*=======================================*/
Graphics::TBitmap* ImageProcess::BinariseImage()
{

for(int i=0;i<Width;i++)
{
for(int j=0;j<Height;j++)
{
pixelVal = getPixel(i,j);

if(pixelVal->rgbtRed>128)
{

pixelVal->rgbtBlue = 0;
pixelVal->rgbtGreen = 0;
pixelVal->rgbtRed = 0;
}
else
{

pixelVal->rgbtBlue = 255;
pixelVal->rgbtGreen = 255;
pixelVal->rgbtRed = 255;
}
SetPixel(pixelVal,i,j);
}
}


return tBitmap;
}

TRGBTriple* __fastcall ImageProcess::getPixel(int row,int col)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[col];
ReturnData->rgbtRed = RowData[row].rgbtRed;
ReturnData->rgbtGreen = RowData[row].rgbtGreen;
ReturnData->rgbtBlue = RowData[row].rgbtBlue;
return ReturnData;
}
/*============================================================================*/
void __fastcall ImageProcess::SetPixel(TRGBTriple* val, int row, int col)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[col]; //Row data from
image
RowData[row].rgbtRed = val->rgbtRed;
RowData[row].rgbtGreen = val->rgbtGreen;
RowData[row].rgbtBlue = val->rgbtBlue;

}

/*==============================================================================*/
Now I have a second class that does exactly the same thing with the
following code.


*******************************************************************************************
ImageProcess::ImageProcess(String Filename)
{
fname = Filename;

tBitmap = new Graphics::TBitmap();
tBitmap->PixelFormat = pf24bit; //set pixel format to 24
tBitmap->LoadFromFile(Filename); //load bitmap into memory

Height = tBitmap->Height; //get bitmap Height top to bottom y
Width = tBitmap->Width; //get bitmap Width left to right x


/*Setup PixelData Array -- 2D*/
PixelData = new TRGBTriple*[Width];
for(int i=0;i<Width;i++)
{
PixelData[i] = new TRGBTriple[Height];
}

/*Copy tBitmap image data into Pixel Data*/
for(int i=0;i<Height;i++)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[i]; //Row data from
image
for(int j=0;j<Width;j++)
{
PixelData[j][i].rgbtRed = RowData[j].rgbtRed; //copy RowData
into image array
PixelData[j][i].rgbtGreen = RowData[j].rgbtGreen;
PixelData[j][i].rgbtBlue = RowData[j].rgbtBlue;

}
}
}
/*************************/
Graphics::TBitmap* ImageProcess::BinariseImage()
{
for(int i=0;i<Width;i++)
{
for(int j=0;j<Height;j++)
{
if(PixelData[i][j].rgbtRed>128)
{

PixelData[i][j].rgbtBlue = 0;
PixelData[i][j].rgbtGreen = 0;
PixelData[i][j].rgbtRed = 0;
}
else
{

PixelData[i][j].rgbtBlue = 255;
PixelData[i][j].rgbtGreen = 255;
PixelData[i][j].rgbtRed = 255;
}
}
}
CopyToRowData();
return tBitmap;
}
/*===========================================================================*/
void ImageProcess::CopyToRowData()
{
for(int i=0;i<Height;i++)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[i]; //Row data from
image
for(int j=0;j<Width;j++)
{
RowData[j] = PixelData[j][i];
}
}
}

The thing that im not sure of is why is the second class much faster than
the first class. I though creating the 2d arrays would slow the process
down and also calling CopyToRowData() should slow things down. Is there
something Im doing wrong with the first class. Also I thought it might be
a problem where u are trying to access pixels from the tbitmap and write
pixels at the same time so I created 2 tbimap objects; 1 for reading and 1
for writing and still had the same problem. Im really sorry about all this
code but if anyone know what the solution I would be greatly appreciated.
Thanks for your time

 

Re:Tbitmap speed issue.

Moon2 wrote:
Quote
>... Im really sorry about all this
>code ...
Yes. Why did you post all again?
Hans.
 

{smallsort}

Re:Tbitmap speed issue.

"Moon2" < XXXX@XXXXX.COM >wrote in message
Quote
The thing that im not sure of is why is the second class much
faster than the first class.
Because BinariseImage() is doing a lot less work in the second class. You
are doing all of the more intense work in ImageProcess() so it is a one-time
performance hit when you are intializing your code, not when you are
processing the data later on.
Quote
I though creating the 2d arrays would slow the process down
and also calling CopyToRowData() should slow things down.
Every time you access the ScanLine property, TBitmap has to re-calculate the
memory offset for the specified column. The fewer times you access the
ScanLine, the faster the code will be since you are caching everything in
your own memory. In your first class, you are accessing the Scanline
(Width*Height*2) times overall. In the second class, you are accessing it
only (Height*2) times overall. That is a lot fewer accesses.
Gambit
 

Re:Tbitmap speed issue.

"Moon2" < XXXX@XXXXX.COM >wrote in message
Quote
pixelVal = getPixel(i,j);
<snip>
SetPixel(pixelVal,i,j);
If you get rid of the above methods in your first class, you can reduce the
number of times the ScanLine is accessed:
Graphics::TBitmap* ImageProcess::BinariseImage()
{
TRGBTriple *RowData;
for(int row = 0; row < Height; ++row)
{
RowData = (TRGBTriple*) tBitmap->ScanLine[row];
for(int col = 0; col < Width; ++col)
{
if(RowData[col].rgbtRed>128)
{
RowData[col].rgbtBlue = 0;
RowData[col].rgbtGreen = 0;
RowData[col].rgbtRed = 0;
}
else
{
RowData[col].rgbtBlue = 255;
RowData[col].rgbtGreen = 255;
RowData[col].rgbtRed = 255;
}
}
}
return tBitmap;
}
Gambit
 

Re:Tbitmap speed issue.

Quote
>pixelVal = getPixel(i,j);
<snip>
>SetPixel(pixelVal,i,j);
>If you get rid of the above methods in your first class, you can reduce
>the
>number of times the ScanLine is accessed:
Are you saying I should access the bitmap row by row and also write row by
row. How does this have an advantage of the 2d array i was using in the 2nd
class. As I would essentially be doing the same thing. Is there another
approach you would recommend?
 

Re:Tbitmap speed issue.

Ok it wouldnt essentially wouldnt be doing the same thing as you wouldnt
have the whole image in memory in my previous method, whereas in the method
u are suggesting you have only a row pixel row of data.
Quote
Are you saying I should access the bitmap row by row and also write row by
row. How does this have an advantage of the 2d array i was using in the
2nd class. As I would essentially be doing the same thing. Is there
another approach you would recommend?

 

Re:Tbitmap speed issue.

"Moon2" < XXXX@XXXXX.COM >wrote in message
Quote
Are you saying I should access the bitmap row by row and also write row by
row.
That is not what I said. Look again at the code I gave you. The ScanLine
is retrieved inside the outer loop. Your code was retreiving it inside the
inner loop instead. In both cases, the inner loop reads/writes a specific
pixel in a specific row.
Gambit
 

Re:Tbitmap speed issue.

"Moon2" < XXXX@XXXXX.COM >wrote in message
Quote
Ok it wouldnt essentially wouldnt be doing the same thing as you wouldnt
have the whole image in memory in my previous method, whereas in the
method u are suggesting you have only a row pixel row of data.
Lookk VERY carefully at the code I gave you. Since the original bitmap is
already in memory, there is no need to make a COPY of the image pixels in
memory as well. By reading/writing the pixels directly in the ScanLine
memory, you are directly accessing/modifying the original bitmap, not a copy
of it.
Gambit
 

Re:Tbitmap speed issue.

Thanks Gambit
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Moon2" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>Are you saying I should access the bitmap row by row and also write row
>by
>row.

That is not what I said. Look again at the code I gave you. The ScanLine
is retrieved inside the outer loop. Your code was retreiving it inside
the
inner loop instead. In both cases, the inner loop reads/writes a specific
pixel in a specific row.


Gambit