qimageview.sip
author Hans Meine <hans_meine@gmx.net>
Fri, 05 Aug 2011 12:22:03 +0200
changeset 56 0682b7bcaca3
parent 6 a849fc90b806
permissions -rw-r--r--
try to auto-detect whether Qt comes in frameworks on OS X
     1 // -*-c++-*-
     2 %Module qimageview 0
     3 
     4 %Import QtCore/QtCoremod.sip
     5 %Import QtGui/QtGuimod.sip
     6 
     7 %ModuleCode
     8 #include <Python.h>
     9 #define PY_ARRAY_UNIQUE_SYMBOL qimageview_PyArray_API
    10 #include <numpy/arrayobject.h>
    11 %End
    12 
    13 %PostInitialisationCode
    14 import_array();
    15 %End
    16 
    17 SIP_PYOBJECT qimageview(QImage &image);
    18 %MethodCode
    19 {
    20 	NPY_TYPES dtype = NPY_NOTYPE;
    21 
    22 	npy_intp dims[2];
    23 	dims[0] = a0->height();
    24 	dims[1] = a0->width();
    25 
    26 	npy_intp strides[2];
    27 	strides[0] = a0->bytesPerLine();
    28 
    29 	switch(a0->format())
    30 	{
    31 	case QImage::Format_Indexed8:
    32 		dtype = NPY_UINT8;
    33 		strides[1] = 1;
    34 		break;
    35 	case QImage::Format_RGB32:
    36 	case QImage::Format_ARGB32:
    37 	case QImage::Format_ARGB32_Premultiplied:
    38 		dtype = NPY_UINT32;
    39 		strides[1] = 4;
    40 		break;
    41 	case QImage::Format_Invalid:
    42         PyErr_SetString(PyExc_ValueError,
    43                         "qimageview got invalid QImage");
    44 		return NULL;
    45 	default:
    46         PyErr_SetString(PyExc_ValueError,
    47                         "qimageview can only handle 8- or 32-bit QImages");
    48 		return NULL;
    49 	}
    50 
    51 	sipRes = PyArray_New(&PyArray_Type, 2, dims, dtype, strides, a0->bits(),
    52 						 0/* itemsize (ignored) */, NPY_CARRAY, NULL);
    53 
    54 	PyObject *image = PyTuple_GetItem(sipArgs, 0);
    55 	PyArray_BASE(sipRes) = image;
    56 	Py_INCREF(image);
    57 }
    58 %End