Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2017-03

TPicture width questions (duplicate ways)

6 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Unknown author

TPicture has several width properties in all sorts of places, for example:

var p: TPicture;
begin
...
p.width
p.jpeg.width
p.bitmap.width
end;

Are any of these pure duplicates that are exactly the same thing, or do some of them have different behaviours and should most definitely not be used interchangeably?

The easiest to read and shortest is just p.width

This is some kind of possible problem with OOP maybe that there are "multiple ways of doing the same thing"... or a feature.. Or, there is a good reason these values are available in multiple places? i.e. jpeg somehow is independent from the main p.width?

Thanks!

Reply by email

José Mejuto
to Unknown author
Lars wrote 14 lines
TPicture has several width properties in all sorts of places, for example: var p: TPicture; begin ... p.width p.jpeg.width p.bitmap.width end; Are any of these pure duplicates that are exactly the same thing, or do some of them have different behaviours and should most definitely not be used interchangeably?

Hello,

AFAIK:

TPicture.Width is the TGraphic container size.
TPicture.jpeg.Width is the width declared in the jpeg image. TPicture.Bitmap.Width is the width of the bitmap in pixels.

Mostly the 3 will have the same value but extracted from different sources.

Reply by email

Unknown author
to José Mejuto
José Mejuto wrote 10 lines
AFAIK: TPicture.Width is the TGraphic container size. TPicture.jpeg.Width is the width declared in the jpeg image. TPicture.Bitmap.Width is the width of the bitmap in pixels. Mostly the 3 will have the same value but extracted from different sources.

If I start off using a bitmap, and all of a sudden start using jpeg, will it automagically work or does one have to initiate the jpeg in some way?

Example: you start off working with a classic BMP, but then 60 lines of code later you want to work with it as a jpeg. Do you just magically start working with a jpeg without any worry, or you have to take some precautions?

Likely, this is explained in a article on the wiki somewhere, I don't know :-)

Reply by email

Kostas Michalopoulos
to Unknown author

You can check yourself easily: Ctrl+Click on the p.Width to go to the definition of the property, then Ctrl+Click on the GetWidth getter, Ctrl+Shift+Down arrow to see the code for the getter and see that it simply gets FGraphic's property. So what is FGraphic? Ctrl+Home to move at the top of the source code (you'd be in picture.inc) and search for "FGraphic :=" to see where it is assigned (the source code style in that file seems to put a space before := in assignments). You can see that it is used in a bunch of places, so you can see where those uses are. ForceType is the first one and if you search for that one, you'll find that it is used wherever a type-specific property (p.Jpeg, p.Bitmap, etc) is used - presumably to convert the underlying graphic to the requested type. So the FGraphic object is actually an instance of the TBitmap, TPortableNetworkGraphic, TJpegImage, etc (depending what you loaded and/or accessed last).

So what p.Width gives you is the FGraphic's width or 0 if there is no FGraphic loaded (because a picture may have nothing loaded until you either load it directly or try to access any of the Bitmap, Jpeg, PNG, etc properties). So if you already have a picture loaded from a PNG file, p.Width, p.Graphic.Width and p.PNG.Width will give you the same number. If your picture doesn't have a graphic (e.g. it is a newly constructed object) then p.Width gives you 0, p.Graphic.Width will crash your program (FGraphic is nil) and p.PNG.Width will give you zero (because accessing the PNG property will call the ForceType function which will create a TPortableNetworkGraphic object that by default is undefined and gives you zero width).

Question for the readers: what happens to the image if you load a PNG file with alpha values using TPicture's LoadFromFile, access the TPicture.Jpeg.Width property and then save the picture as a PNG file using TPicture's SaveToFile? Is the alpha component preserved? Do you get jpeg artifacts? Why?

Bonus question: why is R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height) worse
than R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height)? Why is R := Rect(0, 0,
p.Width, p.Height) better than either of them?

You can find the answers to those (and many more) using Ctrl+Click, Ctrl+Shift+Up/Down Arrow, Search and back and forward buttons (helps to have a mouse with those). Considering how big and complex LCL is, it helps to be able to navigate and familiarize yourself with its source code :-).

On Thu, Mar 9, 2017 at 7:44 PM, Lars via Lazarus <

Show quoted text 31 lines
wrote: On 2017-03-09 10:48, José Mejuto via Lazarus wrote: > > AFAIK: > > TPicture.Width is the TGraphic container size. > TPicture.jpeg.Width is the width declared in the jpeg image. > TPicture.Bitmap.Width is the width of the bitmap in pixels. > > Mostly the 3 will have the same value but extracted from different > sources. > > If I start off using a bitmap, and all of a sudden start using jpeg, will it automagically work or does one have to initiate the jpeg in some way? Example: you start off working with a classic BMP, but then 60 lines of code later you want to work with it as a jpeg. Do you just magically start working with a jpeg without any worry, or you have to take some precautions? Likely, this is explained in a article on the wiki somewhere, I don't know :-) -- _______________________________________________ Lazarus mailing list http://lists.lazarus-ide.org/listinfo/lazarus

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170310/9083ee14/attachment.html>

Reply by email

Kostas Michalopoulos
to Kostas Michalopoulos

Of course the second part should have been Rect(0, 0, p.Jpeg.Width, p.Jpeg.Height)? :-P

On Fri, Mar 10, 2017 at 7:01 PM, Kostas Michalopoulos <

Show quoted text 79 lines
wrote: You can check yourself easily: Ctrl+Click on the p.Width to go to the definition of the property, then Ctrl+Click on the GetWidth getter, Ctrl+Shift+Down arrow to see the code for the getter and see that it simply gets FGraphic's property. So what is FGraphic? Ctrl+Home to move at the top of the source code (you'd be in picture.inc) and search for "FGraphic :=" to see where it is assigned (the source code style in that file seems to put a space before := in assignments). You can see that it is used in a bunch of places, so you can see where those uses are. ForceType is the first one and if you search for that one, you'll find that it is used wherever a type-specific property (p.Jpeg, p.Bitmap, etc) is used - presumably to convert the underlying graphic to the requested type. So the FGraphic object is actually an instance of the TBitmap, TPortableNetworkGraphic, TJpegImage, etc (depending what you loaded and/or accessed last). So what p.Width gives you is the FGraphic's width or 0 if there is no FGraphic loaded (because a picture may have nothing loaded until you either load it directly or try to access any of the Bitmap, Jpeg, PNG, etc properties). So if you already have a picture loaded from a PNG file, p.Width, p.Graphic.Width and p.PNG.Width will give you the same number. If your picture doesn't have a graphic (e.g. it is a newly constructed object) then p.Width gives you 0, p.Graphic.Width will crash your program (FGraphic is nil) and p.PNG.Width will give you zero (because accessing the PNG property will call the ForceType function which will create a TPortableNetworkGraphic object that by default is undefined and gives you zero width). Question for the readers: what happens to the image if you load a PNG file with alpha values using TPicture's LoadFromFile, access the TPicture.Jpeg.Width property and then save the picture as a PNG file using TPicture's SaveToFile? Is the alpha component preserved? Do you get jpeg artifacts? Why? Bonus question: why is R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height) worse than R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height)? Why is R := Rect(0, 0, p.Width, p.Height) better than either of them? You can find the answers to those (and many more) using Ctrl+Click, Ctrl+Shift+Up/Down Arrow, Search and back and forward buttons (helps to have a mouse with those). Considering how big and complex LCL is, it helps to be able to navigate and familiarize yourself with its source code :-). On Thu, Mar 9, 2017 at 7:44 PM, Lars via Lazarus < > wrote: > On 2017-03-09 10:48, José Mejuto via Lazarus wrote: > >> >> AFAIK: >> >> TPicture.Width is the TGraphic container size. >> TPicture.jpeg.Width is the width declared in the jpeg image. >> TPicture.Bitmap.Width is the width of the bitmap in pixels. >> >> Mostly the 3 will have the same value but extracted from different >> sources. >> >> > If I start off using a bitmap, and all of a sudden start using jpeg, will > it automagically work or does one have to initiate the jpeg in some way? > > Example: you start off working with a classic BMP, but then 60 lines of > code later you want to work with it as a jpeg. Do you just magically start > working with a jpeg without any worry, or you have to take some precautions? > > Likely, this is explained in a article on the wiki somewhere, I don't > know :-) > > -- > _______________________________________________ > Lazarus mailing list > > http://lists.lazarus-ide.org/listinfo/lazarus >

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lazarus-ide.org/pipermail/lazarus/attachments/20170310/fb57707d/attachment.html>

Reply by email

Unknown author
to Kostas Michalopoulos: You can check yourself easily: Ctrl+Click on the p.Width to go to the definition of the…
Kostas Michalopoulos wrote
You can check yourself easily: Ctrl+Click on the p.Width to go to the definition of the property, then Ctrl+Click on the GetWidth getter, Ctrl+Shift+Down arrow to see the code for the getter and see that it simply gets FGraphic's property.

You can check the current sources but that does not mean it will stay that way, or that the current sources do not have a bug in them... the documentation would tell you how it is supposed to act, and, the source is one particular implementation, which could not match up with what the doc says, in that case it is buggy ;-)

But thanks very much for explaining it: indeed I figured any call to .jpeg.someproperty would immediately automagically do the work for you

So what is FGraphic? Ctrl+Home to

Show quoted text 11 lines
move at the top of the source code (you'd be in picture.inc) and search for "FGraphic :=" to see where it is assigned (the source code style in that file seems to put a space before := in assignments). You can see that it is used in a bunch of places, so you can see where those uses are. ForceType is the first one and if you search for that one, you'll find that it is used wherever a type-specific property (p.Jpeg, p.Bitmap, etc) is used - presumably to convert the underlying graphic to the requested type. So the FGraphic object is actually an instance of the TBitmap, TPortableNetworkGraphic, TJpegImage, etc (depending what you loaded and/or accessed last).

This is very useful - I thought maybe it would be in a wiki or docs somewhere.. It may be...

Show quoted text 27 lines
So what p.Width gives you is the FGraphic's width or 0 if there is no FGraphic loaded (because a picture may have nothing loaded until you either load it directly or try to access any of the Bitmap, Jpeg, PNG, etc properties). So if you already have a picture loaded from a PNG file, p.Width, p.Graphic.Width and p.PNG.Width will give you the same number. If your picture doesn't have a graphic (e.g. it is a newly constructed object) then p.Width gives you 0, p.Graphic.Width will crash your program (FGraphic is nil) and p.PNG.Width will give you zero (because accessing the PNG property will call the ForceType function which will create a TPortableNetworkGraphic object that by default is undefined and gives you zero width). Question for the readers: what happens to the image if you load a PNG file with alpha values using TPicture's LoadFromFile, access the TPicture.Jpeg.Width property and then save the picture as a PNG file using TPicture's SaveToFile? Is the alpha component preserved? Do you get jpeg artifacts? Why? Bonus question: why is R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height) worse than R := Rect(0, 0, p.Jpeg.Width, p.PNG.Height)? Why is R := Rect(0, 0, p.Width, p.Height) better than either of them? You can find the answers to those (and many more) using Ctrl+Click, Ctrl+Shift+Up/Down Arrow, Search and back and forward buttons (helps to have a mouse with those). Considering how big and complex LCL is, it helps to be able to navigate and familiarize yourself with its source code :-).

IMO the docs should always describe the theory of an object/procedure, and therefore if any time the source code is buggy, the docs are the official sources to what a function does.. :-)

Thanks much for documenting it in this mailing list !

Some of your comments, IMO should be uploaded to wiki/docs somewhere But I'm unfamiliar with how the docs updating process works

Reply by email