coin3d / pivy

python bindings to coin3d
ISC License
53 stars 39 forks source link

unsigned char array has problems in windows #15

Closed looooo closed 7 years ago

looooo commented 7 years ago

https://github.com/looooo/pivy/blob/master/Inventor/nodes/SoMarkerSet.i#L34L47

InventorMentor commented 7 years ago

Hello, the following code fixes this issue for me under Windows 10 using Python 3.6 [msvc14]. Maybe you could also test this reworked version of SoMarkerSet.i with Linux? Best wishes!

%extend SoMarkerSet {
    static void addMarker(int idx, const SbVec2s & size, PyObject* string, 
                        SbBool isLSBFirst = TRUE, SbBool isUpToDown = TRUE)
    {
        short WIDTH, HEIGHT;
        size.getValue(WIDTH, HEIGHT);
        short BYTEWIDTH = (WIDTH + 7) / 2;
        char* coin_marker;
#ifdef PY_2
        if (PyString_Check(string))
        {
            coin_marker = PyString_AsString(string);
        }
#else
        if (PyUnicode_Check(string))
        {
            coin_marker = PyUnicode_AsUTF8(string);            
        }
        else if (PyBytes_Check(string))
        {
            coin_marker = PyBytes_AsString(string);
        }
#endif
        else
        {
            return;
            // raise an attribute error: PyObject string should be of type bytes!
        }
        // https://grey.colorado.edu/coin3d/classSoMarkerSet.html
        // from addMarker example:

        int byteidx = 0;
        unsigned char* bitmapbytes = NULL;
        bitmapbytes = new unsigned char[BYTEWIDTH * HEIGHT];

        for (int h = 0; h < HEIGHT; h++)
        {
            unsigned char bits = 0;
            for (int w = 0; w < WIDTH; w++)
            {
                if (coin_marker[(h * WIDTH) + w] != ' ') { bits |= (0x80 >> (w % 8)); }
                if ((((w + 1) % 8) == 0) || (w == WIDTH - 1)) {
                bitmapbytes[byteidx++] = bits;
                bits = 0;
                }
            }
        }
        SoMarkerSet::addMarker(idx, size, bitmapbytes, isLSBFirst, isUpToDown);
        delete[] bitmapbytes;
        bitmapbytes = NULL;
    }
}
looooo commented 7 years ago

wow, thanks! I spend hours on this problem...

looooo commented 7 years ago

https://github.com/looooo/pivy/commit/4102e3623c61aa98091da775dd03685f65b3432a