Converting c++ array and pointers to C#

  .net, arrays, c++, pointers

I want to know how to convert the below snipped (c++) into C#. The issue is the c++ code is using pointer notations and not sure what is the equivalent in c#.

Tried different syntax to convert the c++ code into C#, not completely successful.

void DataExtractor::runM1(unsigned char* slice, unsigned short* frame, int nALines, int nHeight)
 {
 if (!slice)
 {
    return;
 }

// Apply 
unsigned char* p1 = slice;
for (int k = nHeight - 1; k >= 0; k--)
{
    int nRow = (B_FLIP ? k : (nHeight - 1 - k));

    // Reverse order
    unsigned short* p2 = frame + nALines * nRow;

    unsigned short* p2End = p2 + nALines;
    for (; p2 < p2End;)
    {
        unsigned short M1 = unsigned short((NEW_MAX - NEW_MIN + 1) / 256.0)* *p1++ + NEW_MIN;
        if (M1 < 256)
        {
            M1 = 256;
        }
        *p2++ = M1;
    }
}

}

The calling sequence is like this :

================================

            DataExtractor* fdsDataExtract = new DataExtractor();
            unsigned short** inputVol = new unsigned short*[nFrames];
            float** preTopQVol = new float*[nFrames];

            inputVol[iFrame] = new unsigned short[nALines*nHeight];
            preTopQVol[iFrame] = new float[nALines*nHeight];

            // Read Image Frame
            unsigned char* slice = scanImage[iFrame];


            //-----------------------------------


            dataExtractor->uncompressImg(slice, inputVol[iFrame], nALines, nHeight);


            //----------------------------------

            DataExtractor* fdsDataExtract = new DataExtractor();
            unsigned short** inputVol = new unsigned short*[nFrames];
            float** preTopQVol = new float*[nFrames];

            inputVol[iFrame] = new unsigned short[nALines*nHeight];
            preTopQVol[iFrame] = new float[nALines*nHeight];

            // Read Image Frame
            unsigned char* slice = scanImage[iFrame];


            //-----------------------------------


            dataExtractor->uncompressImg(slice, inputVol[iFrame], nALines, nHeight);


            //----------------------------------


            DataExtractor.cpp
            =====================

            #include "DataExtractor.h"

            #define B_FLIP 0
            #define NEW_MIN 0
            #define NEW_MAX 65535



            void DataExtractor::uncompressImg(unsigned char * slice, unsigned short * inputImg, int nWidth, int nHeight)
            {
                runM1(slice, inputImg, nWidth, nHeight);
            }



            void DataExtractor::runM1(unsigned char* slice, unsigned short* frame, int nALines, int nHeight)
            {
                if (!slice)
                {
                    return;
                }

                // Apply 
                unsigned char* p1 = slice;
                for (int k = nHeight - 1; k >= 0; k--)
                {
                    int nRow = (B_FLIP ? k : (nHeight - 1 - k));

                    // Reverse order
                    unsigned short* p2 = frame + nALines * nRow;

                    unsigned short* p2End = p2 + nALines;
                    for (; p2 < p2End;)
                    {
                        unsigned short M1 = unsigned short((NEW_MAX - NEW_MIN + 1) / 256.0)* *p1++ + NEW_MIN;
                        if (M1 < 256)
                        {
                            M1 = 256;
                        }
                        *p2++ = M1;
                    }
                }
            }

Source: Windows Questions C++

LEAVE A COMMENT