Click here to Skip to main content
6,630,586 members and growing! (17,701 online)
Email Password   helpLost your password?
Multimedia » General Graphics » Bitmaps     Intermediate

Add fast user-extensible image processing support to CBitmap

By .dan.g.

Provides built-in graying, rotating, shearing, resizing, blurring, sharpening, flipping, negating and color replacement to CBitmap as well as support for user-defined processing plug-ins
VC6Win2K, MFC, Dev
Posted:14 Dec 2002
Views:162,521
Bookmarked:99 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
48 votes for this article.
Popularity: 8.16 Rating: 4.86 out of 5
1 vote, 2.1%
1

2

3
6 votes, 12.5%
4
41 votes, 85.4%
5

Sample Image - ExtendedBitmap.jpg

Introduction

This work builds on my previous CEnBitmap class but is sufficiently new to warrant a separate article.

I read Yves Maurer's excellent bitmap rotation article (see Credits section) recently and was prompted to rewrite a bitmap post-processing library I had written some time ago for a proprietary 3D rendering engine.

Since the CEnBitmap class I had previously developed allowed for loading a variety of graphic formats both off disk and via resource ID, I thought it would provide the ideal vehicle for these further extensions.

The code

What I've implemented then is a user-extensible bitmap processing 'library' with some built-in support for graying, rotating, shearing, blurring and resizing a bitmap, which can be used as simply as this:

    :
    :
    CEnBitmap bitmap;
    bitmap.LoadImage("c:\......\image.jpg");

    bitmap.GrayImage();
    bitmap.RotateImage(90); // can use radians also

    bitmap.ResizeImage(0.8);
    :
    :
    pMemDC->SelectObject(&bitmap);
    pDC->BitBlt(...);
    :
    :

To define you own image processing plug-in, just derive a new class from C32BitImageProcessor and override the virtual methods CalcDestSize() and ProcessPixels().

Once you've done this, you simply pass your plug-in to the CEnBitmap object as follows:

    :
    :
    CEnBitmap bitmap;
    :
    :
    bitmap.ProcessImage(&CMyImageProcessor());
    :
    :

For multi-pass processing, you can also pass an array of plug-in pointers to CEnBitmap as follows:

    :
    :
    CEnBitmap bitmap;
    :
    :
    C32BIPArray aProcessors;
    CMyImageProcessor1 p1;
    CMyImageProcessor2 p2;
    CMyImageProcessor3 p3;

    aProcessors.Add(&p1);
    aProcessors.Add(&p2);
    aProcessors.Add(&p3);

    bitmap.ProcessImage(aProcessors);
    :
    :

This has the advantage of only getting and setting the internal HBITMAP bits at the start and end of the operation, and of doing as few memory allocations as possible.

Speed

Try out the 'Spin' button on the sample app to see how quick it is. About 20-30 ms for each iteration on a 200x200 image. (Note: the flipping and shearing plug-ins only take 0-5 ms for a similar sized image).

The key to achieving any degree of real time processing is a good algorithm (see karl Lager's credit) and working on the bitmap at the lowest level (see Yves Maurer's credit).

So all the processing is done on the raw bitmap bits, and not using Set/GetPixel() which would be way too slow.

Ver 1.2

The addition of color weighting adds a small penalty to the processing speed but gives way better results. However, color weighting is managed via a boolean attribute, so its up to you which way you go.

Carrying out multiple processing using an array of processors gives a small improvement, so its worth doing (see the sample app for details).

Sample application

This is a simple app which allows you to load a variety of image formats and apply the built-in plug-ins.

Note: the plug-ins are not necessarily applied in the order you select them but in the order that gives the best results.

Ver 1.2

I've modified the sample app so that the selected plug-ins are also applied when spinning, and can be turned on and off on the fly.

Conditions of use

None.

Except :) that I retain copyright of this original work, notwithstanding the work done by those listed in the credits.

If you write a plug-in of your own then you are free to publish it as your own original work and, if appropriate, I'll add it to the CEnBitmap interface and add a link on this page.

Feedback

I'm happy to receive suggestions for:

  • Other plug-ins which I will attempt :) to implement.
  • Ways to speed up the existing plug-ins
  • Ways to improve the output quality from the existing algorithms.

Versions

  • 1.0 16 Dec 2001
  • 1.1 17 Dec 2001
    • Negating added
    • Flipping added
    • Color replacement added
    • Graying speeded up
  • 1.2 17 Dec 2001
    • Color weighting added (vastly improves quality of rotating and shearing)
    • Sample app improved
  • 1.3 18 Dec 2001
    • Sharpening added
    • Some boundary condition bugs fixed
    • Blurring speeded up
    • Improved user controlled settings added to sample app

Credits

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

.dan.g.


Member
.dan.g. is a former chartered structural engineer from the uk. He's been programming since university and has been developing commercial windows software in Australia since 1998.

For all his latest freeware visit http://www.abstractspoon.com/
Occupation: Software Developer (Senior)
Location: Australia Australia

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.
  • Barcode Image Generation Library
    This library was designed to give an easy class for developers to use when they need to generate barcode images from a string of data.
  • 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 54 (Total in Forum: 54) (Refresh)FirstPrevNext
GeneralDiplaying and processing two images PinmemberMember 360679222:17 25 Jan '09  
GeneralConvert to VS 2005 PinmemberAlexEvans23:01 5 Oct '08  
GeneralJPG rotation in MS PowerPoint is really fast. Pinmemberhujunxi17:17 8 May '08  
GeneralVideo? Pinmembercaptainc/c++20:17 27 Nov '07  
GeneralReally nice work Pinmembermorntide17:48 10 Oct '07  
Generalso GREAT! Pinmemberk8cchaha19:02 6 Sep '07  
Generalhow about c#? Pinmembers.jahangiri3:10 13 Feb '07  
GeneralProblem with Rotation! PinmemberKaren0_01:30 12 Sep '06  
GeneralSaving bitmap to disk file PinmemberAndrew Phillips0:13 2 May '06  
GeneralSmall fix PinmemberAndrew Phillips21:27 1 May '06  
QuestionSupport for tiff files PinmemberMuhammad Azam0:26 30 Mar '06  
AnswerRe: Support for tiff files PinmemberAndrew Phillips21:33 1 May '06  
GeneralWeighted Color Average Pinmemberozantopoglu4:06 22 Feb '06  
QuestionHow to display a PNG? Pinmemberthilon19:05 8 Jan '06  
GeneralPNG support Pinmembervjedlicka4:16 2 Dec '05  
GeneralRe: PNG support Pinmember.dan.g.23:48 3 Dec '05  
AnswerRe: PNG support Pinmembervjedlicka23:26 7 Dec '05  
GeneralCreating from existent bitmap (DIB) Pinmemberfcarello0:03 8 Sep '05  
GeneralRe: Creating from existent bitmap (DIB) PinmemberAndrew Phillips21:38 1 May '06  
Generalloading pictures in Picture box PinmemberKawakami Genzai12:23 25 Aug '05  
GeneralRe: loading pictures in Picture box PinmemberChae Sang, Jung23:09 21 Dec '05  
GeneralHow do i save the resized bitmap Pinmembershikha_dawer20:17 28 Jan '05  
GeneralCongrats, great work!!! PinmemberTomas Danek2:04 14 Nov '04  
GeneralRe: Congrats, great work!!! Pinmember.dan.g.16:09 15 Nov '04  
GeneralRe: Congrats, great work!!! PinmemberTomas Danek10:39 16 Nov '04  

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

PermaLink | Privacy | Terms of Use
Last Updated: 14 Dec 2002
Editor: Smitha Vijayan
Copyright 2002 by .dan.g.
Everything else Copyright © CodeProject, 1999-2009
Web22 | Advertise on the Code Project