bool copyImgDataToCImage(CImage &srcImage,CImage &dstImage,int margin)
{
	margin  = abs(margin);
	//读取图像宽高
	int width = srcImage.GetWidth();
	int height = srcImage.GetHeight();
	if(margin*2 > width)
	{
		return false;
	}
	//new width
	int imgWidth = width-2*margin;

	if(!dstImage.CreateEx(imgWidth,height, 24, BI_RGB))//创建CImage 对象
	{
		return false;
	}
	//设置像素
	for(int i = height - 1;i >= 0 ; i--)
	{
		for(int j = 0; j < imgWidth;j++)
		{
			BYTE *pDst = (BYTE*)dstImage.GetPixelAddress(j, i);
			BYTE *pSrc = (BYTE*)srcImage.GetPixelAddress(j + margin, i);
			//读取并设置 rgb值
			*pDst++ = *pSrc++; // b分量值
			*pDst++ = *pSrc++; //g 分量
			*pDst = *pSrc; // r分量
		}
		//new offset
	}
	return true;
}
//decodeJpeg to gray image
bool decodeToGrayCImage(FIMEMORY *hMem,CImage &image)
{
	FreeImage_SeekMemory(hMem,0,SEEK_SET);
	FIBITMAP *check = FreeImage_LoadFromMemory(FIF_JPEG, hMem, 0);
	unsigned int width = FreeImage_GetWidth(check);
	unsigned int height = FreeImage_GetHeight(check);
	unsigned int pitch = FreeImage_GetPitch(check);
	FREE_IMAGE_TYPE image_type = FreeImage_GetImageType(check);
	if(!image.CreateEx(width,height,24,BI_RGB))
	{
		FreeImage_Unload(check);
		return false;
	}
	int x,y;
	if((image_type == FIT_BITMAP) && (FreeImage_GetBPP(check) == 24)) 
	{
		//像素操作
		BYTE *bits = (BYTE*)FreeImage_GetBits(check);
		for(y = 0; y < (int)height; y++) 
		{
			BYTE *pixel = (BYTE*)bits;
			for(x = 0; x < (int)width; x++) 
			{
				BYTE *p = (BYTE*)image.GetPixelAddress(x, height -1 - y);
				int grayVal = (int)(0.30 * pixel[FI_RGBA_RED] + 0.59 * pixel[FI_RGBA_GREEN] + 0.11*pixel[FI_RGBA_BLUE]);
				*p++ = (BYTE)grayVal;
				*p++ = (BYTE)grayVal;
				*p++ = (BYTE)grayVal;
				pixel += 3;
			}
			// next line
			bits += pitch;
		}
	}
	else
	{
		FreeImage_Unload(check);
		return false;
	}
	FreeImage_Unload(check);
	return true;
}
bool scaleImage(CImage& srcImg,CImage& dstImg,int dstWidth,int dstHeight)
{
	int nHeight = srcImg.GetHeight();
	int nWidth = srcImg.GetWidth();
	
	if(dstImg.CreateEx(dstWidth,dstHeight, 24, BI_RGB))
	{
		HDC ImgDC = dstImg.GetDC();
		int nPreMode = ::SetStretchBltMode(ImgDC,  HALFTONE);
		srcImg.Draw(ImgDC, 0, 0, dstWidth,dstHeight, 0, 0, nWidth, nHeight);
		::SetBrushOrgEx(ImgDC, 0, 0, NULL); 
		::SetStretchBltMode(ImgDC, nPreMode);
		dstImg.ReleaseDC();
	}
	else
	{
		return false;
	}
	return true;
}
//尺度变换
bool Scaling(CImage& srcImg,CImage& dstImg,double ratio)
{
	int nHeight = srcImg.GetHeight();
	int nWidth = srcImg.GetWidth();
	
	if(dstImg.CreateEx((int)(nWidth*ratio),(int)(nHeight*ratio), 24, BI_RGB))
	{
		HDC ImgDC = dstImg.GetDC();
		int nPreMode = ::SetStretchBltMode(ImgDC,  HALFTONE);
		srcImg.Draw(ImgDC, 0, 0, (int)(nWidth*ratio),(int)(nHeight*ratio), 0, 0, nWidth, nHeight);
		::SetBrushOrgEx(ImgDC, 0, 0, NULL); 
		::SetStretchBltMode(ImgDC, nPreMode);
		dstImg.ReleaseDC();
	}
	else
	{
		return false;
	}
	return true;
}