改进的图像拷贝代码
说明:笔者参考了网上各位大佬的拷贝代码,对其中像素复制部分进行了一点改进。代码中有说明,请参考。
本文可能是笔者第一次在CSDN发文,于武汉新冠病毒发作期间,谢谢大家参考。(请问如何将代码块背景颜色改成其他颜色?)
代码如下:
bool copyImg(const CImage & src, CImage & dst)
{
if (src.IsNull())
return FALSE;
//源图像参数
BYTE* srcPtr = (BYTE*)src.GetBits();
int srcBitsCount = src.GetBPP();
int srcWidth = src.GetWidth();
int srcHeight = src.GetHeight();
int srcPitch = src.GetPitch();
//销毁原有图像
if (!dst.IsNull())
{
dst.Destroy();
}
//创建新图像
if (srcBitsCount == 32) //支持alpha通道
{
dst.Create(srcWidth, srcHeight, srcBitsCount, 1);
}
else
{
dst.Create(srcWidth, srcHeight, srcBitsCount, 0);
}
//加载调色板, for 8 bit bitmap
if (srcBitsCount <= 8 && src.IsIndexed())//需要调色板
{
RGBQUAD pal[256];
int nColors = src.GetMaxColorTableEntries();
if (nColors > 0)
{
src.GetColorTable(0, nColors, pal);
dst.SetColorTable(0, nColors, pal);//复制调色板程序
}
}
//目标图像参数
BYTE *destPtr = (BYTE*)dst.GetBits();
int destPitch = dst.GetPitch();
//复制图像数据,the original codes is a redundant
/*for (int i = 0; i < srcHeight; i++)
{
memcpy(destPtr + i*destPitch, srcPtr + i*srcPitch, abs(srcPitch));
}*/
// the new codes by sund
if (srcPitch < 0) {
memcpy(destPtr + destPitch*(srcHeight - 1), srcPtr + srcPitch*(srcHeight - 1), srcHeight*abs(srcPitch));
}
else {
memcpy(destPtr, srcPtr, srcHeight*abs(srcPitch));
}
return true;
}
原始代码请参考
https://blog.csdn.net/weixin_30793643/article/details/96987666