0

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.

2
  • Thanks for the suggestion, I tried that but it seemed to require almost twice the CPU as the method described below. I'm guessing it's because the bitmap data is first copied via the Bitmap constructor, and then again via RotateFlip. The program is running on a rather old and slow embedded Windows XP system so I need to make it as efficient as possible. Aug 12, 2012 at 16:10

1 Answer 1

0

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.

1
  • I ended up copying (with appropriate transformations as you advised) from the image data supplied via capVideoStreamCallback to a buffer that was then displayed by DrawDibDraw in the next paint event. Seems to work straightforwardly and efficiently - thanks! Aug 12, 2012 at 16:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.