Question

Here is my question: How do I loop through the stuff pointed by an IntPtr in C#? I have C# code calling C++ code. The C++ code returns a pointer to a piece of image buffer. The interface between C# and C++ is an IntPtr variable declared in C# So here is my C# code:

private IntPtr _maskData;

public void LoadMask(string maskName)
{            
     _maskData = Marshal.AllocHGlobal(_imgWidth * _imgHeight * 1);
     ReadImage(maskName, ref _maskData);
}

[DllImport(@"D:\Projects\ImageStatistics\ImageStatisticsEllipse\Debug\DiskIO.dll", EntryPoint = "ReadImage")]
        private static extern int ReadImage([MarshalAs(UnmanagedType.LPWStr)]string path, ref IntPtr outputBuffer);

Here is my C++ code:

DllExport_ThorDiskIO ReadImage(char *selectedFileName, char* &outputBuffer)
{
    TIFF* image;
    tsize_t stripSize;
    unsigned long imageOffset, result;
    int stripMax, stripCount;
    unsigned long bufferSize;
    wchar_t * path = (wchar_t*)selectedFileName;
    bool status;

    // Open the TIFF image
    if((image = tiffDll->TIFFOpenW(path, "r")) == NULL){
        //      logDll->TLTraceEvent(VERBOSE_EVENT,1,L"Could not open incoming image");

    }

    // Read in the possibly multiple strips
    stripSize = tiffDll->TIFFStripSize(image);
    stripMax = tiffDll->TIFFNumberOfStrips (image);
    imageOffset = 0;

    bufferSize = tiffDll->TIFFNumberOfStrips (image) * stripSize;

    for (stripCount = 0; stripCount < stripMax; stripCount++)
    {
        if((result = tiffDll->TIFFReadEncodedStrip (image, stripCount, outputBuffer + imageOffset, stripSize)) == -1)
        {
            //logDll->TLTraceEvent(VERBOSE_EVENT,1,L"Read error on input strip number");
        }
        imageOffset += result;
    }

    // Close the TIFF image
    tiffDll->TIFFClose(image);

    if(outputBuffer > 0)
    {
        //logDll->TLTraceEvent(VERBOSE_EVENT,1,L"inside output buffer: TRUE");
        status = TRUE;
    }
    else
    {
        //logDll->TLTraceEvent(VERBOSE_EVENT,1,L"inside output buffer: FALSE");
        status = FALSE;
    }   
    return status;  
}

So right now I think I can get the IntPtr successfully, but the question really is: How do I use it? How do I loop through each pixel in the image buffer, something like (pseudo code):

for (int y = 0; y < imgHeight; y++)
    for (int x = 0; x < imgWidth; x++)
    {
        int pixVal = IntPtr[y * imgWidth + x ];

        // do something to process the pixel value here....

    }
Was it helpful?

Solution

This is how to loop through an image pointed to using an IntPtr (pointer to native memory)

//assume this actually points to something (not zero!!)
IntPtr pNative = IntPtr.Zero;

//assume these are you image dimensions
int w=640; //width
int h=480; //height
int ch =3; //channels

//image loop
//use unsafe
//this is very fast!! 
unsafe
{
    for (int r = 0; r < h; r++)
    {
        byte* pI = (byte*)pNative.ToPointer() + r*w*ch; //pointer to start of row
        for (int c = 0; c < w; c++)
        {
            pI[c * ch]      = 0; //red
            pI[c * ch+1]    = 0; //green
            pI[c * ch+2]    = 0; //blue

//also equivalent to *(pI + c*ch)  = 0 - i.e. using pointer arythmetic;
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top