tags:

views:

196

answers:

3

Hello, I have a method that I call and it returns bitmap data from a capture device.

It returns a pointer to the buffer data as an IntPtr and the buffer length as an int.

I know in advance that this bitmap is a 24bpp and its width and height.

The problem is that the buffer contains the bitmap data upside down and I need this data to be in the right order (reverse it).

What I do is create a for loop and using CopyMemory, I copy each line (stride) of this data from bottom to up to a newly allocated memory space.

Is there any way to make it faster than creating more memory each time I receive a new frame? It makes the application get a bit slow and consumes more memory as each bitmap is pretty big.

I do this because I use another component that analyses the bitmaps and it doesn't work propertly if the bitmaps are upside down.

I'm using .net, c# thanks!

+1  A: 

If you have instance of System.Graphics.Bitmap class, you could have used RotateFlip(RotateFlipType.RotateNoneFlipY) - but even if you created Bitmap from your pointer to buffer data, flipped it with mentioned method and passed pointer to bitmap data elsewhere, I think that would be slower than your approach.

But can't you just swap first line with last, (first+1) with (last-1), etc, without allocating new memory?

Axarydax
A: 

Why not just move the pointer to the end and then work with the data in reverse.

Mimisbrunnr
I could do that, bit as I said, I use another component that expects the data in the right order...
開発者
A: 

It's normal for a bitmap to be stored upside down in memory, and it will then have a negative stride value.

If the component that you use can't handle this, you don't have any other way than to turn the image for it. To reduce memory allocations you could of course allocate a destination bitmap once, and reuse that for each frame.

Guffa