Mega Code Archive

 
Categories / Delphi / Graphic
 

Rotate a bitmap by any angle

Title: Rotate a bitmap by any angle iRotationAxis, jRotationAxis is the center of rotation. You can speed this up a lot by creating an array of scanlines for the input and output bitmaps before calling the procedure, then replace the Scanline calls in the procedure with an assignment to the array entry. [delphi]type TRGBTripleArray = ARRAY[0..MaxPixelCount-1] OF TRGBTriple; pRGBTripleArray = ^TRGBTripleArray; procedure RotateBitmap(CONST BitmapOriginal: TBitmap; VAR BitmapOut : TBitmap; CONST iRotationAxis, jRotationAxis: INTEGER; CONST AngleOfRotation: DOUBLE {radians} ); VAR cosTheta : EXTENDED; i : INTEGER; iOriginal : INTEGER; iPrime : INTEGER; j : INTEGER; jOriginal : INTEGER; jPrime : INTEGER; RowOriginal: pRGBTripleArray; RowRotated : pRGBTRipleArray; sinTheta : EXTENDED; BEGIN // Get SIN and COS in single call from math library sincos(AngleOfRotation, sinTheta, cosTheta); // Step through each row of rotated image. FOR j := BitmapOut.Height-1 DOWNTO 0 DO BEGIN RowRotated := BitmapOut.Scanline[j]; jPrime := j - jRotationAxis; FOR i := BitmapOut.Width-1 DOWNTO 0 DO BEGIN iPrime := i - iRotationAxis; iOriginal := iRotationAxis + ROUND(iPrime * CosTheta - jPrime * sinTheta); jOriginal := jRotationAxis + ROUND(iPrime * sinTheta + jPrime * cosTheta); // Make sure (iOriginal, jOriginal) is in BitmapOriginal. If not, // assign blue color to corner points. IF (iOriginal = 0) AND (iOriginal = 0) AND (jOriginal