Recent

Author Topic: How to Rotate the Image in 90 and 180 degree?  (Read 14098 times)

Dann

  • New Member
  • *
  • Posts: 21
How to Rotate the Image in 90 and 180 degree?
« on: May 05, 2012, 03:12:36 pm »
Hi Everyone:

how to rotate an image(.Jpg or .bmp) in Lazarus in Linux platform.
For example the image was placed in the Timage. by clicking the button how we can rotate the image in 90 degree and 180 degree?
Please post the solution for Linux Platform.

Thanks in advance!

theo

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 1927

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #2 on: May 05, 2012, 03:56:50 pm »
I did this in Delphi for 8-bit images.

http://stackoverflow.com/questions/848025/rotating-bitmaps-in-code

I'm working on a generic implementation, but FPC can't compile these yet.

Delphi can, but due to bugs still needs several worrkarounds.

lainz

  • Guest
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #3 on: May 05, 2012, 09:00:13 pm »
Hi Everyone:

how to rotate an image(.Jpg or .bmp) in Lazarus in Linux platform.
For example the image was placed in the Timage. by clicking the button how we can rotate the image in 90 degree and 180 degree?
Please post the solution for Linux Platform.

Thanks in advance!

I recommend you to use BGRABitmap, really only if you want.

In BGRAControls there is a component called TBGRASpriteAnimation, and just changing a property you can rotate as you wish.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #4 on: May 05, 2012, 11:58:25 pm »
I recommend you to use BGRABitmap, really only if you want.

In BGRAControls there is a component called TBGRASpriteAnimation, and just changing a property you can rotate as you wish.

Using a general rotation component for a 90/180/270 is slower and potentially lossy.

lainz

  • Guest
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #5 on: May 06, 2012, 12:31:18 am »
I recommend you to use BGRABitmap, really only if you want.

In BGRAControls there is a component called TBGRASpriteAnimation, and just changing a property you can rotate as you wish.

Using a general rotation component for a 90/180/270 is slower and potentially lossy.

You're right my component is very slow.

But try with BGRABitmap directly with bitmap.RotateCW, bitmap.RotateCCW, there are also other methods with bgra I don't know.
btw if the only thing he needs is rotation and you've provided the solution there is no reason to download and install bgra.

DJMaster

  • New Member
  • *
  • Posts: 44
    • DJMaster on GitHub
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #6 on: July 11, 2020, 12:46:03 pm »
The following routines perform quick Bitmap clockwise rotations using scanlines.

I don'w know if the code can be used under Linux, anyway it would be nice to include this code somewhere into Lazarus as standard goodies.

Code: Pascal  [Select][+][-]
  1. uses
  2.   lcltype;
  3.  
  4. procedure Rotate90(Bitmap: TBitmap);
  5. type
  6.   TRGBArray = array[0..0] of TRGBTriple;
  7.   pRGBArray = ^TRGBArray;
  8. var
  9.   oldRows, oldColumns: integer;
  10.   rowIn, rowOut: pRGBArray;
  11.   tmpBitmap: TBitmap;
  12. begin
  13.   tmpBitmap := TBitmap.Create;
  14.  
  15.   tmpBitmap.Width := Bitmap.Height;
  16.   tmpBitmap.Height := Bitmap.Width;
  17.   tmpBitmap.PixelFormat := Bitmap.PixelFormat;
  18.  
  19.   for oldColumns := 0 to Bitmap.Width - 1 do
  20.   begin
  21.     rowOut := tmpBitmap.ScanLine[oldColumns];
  22.  
  23.     for oldRows := 0 to Bitmap.Height - 1 do
  24.     begin
  25.       rowIn := Bitmap.ScanLine[oldRows];
  26.       rowOut[Bitmap.Height - oldRows - 1] := rowIn[oldColumns];
  27.     end;
  28.   end;
  29.  
  30.   Bitmap.assign(tmpBitmap);
  31.   tmpBitmap.free;
  32. end;
  33.  
  34. procedure Rotate180(Bitmap: TBitmap);
  35. type
  36.   TRGBArray = array[0..0] of TRGBTriple;
  37.   pRGBArray = ^TRGBArray;
  38. var
  39.   countRows, countColumns: integer;
  40.   rowIn,rowOut: pRGBArray;
  41.   tmpBitmap: TBitmap;
  42. begin
  43.   tmpBitmap := TBitmap.Create;
  44.  
  45.   tmpBitmap.Width := Bitmap.Width;
  46.   tmpBitmap.Height := Bitmap.Height;
  47.   tmpBitmap.PixelFormat := Bitmap.PixelFormat;
  48.  
  49.   for countRows := 0 to Bitmap.Height - 1 do
  50.   begin
  51.     rowIn := Bitmap.ScanLine[countRows];
  52.     rowOut := tmpBitmap.ScanLine[Bitmap.Height - countRows - 1];
  53.  
  54.     for countColumns := 0 to Bitmap.Width - 1 do
  55.       rowOut[Bitmap.Width - countColumns - 1] := rowIn[countColumns];
  56.   end;
  57.  
  58.   Bitmap.assign(tmpBitmap);
  59.   tmpBitmap.free;
  60. end;
  61.  
  62. procedure Rotate270(Bitmap: TBitmap);
  63. type
  64.   TRGBArray = array[0..0] of TRGBTriple;
  65.   pRGBArray = ^TRGBArray;
  66. var
  67.   oldRows, oldColumns: integer;
  68.   rowIn, rowOut: pRGBArray;
  69.   tmpBitmap: TBitmap;
  70. begin
  71.   tmpBitmap := TBitmap.Create;
  72.  
  73.   tmpBitmap.Width := Bitmap.Height;
  74.   tmpBitmap.Height := Bitmap.Width;
  75.   tmpBitmap.PixelFormat := Bitmap.PixelFormat;
  76.  
  77.   for oldColumns := 0 to Bitmap.Width - 1 do
  78.   begin
  79.     rowOut := tmpBitmap.ScanLine[oldColumns];
  80.  
  81.     for oldRows := 0 to Bitmap.Height - 1 do
  82.     begin
  83.       rowIn := Bitmap.ScanLine[oldRows];
  84.       rowOut[oldRows] := rowIn[Bitmap.Width - oldColumns - 1];
  85.     end;
  86.   end;
  87.  
  88.   Bitmap.assign(tmpBitmap);
  89.   tmpBitmap.free;
  90. end;
  91.  

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #7 on: July 11, 2020, 02:27:42 pm »
I have some code hanging around here that does real image rotation at the bit level 0.360 degress etc..

 One of the issues is that a destination bitmap needs to be created larger than the source bitmap in many cases to account for the image being chopped off on the corners..

 taking two points on the grid and rotating you need to calculate the distance between those two points to achieve the bitmap size required.

 All of this can be recalculated before the start of the process of rotating the image.

 So the destination image will most likely be of a different size when completed.
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11382
  • FPC developer.
Re: How to Rotate the Image in 90 and 180 degree?
« Reply #8 on: July 11, 2020, 02:57:30 pm »
I meanwhile did what I said I planned in the 2012 link, I translated the 8-bit greyscale code to assembler. Only SSE2/3, I also did an attempt at AVX2, but aborted after it didn't really seem to have potential to be faster than SSE2.

I also have a generic somewhere, maybe translate that to a generic function.

« Last Edit: July 11, 2020, 03:03:09 pm by marcov »

 

TinyPortal © 2005-2018