Recent

Author Topic: [solved] rotate image problem  (Read 298 times)

speter

  • Hero Member
  • *****
  • Posts: 533
[solved] rotate image problem
« on: June 09, 2026, 05:34:29 am »
I have written a program (attached) that rotates a 100x100px image; but the results are strange!

The image is of a red "racing car", enclosed in a blue circle. The program creates 35 rotated images (for angles 10..350 degrees); each rotated image is in an array; the images are visible and placed in a 6x6 grid (with the zero'th image being the initial version).

If you look at the attached image of the app some of the cars are cropped on the right and some are cropped on the left; BUT, the circle isn't cropped. I could see a reason for the image being strangely cropped, but I can't work out why only the car bits are being cropped.

I believe the issue is with my program's code, not with the LCL; but I can't work out what the error is.

Any comments / suggestions would be very welcome.

cheers
S.
« Last Edit: June 09, 2026, 10:02:18 am by speter »
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

paweld

  • Hero Member
  • *****
  • Posts: 1685
Re: rotate image problem
« Reply #1 on: June 09, 2026, 07:03:20 am »
Try BGRABitmap - it's fast and easy.
Code: Pascal  [Select][+][-]
  1. uses
  2.   BGRABitmap, BGRABitmapTypes, BGRAPath,
  3.   BGRATransform;
  4.  
  5. //--------------------------------------------------------------------
  6. procedure TForm_main.makeracer;
  7. //--------------------------------------------------------------------
  8. var
  9.   a: Integer;
  10.   ang: Single;
  11.   bmp, bmptmp: TBGRABitmap;
  12.   ms: TMemoryStream;
  13.   bmptranform: TBGRAAffineBitmapTransform;
  14. begin
  15.   // create array of 36 racing cars :)
  16.   racer[0] := image1;
  17.   racer[0].parent := form_main;
  18.   racer[0].setbounds(0, 0, 99, 99);
  19.   racer[0].Visible := True;
  20.   racer[0].top := 0;
  21.   racer[0].left := 0;
  22.  
  23.   bmp := TBGRABitmap.Create(Image1.Picture.Bitmap);
  24.  
  25.   for a := 1 to 35 do
  26.   begin
  27.     application.ProcessMessages;
  28.  
  29.     racer[a] := TImage.Create(form_main);
  30.     racer[a].parent := form_main;
  31.     racer[a].setbounds(0, 0, 100, 100);
  32.  
  33.     racer[a].Visible := True;
  34.     racer[a].top := 100 * (a div 6);
  35.     racer[a].left := 100 * (a mod 6);
  36.  
  37.     ang := a * 10;
  38.     //rotate image
  39.     bmptmp := TBGRABitmap.Create(bmp.Width, bmp.Height, BGRAPixelTransparent);
  40.     bmptranform := TBGRAAffineBitmapTransform.Create(bmp);
  41.     bmptranform.Translate(-bmp.Width div 2, -bmp.Height div 2);
  42.     bmptranform.RotateDeg(ang);
  43.     bmptranform.Translate(bmp.Width div 2, bmp.Height div 2);
  44.     bmptmp.Fill(bmptranform);
  45.     bmptranform.Free;
  46.     ms := TMemoryStream.Create;
  47.     bmptmp.SaveToStreamAsPng(ms);
  48.     ms.Position := 0;
  49.     racer[a].Picture.LoadFromStream(ms);
  50.     ms.Free;
  51.     bmptmp.Free;
  52.     //
  53.   end;
  54.   bmp.Free;
  55. end;
Best regards / Pozdrawiam
paweld

speter

  • Hero Member
  • *****
  • Posts: 533
Re: rotate image problem
« Reply #2 on: June 09, 2026, 07:33:02 am »
Thanks paweld, I know BGRABitmap has a rotate bitmap function.

I am not wanting to use any libraries though.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

Ally

  • Jr. Member
  • **
  • Posts: 83
Re: rotate image problem
« Reply #3 on: June 09, 2026, 08:53:58 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.

rvk

  • Hero Member
  • *****
  • Posts: 7063
Re: rotate image problem
« Reply #4 on: June 09, 2026, 08:54:13 am »
In your double loop you check image1.Canvas.pixels[x,y] for non transparency.
But your image1 doesn't have x 99 and y 99.
It is probably less width than height.

Change the original image1 to have width 100 and height 100 and it'll probably work better.
(Haven't tested this because I'm on a mobile)

LeP

  • Sr. Member
  • ****
  • Posts: 441
Re: rotate image problem
« Reply #5 on: June 09, 2026, 09:03:00 am »
It's simply because where you test the transparency, you don't rotate it but set that pixel as transparent.
YOU MUST ALSO ROTATE THE TRANSPARENT.

Code: Pascal  [Select][+][-]
  1.  //if image1.Canvas.pixels[x,y] <> transparent then
  2.             begin
  3.               p := rotate(x,y,ang);
  4.               racer[a].canvas.pixels[p.x,p.y] := image1.Canvas.pixels[x,y];
  5.             end;
  6.           //else
  7.           //  racer[a].canvas.pixels[x,y] := transparent;
« Last Edit: June 09, 2026, 09:06:10 am by LeP »
Un Sistema per domarli, un IDE per trovarli, un codice per ghermirli e nel framework incatenarli.
An operating system to tame them, an IDE to find them, a code to catch them and in the framework chain them.

speter

  • Hero Member
  • *****
  • Posts: 533
Re: rotate image problem
« Reply #6 on: June 09, 2026, 09:59:14 am »
It's simply because where you test the transparency, you don't rotate it but set that pixel as transparent.
YOU MUST ALSO ROTATE THE TRANSPARENT.

Code: Pascal  [Select][+][-]
  1.  //if image1.Canvas.pixels[x,y] <> transparent then
  2.             begin
  3.               p := rotate(x,y,ang);
  4.               racer[a].canvas.pixels[p.x,p.y] := image1.Canvas.pixels[x,y];
  5.             end;
  6.           //else
  7.           //  racer[a].canvas.pixels[x,y] := transparent;
Thanks very much LeP.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

speter

  • Hero Member
  • *****
  • Posts: 533
Re: [solved] rotate image problem
« Reply #7 on: June 09, 2026, 10:03:42 am »
And thanks also, to rvk and Ally.

cheers
S.
I climbed mighty mountains, and saw that they were actually tiny foothills. :)

 

TinyPortal © 2005-2018