0

I'm trying to implement the following c++ code in python. However, the data of the two differs when I print it out. I read this post Receiving 16-bit integers in Python and tried both "> h" and "< h" for the unpack function, but it still doesn't seem to give me the same data as the c++ code. I do however successfully read the width and height correctly. Just the data is wrong. What am I missing?

int16_t* loadDepthImageCompressed( const char* fname ){

//now read the depth image
FILE* pFile = fopen(fname, "rb");
if(!pFile){
    std::cerr << "could not open file " << fname << std::endl;
    return NULL;
}

int im_width = 0;
int im_height = 0;
bool success = true;

std::cout << sizeof(int) << std::endl;
std::cout << sizeof(int16_t) << std::endl;

success &= ( fread(&im_width,sizeof(int),1,pFile) == 1 ); // read width of depthmap
success &= ( fread(&im_height,sizeof(int),1,pFile) == 1 ); // read height of depthmap

int16_t* depth_img = new int16_t[im_width*im_height];

std::cout << im_width << std::endl;
std::cout << im_height << std::endl;

int numempty;
int numfull;
int p = 0;

while(p < im_width*im_height ){

    success &= ( fread( &numempty,sizeof(int),1,pFile) == 1 );

    for(int i = 0; i < numempty; i++)
        depth_img[ p + i ] = 0;

    success &= ( fread( &numfull,sizeof(int), 1, pFile) == 1 );
    success &= ( fread( &depth_img[ p + numempty ], sizeof(int16_t), numfull, pFile) == (unsigned int) numfull );
    p += numempty+numfull;

}

fclose(pFile);

if(success)
    return depth_img;
else{
    delete [] depth_img;
    return NULL;
}
}

The python Code

def loadDepthImageCompressed(fname):

    with open(fname,'rb') as depth_file:

        im_width = struct.unpack('i',depth_file.read(4))[0]
        im_height = struct.unpack('i',depth_file.read(4))[0]

        depth_img = [None] * (im_width * im_height)
        p = 0
        while(p < im_width * im_height):
            numempty = struct.unpack('i',depth_file.read(4))[0]
            for i in range(numempty):
                depth_img[p + i] = 0;
            numfull = struct.unpack('i',depth_file.read(4))[0]

            for i in range(numfull):
                depth_img[p+numempty + i] = struct.unpack('>h',depth_file.read(2))[0]

            p += numempty+numfull

    return depth_img
Masa Hu
  • 127
  • 13
  • 2
    Have you stepped through it with a debugger? Which _specific_ line causes the problem? Presumably you can come up with a sample input, single operation and sample output that differs between the two languages. Then you can study the documentation to see how the operations differ between the two languages. – Lightness Races in Orbit Jan 13 '19 at 23:04
  • Which of the two languages you tagged do you actually know, and know well? Is it C++, is it Python, or is it both? If you know both languages well, then all you need to do is figure out what the C++ code is doing, throw the C++ code away, and implement the same thing in Python. If you don't know one or both languages well,I highly advise not to blindly do a line-by-line translation from C++ to Python. The reason is that if you don't know C++, then you risk missing a lot of subtleties that exist in C++ that you are not aware of. Same thing if you know C++ but not know Python. – PaulMcKenzie Jan 14 '19 at 00:17
  • The bad code is: `for i in range(numfull): depth_img[p+numempty + i] = struct.unpack('>h',depth_file.read(2))[0]` OR `(fread( &depth_img[ p + numempty ], sizeof(int16_t), numfull, pFile)`.. They're obviously two different pieces of logic.. – Brandon Jan 14 '19 at 01:05
  • @Brandon At least at first glance I don't see a big difference. The Python code works list item by list item while the C++ code reads a large block of array items at once. – Michael Butscher Jan 14 '19 at 12:43

0 Answers0