void CPublicStaticMethods::SerializeCImage(CImage&srcImage, CArchive &ar)
{
int srcBitsCount = 0;
int srcWidth = 0;
int srcHeight = 0;
int srcPitch = 0;
int nColors = 0;//调色板数量
if (ar.IsStoring())
{
if (srcImage.IsNull())
{
ar < srcHeight ;
return;
}
else
{
srcBitsCount = srcImage.GetBPP();
srcWidth = srcImage.GetWidth();
srcHeight = srcImage.GetHeight();
srcPitch = srcImage.GetPitch();
ar < srcHeight;
if (srcWidth == 0 || srcHeight == 0)
return;
ar< srcPitch;
RGBQUAD pal[256];
if (srcBitsCount <= 8 && srcImage.IsIndexed())//需要调色板
{
nColors = srcImage.GetMaxColorTableEntries();
}
ar << nColors;
if (nColors>0)
{
srcImage.GetColorTable(0, nColors, pal);
ar.Write(pal, sizeof(RGBQUAD) * nColors);
}
BYTE* srcPtr = (BYTE*)srcImage.GetBits();
//复制图像数据
for (int i = 0; i)
{
ar.Write(srcPtr + i * srcPitch, abs(srcPitch));
}
}
}
else
{
ar >> srcWidth >> srcHeight ;
if (srcWidth == 0 || srcHeight == 0)
return;
ar >> srcBitsCount >> srcPitch;
ar >> nColors;
//销毁原有图像
if (!srcImage.IsNull())
{
//destImage.ReleaseDC();
srcImage.Detach();
srcImage.Destroy();
}
//创建新图像
if (srcBitsCount == 32) //支持alpha通道
{
srcImage.Create(srcWidth, srcHeight, srcBitsCount, 1);
}
else
{
srcImage.Create(srcWidth, srcHeight, srcBitsCount, 0);
}
//加载调色板
if (nColors>0&&nColors<=256)//需要调色板
{
RGBQUAD pal[256];
ar.Read(pal, sizeof(RGBQUAD) * nColors);
srcImage.SetColorTable(0, nColors, pal);//复制调色板程序
}
//目标图像参数
BYTE *srcPtr = (BYTE*)srcImage.GetBits();
//复制图像数据
for (int i = 0; i)
{
ar.Read(srcPtr + i * srcPitch, abs(srcPitch));
}
}
}