分享

Image Processing Using GDI+

 学海无涯GL 2012-09-19

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

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.

    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.

    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.

    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.

    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.

    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

    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.

    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;
        }
    }

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多