BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Tbitmap speed issue.

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Graphics)
View previous topic :: View next topic  
Author Message
Moon2
Guest





PostPosted: Fri Nov 10, 2006 3:23 am    Post subject: Tbitmap speed issue. Reply with 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
Back to top
Moon2
Guest





PostPosted: Fri Nov 10, 2006 3:32 am    Post subject: Re: Tbitmap speed issue. Reply with quote



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" <sbi (AT) telkomsa (DOT) net> wrote in message
news:45539c53 (AT) newsgroups (DOT) borland.com...
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
Back to top
Hans Galema
Guest





PostPosted: Fri Nov 10, 2006 4:18 am    Post subject: Re: Tbitmap speed issue. Reply with quote



Moon2 wrote:

Quote:
... Im really sorry about all this
code ...

Yes. Why did you post all again?

Hans.
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Nov 10, 2006 4:36 am    Post subject: Re: Tbitmap speed issue. Reply with quote

"Moon2" <sbi (AT) telkomsa (DOT) net> wrote in message
news:45539c53 (AT) newsgroups (DOT) borland.com...

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
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Nov 10, 2006 4:41 am    Post subject: Re: Tbitmap speed issue. Reply with quote

"Moon2" <sbi (AT) telkomsa (DOT) net> wrote in message
news:45539c53 (AT) newsgroups (DOT) borland.com...

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
Back to top
Moon2
Guest





PostPosted: Fri Nov 10, 2006 4:53 am    Post subject: Re: Tbitmap speed issue. Reply with quote

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?
Back to top
Moon2
Guest





PostPosted: Fri Nov 10, 2006 5:04 am    Post subject: Re: Tbitmap speed issue. Reply with 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.

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?
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Nov 10, 2006 7:47 am    Post subject: Re: Tbitmap speed issue. Reply with quote

"Moon2" <sbi (AT) telkomsa (DOT) net> wrote in message
news:4553b16a (AT) newsgroups (DOT) borland.com...

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
Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Fri Nov 10, 2006 7:48 am    Post subject: Re: Tbitmap speed issue. Reply with quote

"Moon2" <sbi (AT) telkomsa (DOT) net> wrote in message
news:4553b409 (AT) newsgroups (DOT) borland.com...

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
Back to top
Moon2
Guest





PostPosted: Fri Nov 10, 2006 3:41 pm    Post subject: Re: Tbitmap speed issue. Reply with quote

Thanks Gambit

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote in message
news:4553db3a$1 (AT) newsgroups (DOT) borland.com...
Quote:

"Moon2" <sbi (AT) telkomsa (DOT) net> wrote in message
news:4553b16a (AT) newsgroups (DOT) borland.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

Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> C++ Builder (Graphics) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.