What is the most efficient method of orthogonally rotating a device-independent bitmap (DIB) for display in a window?

StackOverflow https://stackoverflow.com/questions/11917466

Question

I'm writing some code which takes a bitmap from an attached webcam, rotates it either 90, 180 or 270 degrees clockwise and displays it in a window.

The bitmap is originally supplied as a device independent bitmap. What is the most efficient series of calls (in terms of copying memory) that would allow me to go from source (bitmap) to destination (rotated on screen)?

I'm using C++ and Win32 API.

Was it helpful?

Solution

You have to understand how a bitmap is represented in memory, it's row wise. You can choose it to be upside down or not. So for example you start at the top-left position then you get all the pixels at the first row (x=0..cx, y=0) until you reach the width of the bitmap (cx). Then you might have a stride (some padding bytes) and then the next row of pixels (x=0..cx, y=1) starts.

Keeping that in mind the transformations are trivial: for 90 and 270 degrees you need to copy each pixel individually. For 180 degrees you can 'flip' the bitmap using memcpy for each row. You can also use BitBlt with a negative height for that.

The access to the bits is provided directly by using the pointer you have received via CreateDIBSection or using GetDIBits. Remember to call GdiFlush before using the bits if you have drawn to these bitmaps as being memory DCs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top