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

Two Pass Scaling using Filters

, 11 Dec 1999
Rate this:
Please Sign up or sign in to vote.
A smart way of scaling bitmaps
  • Download source files - 4 Kb

    Sample Image - 2_pass_scaling.jpg

    <!-- Article Starts -->

    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

    About the Author

    Eran Yariv

    United States United States
    No Biography provided

    Comments and Discussions

     
    Question16-bit version? Pinmemberkennygoers8-Feb-07 5:13 
    GeneralSo close to what I needed Pinmemberwaldermort11-Sep-06 8:54 
    GeneralRe: So close to what I needed PinmemberTeashirt213-Sep-07 5:52 
    GeneralRe: So close to what I needed PinmemberWalderMort13-Sep-07 6:01 
    Generalinteger division of 1 by 2 PinmemberKummerSurfer26-Feb-06 7:53 
    GeneralHere is HUGE speed improvement PinmemberJake Montgomery17-Jan-05 11:48 
    GeneralRe: Here is HUGE speed improvement PinmemberArlen Albert16-Mar-05 16:24 
    GeneralRe: Here is HUGE speed improvement PinmemberJake Montgomery17-Mar-05 8:04 
    GeneralRe: Here is HUGE speed improvement PinmemberArlen Albert18-Mar-05 4:14 
    GeneralRe: Here is HUGE speed improvement PinmemberGautam Jain13-May-06 7:34 
    GeneralRe: Here is HUGE speed improvement PinmemberJake Montgomery10-Jul-06 9:20 
    I believe that dibSrc is only used in the example code in the comments, therefore it should be understood to be incomplete. dibSrc in this case is not a class, but an instance of an image class that is not defined in this project. There is a comment on the WidthBytes that you removed: "The width of a single line of the source image in bytes (to allow for padding, etc.)". If this is not enough explanation, then I will try again. "WidthBytes" (uOrigWidthBytes and uNewWidthBytes in the code) is the number of bytes between lines of the image, so if line 0 starts at address 0x1000, and line 1 starts at 0x1321, then "WidthBytes" would be 0x0321. If your image is tightly packed, with no memory gap between lines, the WidthBytes is simply the "width in pixels" * "size of a single pixel in bytes". Hope this helps.

    GeneralRe: Here is HUGE speed improvement Pinmembera.mulay5-Oct-06 2:38 
    GeneralRe: Here is HUGE speed improvement PinmemberDKScherpy6-Nov-06 7:19 
    GeneralRe: Here is HUGE speed improvement Pinmembera.mulay7-Nov-06 0:44 
    GeneralBugs for 24 bit. Pinmemberguju4-May-11 3:43 
    Questionwhere is my mistake? Pinmember鄂人6-Dec-04 13:55 
    Questiondo u send theory of those filters to me? PinmemberZita7-Jan-04 16:14 
    GeneralAwesome!! PinmemberWREY18-Nov-03 19:47 
    QuestionAlgorithm? PinmemberCohen17-Jan-03 1:04 
    AnswerRe: Algorithm? PinmemberGuy Bonneau18-Feb-04 3:24 
    GeneralRe: Algorithm? PinmemberGuy Bonneau18-Feb-04 11:20 
    QuestionFaster but less "accurate" version? PinmemberJeremy Davis18-Apr-02 0:24 
    AnswerRe: Faster but less &quot;accurate&quot; version? Pinmemberpeterchen29-Nov-03 22:51 
    QuestionHow to use scailing with grayscale bitmap PinmemberSun Satis21-Jan-02 19:44 
    Questionsample code??? Pinmemberobjects30-Mar-01 15:54 

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

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

    | Advertise | Privacy | Mobile
    Web03 | 2.8.140721.1 | Last Updated 12 Dec 1999
    Article Copyright 1999 by Eran Yariv
    Everything else Copyright © CodeProject, 1999-2014
    Terms of Service
    Layout: fixed | fluid