Click here to Skip to main content
6,292,426 members and growing! (9,346 online)
Email Password   helpLost your password?
Multimedia » General Graphics » General     Intermediate

Two Pass Scaling using Filters

By Eran Yariv

A smart way of scaling bitmaps
VC6, Visual Studio, MFC, Dev
Posted:11 Dec 1999
Views:120,932
Bookmarked:39 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
36 votes for this article.
Popularity: 6.96 Rating: 4.47 out of 5

1
2 votes, 11.8%
2

3
2 votes, 11.8%
4
13 votes, 76.5%
5
  • 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

    About the Author

    Eran Yariv


    Member

    Location: United States United States

    Other popular General Graphics articles:

    • A flexible charting library for .NET
      Looking for a way to draw 2D line graphs with C#? Here's yet another charting class library with a high degree of configurability, that is also easy to use.
    • CxImage
      CxImage is a C++ class to load, save, display, transform BMP, JPEG, GIF, PNG, TIFF, MNG, ICO, PCX, TGA, WMF, WBMP, JBG, J2K images.
    • 3D Pie Chart
      A class library for drawing 3D pie charts.
    • Really cool visual FX
      A set of classes for doing stunning visual effects, including water, plasma and fire.
    • ImageStone
      An article on a library for image manipulation.
    Article Top
    You must Sign In to use this message board.
    FAQ FAQ 
     
    Noise Tolerance  Layout  Per page   
     Msgs 1 to 25 of 27 (Total in Forum: 27) (Refresh)FirstPrevNext
    Question16-bit version? Pinmemberkennygoers6:13 8 Feb '07  
    GeneralSo close to what I needed Pinmemberwaldermort9:54 11 Sep '06  
    GeneralRe: So close to what I needed PinmemberTeashirt26:52 13 Sep '07  
    GeneralRe: So close to what I needed PinmemberWalderMort7:01 13 Sep '07  
    Generalinteger division of 1 by 2 PinmemberKummerSurfer8:53 26 Feb '06  
    GeneralHere is HUGE speed improvement PinmemberJake Montgomery12:48 17 Jan '05  
    GeneralRe: Here is HUGE speed improvement PinmemberArlen Albert17:24 16 Mar '05  
    GeneralRe: Here is HUGE speed improvement PinmemberJake Montgomery9:04 17 Mar '05  
    GeneralRe: Here is HUGE speed improvement PinmemberArlen Albert5:14 18 Mar '05  
    GeneralRe: Here is HUGE speed improvement PinmemberGautam Jain8:34 13 May '06  
    GeneralRe: Here is HUGE speed improvement PinmemberJake Montgomery10:20 10 Jul '06  
    GeneralRe: Here is HUGE speed improvement Pinmembera.mulay3:38 5 Oct '06  
    GeneralRe: Here is HUGE speed improvement PinmemberDKScherpy8:19 6 Nov '06  
    GeneralRe: Here is HUGE speed improvement Pinmembera.mulay1:44 7 Nov '06  
    Generalwhere is my mistake? Pinmember鄂人14:55 6 Dec '04  
    Generaldo u send theory of those filters to me? PinmemberZita17:14 7 Jan '04  
    GeneralAwesome!! PinmemberWREY20:47 18 Nov '03  
    GeneralAlgorithm? PinmemberCohen2:04 17 Jan '03  
    GeneralRe: Algorithm? PinmemberGuy Bonneau4:24 18 Feb '04  
    GeneralRe: Algorithm? PinmemberGuy Bonneau12:20 18 Feb '04  
    GeneralFaster but less "accurate" version? PinmemberJeremy Davis1:24 18 Apr '02  
    GeneralRe: Faster but less "accurate" version? Pinsupporterpeterchen23:51 29 Nov '03  
    GeneralHow to use scailing with grayscale bitmap PinmemberSun Satis20:44 21 Jan '02  
    Generalsample code??? Pinmemberobjects16:54 30 Mar '01  
    GeneralError in code PinsussAndrei Stcherbatchenko21:23 7 Aug '00  

    General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

    PermaLink | Privacy | Terms of Use
    Last Updated: 11 Dec 1999
    Editor: Chris Maunder
    Copyright 1999 by Eran Yariv
    Everything else Copyright © CodeProject, 1999-2009
    Web11 | Advertise on the Code Project