Bitmap Rotation?

This is a discussion on Bitmap Rotation? within the Windows Programming forums, part of the Platform Specific Boards category; Hi, How can I rotate a bitmap using Win32 GDI? I am using the bitmap class from michael morrison book ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    223

    Bitmap Rotation?

    Hi,

    How can I rotate a bitmap using Win32 GDI?

    I am using the bitmap class from michael morrison book and want to rotate tank turret according to mouse position?

    Please help

    thanks

  2. #2
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    The bitmap I want to roatae is only 4bit, 14 x 23. I can code to retrieve the necessary angle required for it to follow the mouse its just actually gettin the bitmap to rotate. Totally at a loss.
    I dont really want to start delving in to other libraries if i can. Id prefer to stick with the win32 GDI if I can help it.
    Help is much, much appreciated

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Typing "GDI rotate" into MSDN got me this page.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Google for "plgblt rotate"
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    223
    Hi,

    I found this function for rotating the bitmap but I dont know what I'm supposed to do with the result. ie, how to implement it.

    Can anyone shed any light on this please?

    Thanks

    Code:
    // GetRotatedBitmap	- Create a new bitmap with rotated image
    // Returns		- Returns new bitmap with rotated image
    // hDIB			- Device-independent bitmap to rotate
    // radians		- Angle of rotation in radians
    // clrBack		- Color of pixels in the resulting bitmap that do
    //			  not get covered by source pixels
    HANDLE GetRotatedBitmap( HANDLE hDIB, float radians, COLORREF clrBack )
    {
    	// Get source bitmap info
    	BITMAPINFO &bmInfo = *(LPBITMAPINFO)hDIB ;
    	int bpp = bmInfo.bmiHeader.biBitCount;		// Bits per pixel
    
    	int nColors = bmInfo.bmiHeader.biClrUsed ? bmInfo.bmiHeader.biClrUsed :
    					1 << bpp;
    	int nWidth = bmInfo.bmiHeader.biWidth;
    	int nHeight = bmInfo.bmiHeader.biHeight;
    	int nRowBytes = ((((nWidth * bpp) + 31) & ~31) / 8);
    
    	// Make sure height is positive and biCompression is BI_RGB or BI_BITFIELDS
    	DWORD &compression = bmInfo.bmiHeader.biCompression;
    	if( nHeight < 0 || (compression!=BI_RGB && compression!=BI_BITFIELDS))
    		return NULL;
    
    	LPVOID lpDIBBits;
    	if( bmInfo.bmiHeader.biBitCount > 8 )
    		lpDIBBits = (LPVOID)((LPDWORD)(bmInfo.bmiColors +
    			bmInfo.bmiHeader.biClrUsed) +
    			((compression == BI_BITFIELDS) ? 3 : 0));
    	else
    		lpDIBBits = (LPVOID)(bmInfo.bmiColors + nColors);
    
    
    	// Compute the cosine and sine only once
    	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
    	int x1 = (int)(-nHeight * sine);
    	int y1 = (int)(nHeight * cosine);
    	int x2 = (int)(nWidth * cosine - nHeight * sine);
    	int y2 = (int)(nHeight * cosine + nWidth * sine);
    	int x3 = (int)(nWidth * cosine);
    	int y3 = (int)(nWidth * sine);
    
    	int minx = min(0,min(x1, min(x2,x3)));
    	int miny = min(0,min(y1, min(y2,y3)));
    	int maxx = max(x1, max(x2,x3));
    	int maxy = max(y1, max(y2,y3));
    
    	int w = maxx - minx;
    	int h = maxy - miny;
    
    
    	// Create a DIB to hold the result
    	int nResultRowBytes = ((((w * bpp) + 31) & ~31) / 8);
    	long len = nResultRowBytes * h;
    	int nHeaderSize = ((LPBYTE)lpDIBBits-(LPBYTE)hDIB) ;
    	HANDLE hDIBResult = GlobalAlloc(GMEM_FIXED,len+nHeaderSize);
    	// Initialize the header information
    	memcpy( (void*)hDIBResult, (void*)hDIB, nHeaderSize);
    	BITMAPINFO &bmInfoResult = *(LPBITMAPINFO)hDIBResult ;
    	bmInfoResult.bmiHeader.biWidth = w;
    	bmInfoResult.bmiHeader.biHeight = h;
    	bmInfoResult.bmiHeader.biSizeImage = len;
    
    	LPVOID lpDIBBitsResult = (LPVOID)((LPBYTE)hDIBResult + nHeaderSize);
    
    	// Get the back color value (index)
    	ZeroMemory( lpDIBBitsResult, len );
    	DWORD dwBackColor;
    	switch(bpp)
    	{
    	case 1:	//Monochrome
    		if( clrBack == RGB(255,255,255) )
    			memset( lpDIBBitsResult, 0xff, len );
    		break;
    	case 4:
    	case 8:	//Search the color table
    		int i;
    		for(i = 0; i < nColors; i++ )
    		{
    			if( bmInfo.bmiColors[i].rgbBlue ==  GetBValue(clrBack)
    				&& bmInfo.bmiColors[i].rgbGreen ==  GetGValue(clrBack)
    				&& bmInfo.bmiColors[i].rgbRed ==  GetRValue(clrBack) )
    			{
    				if(bpp==4) i = i | i<<4;
    				memset( lpDIBBitsResult, i, len );
    				break;
    			}
    		}
    		// If not match found the color remains black
    		break;
    	case 16:
    		// Windows95 supports 5 bits each for all colors or 5 bits for red & blue
    		// and 6 bits for green - Check the color mask for RGB555 or RGB565
    		if( *((DWORD*)bmInfo.bmiColors) == 0x7c00 )
    		{
    			// Bitmap is RGB555
    			dwBackColor = ((GetRValue(clrBack)>>3) << 10) +
    					((GetRValue(clrBack)>>3) << 5) +
    					(GetBValue(clrBack)>>3) ;
    		}
    		else
    		{
    			// Bitmap is RGB565
    			dwBackColor = ((GetRValue(clrBack)>>3) << 11) +
    					((GetRValue(clrBack)>>2) << 5) +
    					(GetBValue(clrBack)>>3) ;
    		}
    		break;
    	case 24:
    	case 32:
    		dwBackColor = (((DWORD)GetRValue(clrBack)) << 16) |
    				(((DWORD)GetGValue(clrBack)) << 8) |
    				(((DWORD)GetBValue(clrBack)));
    		break;
    	}
    
    
    	// Now do the actual rotating - a pixel at a time
    	// Computing the destination point for each source point
    	// will leave a few pixels that do not get covered
    	// So we use a reverse transform - e.i. compute the source point
    	// for each destination point
    
    	for( int y = 0; y < h; y++ )
    	{
    		for( int x = 0; x < w; x++ )
    		{
    			int sourcex = (int)((x+minx)*cosine + (y+miny)*sine);
    			int sourcey = (int)((y+miny)*cosine - (x+minx)*sine);
    			if( sourcex >= 0 && sourcex < nWidth && sourcey >= 0
    				&& sourcey < nHeight )
    			{
    				// Set the destination pixel
    				switch(bpp)
    				{
    					BYTE mask;
    				case 1:		//Monochrome
    					mask = *((LPBYTE)lpDIBBits + nRowBytes*sourcey +
    						sourcex/8) & (0x80 >> sourcex%8);
    					//Adjust mask for destination bitmap
    					mask = mask ? (0x80 >> x%8) : 0;
    					*((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
    								(x/8)) &= ~(0x80 >> x%8);
    					*((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
    								(x/8)) |= mask;
    					break;
    				case 4:
    					mask = *((LPBYTE)lpDIBBits + nRowBytes*sourcey +
    						sourcex/2) & ((sourcex&1) ? 0x0f : 0xf0);
    					//Adjust mask for destination bitmap
    					if( (sourcex&1) != (x&1) )
    						mask = (mask&0xf0) ? (mask>>4) : (mask<<4);
    					*((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
    							(x/2)) &= ~((x&1) ? 0x0f : 0xf0);
    					*((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
    							(x/2)) |= mask;
    					break;
    				case 8:
    					BYTE pixel ;
    					pixel = *((LPBYTE)lpDIBBits + nRowBytes*sourcey +
    							sourcex);
    					*((LPBYTE)lpDIBBitsResult + nResultRowBytes*(y) +
    							(x)) = pixel;
    					break;
    				case 16:
    					DWORD dwPixel;
    					dwPixel = *((LPWORD)((LPBYTE)lpDIBBits +
    							nRowBytes*sourcey + sourcex*2));
    					*((LPWORD)((LPBYTE)lpDIBBitsResult +
    						nResultRowBytes*y + x*2)) = (WORD)dwPixel;
    					break;
    				case 24:
    					dwPixel = *((LPDWORD)((LPBYTE)lpDIBBits +
    						nRowBytes*sourcey + sourcex*3)) & 0xffffff;
    					*((LPDWORD)((LPBYTE)lpDIBBitsResult +
    						nResultRowBytes*y + x*3)) |= dwPixel;
    					break;
    				case 32:
    					dwPixel = *((LPDWORD)((LPBYTE)lpDIBBits +
    						nRowBytes*sourcey + sourcex*4));
    					*((LPDWORD)((LPBYTE)lpDIBBitsResult +
    						nResultRowBytes*y + x*4)) = dwPixel;
    				}
    			}
    			else
    			{
    				// Draw the background color. The background color
    				// has already been drawn for 8 bits per pixel and less
    				switch(bpp)
    				{
    				case 16:
    					*((LPWORD)((LPBYTE)lpDIBBitsResult +
    						nResultRowBytes*y + x*2)) =
    						(WORD)dwBackColor;
    					break;
    				case 24:
    					*((LPDWORD)((LPBYTE)lpDIBBitsResult +
    						nResultRowBytes*y + x*3)) |= dwBackColor;
    					break;
    				case 32:
    					*((LPDWORD)((LPBYTE)lpDIBBitsResult +
    						nResultRowBytes*y + x*4)) = dwBackColor;
    					break;
    				}
    			}
    		}
    	}
    
    	return hDIBResult;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Image rotation - doesn't always work
    By ulillillia in forum C Programming
    Replies: 12
    Last Post: 05-03-2007, 12:46 PM
  2. Bitmap rotation
    By dwks in forum Game Programming
    Replies: 6
    Last Post: 01-11-2006, 12:24 AM
  3. Bitmap Rotation System Questions
    By loopshot in forum Game Programming
    Replies: 2
    Last Post: 05-13-2005, 01:32 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. OpenGL -- Bitmaps
    By HQSneaker in forum Game Programming
    Replies: 14
    Last Post: 09-06-2004, 04:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21