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 

Strange problem with TBitmap's Empty

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





PostPosted: Thu Nov 10, 2005 7:19 am    Post subject: Strange problem with TBitmap's Empty Reply with quote



Having the following snippet

TImage *image=new TImage(...);
...
image->Picture->LoadFromFile(my_filename);
assert(image->Picture->Width!=NULL); // no assertion-failure
if(image->Picture->Bitmap->Empty)
assert(image->Picture->Width!=NULL); // assertion-failure

I am irritated, that the second assertion would come to a failure, if
image->Picture->Bitmap->Empty
is true. I thought testing Empty is just a check and does not have any
side-effects.

The original problem occured with a (every?) .jpg-file when I wanted
to set a text font with
image->Picture->Bitmap->Canvas->Font->Height=...;
and found out that trying to do this did reset
image->Picture->Width
back to 0.

So do I have to convert my loaded image into a TImage where the Bitmap
is not empty. If yes, how can I check this (without the side-effect,
that my original-image is deleted) and how this is done?

Thanks,

Michael
Back to top
Vladimir Stefanovic
Guest





PostPosted: Thu Nov 10, 2005 3:45 pm    Post subject: Re: Strange problem with TBitmap's Empty Reply with quote



Try this:

bool IsImageEmpty( TImage *I )
{
if ( ( I->Picture->Graphic ) && ( !I->Picture->Graphic->Empty ) )
return ( false );

return ( true );
}


--
Best regards,
Vladimir Stefanovic


Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Nov 10, 2005 5:50 pm    Post subject: Re: Strange problem with TBitmap's Empty Reply with quote




<MR> wrote


Quote:
I am irritated, that the second assertion would come to a
failure, if image->Picture->Bitmap->Empty is true. I thought
testing Empty is just a check and does not have any side-effects.

Empty itself has no side-effects. But if you did not load a bitmap file
into the TImage to begin with, then accessing the Bitmap property for the
first time will discard the current Picture contents and create an empty
TBittmap object. That is why the Empty properrty can be true and the Width
can be 0.

Quote:
The original problem occured with a (every?) .jpg-file when
I wanted to set a text font with
image->Picture->Bitmap->Canvas->Font->Height=...;
and found out that trying to do this did reset
image->Picture->Width
back to 0.

If you load a jpg, the Picture will contain a TJPEGImage, not a TBitmap.
Accessing the Bitmap property will free the TJPEGImage and create an empty
TBitmap, ths your jpg will be lost.

JPGs do not have a Canvas of their own, so you cannot set the Font
characteristics of a jpg.

Quote:
So do I have to convert my loaded image into a TImage
where the Bitmap is not empty. If yes, how can I check this
(without the side-effect, that my original-image is deleted)
and how this is done?

Use the Graphic property instead of the Bitmap property in order to access
the image contents in a generic manner:

image->Picture->LoadFromFile(my_filename); // load a jpg
if( image->Picture->Graphic->Empty )
// ...

As for the specific issue of using a Canvas with a jpg, use a temporary
TBitmap instead of trying to modify the original jpg directly:

image->Picture->LoadFromFile(my_filename); // load a jpg
if( !image->Picture->Graphic->Empty )
{
// ...
Graphics::TBitmap *tmp = new Graphics::TBitmap;
try
{
tmp->Assign(image->Picture->Graphic); // copy the graphic to a
bmp
// modify tmp as needed...
image->Picture->Graphic->Assign(tmp); // copy the bmp back to
the graphic
}
__finally {
delete tmp;
}
//...
}


Gambit



Back to top
MR
Guest





PostPosted: Thu Nov 10, 2005 8:41 pm    Post subject: Re: Strange problem with TBitmap's Empty Reply with quote

Thanks a lot: this was exactly what I was looking for.

One little thing:

After trying and having not succeeded I had to change the line
Quote:
image->Picture->Graphic->Assign(tmp);
to

image->Picture->Assign(tmp);


After that

assert(!image->Picture->Bitmap->Empty);

succeeded.


(Hope, I did it right!?)


Thanks,

Michael

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Thu Nov 10, 2005 9:06 pm    Post subject: Re: Strange problem with TBitmap's Empty Reply with quote


<MR> wrote


Quote:
After trying and having not succeeded I had to change the line
image->Picture->Graphic->Assign(tmp);
to
image->Picture->Assign(tmp);

Why are you doing that? You will lose your jpg again, because you are
changing the TPicture to hold a TBitmap instead of a TJPEGImage. By
accessing the Graphic property instead, you can preserve/update what is
already in the TPicture (aka the original jpg).

Quote:
After that

assert(!image->Picture->Bitmap->Empty);

succeeded.

Of course it does, because you forced the contents of the TPicture to hold a
bitmap instead of a jpg, and then you are accessing that bitmap. You are
not paying attention to what has been explained to you, so let me try again.
EVERY TIME you access the TPicture::Bitmap property, the contents of the
TPicture are THROWN AWAY if they are not already in a bitmap format.
TPicture DOES NOT convert images from one type to another automatically. If
you load one image type into TPicture and then access a property that
requires a different image type, the original type is discarded and a new
BLANK image of the required type is put in its place. Since you are not
loading any bitmap initially, you WILL LOSE YOUR JPG DATA whenever you
access the Bitmap property for any reason. So unless you have actually
loaded a bmp into TPicture, you SHOULD NOT be accessing the Bitmap property
at all, unless you don't care about losing your images.

This is explained in the VCL documentation.


Gambit



Back to top
MR
Guest





PostPosted: Fri Nov 11, 2005 6:35 am    Post subject: Re: Strange problem with TBitmap's Empty Reply with quote

"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> schrieb:

Quote:

MR> wrote


After trying and having not succeeded I had to change the line
image->Picture->Graphic->Assign(tmp);
to
image->Picture->Assign(tmp);

Why are you doing that? You will lose your jpg again, because you are
changing the TPicture to hold a TBitmap instead of a TJPEGImage. By
accessing the Graphic property instead, you can preserve/update what is
already in the TPicture (aka the original jpg).

Perhaps I misunderstood something, but I thought the reason for
temporary TBitmap 'tmp' was to convert the jpeg-image, which has an
empty TBitmap property to a Bitmap and afterwards to assign this
Bitmap to the original image, to be able to set
image->Picture->Bitmap->Canvas->Font->Height=...;
to a value and write some text onto the Bitmap.
Sure, the jpeg-image has now been replaced by a bitmap-image, but 1)
how else could I display a text on it and 2) I do not intend to save
it back?

Michael

Back to top
Remy Lebeau (TeamB)
Guest





PostPosted: Sat Nov 12, 2005 6:25 am    Post subject: Re: Strange problem with TBitmap's Empty Reply with quote


<MR> wrote


Quote:
I thought the reason for temporary TBitmap 'tmp' was
to convert the jpeg-image

It is. JPG images do not haave any Canvas available, so you have to convert
to TBitmap manually in order to do any custom drawing on the JPG.

Quote:
which has an empty TBitmap property

That is not what I said. The Bitmap property of TPicture DOES NOT access
any bitmap data for an image that is loaded in the TPicture (unless the
image is really a bitmap to begin with, of course). If the TPicture does
not already contain a TBitmap object, then accessing the Bitmap property of
TPicture DESTROYS whatever image is currently loaded in the TPicture, and
then places a newly created BLANK TBitmap into the TPicture. That is why
your code is reporting an empty Bitmap after loading a jpg file.

Quote:
to a Bitmap to be able to set
image->Picture->Bitmap->Canvas->Font->Height=...;
to a value and write some text onto the Bitmap.

That is the reason for converting a JPG to a temporary bitmap, yes.

Quote:
and afterwards to assign this Bitmap to the original image

Yes, that is the goal, but that is not what you are actually doing in your
code yet. In order to do that, you need to call Assign() on the Graphic
property of TPicture, not on the TPicture itself. Calling Assign() on the
Graphic property of TPicture pases the temporary bitmap to whatever image is
currently loaded in the TPicture. Calling Assign() on the TPicture itself,
on the other hand, DESTROYS the currently loaded image and then places a
COPY of the temporary bitmap into the TPicture.

Quote:
Sure, the jpeg-image has now been replaced by a
bitmap-image, but how else could I display a text on it

The only way to draw on a JPG is to use a temporary bitmap.


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.