Click here to Skip to main content
Click here to Skip to main content

An image preview dialog for adjusting brightness and contrast

By , 30 Apr 2003
Rate this:
Please Sign up or sign in to vote.

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

About the Author

jemodurn
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 PinmemberMember 101009489-Jun-13 19:04 
GeneralSuggestion for contrast PinmemberSolarAngel22-Jun-08 3:50 
Generalmsg!!!! Pinmembershikha_dawer16-Jan-05 21:03 
GeneralAbout Auto-Level and Auto-Contrast PinmemberHanney Wang31-Mar-04 15:29 
GeneralRe: About Auto-Level and Auto-Contrast PinmemberPaul Kissel14-Sep-07 11:07 
Generalfor jpeg Pinmemberhenli6-Aug-03 15:06 
GeneralRe: for jpeg Pinmemberjemodurn7-Aug-03 11:28 
GeneralRe: for jpeg Pinmemberhenli10-Aug-03 15:02 
GeneralNot bad! PinmemberWREY4-Jun-03 7:07 
Provides a GOOD starting point for someone who wants to take it to the next level.
 
An excellent foundation is what this sample offers.
 
Thanks for sharing it!
 
Cool | :cool:

 
William
 
Fortes in fide et opere!
GeneralNice! PinmemberPeter Mares1-May-03 23:21 
QuestionWhere's the article body? PinmemberJohann Gerell1-May-03 19:30 
AnswerRe: Where's the article body? Pinmemberjemodurn1-May-03 21:46 

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 | Mobile
Web01 | 2.8.140415.2 | Last Updated 1 May 2003
Article Copyright 2003 by jemodurn
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid