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 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477
|
// The code for the interface PyQwt <-> N-D array interface
// See: http://numpy.scipy.org/array_interface.shtml.
//
// 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.
#include <Python.h>
#include <qwt_ndarray.h>
// The NumPy Array Interface
typedef struct {
int two;
int nd;
char typekind;
int itemsize;
int flags;
Py_intptr_t *shape;
Py_intptr_t *strides;
void *data;
PyObject *descr;
} PyArrayInterface;
enum {
CONTIGUOUS = 0x1,
FORTRAN = 0x2,
ALIGNED = 0x100,
NOTSWAPPED = 0x200,
WRITABLE = 0x400,
ARR_HAS_DESCR = 0x800,
};
void trace(PyArrayInterface *source)
{
fprintf(stderr, "two: %i\n", source->two);
fprintf(stderr, "nd: %i\n", source->nd);
fprintf(stderr, "typekind: '%c'\n", source->typekind);
fprintf(stderr, "itemsize: %i\n", source->itemsize);
fprintf(stderr, "flags:");
if (source->flags & CONTIGUOUS) {
fprintf(stderr, " CONTIGUOUS");
}
if (source->flags & FORTRAN) {
fprintf(stderr, " FORTRAN");
}
if (source->flags & ALIGNED) {
fprintf(stderr, " ALIGNED");
}
if (source->flags & NOTSWAPPED) {
fprintf(stderr, " NOTSWAPPED");
}
if (source->flags & WRITABLE) {
fprintf(stderr, " WRITABLE");
}
if (source->flags & ARR_HAS_DESCR) {
fprintf(stderr, " ARR_HAS_DESCR");
}
fprintf(stderr, "\n");
fprintf(stderr, "shape: (");
if (source->nd==1) {
fprintf(stderr, "%i,", int(source->shape[0]));
} else if (source->nd>1) {
fprintf(stderr, "%i", int(source->shape[0]));
}
for (int i=1; i<source->nd; ++i) {
fprintf(stderr, ", %i", int(source->shape[0]));
}
fprintf(stderr, ")\n");
fprintf(stderr, "strides: (");
if (source->nd==1) {
fprintf(stderr, "%i,", int(source->strides[0]));
} else if (source->nd>1) {
fprintf(stderr, "%i", int(source->strides[0]));
}
for (int i=1; i<source->nd; ++i) {
fprintf(stderr, ", %i", int(source->strides[i]));
}
fprintf(stderr, ")\n");
}
int try_NDArray_to_QwtArray(PyObject *in, QwtArray<double> &out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NDArray_to_QwtArray() // QwtArray<double>\n");
#endif
if (!PyObject_HasAttrString(in, "__array_struct__")) {
return 0;
}
PyObject *csource = PyObject_GetAttrString(in, "__array_struct__");
if (!csource) { // FIXME
return 0;
}
PyArrayInterface *source =
reinterpret_cast<PyArrayInterface *>(PyCObject_AsVoidPtr(csource));
if (!source) { // FIXME
return 0;
}
#ifdef TRACE_PYQWT
trace(source);
#endif
int stride;
if ((source->two != 2)|| (source->nd != 1)) {
goto error;
}
stride = source->strides[0]/source->itemsize;
out.resize(source->shape[0]);
if (source->typekind == 'f') {
if (source->itemsize == sizeof(double)) {
double *data = reinterpret_cast<double *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(float)) {
float *data = reinterpret_cast<float *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else {
goto error;
}
} else if (source->typekind == 'i') {
if (source->itemsize == sizeof(char)) {
char *data = reinterpret_cast<char *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(short)) {
short *data = reinterpret_cast<short *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(int)) {
int *data = reinterpret_cast<int *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(long)) {
long *data = reinterpret_cast<long *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(long long)) {
long long *data = reinterpret_cast<long long *>(source->data);
for (double *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else {
goto error;
}
} else {
goto error;
}
Py_DECREF(csource);
return 1;
error:
Py_DECREF(csource);
PyErr_SetString(
PyExc_RuntimeError,
"The array is no 1D array containing real or signed integer types");
return -1;
}
int try_NDArray_to_QwtArray(PyObject *in, QwtArray<int> &out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NDArray_to_QwtArray() // QwtArray<int>\n");
#endif
if (!PyObject_HasAttrString(in, "__array_struct__")) {
return 0;
}
PyObject *csource = PyObject_GetAttrString(in, "__array_struct__");
if (!csource) { // FIXME
return 0;
}
PyArrayInterface *source =
reinterpret_cast<PyArrayInterface *>(PyCObject_AsVoidPtr(csource));
if (!source) { // FIXME
return 0;
}
#ifdef TRACE_PYQWT
trace(source);
#endif
int stride;
if ((source->two != 2) || (source->nd != 1)) {
goto error;
}
out.resize(source->shape[0]);
stride = source->strides[0]/source->itemsize;
if (source->typekind == 'i') {
if (source->itemsize == sizeof(char)) {
char *data = reinterpret_cast<char *>(source->data);
for (int *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(short)) {
short *data = reinterpret_cast<short *>(source->data);
for (int *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(int)) {
int *data = reinterpret_cast<int *>(source->data);
for (int *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(long)) {
long *data = reinterpret_cast<long *>(source->data);
for (int *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(long long)) {
long long *data = reinterpret_cast<long long *>(source->data);
for (int *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else {
goto error;
}
} else {
goto error;
}
Py_DECREF(csource);
return 1;
error:
Py_DECREF(csource);
PyErr_SetString(
PyExc_RuntimeError,
"The array is no 1D array containing signed integer types");
return -1;
}
int try_NDArray_to_QwtArray(PyObject *in, QwtArray<long> &out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NumPyArray_to_QwtArray() // QwtArray<long>\n");
#endif
if (!PyObject_HasAttrString(in, "__array_struct__")) {
return 0;
}
PyObject *csource = PyObject_GetAttrString(in, "__array_struct__");
if (!csource) { // FIXME
return 0;
}
PyArrayInterface *source =
reinterpret_cast<PyArrayInterface *>(PyCObject_AsVoidPtr(csource));
if (!source) { // FIXME
return 0;
}
#ifdef TRACE_PYQWT
trace(source);
#endif
int stride;
if ((source->two != 2) || (source->nd != 1)) {
goto error;
}
out.resize(source->shape[0]);
stride = source->strides[0]/source->itemsize;
if (source->typekind == 'i') {
if (source->itemsize == sizeof(char)) {
char *data = reinterpret_cast<char *>(source->data);
for (long *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(short)) {
short *data = reinterpret_cast<short *>(source->data);
for (long *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(int)) {
int *data = reinterpret_cast<int *>(source->data);
for (long *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(long)) {
long *data = reinterpret_cast<long *>(source->data);
for (long *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else if (source->itemsize == sizeof(long long)) {
long long *data = reinterpret_cast<long long *>(source->data);
for (long *it = out.begin(); it != out.end();) {
*it++ = *data;
data += stride;
}
} else {
goto error;
}
} else {
goto error;
}
Py_DECREF(csource);
return 1;
error:
Py_DECREF(csource);
PyErr_SetString(
PyExc_RuntimeError,
"The array is no 1D array containing signed integer types");
return -1;
}
int try_NDArray_to_QImage(PyObject *in, QImage **out)
{
#ifdef TRACE_PYQWT
fprintf(stderr, "Qwt: try_NDArray_to_QImage()\n");
#endif
if (!PyObject_HasAttrString(in, "__array_struct__")) {
return 0;
}
PyObject *csource = PyObject_GetAttrString(in, "__array_struct__");
if (!csource) { // FIXME
return 0;
}
PyArrayInterface *source =
reinterpret_cast<PyArrayInterface *>(PyCObject_AsVoidPtr(csource));
if (!source) { // FIXME
return 0;
}
#ifdef TRACE_PYQWT
trace(source);
#endif
if (!((source->two==2) && (source->nd==2) && (source->flags&CONTIGUOUS))) {
Py_DECREF(csource);
PyErr_SetString(PyExc_RuntimeError,
"Image array must be an contiguous 2-D array");
return -1;
}
const int nx = source->shape[0];
const int ny = source->shape[1];
const int xstride = source->strides[0];
const int ystride = source->strides[1];
// 8 bit data
if ((source->typekind =='u') && (source->itemsize==1)) {
#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 = (char *)(source->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));
}
Py_DECREF(csource);
return 1;
}
// 32 bit data.
// FIXME: what does it do on a 64 bit platform?
// FIXME: endianness
if ((source->typekind=='u') && (source->itemsize==4)) {
#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 = (char *)(source->data) + j*ystride;
for (int i=0; i<nx; i++) {
*line++ = data[0];
*line++ = data[1];
*line++ = data[2];
*line++ = data[3];
data += xstride;
}
}
Py_DECREF(csource);
return 1;
}
PyErr_SetString(PyExc_RuntimeError,
"Data type must be an 'unsigned int' having 8 or 32 bits");
Py_DECREF(csource);
return -1;
}
// Local Variables:
// mode: C++
// c-file-style: "stroustrup"
// indent-tabs-mode: nil
// End:
|