Search code examples
c++image-processingmfcmdi

How to "Padding" Image in C++


I am running for displaying RGB image from raw in C++ without any library. When I input the square image (e.g: 512x512), my program can display the image perfectly, but it does not in not_square size image (e.g: 350x225). I understand that I need padding for this case, then I tried to find the same case but it didn't make sense for me how people can pad their image.

If anyone can show me how to pad, I would be thanks for this. And below is what I have done for RGB from Raw.

void CImage_MyClass::Class_MakeRGB(void)
{

        m_BMPheader.biHeight = m_uiHeight;
        m_BMPheader.biWidth  = m_uiWidth;
        m_pcBMP = new UCHAR[m_uiHeight * m_uiWidth * 3];

    //RGB Image
    {
        int ind = 0;
        for (UINT y = 0; y < m_uiHeight; y++)
        {
            for (UINT x = 0; x < m_uiHeight*3; x+=3)
            {
                m_pcBMP[ind++] = m_pcIBuff[m_uiHeight - y -1][x+2];
                m_pcBMP[ind++] = m_pcIBuff[m_uiHeight - y -1][x+1];
                m_pcBMP[ind++] = m_pcIBuff[m_uiHeight - y -1][x];
            }
        }
    }

}

Solution

  • @Retired Ninja, Thanks anyway for your answer... you showed me a simple way for this... But by the way, I have fixed mine as well with different way.. here is it:

    void CImage_MyClass::Class_MakeRGB(void) {

        m_BMPheader.biHeight = m_uiHeight;
        m_BMPheader.biWidth  = m_uiWidth;
    
        int padding = 0;
    
        int scanline = m_uiWidth * 3;
    
    while ( ( scanline + padding ) % 4 != 0 )
    {
        padding++;
    }
    
    int psw = scanline + padding;
    
    m_pcBMP = new UCHAR[m_uiHeight * m_uiWidth * 3 + m_uiHeight * padding];
    
    //RGB Image
        int ind = 0;
        for (UINT y = 0; y < m_uiHeight; y++)
        {
            for (UINT x = 0; x < m_uiHeight*3; x+=3)
            {
                m_pcBMP[ind++] = m_pcIBuff[m_uiHeight - y -1][x+2];
                m_pcBMP[ind++] = m_pcIBuff[m_uiHeight - y -1][x+1];
                m_pcBMP[ind++] = m_pcIBuff[m_uiHeight - y -1][x];
            }
            for(int i = 0; i < padding; i++)
    
            ind++;
        }
    

    }