Click here to Skip to main content
15,886,055 members
Articles / Desktop Programming / MFC
Article

Add fast user-extensible image processing support to CBitmap

Rate me:
Please Sign up or sign in to vote.
4.94/5 (52 votes)
14 Dec 20023 min read 286.9K   8.7K   124   54
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

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


Written By
Software Developer Maptek
Australia Australia
.dan.g. is a naturalised Australian and has been developing commercial windows software since 1998.

Comments and Discussions

 
GeneralRe: Nice Pin
CPAVG2-Nov-04 17:44
CPAVG2-Nov-04 17:44 
GeneralRe: Nice Pin
Niklas L26-Nov-04 2:56
Niklas L26-Nov-04 2:56 
GeneralExpose CImageRotator(radians) Pin
GSIJoe12-May-04 5:49
GSIJoe12-May-04 5:49 
Generalsaving changes Pin
Monty27-Apr-04 20:43
Monty27-Apr-04 20:43 
Questionmove to MDI? Pin
ipichet6-Apr-04 22:18
ipichet6-Apr-04 22:18 
AnswerRe: move to MDI? Pin
.dan.g.6-May-04 14:00
professional.dan.g.6-May-04 14:00 
GeneralWindows CE compatibility Pin
CodeRed127712-Feb-04 8:19
CodeRed127712-Feb-04 8:19 
GeneralRe: Windows CE compatibility Pin
.dan.g.12-Feb-04 10:29
professional.dan.g.12-Feb-04 10:29 
not that i can easily see, roman.

but this is not my area of expertise.

i suggest you look through the windows ce section of codeproject and ask some questions there.

sorry.

.dan.g.

AbstractSpoon (subscribe)
GeneralSmall improvement Pin
Brendan Tregear9-Oct-03 17:29
Brendan Tregear9-Oct-03 17:29 
GeneralRe: Small improvement Pin
.dan.g.9-Oct-03 20:52
professional.dan.g.9-Oct-03 20:52 
GeneralRe: Small improvement Pin
CPAVG2-Nov-04 17:59
CPAVG2-Nov-04 17:59 
QuestionHow to get a CBitmap* Pin
thowa7-Sep-03 6:26
thowa7-Sep-03 6:26 
AnswerRe: How to get a CBitmap* Pin
.dan.g.7-Sep-03 15:06
professional.dan.g.7-Sep-03 15:06 
GeneralCreateCompatibleBitmap fails Pin
Member 3100755-Aug-03 6:26
Member 3100755-Aug-03 6:26 
GeneralRe: CreateCompatibleBitmap fails Pin
.dan.g.5-Aug-03 14:08
professional.dan.g.5-Aug-03 14:08 
GeneralMasking Color Pin
trivikram919-Jun-03 18:20
trivikram919-Jun-03 18:20 
GeneralRotation problem Pin
Member 129322911-Jun-03 0:14
Member 129322911-Jun-03 0:14 
GeneralRe: Rotation problem Pin
.dan.g.11-Jun-03 14:23
professional.dan.g.11-Jun-03 14:23 
GeneralRe: Rotation problem Pin
Shariq2212-Jun-03 22:00
Shariq2212-Jun-03 22:00 
GeneralRe: Rotation problem Pin
ben.tsai1-Aug-07 23:10
ben.tsai1-Aug-07 23:10 
Generalthanks Pin
Iceheart1-Jan-03 11:40
Iceheart1-Jan-03 11:40 
GeneralRe: thanks Pin
.dan.g.2-Jan-03 14:20
professional.dan.g.2-Jan-03 14:20 
GeneralThank you Pin
Anonymous30-Dec-02 1:42
Anonymous30-Dec-02 1:42 
GeneralRe: Thank you Pin
.dan.g.2-Jan-03 15:30
professional.dan.g.2-Jan-03 15:30 

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.