1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
// The code for the interface PyQwt <-> NumPy.
//
// Copyright (C) 2001-2008 Gerard Vermeulen
// Copyright (C) 2000 Mark Colclough
//
// This file is part of PyQwt.
//
// PyQwt is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// PyQwt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with PyQwt; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
// In addition, as a special exception, Gerard Vermeulen gives permission
// to link PyQwt dynamically with non-free versions of Qt and PyQt,
// and to distribute PyQwt in this form, provided that equally powerful
// versions of Qt and PyQt have been released under the terms of the GNU
// General Public License.
//
// If PyQwt is dynamically linked with non-free versions of Qt and PyQt,
// PyQwt becomes a free plug-in for a non-free program.
#ifdef HAS_NUMPY
#include <Python.h>
#include <numpy/arrayobject.h>
#include <qwt_numerical_interface.h>
#include <qwt_numpy.h>
void qwt_import_numpy() {
// this is a function which does error handling
import_array();
}
int try_NumPyArray_to_QwtArray(PyObject *in, QwtArray<double> &out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NumPyArray_to_QwtArray()\n");
#endif
if (!PyArray_Check(in))
return 0;
PyArrayObject *array = (PyArrayObject *)PyArray_ContiguousFromObject(
in, PyArray_DOUBLE, 1, 0);
if (!array) {
PyErr_SetString(PyExc_RuntimeError,
"Failed to make contiguous array of PyArray_DOUBLE");
return -1;
}
double *data = (double *) array->data;
out.resize(array->dimensions[0]);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data++;
}
Py_DECREF(array);
return 1;
}
int try_NumPyArray_to_QwtArray(PyObject *in, QwtArray<int> &out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NumPyArray_to_QwtArray()\n");
#endif
if (!PyArray_Check(in))
return 0;
PyArrayObject *array = (PyArrayObject *)PyArray_ContiguousFromObject(
in, PyArray_INT, 1, 0);
if (!array) {
PyErr_SetString(PyExc_RuntimeError,
"Failed to make contiguous array of PyArray_INT");
return -1;
}
int *data = (int *) array->data;
out.resize(array->dimensions[0]);
for (int *it = out.begin(); it != out.end();) {
*it++ = *data++;
}
Py_DECREF(array);
return 1;
}
int try_NumPyArray_to_QwtArray(PyObject *in, QwtArray<long> &out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NumPyArray_to_QwtArray()\n");
#endif
if (!PyArray_Check(in))
return 0;
PyArrayObject *array = (PyArrayObject *)PyArray_ContiguousFromObject(
in, PyArray_LONG, 1, 0);
if (!array) {
PyErr_SetString(PyExc_RuntimeError,
"Failed to make contiguous array of PyArray_LONG");
return -1;
}
long *data = (long *) array->data;
out.resize(array->dimensions[0]);
for (long *it = out.begin(); it != out.end();) {
*it++ = *data++;
}
Py_DECREF(array);
return 1;
}
int try_NumPyArray_to_QImage(PyObject *in, QImage **out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NumPyArray_to_QImage()\n");
#endif
if (!PyArray_Check(in))
return 0;
if (2 != ((PyArrayObject *)in)->nd) {
PyErr_SetString(PyExc_RuntimeError,
"Image array must be 2-dimensional");
return -1;
}
const int nx = ((PyArrayObject *)in)->dimensions[0];
const int ny = ((PyArrayObject *)in)->dimensions[1];
const int xstride = ((PyArrayObject *)in)->strides[0];
const int ystride = ((PyArrayObject *)in)->strides[1];
// 8 bit data
if (((PyArrayObject *)in)->descr->type_num == PyArray_UBYTE) {
#if QT_VERSION < 0x040000
if (!(*out = new QImage(nx, ny, 8, 256))) {
#else
if (!(*out = new QImage(nx, ny, QImage::Format_Indexed8))) {
#endif
PyErr_SetString(PyExc_RuntimeError,
"failed to create a 8 bit image");
return -1;
}
for (int j=0; j<ny; j++) {
char *line = (char *)((*out)->scanLine(j));
char *data = ((PyArrayObject *)in)->data + j*ystride;
for (int i=0; i<nx; i++) {
*line++ = data[0];
data += xstride;
}
}
// initialize the palette as all gray
(*out)->setNumColors(256);
for (int i = 0; i<(*out)->numColors(); i++)
(*out)->setColor(i, qRgb(i, i, i));
return 1;
}
// 32 bit data.
// FIXME: what does it do on a 64 bit platform?
// FIXME: endianness
if (((PyArrayObject *)in)->descr->type_num == PyArray_UINT) {
#if QT_VERSION < 0x040000
if (!(*out = new QImage(nx, ny, 32))) {
#else
if (!(*out = new QImage(nx, ny, QImage::Format_ARGB32))) {
#endif
PyErr_SetString(PyExc_RuntimeError,
"failed to create a 32 bit image");
return -1;
}
for (int j=0; j<ny; j++) {
char *line = (char *)((*out)->scanLine(j));
char *data = ((PyArrayObject *)in)->data + j*ystride;
for (int i=0; i<nx; i++) {
*line++ = data[0];
*line++ = data[1];
*line++ = data[2];
*line++ = data[3];
data += xstride;
}
}
return 1;
}
PyErr_SetString(
PyExc_RuntimeError,
"Data type must be UnsignedInt8, or UnsignedInt32");
return -1;
}
PyObject *toNumpy(const QImage &image)
{
PyArrayObject *result = 0;
const int nx = image.width();
const int ny = image.height();
// 8 bit data
if (image.depth() == 8) {
int dimensions[2] = { nx, ny };
if (0 == (result = (PyArrayObject *)PyArray_FromDims(
2, dimensions, PyArray_UBYTE))) {
PyErr_SetString(PyExc_MemoryError,
"failed to allocate memory for array");
return 0;
}
const int xstride = result->strides[0];
const int ystride = result->strides[1];
for (int j=0; j<ny; j++) {
unsigned char *line = (unsigned char *)image.scanLine(j);
unsigned char *data = (unsigned char *)(result->data + j*ystride);
for (int i=0; i<nx; i++) {
data[0] = *line++;
data += xstride;
}
}
return PyArray_Return(result);
}
// 32 bit data.
if (image.depth() == 32) {
int dimensions[2] = { nx, ny };
if (0 == (result = (PyArrayObject *)PyArray_FromDims(
2, dimensions, PyArray_UINT))) {
PyErr_SetString(PyExc_MemoryError,
"failed to allocate memory for array");
return 0;
}
const int xstride = result->strides[0];
const int ystride = result->strides[1];
for (int j=0; j<ny; j++) {
unsigned char *line = (unsigned char *)image.scanLine(j);
unsigned char *data = (unsigned char *)(result->data + j*ystride);
for (int i=0; i<nx; i++) {
data[0] = *line++;
data[1] = *line++;
data[2] = *line++;
data[3] = *line++;
data += xstride;
}
}
return PyArray_Return(result);
}
return 0;
}
#endif // HAS_NUMPY
// Local Variables:
// mode: C++
// c-file-style: "stroustrup"
// indent-tabs-mode: nil
// End:
|