How to Rotate a TImage Programmatically in Lazarus IDE? lazarus / 21-General ---------------------------------------------------------------------- Aruna · 2025-03-28 13:54 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 · 2025-03-28 14:15 The fastest by my knowledge for 8-bit images:https://www.stack.nl/~marcov/rot8x8.txt ---------------------------------------------------------------------- TRon · 2025-03-28 14:20 In case it is about the learning process on how to rotate then you could perhaps have a look here. ---------------------------------------------------------------------- marcov · 2025-03-28 14:27 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 · 2025-03-28 14:48 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). ---------------------------------------------------------------------- Hartmut · 2025-03-28 17:18 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 · 2025-03-28 17:31 Quote from: Hartmut on March 28, 2025, 06:18:47 pmIf 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 · 2025-03-28 19:32 It's always better to use hardware acceleration in such cases, like OpenGL for example, because per-pixel operations are slow. ---------------------------------------------------------------------- marcov · 2025-03-28 19:34 Quote from: Mr.Madguy on March 28, 2025, 08:32:48 pmIt'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 · 2025-03-28 19:59 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. ---------------------------------------------------------------------- Arend041 · 2025-03-29 09:00 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 · 2025-03-29 09:13 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. ---------------------------------------------------------------------- olatov · 2025-03-29 10:56 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 · 2025-03-30 10:45 Quote from: marcov on March 28, 2025, 03:15:46 pm The fastest by my knowledge for 8-bit images:https://www.stack.nl/~marcov/rot8x8.txt Thank you @marcov ---------------------------------------------------------------------- Aruna · 2025-03-30 10:45 Quote from: TRon on March 28, 2025, 03:20:48 pmIn case it is about the learning process on how to rotate then you could perhaps have a look here. Thank you @TRon. ---------------------------------------------------------------------- Aruna · 2025-03-30 10:47 Quote from: Hartmut on March 28, 2025, 06:18:47 pmIf 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. Thank you @Hartmut. ---------------------------------------------------------------------- Aruna · 2025-03-30 10:48 Quote from: Arend041 on March 29, 2025, 10:00:12 amLS, 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 @Arend041 thank you for sharing. ---------------------------------------------------------------------- Aruna · 2025-03-30 10:49 Quote from: Ally on March 29, 2025, 10:13:46 amHere 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. @Ally, thank you. ---------------------------------------------------------------------- Aruna · 2025-03-30 11:27 Quote from: Nimbus on March 29, 2025, 11:56:50 amThere'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. @Nimbus thank you very much. This looks very interesting. I think I will go with this and write my own. I will keep everyone posted. ---------------------------------------------------------------------- Aruna · 2025-03-31 02:50 Hello Everyone, I am sharing my first attempt at rotating an image using FPImage. It took a while to figure things out and now we have 90/180/270/360-degree clockwise rotation. A big thank you to everyone who offered advice shared helpful links, and provided support along the way — every bit truly made a difference. YOu can load a image, rotate then save to a file. I have attached a zip file containing the full source code. I hope you enjoy using this as much as I did putting it together! Aruna