Fast and high quality true-color bitmap rotation function |
The code is Pentium MMX optimized implementation of bitmap rotation by means of 3 shears.
This popular algorithm was proposed by Alan Paeth (Alan Paeth, "A Fast Algorithm for General Raster Rotation," Graphics Interface '86, pp. 77-81, May 1986.) For online explanation see Tobin Fricke notes. The software implementation I came across on codeproject.com belongs to Eran Yariv.
This implementation
My implementation is about 6 times faster (tested on Athlon 1.4) and more flexible.
Speed is achived by:
Flexibility is achived by using design policies, inspired by Andrei Alexandrescu.
Class ImageRotate is parameterized by callback type, Allocator and Alignment policies.
template<
typename ProgressAnbAbortCallBack = CallbackFn,
class AllocatorPolicy = AllocatePolicyStdNew,
class AlignmentPolicy = AlignmentPolicyBmp
>
class ImageRotate;
Source and destination images are described by
class ImageRotate::Img | |||||||||||
Pixel * | pixel | struct { unsigned __int8 c[4]; } | unsigned | width | in Pixels | unsigned | height | in Pixels | unsigned | width_pad | in bytes |
Public interface of ImageRotate consists from single function
static Img AllocAndRotate (
const Img &src,
Pixel clrBack, // Background color
double angle, // Rotation angle
ProgressAnbAbortCallBack *cb = 0);
static Img AllocAndRotate ( const Img &src, Pixel clrBack, // Background color double angle, // Rotation angle ProgressAnbAbortCallBack *cb = 0); Callback type should support
bool operator() (double)
bool operator() (double) and may not raise exceptions. It could be function pointer (default) or functor class (e.g. Loki::Functor). Function gets double value of progress percentage in 0-100 range and returns false to abort calculation or true otherwise.
AllocatorPolicy should provide 2 methods w/o exceptions raising:
void* allocate(size_t);
void free(void *t);
void* allocate(size_t); void free(void *t); It could be "C" malloc/free (default) or custom memory manager.
AlignmentPolicy should provide 1 method w/o exception raising:
unsigned aligned_width(unsigned width);
unsigned aligned_width(unsigned width); By default, it implements Windows BMP 4 byte alignment.
Source and rotated images are kept in ImageRotate::Img classes. Client is responsible to delete returned rotated image pixel array, using image.Free();
References | ||||||||||||||||||||
1 | Alan Paeth | A Fast Algorithm for General Raster Rotation, Graphics Interface '86, pp. 77-81, May 1986. | 2 | Tobin Fricke | Rotation by Shearing | 3 | Eran Yariv | High quality image rotation (rotate by shear) | 4 | Intel Corporation | Using MMX™ Instructions to Implement Alpha Blending | 5 | Todd Veldhuizen | Techniques for Scientific C++ | 6 | Andrei Alexandrescu | Modern C++ Design | 7 | SourceFORGE.net | Project: Loki |
Copyright © 2003 Anton Treskunov
Permission to copy, use, modify, sell and distribute this document
is granted provided this copyright notice appears in all copies. This document
is provided "as is" without express or implied warranty, and with
no claim as to its suitability for any purpose.