 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Luke Guest
|
Posted: Thu Jun 17, 2004 8:06 am Post subject: timage and Jpeg |
|
|
Hi,
I have a situation where I need to display an image in a TImage.
It can be either bmp or Jpg format. Once the image is loaded,
they user can then draw on the image. I have been able to get around
the whole copying the Jpeg to the Canvas of the Timage and works fine.
The problem is that if the jpeg is bigger than what the timage size is (the
timage cant
be set to Autosize either, must be fixed), I draw on it, and then save the
changes, the
image that I end up with is not the original size, but the size of the
TImage component.
Is there any way that when I save the changes to a the jpg, that the
original size
is not lost?
Sample Code Loading the Image:
procedure TForm1.Button1Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MyBMP : TBitmap;
begin
MyJPEG := TJPEGImage.Create;
MyJPEG.LoadFromFile('C:SamplePic.jpg');
MyBMP := TBitmap.Create;
MyBMP.Width := MyJPEG.Width;
MyBMP.Height := MyJPEG.Height;
MyBMP.Canvas.StretchDraw(Rect(0,0,MYJpeg.Width, MYJpeg.Height),
MyJPEG);
image1.Stretch := false;
image1.Canvas.StretchDraw(image1.ClientRect,MyBMP);
image1.Stretch := true;
MyBMP.Free;
MyJPEG.Free;
end;
end;
Sample Code Saving the changes:
procedure TForm1.Button2Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MyBMP : TBitmap;
begin
MyJPEG := TJPEGImage.Create;
MyJPEG.Assign(image1.Picture.Bitmap);
MyJPEG.Scale := jsFullSize;
MyJPEG.SaveToFile('C:Changed.jpg');
MyJPEG.Free;
end;
Thanks for any help,
Luke.
|
|
| Back to top |
|
 |
Yannis Guest
|
Posted: Thu Jun 17, 2004 8:34 am Post subject: Re: timage and Jpeg |
|
|
"Luke" <johnsluk (AT) hotmail (DOT) com> wrote in
news:40d150ee$1 (AT) newsgroups (DOT) borland.com:
| Quote: | procedure TForm1.Button1Click(Sender: TObject);
var
MyJPEG : TJPEGImage;
MyBMP : TBitmap;
begin
MyJPEG := TJPEGImage.Create;
MyJPEG.LoadFromFile('C:SamplePic.jpg');
MyBMP := TBitmap.Create;
MyBMP.Width := MyJPEG.Width;
MyBMP.Height := MyJPEG.Height;
MyBMP.Canvas.StretchDraw(Rect(0,0,MYJpeg.Width, MYJpeg.Height),
MyJPEG);
|
Up until here everything is OK but in the next three lines of code
you have managed to destroy all the previus work.
It is clear that you do not have a clear understanding of what TImage
is and how it works, So here are some basics
TImage is not a component which handles the image data it self it is a
painter component for TGraphic descentants only. I mean that you use
TImage with a Tgraphic in order to interact with the user screen,
read mouse movement paint graphics etc. Every time you do a draw in
a Timage this is been redirected to the TGraphic that this component
owns, If it doesn't have any Tgraphic Component then it creates a
TBitmap object and it gives to it the dimensions of it self and the
color depth of the computer it is running on.
In sort at this point in your software you just need to assign
the TBitmap you have created to the Timage component and you
will be ready to start working, something like
Image1.picture.assign(MyBMP);
This will place the newly created MyBMP as the default paint
object of the Image1 control and this will also correct the
problem with autosize you have.
| Quote: | image1.Stretch := false;
image1.Canvas.StretchDraw(image1.ClientRect,MyBMP);
image1.Stretch := true;
|
I thing that the above line shoold be removed otherwise you will either
end up with a Timage component with a wrong referense or you will force
it to duplicate the existing (I'm not sure about this to much VB I guess)
in any case this at least will make you loose time or at worst crash your
program later on.
| Quote: | MyJPEG.Free;
end;
end;
|
Regards
Yannis.
|
|
| Back to top |
|
 |
Joris Van Damme Guest
|
Posted: Thu Jun 17, 2004 9:27 am Post subject: Re: timage and Jpeg |
|
|
"Yannis" <None (AT) Noware (DOT) non> wrote
| Quote: | It is clear that you do not have a clear understanding of what TImage
is and how it works, So here are some basics
|
A good (I hope) explanation can be found here
http://www.awaresystems.be/techtalks/003_graphic_picture_image.html
It is now available in both English and Dutch. A French version will be
added soon.
| Quote: | MyBMP.Free;
I thing that the above line shoold be removed otherwise you will either
end up with a Timage component with a wrong referense or you will force
it to duplicate the existing (I'm not sure about this to much VB I guess)
in any case this at least will make you loose time or at worst crash your
program later on.
|
That is not correct. Not freeing the bitmap is a leak. The bitmap's
ownership is *not* transfered in the previous lines.
| Quote: | MyJPEG := TJPEGImage.Create;
MyJPEG.LoadFromFile('C:SamplePic.jpg');
MyBMP := TBitmap.Create;
MyBMP.Width := MyJPEG.Width;
MyBMP.Height := MyJPEG.Height;
MyBMP.Canvas.StretchDraw(Rect(0,0,MYJpeg.Width, MYJpeg.Height),
MyJPEG); |
Assigning would be the proper way to do this (if it needs to be done, see
below). Drawing, like you do here, involves duplicate memory, and
duplicating content. Assigning involves merely incrementing a usage counter
(untill the time that either copy is actually edited, a principle that is
called copy-on-write).
| Quote: | image1.Stretch := false;
image1.Canvas.StretchDraw(image1.ClientRect,MyBMP);
image1.Stretch := true;
|
That's the third copy then, even if this copy is scaled down.
| Quote: | MyJPEG := TJPEGImage.Create;
MyJPEG.Assign(image1.Picture.Bitmap);
|
Assigning is the proper thing to do. But of course, you're using
image1.Picture.Bitmap, which is the scaled copy, so of course your resulting
saved jpeg will be scaled.
In my opinion, the usage of TImage here is incorrect. The proper way to do
it would be to handle an off-screen bitmap, do your editing on that off-scre
en bitmap, and use a paintbox or similar for UI feedback. That is a bit more
elaborate, code-wise, off course. Things that come to mind that may be
usefull are Nils's virtual scrollbox and DTPDocuments component.
(www.simdesign.nl)
Good luck!
Joris Van Damme
[email]info (AT) awaresystems (DOT) be[/email]
http://www.awaresystems.be
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html
|
|
| Back to top |
|
 |
Luke Guest
|
Posted: Thu Jun 17, 2004 9:57 pm Post subject: Re: timage and Jpeg |
|
|
"Yannis" <None (AT) Noware (DOT) non> wrote
| Quote: | "Luke" <johnsluk (AT) hotmail (DOT) com> wrote in
news:40d150ee$1 (AT) newsgroups (DOT) borland.com:
Image1.picture.assign(MyBMP);
This will place the newly created MyBMP as the default paint
object of the Image1 control and this will also correct the
problem with autosize you have.
Regards
Yannis.
|
Thanks for your help, works fine now.
Luke.
|
|
| Back to top |
|
 |
Yannis Guest
|
Posted: Fri Jun 18, 2004 7:45 am Post subject: Re: timage and Jpeg |
|
|
"Joris Van Damme" <PleaseReplyTo (AT) TheGroupInstead (DOT) be> wrote in
news:40d163a3 (AT) newsgroups (DOT) borland.com:
Nice explenations and clear enough I have one question though
at the end of the page you are saying and I quote
/Quote
Now you see where the Assign comes in. Internally, these
TJpegImage and TBitmap objects are supposed to maintain
rasters with usage counters. The Assign statement, officially,
copies the raster content of the jpeg object into the bmp object.
Underneath, this does *not* involve creating a second raster
and copying the pixel data, but boils down to sharing the raster
object and incrementing its usage counter, eliminating the need
for duplicating memory.
Quote/
I was under the impression that the raster content was the same
until on of the objects made a change on it which in this time
it would duplicate the device and apply the change tot he new
content.
regards
Yannis.
|
|
| Back to top |
|
 |
Joris Van Damme Guest
|
Posted: Fri Jun 18, 2004 8:29 am Post subject: Re: timage and Jpeg |
|
|
"Yannis" <None (AT) Noware (DOT) non> wrote
Thank you!
| Quote: | at the end of the page you are saying and I quote
/Quote
Now you see where the Assign comes in. Internally, these
TJpegImage and TBitmap objects are supposed to maintain
rasters with usage counters. The Assign statement, officially,
copies the raster content of the jpeg object into the bmp object.
Underneath, this does *not* involve creating a second raster
and copying the pixel data, but boils down to sharing the raster
object and incrementing its usage counter, eliminating the need
for duplicating memory.
Quote/
I was under the impression that the raster content was the same
until on of the objects made a change on it which in this time
it would duplicate the device and apply the change tot he new
content.
|
Yes, as far as I understand you, that is exactly what I meant with the above
quoted paragraph. If that wasn't clear, please help me make it clearer. I'm
very much open for all suggestions.
Joris Van Damme
[email]info (AT) awaresystems (DOT) be[/email]
http://www.awaresystems.be
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html
|
|
| Back to top |
|
 |
Gordon Whittam Guest
|
Posted: Fri Jun 18, 2004 10:13 am Post subject: Re: timage and Jpeg |
|
|
In article <40d2a7a9 (AT) newsgroups (DOT) borland.com>, Joris Van Damme
wrote:
| Quote: | I'm
very much open for all suggestions.
|
"A TPicture is a descendant of TGraphic. " It's a descendent of
TPersistent.
Gordon
|
|
| Back to top |
|
 |
Joris Van Damme Guest
|
Posted: Fri Jun 18, 2004 10:45 am Post subject: Re: timage and Jpeg |
|
|
"Gordon Whittam" <gordon (AT) mgcsoft (DOT) com> wrote
| Quote: | "A TPicture is a descendant of TGraphic. " It's a descendent of
TPersistent.
|
You are right. Mea culpa. I don't understand howcome I was convinced of
this, obviously without ever checking. I will correct the text without
delay.
Many thanks for pointing this out!
Joris Van Damme
[email]info (AT) awaresystems (DOT) be[/email]
http://www.awaresystems.be
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html
|
|
| Back to top |
|
 |
Joris Van Damme Guest
|
Posted: Fri Jun 18, 2004 10:58 am Post subject: Re: timage and Jpeg |
|
|
"Joris Van Damme" <PleaseReplyTo (AT) TheGroupInstead (DOT) be> wrote
| Quote: | ...will correct the text...
|
Done (remember to hit reload/refresh if you're interested in viewing the
changes, first couple of hours). Thanks again.
Joris Van Damme
[email]info (AT) awaresystems (DOT) be[/email]
http://www.awaresystems.be
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html
|
|
| Back to top |
|
 |
Yannis Guest
|
Posted: Fri Jun 18, 2004 11:27 am Post subject: Re: timage and Jpeg |
|
|
"Joris Van Damme" <PleaseReplyTo (AT) TheGroupInstead (DOT) be> wrote in
news:40d2a7a9 (AT) newsgroups (DOT) borland.com:
| Quote: | "Yannis" <None (AT) Noware (DOT) non> wrote in message
news:Xns950C6DB5DD9A9NoneNowarenon (AT) 207 (DOT) 105.83.66...
http://www.awaresystems.be/techtalks/003_graphic_picture_image.html
Nice explenations and clear enough I have one question though
Thank you!
at the end of the page you are saying and I quote
/Quote
Now you see where the Assign comes in. Internally, these
TJpegImage and TBitmap objects are supposed to maintain
rasters with usage counters. The Assign statement, officially,
copies the raster content of the jpeg object into the bmp object.
Underneath, this does *not* involve creating a second raster
and copying the pixel data, but boils down to sharing the raster
object and incrementing its usage counter, eliminating the need
for duplicating memory.
Quote/
I was under the impression that the raster content was the same
until on of the objects made a change on it which in this time
it would duplicate the device and apply the change tot he new
content.
Yes, as far as I understand you, that is exactly what I meant with the
above quoted paragraph. If that wasn't clear, please help me make it
clearer. I'm very much open for all suggestions.
|
I would have said it like this.
The assign statement adds a referense (or a pointer )to the existing
raster contents increasing a counter which shows how many objects have
access to this contents and it doesn't copy its data. At least not until
the first time one of the objects that referencing the same raster makes
a change to the contents. This change will actualy force a copy of the
raster's data to a new memory address decreasing the referense counter
and then apply the change.
This way I find to be more accurate because
1) It descripes the steps of an assign
2) It descripes the steps taken when a change is made.
3) The raster is not an object (which is implied in your text) just a
type like a record.
Regards
Yannis.
|
|
| Back to top |
|
 |
Joris Van Damme Guest
|
Posted: Fri Jun 18, 2004 12:10 pm Post subject: Re: timage and Jpeg |
|
|
| Quote: | I would have said it like this.
The assign statement adds a referense (or a pointer )to the existing
raster contents increasing a counter which shows how many objects have
access to this contents and it doesn't copy its data. At least not until
the first time one of the objects that referencing the same raster makes
a change to the contents. This change will actualy force a copy of the
raster's data to a new memory address decreasing the referense counter
and then apply the change.
|
There is most defenetly parts of this that I want to use. Thank you very
much!
| Quote: | This way I find to be more accurate because
1) It descripes the steps of an assign
|
Agreed.
| Quote: | 2) It descripes the steps taken when a change is made.
|
Agreed.
| Quote: | 3) The raster is not an object (which is implied in your text) just a
type like a record.
|
I don't enterely agree here. After all, it's the descendant object that is
repsonsable for maintaining the raster, and it can choose any type of record
or object or whatever structure it sees fit. (Which kind of steps on the
point... Yes, a descendant could decide to build an actual real duplicate
copy inside the Assign implementation... Sigh.) Plus, I feel the word
'object' can also be used in the very general sense of 'thingy'. Plus, this
is not a text for the very advanced, and we are discussing a detail in a
subsection in a relatively small introductary text.
But thank you for pointing out the copy-on-write role of Assign can be
explained more clearly. I'm defenetly going to use part of your
contribution. Thanks!
Joris Van Damme
[email]info (AT) awaresystems (DOT) be[/email]
http://www.awaresystems.be
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html
|
|
| Back to top |
|
 |
Yannis Guest
|
Posted: Fri Jun 18, 2004 12:22 pm Post subject: Re: timage and Jpeg |
|
|
"Joris Van Damme" <PleaseReplyTo (AT) TheGroupInstead (DOT) be> wrote in
news:40d2db3a (AT) newsgroups (DOT) borland.com:
| Quote: | I don't enterely agree here. After all, it's the descendant object
that is repsonsable for maintaining the raster, and it can choose any
type of record or object or whatever structure it sees fit. (Which
kind of steps on the point... Yes, a descendant could decide to build
an actual real duplicate copy inside the Assign implementation...
Sigh.) Plus, I feel the word 'object' can also be used in the very
general sense of 'thingy'. Plus, this is not a text for the very
advanced, and we are discussing a detail in a subsection in a
relatively small introductary text.
|
Neet picking I know but i have this thing to say the things exactly as
I see/understand them and be as accurate as possible. In my defense
though I have taken in to account only the TBitmap object which holds
a windows Tbitmap as raster contents which is in fact just a record
in memory.
I agree that it does not worth the effort especially since this might
change based on the implementation of the object.
| Quote: | But thank you for pointing out the copy-on-write role of Assign can be
explained more clearly. I'm defenetly going to use part of your
contribution. Thanks!
|
I'm honored thank you
Regards
Yannis.
|
|
| Back to top |
|
 |
|
|
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
|
|