Click here to Skip to main content
15,885,309 members
Articles / Desktop Programming / MFC
Article

Image Processing Using GDI+

Rate me:
Please Sign up or sign in to vote.
4.66/5 (36 votes)
8 May 20062 min read 152.9K   5.5K   97   36
This article describes the implementation of an image processing tool.

Introduction

There are many image processing tools similar to what I describe in this article. In this personal tool, I have used very basic image processing algorithms. Here I have focused on brightness, color inversion, contrast, blur, sharpness, and black and white color algorithms. I have used GDI+ for loading the image into memory, with the help of formulas mentioned for each kind of image processing. The code includes the CImageProcess.cpp and CImageProcess.h files. I would like to suggest that for image processing, do not use GetPixel and SetPixel. Performance wise, they are very slow. For more information, check this article: Rotate a Bitmap at Any Angle Without GetPixel/SetPixel.

Original Image

Image 1

Algorithm and Code

  1. Brightness

    An image can be light or sark. If we want to have more light in the image, we should decrease the RGB value. If we want an image to be darker, we should increase the RGB value.

    Image 2

    Formula

    Color = Color + Value(may be –ve)
    if (Color <0) Color = 0; if (Color>255) Color = 255;

    For example, if we want to decrease the red color component:

    Red = Red - DecrementVal

    To increase the red color component:

    Red = Red + IncrementVal

    The code

    BITMAPINFO bi;
    BOOL bRes;
    char *buf;
    
    // Bitmap header
    bi.bmiHeader.biSize = sizeof(bi.bmiHeader);
    bi.bmiHeader.biWidth = m_nWidth;
    bi.bmiHeader.biHeight = m_nHeight;
    bi.bmiHeader.biPlanes = 1;
    bi.bmiHeader.biBitCount = 32;
    bi.bmiHeader.biCompression = BI_RGB;
    bi.bmiHeader.biSizeImage = m_nWidth * 4 * m_nHeight;
    bi.bmiHeader.biClrUsed = 0;
    bi.bmiHeader.biClrImportant = 0;
    
    // Buffer
    buf = (char *) malloc(m_nWidth * 4 * m_nHeight);
    // Don't use getPixel and SetPixel.It's very slow.
    // Get the all scanline.
    bRes = GetDIBits(hMemDC, m_hBitmap, 0, m_nHeight, buf, &bi,
                           DIB_RGB_COLORS);
    long nCount=0;
    for (int i=0; i<m_nHeight; ++i)
    {
        for (int j=0; j<m_nWidth; ++j)
        {
            long lVal=0;
            memcpy(&lVal, &buf[nCount], 4);
            // Get the reverse order
            int b = GetRValue(lVal);
            int g = GetGValue(lVal);
            int r = GetBValue(lVal);
            
            // Red
            r += nRedVal;
            if (r >255)
            {
                r = 255;
            }
            if (r <0)
            {
                r = 0;
            }
    
            // Green
            g += nGreenVal;
            if (g>255)
            {
                g = 255;
            }
            if (g<0)
            {
                g = 0;
            }
    
            // Blue
            b += nBlueVal;
            if (b >255)
            {
                b = 255;
            }
            if (b<0)
            {
                b = 0;
            }
            
            // Store reverse order
            lVal = RGB(b, g, r);
            memcpy(&buf[nCount], &lVal, 4);
    
        // Increment with 4. RGB color take 4 bytes. 
        // The high-order byte must be zero
        // See in MSDN COLORREF
            nCount+=4;
          }
        }
    
        // Set again
        SetDIBits(hMemDC, m_hBitmap, 0, bRes, buf,  &bi,
                           DIB_RGB_COLORS);    
        free(buf);
  2. Invert Color

    When we invert color, we get the opposite colors of the current pixels.

    Image 3

    Formula

    Color = 255 – Color; if (Color <0) Color = 0;
    if (Color>255) Color = 255;

    The code

    // Same header format
    for (int i=0; i<m_nHeight; ++i)
    {
        for (int j=0; j<m_nWidth; ++j)
        {
            long lVal=0;
            memcpy(&lVal, &buf[nCount], 4);
            int b = 255-GetRValue(lVal);
            int g = 255-GetGValue(lVal);
            int r = 255-GetBValue(lVal);
                
            lVal = RGB(b, g, r);
                
            memcpy(&buf[nCount], &lVal, 4);
            nCount+=4;
        }
    }
  3. Contrast Color

    Contrasting colors are colors that are opposite on the color wheel. In a high contrast image, you can see definite edges, and the different elements of that image are accented. In a low contrast image, all the colors are nearly the same, and it's hard to make out the details.

    Image 4

    Formula

    Color = (((Color - 128) * ContrastVal) / 100) +128
    if (Color <0) Color = 0; if (Color>255) Color = 255;

    Here, the contrast value is between 0 and 200.

    The code

    for (int i=0; i<m_nHeight; ++i)
    {
        for (int j=0; j<m_nWidth; ++j)
        {            
            long lVal=0;
            memcpy(&lVal, &buf[nCount], 4);
            // Get from buffer in reverse order
            int b = GetRValue(lVal);
            int g = GetGValue(lVal);
            int r = GetBValue(lVal);
    
            r = ((r-128)*nContrastVal)/100 +128;
            g = ((g-128)*nContrastVal)/100 +128;
            b = ((b-128)*nContrastVal)/100 +128;            
            
            // Red
            if (r >255)
            {
                r = 255;
            }
            if (r <0)
            {
                r = 0;
            }
                
            // Green
            if (g>255)
            {
                g = 255;
            }
            if (g<0)
            {
                g = 0;
            }
    
            // Blue            
            if (b >255)
            {
                b = 255;
            }
            if (b<0)
            {
                b = 0;
            }
    
            // Store in reverse order            
            lVal = RGB((int)b, (int)g, (int)r);
            
            memcpy(&buf[nCount], &lVal, 4);
            nCount+=4;
        }
    }
  4. Blur

    Blur is the well-known effect on computer screens, in fact on all pixel devices, where the diagonal and curved lines are displayed as a series of little zigzag horizontal and vertical lines.

    Image 5

    Formula

    Color = (C1+C2+C3+C4+C5)/5

    For more details about blur and anti-aliasing check this article: Creating graphics for the Web: Anti-aliasing.

    Here, C1 is the current pixel. C2, C3, C4, and C5 are the nearby pixels.

    Image 6

    The code

    pOriBuf = (char *) malloc(m_nWidth * 4 * m_nHeight);
    // Store new value into tempravary buffer
    char *tmpBuf = (char *) malloc(m_nWidth * 4 * m_nHeight);
    bRes = GetDIBits(hMemDC, m_hBitmap, 0, m_nHeight, pOriBuf, &bi,
                           DIB_RGB_COLORS);    
    long nCount=0;
    long c1, c2, c3, c4, c5;
    
    // Retrive from original buffer
    // Caluculate the value and store new value into tmpBuf
    for (int i=0; i<m_nHeight; ++i)
    {
        for (int j=0; j<m_nWidth; ++j)
        {
            long lVal=0;
            memcpy(&lVal, &pOriBuf[nCount], 4);
            int b = GetRValue(lVal);
            int g = GetGValue(lVal);
            int r = GetBValue(lVal);
    
            c1 = r;
            // Red
            if ((nCount < ((m_nHeight-1)*m_nWidth*4l)) && 
                (nCount > (m_nWidth*4)))
            {
                memcpy(&lVal, &pOriBuf[nCount-(m_nWidth*4l)], 4);
                c2 = GetBValue(lVal);
        
                memcpy(&lVal, &pOriBuf[nCount+4], 4);
                c3 = GetBValue(lVal);
        
                memcpy(&lVal, &pOriBuf[(nCount+(m_nWidth*4l))], 4);
                c4 = GetBValue(lVal);
        
                memcpy(&lVal, &pOriBuf[nCount-4], 4);
                c5 = GetBValue(lVal);
                        
                r = (c1+c2+c3+c4+c5)/5;
            }
    
            // Green
            c1 = g;
            if ((nCount < ((m_nHeight-1)*m_nWidth*4l)) && 
                (nCount > (m_nWidth*4)))
            {
                memcpy(&lVal, &pOriBuf[(nCount-(m_nWidth*4l))], 4);
                c2 = GetGValue(lVal);
        
                memcpy(&lVal, &pOriBuf[nCount+4], 4);
                c3 = GetGValue(lVal);
        
                memcpy(&lVal, &pOriBuf[(nCount+(m_nWidth*4l))], 4);
                c4 = GetGValue(lVal);
        
                memcpy(&lVal, &pOriBuf[nCount-4], 4);
                c5 = GetGValue(lVal);
        
                g = (c1+c2+c3+c4+c5)/5;
            }
    
            // Blue
            c1 = b;
            if ((nCount < ((m_nHeight-1)*m_nWidth*4l)) && 
                (nCount > (m_nWidth*4)))
            {
                memcpy(&lVal, &pOriBuf[(nCount-(m_nWidth*4l))], 4);
                c2 = GetRValue(lVal);
        
                memcpy(&lVal, &pOriBuf[nCount+4], 4);
                c3 = GetRValue(lVal);
        
                memcpy(&lVal, &pOriBuf[(nCount+(m_nWidth*4l))], 4);
                c4 = GetRValue(lVal);
        
                memcpy(&lVal, &pOriBuf[nCount-4], 4);
                c5 = GetRValue(lVal);
        
                b = (c1+c2+c3+c4+c5)/5;
            }
    
            // Store in reverse order
            lVal = RGB(b, g, r);
                    
            memcpy(&tmpBuf[nCount], &lVal, 4);
        
            nCount+=4;
        }
    }
    
    // Store tmpBuf
    SetDIBits(hMemDC, m_hBitmap, 0, bRes, 
              tmpBuf,  &bi, DIB_RGB_COLORS);
    
    free(pOriBuf);
    free(tmpBuf);
  5. Sharpness

    Image 7

    Formula

    Color = (C1*5) – (C2+C3+C4+C5).
    if (Color <0) Color = 0; if (Color>255) Color = 255;

    The code

    for (int i=0; i<m_nHeight; ++i)
    {
        for (int j=0; j<m_nWidth; ++j)
        {
            long lVal=0;
            memcpy(&lVal, &pOriBuf[nCount], 4);
            int b = GetRValue(lVal);
            int g = GetGValue(lVal);
            int r = GetBValue(lVal);
        
            c1 = r;
            // Red
            if ((nCount < ((m_nHeight-1)*m_nWidth*4l)) && (nCount > (m_nWidth*4)))
            {
                memcpy(&lVal, &pOriBuf[nCount-(m_nWidth*4l)], 4);
                c2 = GetBValue(lVal);
            
                memcpy(&lVal, &pOriBuf[nCount+4], 4);
                c3 = GetBValue(lVal);
            
                memcpy(&lVal, &pOriBuf[(nCount+(m_nWidth*4l))], 4);
                c4 = GetBValue(lVal);
            
                memcpy(&lVal, &pOriBuf[nCount-4], 4);
                c5 = GetBValue(lVal);
            
                r = (c1*5) - (c2+c3+c4+c5);
            }
    
            // Green
            c1 = g;
            if ((nCount < ((m_nHeight-1)*m_nWidth*4l)) && (nCount > (m_nWidth*4)))
            {
                memcpy(&lVal, &pOriBuf[(nCount-(m_nWidth*4l))], 4);
                c2 = GetGValue(lVal);
            
                memcpy(&lVal, &pOriBuf[nCount+4], 4);
                c3 = GetGValue(lVal);
            
                memcpy(&lVal, &pOriBuf[(nCount+(m_nWidth*4l))], 4);
                c4 = GetGValue(lVal);
            
                memcpy(&lVal, &pOriBuf[nCount-4], 4);
                c5 = GetGValue(lVal);
            
                g = (c1*5) - (c2+c3+c4+c5);
            }
    
            // Blue
            c1 = b;
            if ((nCount < ((m_nHeight-1)*m_nWidth*4l)) && (nCount > (m_nWidth*4)))
            {
                memcpy(&lVal, &pOriBuf[(nCount-(m_nWidth*4l))], 4);
                c2 = GetRValue(lVal);
            
                memcpy(&lVal, &pOriBuf[nCount+4], 4);
                c3 = GetRValue(lVal);
            
                memcpy(&lVal, &pOriBuf[(nCount+(m_nWidth*4l))], 4);
                c4 = GetRValue(lVal);
            
                memcpy(&lVal, &pOriBuf[nCount-4], 4);
                c5 = GetRValue(lVal);
            
                b = (c1*5) - (c2+c3+c4+c5);
            }
    
            // Red
            if (r >255)
            {
                r = 255;
            }
            if (r <0)
            {
                r = 0;
            }
            
            // Green
            if (g>255)
            {
                g = 255;
            }
            if (g<0)
            {
                g = 0;
            }
    
            // Blue
            if (b >255)
            {
                b = 255;
            }
            if (b<0)
            {
                b = 0;
            }
            
            // Store in reverse order
            lVal = RGB(b, g, r);
            
            memcpy(&tmpBuf[nCount], &lVal, 4);
            
            nCount+=4;
        }
    }
  6. Black and white color

    Black and white images can be arrived at by giving the same color value for Red, Green, and Blue.

    Image 8

    Formula

    Color = (R+G+B)/3; R = Color; G = Color; B = Color;

    The code

    for (int i=0; i<m_nHeight; ++i)
    {
        for (int j=0; j<m_nWidth; ++j)
        {
            long lVal=0;
            memcpy(&lVal, &buf[nCount], 4);
            // Get the color value from buffer
            int b = GetRValue(lVal);
            int g = GetGValue(lVal);
            int r = GetBValue(lVal);
    
            // get the average color value
            lVal = (r+g+b)/3;
    
            // assign to RGB color            
            lVal = RGB(lVal, lVal, lVal);
            memcpy(&buf[nCount], &lVal, 4);
    
            nCount+=4;
        }
    }

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
Japan Japan
Skills: VC++, ATL COM, Embeded VC++, BREW for Mobile and TCP/IP

Current Location: Bangalore

Born & Brought : Ayankollankondan,RJPM, India

Work Experience:
6+Years experience in IT field. Now i'm working in MindTree Consulting Ltd, B'lore.

5Digistar Inc, Japan
HCL Technologies Ltd, Chennai
T&B international Pvt ltd, Chennai
Aram menporulagam Pvt Ltd, Chennai


Comments and Discussions

 
AnswerRe: How to save modified image ? Pin
S.Balasubramanian15-May-06 18:12
S.Balasubramanian15-May-06 18:12 
GeneralRe: How to save modified image ? Pin
kiranchikhale13-Feb-07 23:13
kiranchikhale13-Feb-07 23:13 
GeneralRe: How to save modified image ? Pin
kiranchikhale13-Feb-07 23:46
kiranchikhale13-Feb-07 23:46 
GeneralRe: How to save modified image ? Pin
S.Balasubramanian14-Feb-07 1:28
S.Balasubramanian14-Feb-07 1:28 
GeneralRe: How to save modified image ? Pin
kiranchikhale14-Feb-07 20:31
kiranchikhale14-Feb-07 20:31 
AnswerRe: How to save modified image ? Pin
S.Balasubramanian15-Feb-07 21:18
S.Balasubramanian15-Feb-07 21:18 
GeneralRe: How to save modified image ? Pin
AbhijeetNevaskar17-Oct-07 0:47
AbhijeetNevaskar17-Oct-07 0:47 
GeneralB&W: Luminocity way Pin
Eugene Podkopaev10-May-06 0:50
Eugene Podkopaev10-May-06 0:50 
Hi!
Nice article, thank you!

Small note about converting to black and white. Hence converting to B&W isn't a trivial task as it may seems, one should decide what he wants to get after this conversion.
Another way to convert color pictures to B&W is based on the fact that human eye is more sensitive to green than red or blue. So weights should be represented as 59% for green, 30% for red and 11% for blue; and formula will look like this:

<br />
Color = (RL+GL+BL)/3;<br />
RL = R * 30 / 100; R = Color;<br />
GL = G * 59 / 100; G = Color;<br />
BL = B * 11 / 100; B = Color;<br />


Good luck!
GeneralRe: B&W: Luminocity way Pin
S.Balasubramanian10-May-06 15:44
S.Balasubramanian10-May-06 15:44 
GeneralRe: B&W: Luminocity way Pin
Zlosk16-May-06 5:15
Zlosk16-May-06 5:15 
GeneralRe: B&W: Luminocity way Pin
S.Balasubramanian16-May-06 14:51
S.Balasubramanian16-May-06 14:51 
Generalnice photo Pin
Hillmann-20239-May-06 4:27
Hillmann-20239-May-06 4:27 
AnswerRe: nice photo Pin
S.Balasubramanian9-May-06 22:32
S.Balasubramanian9-May-06 22:32 

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.