Invert (mirror) a bitmap


We can invert an image either laterally or vertically. The effect of the inversions is shown in the image below. The image on the top left is the normal image, the image on the top right is laterally inverted, the image on the bottom left is vertically inverted and the image on the bottom right has been inverted twice - once laterally and once vertically. The effect of applying both kinds of inversion is that the image gets rotated by 180 degrees.

Two functions have been listed below, one of them will draw the image onto a device context after inverting it and the other will create a new bitmap with the inverted image.

Function 1: DrawInvertedBitmap

The DrawInvertedBitmap() function draws an inverted image of a bitmap at the specified location. It uses StretchBlt() to do the actual inversion. When the signs of the source width and the destination width are different then the image is inverted along the x-axis. When the signs of the source height and the destination height are different then the image is inverted along the y-axis.
// DrawInvertedBitmap	- Draws the bitmap after inverting it
// hBimtap		- Bitmap handle
// hPal			- Palette to use when drawing the bitmap
// bLateral		- Flag to indicate whether to invert laterally or vertically
// xDest		- X coordinate of top left corner to draw at
// yDest		- Y coordinate of top left corner to draw at
void DrawInvertedBitmap( CDC *pDC, HBITMAP hBitmap, HPALETTE hPal, BOOL bLateral, 
							int xDest, int yDest )
{
	// Create a memory DC compatible with the destination DC
	CDC memDC;
	memDC.CreateCompatibleDC( pDC );

	// Get logical coordinates
	BITMAP bm;
	::GetObject( hBitmap, sizeof( bm ), &bm );

	//memDC.SelectObject( &bitmap );
	HBITMAP hBmOld = (HBITMAP)::SelectObject( memDC.m_hDC, hBitmap );
	
	
	// Select and realize the palette
	if( hPal && pDC->GetDeviceCaps(RASTERCAPS) & RC_PALETTE)
	{
		SelectPalette( pDC->GetSafeHdc(), hPal, FALSE );
		pDC->RealizePalette();
	}
	if( bLateral )
		pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC, 
				bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );
	else
		pDC->StretchBlt( xDest, yDest, bm.bmWidth, bm.bmHeight, &memDC, 
				0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY );

	// Restore the old bitmap
	::SelectObject( memDC.m_hDC, hBmOld );
}

Function 2: GetInvertedBitmap

The GetInvertedBitmap() function creates a new bitmap to store the inverted image. This function too uses the StretchBlt() function for the actual inversion. In this case the destination device context is another memory device context so that the image is rendered into a bitmap.
// GetInvertedBitmap	- Creates a new bitmap with the inverted image
// Returns		- Handle to a new bitmap with inverted image
// hBitmap		- Bitmap to invert
// bLateral		- Flag to indicate whether to invert laterally or vertically
HBITMAP GetInvertedBitmap( HBITMAP hBitmap, BOOL bLateral )
{
	// Create a memory DC compatible with the display
	CDC sourceDC, destDC;
	sourceDC.CreateCompatibleDC( NULL );
	destDC.CreateCompatibleDC( NULL );

	// Get logical coordinates
	BITMAP bm;
	::GetObject( hBitmap, sizeof( bm ), &bm );

	// Create a bitmap to hold the result
	HBITMAP hbmResult = ::CreateCompatibleBitmap(CClientDC(NULL), 
						bm.bmWidth, bm.bmHeight);

	// Select bitmaps into the DCs
	HBITMAP hbmOldSource = (HBITMAP)::SelectObject( sourceDC.m_hDC, hBitmap );
	HBITMAP hbmOldDest = (HBITMAP)::SelectObject( destDC.m_hDC, hbmResult );
	
	if( bLateral )
		destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC, 
				bm.bmWidth-1, 0, -bm.bmWidth, bm.bmHeight, SRCCOPY );
	else
		destDC.StretchBlt( 0, 0, bm.bmWidth, bm.bmHeight, &sourceDC, 
				0, bm.bmHeight-1, bm.bmWidth, -bm.bmHeight, SRCCOPY );

	// Reselect the old bitmaps
	::SelectObject( sourceDC.m_hDC, hbmOldSource );
	::SelectObject( destDC.m_hDC, hbmOldDest );

	return hbmResult;
}



Comments

  • Replica Oakley M Frame wholesale store

    Posted by cfjvueeyi on 06/27/2013 01:07pm

    Cheap Oakley Sunglasses ,Oakley sunglasses in the living space, polarization, for that Asian sports, active Limited and exclusive edition sometimes appears as timeless and stunning sunglasses. A shift from sports gear sunglasses, super-polished and chic Armani superb range of a fresh style just put into the gathering. fake oakley sunglasses ,You will be glad to know this is not merely to offer a stylish celebrity to see you, however these sunglasses, and in some cases to produce excellent UV protection of their own eyes. Oakley sunglasses designed each and every year to change, like almost every other styles. However, different alternatives, an excellent practical fashion occasions. OAkley dispAtch ,Replacement lenses twin-screw components, the effective consolidation with the lens 2, strict distorted test to make certain the toughness and stability on the frame towards the using high-definition optical technology, so the object appears sharp and clear. People always want to buy top quality, though over the discount prices. How should we purchase a two of their own model of sunglasses? If you realise the glasses frame distortion, sagging dashing towards dealer for a free adjustment or lens clarity affected. Especially frame by specialized type O substance material, and that is closely flexibility and production, to soak up the impact. This particular function, relaxed glasses of Oakley sunglasses. The popularity is changing rapidly, classic maintenance isn't good! Therefore, designers are constantly updated sunglasses to show the newest model, instead of behind it's natural being eyes spacious. Oakley polarized Oakley online to find the safety needed, there is a perfect couple of sunglasses. A large collection of frames and lenses they are available, and price range. A really professional sports mirror using a unique personality, the whole array of functionality to win the respect of countless celebrities around the world. Recently introduced the "wearable electronics" to accumulate weapons to offer Bluetooth solutions, and implantation on the solution framework to reduce the necessitie of the headset, headphone, line and wire. Oakley sunglasses they HDO (high-definition optical) is well know, and so they continue to use this breakthrough technology within their design. You'll find an honest company, providing high-quality products, the best cost on the minutes of the most effective pattern, variety, sunglasses add-ons monitor.

    Reply
  • Replica Oakley X Squared for sale

    Posted by dxnttwigk on 06/25/2013 01:05pm

    chEap oakLEy SungLaSSeS ,Oakley sunglasses are often people the depth on the main name is well known world wide, to be sure the optical quality, it could beautify almost any clothes you wear. Exciting glasses Oakley style and chic fighter born designer labels Oakley solar shades to provide the usually more stylish, using a selection of substances. Replica Oakley Sunglasses ,You'll be able to make a decision about sun glasses, male, female or neutral trend, Oakley run, transparent dark framework and a lot of like glasses. Fashion: considering stylish Oakley sunglasses manufacturer usually is: pilot Oakley sunglasses, clip cups, lens tilt, flip Oakley sun's shadow copy Oakley sun glasses. Cheap Oakley Holbrook ,Regardless of the sort of you may be, you?? Will discover always the cheapest Oakley sunglasses on your design. Sunglasses every various kind of an individual color, although the ancient sunglasses brand name is all set, nevertheless the eyes in the first factor. The outer skin and eyes close to the photovoltaic ultraviolet (UV) risk. This is actually the well-known experience UV sun, might result in a burning sun damage, and finally cancer of the skin. Pointed out on the way to determine the Oakley sun's shadow, how come not we here we are at re-examine why teenagers need to use the Oakley shades. The primary reason is really because they could increase security on the eyes. Your actual secret society's attention to orbital, which shows well-known brands have no need for a higher temperature the complete ray of hope to solve those two 8 oz glasses. Oakley shades of good quality (especially the "surround variety), major safety and solar power, ultraviolet about the eye to offer, there're suited to people of all ages. Hardly believe you can succeed, whether the most people feel that your move is tantamount to breaks against it, but who'd understand that your creation can be a mainstay of certain areas. Actually your discerning noted the unique tone of Oakley, Oakley colors enables you to observe the product from the hour. Many people like things could possibly be excellent lens using the polarization. Most lenses are likely to be plutonite This is a specific polycarbonate use a great deal of comfort. Oakley sunglasses are the most useful. They might preferably be the planet's most stylish and chic, Oakley sunglasses collection is obviously very stylish and fashionable. It is a real collection.

    Reply
  • See it!

    Posted by wherlereeni on 10/23/2012 04:19pm

    Amazing! when can you buy viagra from? , cialis wears off

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds