找不到这段缩放图像代码的异常原因?
这段代码是在vc 2003环境下缩放图像,但是缩放出现异常,找不到原因。请各位高手给指点指点。
int pixel_width = 4;
void CImgProcessDlg::OnLoadImg()
{
m_MyImage.Load(_T("F:\\54321.jpg"));
DWORD imageWidth = m_MyImage.GetWidth(); //获得图像宽度(像素单位)
DWORD imageHeight = m_MyImage.GetHeight(); //获得图像高度(像素单位)
DWORD pitch = m_MyImage.GetPitch(); //获得图像宽度(以Byte为单位)
BYTE* lpBits = (BYTE*)m_MyImage.GetBits(); //获得图像数据地址
BYTE *lpDIBBits = new BYTE[imageHeight*imageWidth*pixel_width];
memset( lpDIBBits, 0, imageHeight*imageWidth*pixel_width);
//复制图像数据到lpDIBBits;
if(m_MyImage.GetBPP()==24)
{
for (int i = 0; i < imageHeight; i++)
{
for (int j = 0; j < imageWidth; j++)
{
BYTE r, g, b, a;
COLORREF pixel = m_MyImage.GetPixel( j, i);
r = GetRValue(pixel); // 红
g = GetGValue(pixel); // 绿
b = GetBValue(pixel); // 蓝
a = 0; // 透明色
*lpDIBBits++ = r; // 红
*lpDIBBits++ = g; // 绿
*lpDIBBits++ = b; // 蓝
*lpDIBBits++ = a; // 透明色
}
}
}
else
{
::MessageBox(NULL,_T("只能打开8或24位图像!"),NULL,MB_ICONSTOP);
return;
}
DWORD iDestWidth = 600;
DWORD iDestHeight = 800;
byte *dst = new BYTE[iDestWidth*iDestHeight*pixel_width];
// 转换图像
zoom( dst, lpDIBBits, imageWidth, imageHeight, iDestWidth, iDestHeight);
m_DestImg.CreateEx( iDestWidth, iDestHeight, 24, BI_BITFIELDS);
COLORREF pixel;
for( DWORD i = 0; i < iDestHeight; i++)
{
for( DWORD j = 0; j < iDestWidth; j++)
{
pixel = RGB( *dst++, *dst++, *dst++);
dst++; // 透明色
m_DestImg.SetPixel( i, j, pixel);
}
}
m_DestImg.Save( _T("F:\\liuwei.jpg"));
return;
}
/*
dst : 目标缓冲区
src : 来源缓冲区
sw : 来源图像宽度
sh : 来源图像高度
dw : 目标图像宽度
dh : 目标图像高度
*/
void CImgProcessDlg::zoom(byte *dst, byte *src, long sw, long sh, long dw,long dh)
{
assert(dst != 0);
assert(src != 0);
assert(sw > 0 && sh > 0);
assert(dw > 0 && dh > 0);
float scalex = static_cast<float>(sw) / static_cast<float>(dw);
float scaley = static_cast<float>(sh) / static_cast<float>(dh);
long width_bytes = static_cast<long>(sw) * pixel_width;
long *pbyDst = reinterpret_cast<long*>(dst);
for(long i = 0; i < dh; i++)
{
float inverse_y = i * scaley;
long yy = static_cast<long>(inverse_y);
byte *pbySrc = src + yy * width_bytes;
for(long j = 0; j < dw; j++)
{
float inverse_x = j * scalex;
long xx = static_cast<long>(inverse_x);
long* pbyCurrent = reinterpret_cast<long*>(pbySrc + pixel_width * xx);
*pbyDst = *pbyCurrent;
pbyDst++;
}
}
}