Frage

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

}
War es hilfreich?

Lösung

You need to pad the number of bytes in each line out to a multiple of 4.

void CImage_MyClass::Class_MakeRGB(void)
{
    m_BMPheader.biHeight = m_uiHeight;
    m_BMPheader.biWidth = m_uiWidth;
    //Pad buffer width to next highest multiple of 4
    const int bmStride = m_uiWidth * 3 + 3 & ~3;
    m_pcBMP = new UCHAR[m_uiHeight * bmStride];
    //Clear buffer so the padding bytes are 0
    memset(m_pcBMP, 0, m_uiHeight * bmStride);

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

I also changed the inner for loop to use m_uiWidth instead of m_uiHeight.

Andere Tipps

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

}

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top