### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: Position BGrabitmap regarding their-dimension of another.  (Read 2253 times)

#### mosquito

• Jr. Member
• Posts: 72
##### Position BGrabitmap regarding their-dimension of another.
« on: September 28, 2021, 09:36:19 am »
I have a BGrabitmap, this is a 800x600 background. Above, I have several BGrabitmap placed in fixed positions with a certain size, these are floating elements on the background.

Now what I want to do is resize the background and that all floating elements are re-escaled and re-positioned in proportion and keep the right aspect.

Any ideas ?
I would not mind any advice, although the resize was keeping the aspect-ratio of the image. Anything is welcome.

Thanks.

#### winni

• Hero Member
• Posts: 2807
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #1 on: September 28, 2021, 05:37:47 pm »
Hi!

I try to understand, but:

Where do you draw on?

Is the Background_BGRAbitmap part of a BGRA component?
Do you draw on it?

Winni

#### mosquito

• Jr. Member
• Posts: 72
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #2 on: September 29, 2021, 10:18:22 am »
Yes, sorry...
I'm drawing in a bgravirtualscreen // 800x600

// Background.
virtualscreen.bitmap.putimage (0.0, back.png ...) // 800x600
// Floating Elements
VirtualScreen.Bitmap.Putimage (125,233, floating1.png ...) // 120x300
VirtualScreen.Bitmap.Putimage (489,134, floating7.png ...) // 120x200

If it was only scale, I think with a linear interpolation could.
If only distances were, you also do it.
But re-positioning and scaling based on a given proportion, I think it requires some trigonometry that I do not know.

Obviously I want to resize the VirtualScreen, and that everything else maintains the original look but expanded or decreased in size and relative positions.

#### circular

• Hero Member
• Posts: 3747
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #3 on: September 30, 2021, 07:58:02 am »
Hello mosquito,

You could use a layered bitmap, add image originals, and then resize the the layered bitmap.

There is sample projet here:
https://github.com/bgrabitmap/bgrabitmap/tree/master/test/layeroriginal

For images, you can add images, gradients or SVG. To add an image it would be those lines:
Code: Delphi  [Select][+][-]
1.   img := TBGRALayerImageOriginal.Create;
2.   img.LoadFromFile(Application.Location + 'lazarus.jpg');
3.   idxImg := FLayers.AddLayerFromOwnedOriginal(img);

Then you can do FLayers.Resample. Use rmFileResample parameter to render at good quality.

Using originals will preserve the information, for example if you resize to a smaller size.
Conscience is the debugger of the mind

#### mosquito

• Jr. Member
• Posts: 72
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #4 on: October 01, 2021, 10:31:56 am »
Thanks Circular;
I know this method, I have used it, it is great.

But I have a problem (I forgot to comment). The floating bitmaps are part of an Trecord, so I need to know at all times its size and position.

This is why I think a re-sampling or Layered Bitmap will not serve me.

#### winni

• Hero Member
• Posts: 2807
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #5 on: October 01, 2021, 11:44:23 am »
Hi!

I think there are no big tricks involved.
It is just simple rescaling for the size and the positions.

Example:

You wan to resize your VirtualScreen to a width of 1000:

Code: Pascal  [Select][+][-]
1. var factor : single;
2. ....
3. factor := 1000/800;
4. BGRAreplace (Background, Background.resample(round(Background.width * factor),
5.                            (round(Background.height * factor) );
6.
7.

Now do the same for the floating Images.
And then compute the positions of the floating images:

Code: Pascal  [Select][+][-]
1. NewPositionX := round(OldPositionX * factor);
2. NewPositionY := round(OldPositionY * factor);
3.

That's the whole secret.

Winni

« Last Edit: October 01, 2021, 11:45:57 am by winni »

#### circular

• Hero Member
• Posts: 3747
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #6 on: October 02, 2021, 11:32:25 am »
But I have a problem (I forgot to comment). The floating bitmaps are part of an Trecord, so I need to know at all times its size and position.
Not sure what you mean by TRecord. For what purpose do you need to know the coordinates?

If you want to know if a pixel is in an image, you could check that the pixel at the specified coordinate is not transparent in the layer bitmap.
Conscience is the debugger of the mind

#### mosquito

• Jr. Member
• Posts: 72
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #7 on: October 02, 2021, 01:36:15 pm »
winni:
This is exactly what she was looking for, thank you very much.

circular:
Floating bitmaps are transparent clicked hotspots. They are on a world map of temperatures (background). I need updated coordinates for this. For now the Winni solution works very well, but I will try to mix this with a Layered Bitmap, maybe it's more versatile.

Thanks to both.

#### circular

• Hero Member
• Posts: 3747
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #8 on: October 02, 2021, 10:37:04 pm »
You can use the matrix of the layer to compute the coordinates.

PointF(0,0)*bmp.LayerMatrix[layerIndex] will give you the upper-left corner
PointF(originalWidth,originalHeight)*bmp.LayerMatrix[layerIndex] will give you the lower-right corner

The matrix is updated depending on the transformation you applied to the whole layered bitmap.
Conscience is the debugger of the mind

#### mosquito

• Jr. Member
• Posts: 72
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #9 on: October 03, 2021, 04:58:47 pm »
Awesome, BGRABitmap never ceases to surprise me. Thank you very much for sharing this great project and for the good price of the support.
« Last Edit: October 03, 2021, 05:33:54 pm by mosquito »

#### circular

• Hero Member
• Posts: 3747
##### Re: Position BGrabitmap regarding their-dimension of another.
« Reply #10 on: October 03, 2021, 05:29:32 pm »

Conscience is the debugger of the mind