Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / MFC
Article

Two Pass Scaling using Filters

Rate me:
Please Sign up or sign in to vote.
4.94/5 (23 votes)
11 Dec 1999 572.4K   2.8K   64   28
A smart way of scaling bitmaps
  • Download source files - 4 Kb

    Sample Image - 2_pass_scaling.jpg

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

    It takes a buffer of RGB values (as COLORREFs) and creates another buffer with new dimensions. The input / output buffers are sequential pixels (not compressed) compatible with the format used in 24-bit DIBs.

    The template is instantiated with a specific filter. The filter determines the quality of the output image. Different basic filters are supplied with this template and additional filters can be easily added.

    Major features:

    • Provides professional quality image scaling.
    • Code is optimized for image quality, not speed.
    • Supports various image filters:
      • Box filter.
      • Bilinear filter.
      • Gaussian filter.
      • Hamming filter.
      • Blackman filter.
      • ...New filters can be easily added.
    • Supports both magnification and minification.
    • Does not force aspect ratio limitations. e.g. an image can be magnified horizontally and minified vertically.
    • Supports 24-bit images. With little change can support other image formats / depths.
    • Template based - no need for libraries, DLLs etc. No linkage problems.

    How to use the scaling template:

    Assuming you have a non-compressed true-color 24-bit DIB in memory (the bits array is pointed by m_pBits), where the original bitmap width is m_dwWidth and height is m_dwHeight.

    Your code should look something like this:

    #include <2PassScale.h>
    ...
    
    void CMyDIB::ScaleTo (DWORD dwNewWidth, DWORD dwNewHeight)
    {
    	C2PassScale <CBilinearFilter> ScaleEngine;
    	COLORREF *pOldBitmap = m_pBits;
    	m_pBits = ScaleEngine.AllocAndScale(m_pBits,
    	                                    m_dwWidth,
    	                                    m_dwHeight,
    	                                    dwNewWidth,
    	                                    dwNewHeight
    	                                    );
    	if (NULL == m_pBits)
    	{
    		//
    		// Handle errors here
    		//
    	}
    	m_dwWidth = dwNewWidth;
    	m_dwHeight = dwNewHeight;
    	delete pOldBitmap;
    }
    
    // Alternatively, if you already have a pre-allocated destination buffer
    // in the new size you can call ScaleEngine.Scale (...) and give it that buffer
  • 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


    Written By
    United States United States
    This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

    Comments and Discussions

     
    GeneralSo close to what I needed Pin
    Waldermort11-Sep-06 8:54
    Waldermort11-Sep-06 8:54 
    GeneralRe: So close to what I needed Pin
    Teashirt213-Sep-07 5:52
    Teashirt213-Sep-07 5:52 
    GeneralRe: So close to what I needed Pin
    Waldermort13-Sep-07 6:01
    Waldermort13-Sep-07 6:01 
    Wow, it's been a long time since I worked on this project.

    I did try out the scaling and rotations in Photoimpact and noticed the 'undesired' behaviour also. When I worked on this project it was a neccessity to scan for an exact coloref and not a range. As it happens, I modified the class slightly to leave that color untouched. In most cases this would cause anti-aliasing problems and leave jagged edges, but for what I was working on it was not a problem.

    Waldermort

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

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.