PDA

Click to See Complete Forum and Search --> : Image Raw Data Copy


prodigal son
Jan 17th, 2006, 07:13 AM
Hi,

I have an intptr that points to an image stored in memory. The image is a frame from an avi stream. I need to copy the raw data from the image into a structure in unmanaged memory. What is the best way of doing this?

I have received hints but i am not sure how to implement the following. Do any of you gurus have an idea? :ehh:

-=Code Hint=-

Rather than copying these to managed memory, copy them over the OpenCV buffer allocated for the data. (If you have to have the data in managed memory, then I guess you must copy back from managed to unmanaged. )

byte *src = what I get from the grabber, before it is marshaled to managed memory,

byte *dest = (byte *) this.getPixelAddr(0,0,0);

somehow raw copy the data from src to dest (I think there are some raw copy utils in System.Runtime.InteropServices.Marshall.Copy)

There is a gotcha: Windows images are “upside down” relative to DirectX images, so your copy may need to go one scan line at a time, and flip the image as it copies.
-=Code Hint=-

What is the best marshal.copy method to use in this situation and will i have to take into consideration that the image is upside down?

Paul

RohanWest
Jan 18th, 2006, 05:00 AM
Hi, i have worked with some unsafe image processing algorithms in C#.

Firstly you will need to find out how the raw data is structured, how much memory it uses... if it is a simple Bitmap Format then you can use something like this to load the bitmap.


public static Bitmap LoadBitmap(string path)
{
Bitmap srcBmp = new Bitmap(path);
Bitmap destBmp = new Bitmap(srcBmp.Width,srcBmp.Height,srcBmp.PixelFormat);
Rectangle bmpRect = new Rectangle(0,0,srcBmp.Width,srcBmp.Height);

ColorPalette destPal = srcBmp.Palette;

BitmapData srcData = srcBmp.LockBits(bmpRect,ImageLockMode.ReadOnly,srcBmp.PixelFormat);
BitmapData destData = destBmp.LockBits(bmpRect,ImageLockMode.WriteOnly,srcBmp.PixelFormat);

Byte* sourceRow, destRow;
int bytesPerRow = srcData.Stride;

for(int y=0;y<srcData.Height;y++)
{
sourceRow = (byte*)srcData.Scan0 + (y * srcData.Stride);
destRow = (byte*)destData.Scan0 + (y * destData.Stride);
for(int x=0;x<bytesPerRow;x++)
{
//Copy the data a byte at a time
*destRow = *sourceRow;
//Note that when sourceRow and destRow are incremented they are only moving
//to the next byte in the row, not to the next row
sourceRow++;
destRow++;
}
}

srcBmp.UnlockBits(srcData);
destBmp.UnlockBits(destData);

//Get rid of the source bitmap as we don't need it anymore
srcBmp.Dispose();
srcBmp = null;

//Apply the source palette
destBmp.Palette = destPal;

return destBmp;
}

prodigal son
Jan 18th, 2006, 08:16 PM
The image stream is presented as a intptr to a buffered pixel stream. I wouldnt have thought that converting it to a bitmap first would be too efficient as efficiency is key if i am going to have the app run in real time.

I am a relatively new programmer and have a lot to learn. I am having trouble working how to copy the pixel stream at the intptr to the raw data of the empty sharpercv structure.

Do i have to convert the intptr to another pointer type and what marshal.copy would be best to do this. Learning is all part of the process so sorry if i ask too simple questions.