Recent

Author Topic: How to Rotate a TImage Programmatically in Lazarus IDE?  (Read 2818 times)

Aruna

  • Hero Member
  • *****
  • Posts: 790
How to Rotate a TImage Programmatically in Lazarus IDE?
« on: March 28, 2025, 02:54:09 pm »
Hello,

I am working on a personal project in Lazarus IDE, and I need to rotate a TImage component programmatically at runtime. I’m trying to find a way to apply a rotation transformation to the image, but I’m not sure how to approach it.

Can anyone provide some guidance or an example on how to rotate an image using TImage in Lazarus? Do we have built-in methods to do this? I am aware of BGRA but I prefer to learn how to do this through code.

Thank you in advance for your help!

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12693
  • FPC developer.
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #1 on: March 28, 2025, 03:15:46 pm »

The fastest by my knowledge for 8-bit images:https://www.stack.nl/~marcov/rot8x8.txt

TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #2 on: March 28, 2025, 03:20:48 pm »
In case it is about the learning process on how to rotate then you could perhaps have a look here.
Today is tomorrow's yesterday.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12693
  • FPC developer.
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #3 on: March 28, 2025, 03:27:03 pm »
In my admitted non practical URL, there is an URL to a stackoverflow post with a bit more theory. It is mainly an optimization for rotations by 90 and 270 degrees, which are  faster and reversible operations, contrary to "by any angle" routines which are not.


TRon

  • Hero Member
  • *****
  • Posts: 4377
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #4 on: March 28, 2025, 03:48:16 pm »
Sorry marcov as I've initially missed that so thank you for pointing that out.

I also came across this image component. No idea how cross-compatible it is though (seems reasonable at quick glance).
Today is tomorrow's yesterday.

Hartmut

  • Hero Member
  • *****
  • Posts: 1094
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #5 on: March 28, 2025, 06:18:47 pm »
If you want a procedure that can rotate the Bitmap of a TImage by 90/180/270° then have a look at reply #6 in https://forum.lazarus.freepascal.org/index.php/topic,69327.0.html where you can find a nice solution by wp.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12693
  • FPC developer.
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #6 on: March 28, 2025, 06:31:03 pm »
If you want a procedure that can rotate the Bitmap of a TImage by 90/180/270° then have a look at reply #6 in https://forum.lazarus.freepascal.org/index.php/topic,69327.0.html where you can find a nice solution by wp.

Aka the cache murderer. (the algorithm of the routine, not WP :-) )

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 881
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #7 on: March 28, 2025, 08:32:48 pm »
It's always better to use hardware acceleration in such cases, like OpenGL for example, because per-pixel operations are slow.
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12693
  • FPC developer.
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #8 on: March 28, 2025, 08:34:14 pm »
It's always better to use hardware acceleration in such cases, like OpenGL for example, because per-pixel operations are slow.

I actually do, but only for the more performance dependent applications. For the lighter applications it is easier to just rotate to get the way the camera is mounted synced with a standing (wo)man's viewpoint.

Mr.Madguy

  • Hero Member
  • *****
  • Posts: 881
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #9 on: March 28, 2025, 08:59:17 pm »
How 3D library usually do it:
1) Split your quad into 2 triangles.
2) For each triangle vertex define texture coordinates (u, v) like (0, 0), (Width - 1, 0), (Width - 1, Height - 1). You can use universal float values, like 0..1, but it's not necessary, if you use one fixed texture resolution only.
3) Use linear interpolation to get (u, v) values for all other points. It's usually done via following algorithm:
For each u and v you can obtain new value via u/v=a*x+b*y+c*z formula. But you need to find a, b and c coefficients first. It's actually very easy to do. As you have 3 vertices, you have system of linear equations. Something like:
a*x1+b*y1+c*z1=u1
a*x2+b*y2+c*z2=u2
a*x3+b*y3+c*z3=u3
You should solve it and find a, b and c. Via finding inverse matrix for example.
4) Use u/v=a*x+b*y+c*z to find u/v values for all other points. Please note, that u/v value you'd get can be outside of initial 0..(Width - 1) or 0..(Height - 1) interval. It's up to you, how to solve this problem. You can fill such areas by some background color. Or just repeat texture via simple u=u mod Width formula.

For 2D case things are even easier:
a*x1+b*y1=u1
a*x2+b*y2=u2
That is much easier to solve.
« Last Edit: March 28, 2025, 09:13:14 pm by Mr.Madguy »
Is it healthy for project not to have regular stable releases?
Just for fun: Code::Blocks, GCC 13 and DOS - is it possible?

Arend041

  • New Member
  • *
  • Posts: 27
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #10 on: March 29, 2025, 10:00:12 am »
LS,
have a look at Marek Mauder's 'Vampyre Imaging Library' which has ready-made functions in file Imaging.pas to rotate or mirror an image.

https://github.com/galfar/imaginglib

Ally

  • Jr. Member
  • **
  • Posts: 82
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #11 on: March 29, 2025, 10:13:46 am »
Here is another example that rotates by 90/180/270°, but also by any angle.
When rotating by any angle, the image content is interpolated.
And the transparency is retained.

The core of the whole thing is the unit rhsBitmapRotate.pas, which I have built into the enclosed test program.

Nimbus

  • Jr. Member
  • **
  • Posts: 85
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #12 on: March 29, 2025, 11:56:50 am »
There's one example on the wiki
https://wiki.freepascal.org/fcl-image#Rotating_a_bitmap_image_by_an_arbitrary_angle

It's for fcl-image, still can easily be adapted to LCL though.

Aruna

  • Hero Member
  • *****
  • Posts: 790
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #13 on: March 30, 2025, 12:45:23 pm »

The fastest by my knowledge for 8-bit images:https://www.stack.nl/~marcov/rot8x8.txt
Thank you @marcov

Aruna

  • Hero Member
  • *****
  • Posts: 790
Re: How to Rotate a TImage Programmatically in Lazarus IDE?
« Reply #14 on: March 30, 2025, 12:45:50 pm »
In case it is about the learning process on how to rotate then you could perhaps have a look here.
Thank you @TRon.

 

TinyPortal © 2005-2018