Click here to Skip to main content
15,881,578 members
Articles / Desktop Programming / MFC
Article

An image preview dialog for adjusting brightness and contrast

Rate me:
Please Sign up or sign in to vote.
4.29/5 (23 votes)
30 Apr 20032 min read 115K   4.6K   41   12
An image preview dialog for adjusting brightness and contrast

Sample Image - ImagePreview.jpg

Introduction

For one of my projects, I need to show an image preview dialog while adjusting brightness and contrast. Here is a sample of what I have created. It doesn't rely on any other graphics library. No GDI+ needed.

Why do it?

Sometimes refreshing a big image file takes a lot of RAM and processor power. A more efficient way is to take a sample from the complete image and show the effect in a preview dialog.

In my example, I am loading the bitmap from a resource file. However, you can easily load a 100x100 sample from your full image to show your user, the effect of your adjustment. I have seen programs that implement a caret box that let you drag around the full image to take such sample.

It is usually not a good idea to resample/resize the image into a 100x100 bitmap and put it into the preview window. The user won't be able to see clearly the final effect.

How?

Only 2 files in the sample project are important:

  • DialogBrightnessContrast.h
  • DialogBrightnessContrast.cpp

You should look at the BrightnessAndContrast() function. It loops through every pixel in the bitmap and changes its RGB value.

for (m_nY = 0; m_nY < m_bmp.bmHeight; m_nY++) {
     for (m_nX = 0; m_nX < m_bmp.bmWidth; m_nX++) {
         m_crC = m_dcSource.GetPixel(m_nX, m_nY);

         m_nC = GetRValue(m_crC) + nStepB;
         m_nC = floor((m_nC - GREY) * dStepC) + GREY;
         m_nR = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;

         m_nC = GetGValue(m_crC) + nStepB;
         m_nC = floor((m_nC - GREY) * dStepC) + GREY;
         m_nG = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;

         m_nC = GetBValue(m_crC) + nStepB;
         m_nC = floor((m_nC - GREY) * dStepC) + GREY;
         m_nB = (m_nC < 0x00) ? 0x00 : (m_nC > 0xff) ? 0xff : m_nC;

         m_crC = m_nR + (m_nG << 8) + (m_nB << 16);
         m_dcOffScreen.SetPixel(m_nX, m_nY, m_crC);
    }
}

To adjust brightness you are increasing all the RGB values of a color. To adjust contrast you are exaggerating the difference of a RGB value from GREY(128, 128, 128).

If you have library that does invert, tint and do other graphic manipulations, you can use them here and modify the preview image.

Final words

This is not the most efficient way to apply filter to your whole image. However it is sufficient for the preview dialog. When you hit OK button, you should pass the brightness and contrast values to your super JPG, GIF, PNG engine to crank out the best optimized photo.

May the code be with you.

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
Web Developer
United States United States
Frank is a Sr. UI Software Engineer specialized in MFC and Business Applications.

He has worked in BROADBASE, KANA, Macromeda and Siebel Systems.

Comments and Discussions

 
GeneralMy vote of 5 Pin
PickRelated9-Jun-13 19:04
PickRelated9-Jun-13 19:04 
GeneralSuggestion for contrast Pin
SolarAngel22-Jun-08 3:50
SolarAngel22-Jun-08 3:50 
Generalmsg!!!! Pin
shikha_dawer16-Jan-05 21:03
shikha_dawer16-Jan-05 21:03 
GeneralAbout Auto-Level and Auto-Contrast Pin
Hanney Wang31-Mar-04 15:29
Hanney Wang31-Mar-04 15:29 
GeneralRe: About Auto-Level and Auto-Contrast Pin
Paul Kissel14-Sep-07 11:07
Paul Kissel14-Sep-07 11:07 
Generalfor jpeg Pin
henli6-Aug-03 15:06
henli6-Aug-03 15:06 
GeneralRe: for jpeg Pin
jemodurn7-Aug-03 11:28
jemodurn7-Aug-03 11:28 
GeneralRe: for jpeg Pin
henli10-Aug-03 15:02
henli10-Aug-03 15:02 
GeneralNot bad! Pin
WREY4-Jun-03 7:07
WREY4-Jun-03 7:07 
GeneralNice! Pin
Peter Mares1-May-03 23:21
Peter Mares1-May-03 23:21 
QuestionWhere's the article body? Pin
Johann Gerell1-May-03 19:30
Johann Gerell1-May-03 19:30 
AnswerRe: Where's the article body? Pin
jemodurn1-May-03 21:46
jemodurn1-May-03 21:46 

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.