Click here to Skip to main content
11,495,272 members (35,885 online)
Click here to Skip to main content

Add fast user-extensible image processing support to CBitmap

, 14 Dec 2002 232.9K 7.4K 120
Rate this:
Please Sign up or sign in to vote.
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 Smile | :) 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 Smile | :) 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

Share

About the Author

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

For all his latest freeware visit AbstractSpoon.

Comments and Discussions

 
GeneralDiplaying and processing two images Pin
Member 360679225-Jan-09 22:17
memberMember 360679225-Jan-09 22:17 
GeneralConvert to VS 2005 Pin
AlexEvans5-Oct-08 23:01
memberAlexEvans5-Oct-08 23:01 
GeneralJPG rotation in MS PowerPoint is really fast. Pin
hujunxi8-May-08 17:17
memberhujunxi8-May-08 17:17 
QuestionVideo? Pin
captainc/c++27-Nov-07 20:17
membercaptainc/c++27-Nov-07 20:17 
GeneralReally nice work Pin
morntide10-Oct-07 17:48
membermorntide10-Oct-07 17:48 
Generalso GREAT! Pin
k8cchaha6-Sep-07 19:02
memberk8cchaha6-Sep-07 19:02 
Questionhow about c#? Pin
s.jahangiri13-Feb-07 3:10
members.jahangiri13-Feb-07 3:10 
GeneralProblem with Rotation! Pin
Karen0_012-Sep-06 1:30
memberKaren0_012-Sep-06 1:30 
GeneralSaving bitmap to disk file Pin
Andrew Phillips2-May-06 0:13
memberAndrew Phillips2-May-06 0:13 
GeneralSmall fix Pin
Andrew Phillips1-May-06 21:27
memberAndrew Phillips1-May-06 21:27 
QuestionSupport for tiff files Pin
Muhammad Azam30-Mar-06 0:26
memberMuhammad Azam30-Mar-06 0:26 
AnswerRe: Support for tiff files Pin
Andrew Phillips1-May-06 21:33
memberAndrew Phillips1-May-06 21:33 
GeneralWeighted Color Average Pin
ozantopoglu22-Feb-06 4:06
memberozantopoglu22-Feb-06 4:06 
QuestionHow to display a PNG? Pin
thilon8-Jan-06 19:05
memberthilon8-Jan-06 19:05 
GeneralPNG support Pin
vjedlicka2-Dec-05 4:16
membervjedlicka2-Dec-05 4:16 
GeneralRe: PNG support Pin
.dan.g.3-Dec-05 23:48
member.dan.g.3-Dec-05 23:48 
AnswerRe: PNG support Pin
vjedlicka7-Dec-05 23:26
membervjedlicka7-Dec-05 23:26 
GeneralCreating from existent bitmap (DIB) Pin
fcarello8-Sep-05 0:03
memberfcarello8-Sep-05 0:03 
GeneralRe: Creating from existent bitmap (DIB) Pin
Andrew Phillips1-May-06 21:38
memberAndrew Phillips1-May-06 21:38 
Generalloading pictures in Picture box Pin
Kawakami Genzai25-Aug-05 12:23
memberKawakami Genzai25-Aug-05 12:23 
GeneralRe: loading pictures in Picture box Pin
Chae Sang, Jung21-Dec-05 23:09
memberChae Sang, Jung21-Dec-05 23:09 
QuestionHow do i save the resized bitmap Pin
shikha_dawer28-Jan-05 20:17
membershikha_dawer28-Jan-05 20:17 
GeneralCongrats, great work!!! Pin
Tomas Danek14-Nov-04 2:04
memberTomas Danek14-Nov-04 2:04 
GeneralRe: Congrats, great work!!! Pin
.dan.g.15-Nov-04 16:09
member.dan.g.15-Nov-04 16:09 
GeneralRe: Congrats, great work!!! Pin
Tomas Danek16-Nov-04 10:39
memberTomas Danek16-Nov-04 10:39 
GeneralRe: Congrats, great work!!! Pin
.dan.g.16-Nov-04 14:08
member.dan.g.16-Nov-04 14:08 
QuestionDisplay in Picture Control ? Pin
IndianX5-Nov-04 3:03
memberIndianX5-Nov-04 3:03 
AnswerRe: Display in Picture Control ? Pin
Piyush Verma3-Nov-06 16:52
memberPiyush Verma3-Nov-06 16:52 
GeneralGray with mask color!! Pin
CPAVG2-Nov-04 19:31
memberCPAVG2-Nov-04 19:31 
GeneralNice Pin
Niklas Lindquist27-May-04 23:11
memberNiklas Lindquist27-May-04 23:11 
GeneralRe: Nice Pin
CPAVG2-Nov-04 18:44
memberCPAVG2-Nov-04 18:44 
GeneralRe: Nice Pin
Niklas Lindquist26-Nov-04 3:56
memberNiklas Lindquist26-Nov-04 3:56 
GeneralExpose CImageRotator(radians) Pin
Joe Fitzgerald12-May-04 6:49
memberJoe Fitzgerald12-May-04 6:49 
Generalsaving changes Pin
mailMonty7-Apr-04 21:43
membermailMonty7-Apr-04 21:43 
Questionmove to MDI? Pin
ipichet6-Apr-04 23:18
memberipichet6-Apr-04 23:18 
AnswerRe: move to MDI? Pin
.dan.g.6-May-04 15:00
member.dan.g.6-May-04 15:00 
GeneralWindows CE compatibility Pin
Roman127712-Feb-04 9:19
memberRoman127712-Feb-04 9:19 
GeneralRe: Windows CE compatibility Pin
.dan.g.12-Feb-04 11:29
member.dan.g.12-Feb-04 11:29 
GeneralSmall improvement Pin
Brendan Tregear9-Oct-03 18:29
sussBrendan Tregear9-Oct-03 18:29 
GeneralRe: Small improvement Pin
dang!9-Oct-03 21:52
memberdang!9-Oct-03 21:52 
GeneralRe: Small improvement Pin
CPAVG2-Nov-04 18:59
memberCPAVG2-Nov-04 18:59 
QuestionHow to get a CBitmap* Pin
thowa7-Sep-03 7:26
memberthowa7-Sep-03 7:26 
AnswerRe: How to get a CBitmap* Pin
dang!7-Sep-03 16:06
memberdang!7-Sep-03 16:06 
GeneralCreateCompatibleBitmap fails Pin
Robert Bergs5-Aug-03 7:26
memberRobert Bergs5-Aug-03 7:26 
GeneralRe: CreateCompatibleBitmap fails Pin
dang!5-Aug-03 15:08
memberdang!5-Aug-03 15:08 
GeneralMasking Color Pin
trivikram919-Jun-03 19:20
membertrivikram919-Jun-03 19:20 
GeneralRotation problem Pin
Shariq2211-Jun-03 1:14
sussShariq2211-Jun-03 1:14 
GeneralRe: Rotation problem Pin
DanG11-Jun-03 15:23
memberDanG11-Jun-03 15:23 
GeneralRe: Rotation problem Pin
Shariq2212-Jun-03 23:00
memberShariq2212-Jun-03 23:00 
GeneralRe: Rotation problem Pin
ben.tsai2-Aug-07 0:10
memberben.tsai2-Aug-07 0:10 

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 | Terms of Use | Mobile
Web02 | 2.8.150520.1 | Last Updated 15 Dec 2002
Article Copyright 2002 by .dan.g.
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid