How to use Pointer in microframework?


 private void ReadImage()
        {
            int i, j;
            //GreyImage = new int[Width][Height];  //[Row,Column]
            Debug.GC(true);
            
            GreyImage = new int[Width *Height ];
            
            Bitmap image = Obj;
            
            //BitmapData bitmapData1 = image.LockBits(new Rectangle(0, 0, image.Width, image.Height),
                                     //ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
            unsafe
            {
                byte* imagePointer1 = (byte*)bitmapData1.Scan0;

                for (i = 0; i < bitmapData1.Height; i++)
                {
                    for (j = 0; j < bitmapData1.Width; j++)
                    {
                        GreyImage[j * i] = (int)((imagePointer1[0] + imagePointer1[1] + imagePointer1[2]) / 3.0);
                        //4 bytes per pixel
                        imagePointer1 += 4;
                    }//end for j
                    //4 bytes per pixel
                    imagePointer1 += bitmapData1.Stride - (bitmapData1.Width * 4);
                }//end for i
            }//end unsafe
            image.UnlockBits(bitmapData1);
            return;
        }

but unsafe and many other types of function not support in microframework . so how to use pointers

@ biren - right click your solution select properties then build and tick allow unsafe code

As a side note - i think you will struggle to use alot of these functions from AForge on the microframework.

@ Justin - yes i want to use pointer. is this possible without unsafe?

@ biren - nope

WHY do you want to use a pointer? What advantage is there for the type code you posted?

@ Jeff_Birt - It’s alot faster, it’s from a library aimed at the full framework.

Faster than what and on what platform? Any array is a chunk of memory with a series of consecutive objects. An array of bytes therefore looks like this in memory “ByteByteByte…”. When you access an array by a pointer you are accessing each byte by its position in the array. When you access an array by index myArray(someIndex) you are essentially doing the same thing. (Yes there may be some overhead.)

The huge advantage of pointers though is being able to pass a pointer to an object rather than having to make a copy of the object. NETMF like .NET treats arrays as reference types so you pass a reference not a copy by default. (So passing a pointer is not an advantage when you are already passing a reference). Pointers can let you do other tricks like access a byte wide chuck of data at a time from an array of integers for example (but that is tricky and a really good way to create problems.)

Pointers are the #1 way to make your life difficult but are useful at times. My ‘point’ though is just don’t assume you have to do something with pointers, or that by using pointers your code will be better/faster.

In the code the OP posted all the arrays in question were declared locally, so the only speed advantage would be in how NETMF might differ in dealing with a pointer as opposed to an array index. Since NETMF is interpreted I suspect there would be no difference whatsoever. Which brings me to my central theme, “Faster than what and on what platform?”

Try it both ways and see what works better/faster. My guess is that if your concerned with doing a greyscale conversion quickly RLP would be the way to go.

in order to use pointers you have to be able to pin an object. is pinning supported in MF?

Most likely not.

@ Jeff_Birt - I’m talking about image manipulation in the full framework not netmf.