[PyQt] QImage.bits() pointer in another QThread

ukpyr666 ukpyr666 at gmail.com
Thu Nov 19 09:41:11 GMT 2009


|Debian, python 2.5.4, PyQt4 V4.6

In compiled C module trying to copy QImage alpha to 8-bit buffer.
Function called from separate QThread.
Problem in wrong buffer data shifted 29 bytes back.
When doing operation completely in main thread or in calling thread, or doing image.pixel() >> 24, alpha data is ok.

Alpha extraction function:
///////////////////////////////////////////////////////////////////////////////////////////////////////////
// Get image alpha mask to byte buffer.
//   input: QImage image.
//   output: str maskBuf.
static PyObject *gray_c32GetAlphaMask(PyObject *self, PyObject *args) {
    PyObject *image; // input ARGB32 QImage

    // parse function parameters
    if (!PyArg_ParseTuple(args, "O", &image)) return NULL;
    
    // get image width and height
    U32 w = PyInt_AsLong(PyObject_CallMethod(image, "width", NULL));
    U32 h = PyInt_AsLong(PyObject_CallMethod(image, "height", NULL));
    
    if (w*h > 0) {
        // pointer to first color pixel
        U32 *c32Buf = PyCObject_AsVoidPtr(PyObject_CallMethod(PyObject_CallMethod((PyObject *)image, "bits", NULL), "ascobject", NULL));
        U8 *maskBuf = PyMem_Malloc(w * h); // allocate output buffer

        for (U32 yCnt=0; yCnt<h; yCnt++) { // y loop
            for (U32 xCnt=0; xCnt<w; xCnt++) { // x loop
                *maskBuf = *c32Buf >> 24; // get pixel alpha value
                c32Buf++;
                maskBuf++;
            }
        }
        return Py_BuildValue("s#", maskBuf, w*h);
    }
    return Py_BuildValue("O", Py_None);
}

|



More information about the PyQt mailing list