Board index » delphi » How to optimize rotation of a bitmap
Enquiring Mind
Delphi Developer |
How to optimize rotation of a bitmap2007-05-30 11:13:12 PM delphi268 Hi, Below is my version of a procedure to rotate a bitmap through 90 degrees. It works, but is slow compared to a similar function in the OS, and the code is unsafe by .Net standards, because of lack of range checking on the Scanline data. On my computer it takes about 3.2 sec to rotate a 2592x1944 bitmap (including up-front processing). Can anyone suggest any ways to improve speed, and/or safety? Is there a better interface to the bitmap data than Scanline available? procedure GetRotatedBitmap90deg(InputBitmap: TBitmap; var OutputBitmap: TBitmap); type TByteArrayFixed= array[0..High(ShortInt)] of Byte; TByteArrayPtr= ^TByteArrayFixed; var PixelByteCount: integer; InputWidth, InputHeight, OutputWidth, OutputHeight: integer; Offsets: TIntegerArray; RowIndex, ColIndex, RowIndex1, ColIndex1: integer; OutputRowPtr, InputRowPtr: TByteArrayPtr; begin SetPixelByteCount(InputBitmap, PixelByteCount); {Determine output bitmap dimensions:} InputWidth:= InputBitmap.Width; InputHeight:= InputBitmap.Height; OutputWidth:= InputHeight; OutputHeight:= InputWidth; {Create output bitmap:} if (OutputBitmap<>nil) and (OutputBitmap<>InputBitmap) then OutputBitmap.Free; OutputBitmap:= TBitmap.Create; OutputBitmap.Width:= OutputWidth; OutputBitmap.Height:= OutputHeight; OutputBitmap.PixelFormat:= InputBitmap.PixelFormat; {Generate offset table:} CreateOffsetLookupTable(InputWidth, InputHeight, PixelByteCount, Offsets); {Generate output bitmap:} for RowIndex:= 0 to OutputHeight-1 do begin OutputRowPtr:= OutputBitmap.ScanLine[RowIndex]; ColIndex1:= RowIndex; for ColIndex:= 0 to OutputWidth-1 do begin {Transform output pixel indeces to input pixel indeces:} RowIndex1:= InputHeight - ColIndex - 1; {Copy pixel data from input bitmap to output bitmap:} InputRowPtr:= InputBitmap.Scanline[RowIndex1]; Move(InputRowPtr^[Offsets[ColIndex1]], OutputRowPtr^[Offsets[ColIndex]], PixelByteCount); end; end; end; Thanks for any suggestions. Enquiring Mind |