Fast and high quality true-color bitmap rotation function

The code is Pentium MMX optimized implementation of bitmap rotation by means of 3 shears.

Click here to download.

Sources and Motivation

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

Benefits

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.

Interface

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 *pixelstruct { unsigned __int8 c[4]; }
unsignedwidthin Pixels
unsignedheightin Pixels
unsignedwidth_padin 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
1Alan PaethA Fast Algorithm for General Raster Rotation,
Graphics Interface '86, pp. 77-81, May 1986.
2Tobin Fricke Rotation by Shearing
3Eran Yariv High quality image rotation (rotate by shear)
4Intel Corporation Using MMX™ Instructions to Implement Alpha Blending
5Todd Veldhuizen Techniques for Scientific C++
6Andrei Alexandrescu Modern C++ Design
7SourceFORGE.net Project: Loki