Click here to Skip to main content
Click here to Skip to main content

High quality image rotation (rotate by shear)

By , 9 Jan 2000
 
  • Download demo project - 21 Kb
  • Download source files - 4 Kb

    Sample Image - RotateByShear.gif

    Unlike traditional rotation of images, where every n'th pixel is sampled and copied to the result image, this template provides much more accurate image rotation features (weighing the pixels).

    template is instantiated with a specific pixel format and must be inherited to support pixel access functions for a specific bitmap implementation. This provides abstaction of the rotation mechanism from the actual bitmap representation.

    Major features:

    • ides professional quality image rotation.
    • Code is optimized for image quality, not speed.
    • Rotated image retains size and aspect ratio of source image (destination image size is usally bigger).
    • Supports double precision rotation angles (0..360).
    • Supports generic bitmap structures. Specific bitmap structure is defined by inheritance.
    • Template based - no need for libraries, DLLs etc. No linkage problems.

    How to use the rotation template:

    You must derive you own class from the rotation template.

    For example:

    class CRotateByShearRGB : public CRotateByShear<COLORREF>

    will define a concrete class where each pixel is of COLORREF type.

    Next, you must override 5 abstract functions which support creation, destruction of bitmap buffers and pixel level access to the bitmap buffers.

    Then you instantiate your derived class and call the AllocAndRotate(...) function which allocates a new destination bitmap buffer and rotates the input image into it.

    That's it.

    For further instructions please view the attached sample command-line project (Rotate.cpp). It is a simple command line utility which reads PPM (ASCII or binary) image file into a DIB structure, rotates it and saves it to another PPM image file.

  • License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here

    About the Author

    Eran Yariv
    United States United States
    Member
    No Biography provided

    Sign Up to vote   Poor Excellent
    Add a reason or comment to your vote: x
    Votes of 3 or less require a comment

    Comments and Discussions

     
    You must Sign In to use this message board.
    Search this forum  
        Spacing  Noise  Layout  Per page   
    GeneralMy vote of 1membersujeshsasi5 Mar '09 - 18:57 
    the design of the page is not good ...
    Generalhave bugs in the demomemberWeiHua zhang24 Oct '07 - 18:06 
    if you set back color with white color, destinition bitmap have many black lines in the non-bitmap region.
    Generala show stopper bugmemberXiaotian Guo7 Jul '03 - 20:12 
    I think the code has some serious bug on pixel handling at the edge of source image, to reproduce this, try to set the background color to white, and rotate, and observe: (I did this with the demo program)
     
    1. there are 2 unwanted lines in the rotated image.
    2. the edges of source image in the rotated image are aliased which makes it hardly "high quality"

    GeneralRe: a show stopper bugmemberJim the Bad11 Aug '05 - 6:06 
    Fixed it!
     
    The problem was this:
    Consider a bitmap pixel on the edge of an all-red bitmap drawn on a blue background. Let's say the bitmap is rotated in such a way that the image covers 75% of this pixel. What the original code does is it draws this pixel at 75% red - which is wrong! It should draw it 75% red PLUS 25% blue. Drawing just 75% red is implicitly drawing 75% red plus 25% black, and this explains the black lines around the image.
     
    The fix:
    In the HorizSkew() and VertSkew() functions, change:
     

    COLORREF pxlLeft = RGB (BYTE ( double (GetRValue (pxlSrc)) * dWeight ),
    BYTE ( double (GetGValue (pxlSrc)) * dWeight ),
    BYTE ( double (GetBValue (pxlSrc)) * dWeight ));

     
    to:
     

    COLORREF pxlLeft = RGB (BYTE ( (double (GetRValue (pxlSrc)) * dWeight) + (double(GetRValue(clrBack)) * (1.0 - dWeight)) ),
    BYTE ( (double (GetGValue (pxlSrc)) * dWeight) + (double(GetGValue(clrBack)) * (1.0 - dWeight))),
    BYTE ( (double (GetBValue (pxlSrc)) * dWeight) + (double(GetBValue(clrBack)) * (1.0 - dWeight))));

     


    GeneralRotation of BMPmemberSandeep11_163 Jun '03 - 4:21 
    I have created a control on a Dialog. I have set a BMP on that Control.
    Now I want to rotate that control.
     
    Please tell me how can I do that?
    GeneralSpeed optimized versionmemberantontreskunov24 Apr '03 - 15:03 
    Fast and high quality true-color bitmap rotation function
    GeneralRe: Speed optimized versionmemberyyt_yyt8 Jul '03 - 22:20 
    the source file download link can not use
    can you upload it on codeproject
    GeneralRe: Speed optimized versionsussAnonymous12 Jul '03 - 8:39 
    I just tried the link, it works fine.
    GeneralRe: Speed optimized versionmemberpmidd17 Oct '03 - 9:23 
    The link does not work for me
    GeneralRe: Speed optimized versionmemberantontreskunov21 Oct '03 - 6:55 
    The link is: http://antontreskunov.home.comcast.net/Anton/Software/
    Anton Treskunov

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    Permalink | Advertise | Privacy | Mobile
    Web04 | 2.6.130523.1 | Last Updated 10 Jan 2000
    Article Copyright 2000 by Eran Yariv
    Everything else Copyright © CodeProject, 1999-2013
    Terms of Use
    Layout: fixed | fluid