VOID CDibUtil::Translation(CDib& src, CDib& dest, CPoint& translation)
{
	CSize imageSize = src.GetDimensions();

	ASSERT(imageSize.cx + translation.x >= 0);
	ASSERT(imageSize.cy + translation.y >= 0);
	dest.CreateCDib(CSize(imageSize.cx + translation.x, imageSize.cy + translation.y),
		src.m_lpBMIH->biBitCount);

	for (LONG y = 0, dy = translation.y; y != imageSize.cy; ++y, dy = y + translation.y)
		for (LONG x = 0, dx = translation.x; x != imageSize.cx; ++x, dx = x + translation.x)
		{
			if (dy >= 0 && dx >= 0)
			{
				dest.WritePixel(dx, dy, src.GetPixel(x, y));
			}
		}
}
/*
 * See http://www.leunen.com/cbuilder/rotbmp.html
 */
VOID CDibUtil::Rotation(CDib& src, CDib& dest, DOUBLE angle)
{
	CSize imageSize = src.GetDimensions();
	LONG width = imageSize.cx;
	LONG height = imageSize.cy;

	// Convert angle to radians
	FLOAT radians = (FLOAT)(angle * M_PI / 180.0);
	FLOAT cosine = (FLOAT)cos(radians);
	FLOAT sine = (FLOAT)sin(radians);
	
	// Compute dimensions of the resulting bitmap
	// First get the coordinates of the 3 corners other than origin
	FLOAT x1 = (-height * sine);
	FLOAT y1 = ( height * cosine);
	FLOAT x2 = ( width  * cosine - height * sine);
	FLOAT y2 = ( height * cosine + width * sine);
	FLOAT x3 = ( width  * cosine);
	FLOAT y3 = ( width  * sine);
	
	FLOAT minx = min(0, min(x1, min(x2, x3)));
	FLOAT miny = min(0, min(y1, min(y2, y3)));
	FLOAT maxx = max(x1, max(x2, x3));
	FLOAT maxy = max(y1, max(y2, y3));
	
	LONG newWidth = (LONG)ceil(fabs(maxx) - minx);
	LONG newHeight = (LONG)ceil(fabs(maxy) - miny);

	dest.CreateCDib(CSize(newWidth, newHeight), src.m_lpBMIH->biBitCount);

	LONG sx, sy;
	for(LONG y = 0; y < newHeight; ++y )
		for(LONG x = 0; x < newWidth; ++x )
		{
			sx = (LONG)((x + minx) * cosine + (y + miny) * sine);
			sy = (LONG)((y + miny) * cosine - (x + minx) * sine);
			if( sx >= 0 && sx < width && sy >= 0 && sy < height )
				dest.WritePixel(x, y, src.GetPixel(sx, sy));
		}
}