gpt4 book ai didi

c++ - 如何在 C++ 中获取 "Padding"图像

转载 作者:行者123 更新时间:2023-12-03 12:51:09 24 4
gpt4 key购买 nike

我正在运行以在 C++ 中显示原始 RGB 图像,无需任何库。当我输入方形图像(例如:512x512)时,我的程序可以完美显示图像,但在 not_square 尺寸图像(例如:350x225)中却不能。我知道我需要为这个案例填充,然后我尝试找到相同的案例,但对我来说人们如何填充他们的图像没有意义。

如果有人能告诉我如何填充,我将非常感谢。下面是我对 Raw 中的 RGB 所做的工作。

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

}

最佳答案

您需要将每行中的字节数填充为 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];
}
}
}
}

我还更改了内部 for 循环以使用 m_uiWidth 而不是 m_uiHeight

关于c++ - 如何在 C++ 中获取 "Padding"图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19473659/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com