Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9211cb3

Browse files
committed
added image support for raw image
svn path=/trunk/matplotlib/; revision=353
1 parent 55b39ff commit 9211cb3

4 files changed

Lines changed: 82 additions & 21 deletions

File tree

examples/mri_with_eeg.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
from matplotlib.matlab import *
88
from matplotlib.lines import Line2D
99
from matplotlib.transforms import get_bbox_transform, Point, Value, Bbox,\
10-
translation_transform, zero
10+
unit_bbox
11+
12+
1113
# I use if 1 to break up the different regions of code visually
1214

1315
if 1: # load the data
@@ -41,15 +43,30 @@
4143
ticklocs = []
4244
ax = subplot(212)
4345

44-
height = 72 # height of one EEG in pixels
45-
# transform data to axes coord (0,1)
46-
46+
boxin = Bbox(
47+
Point(ax.viewLim.ll().x(), Value(-20)),
48+
Point(ax.viewLim.ur().x(), Value(20)))
49+
50+
51+
height = ax.bbox.ur().y() - ax.bbox.ll().y()
52+
boxout = Bbox(
53+
Point(ax.bbox.ll().x(), Value(-1)*height),
54+
Point(ax.bbox.ur().x(), Value(1) * height))
55+
56+
57+
transOffset = get_bbox_transform(
58+
unit_bbox(),
59+
Bbox( Point( Value(0), ax.bbox.ll().y()),
60+
Point( Value(1), ax.bbox.ur().y())
61+
))
62+
4763

4864
for i in range(numRows):
49-
trans = get_bbox_transform(ax.viewLim, ax.bbox)
65+
# effectively a copy of transData
66+
trans = get_bbox_transform(boxin, boxout)
5067
offset = (i+1)/(numRows+1)
51-
height = Value(offset) * (ax.bbox.ur().y() - ax.bbox.ll().y())
52-
trans.set_offset( (0,0), translation_transform(zero(), height) )
68+
69+
trans.set_offset( (0, offset), transOffset)
5370

5471
thisLine = Line2D(
5572
t, data[:,i]-data[0,i],
@@ -61,13 +78,20 @@
6178
ticklocs.append(offset)
6279

6380
set(gca(), 'xlim', [0,10])
64-
set(gca(), 'ylim', [0,200])
6581
set(gca(), 'xticks', arange(10))
66-
yticks = set(gca(), 'yticks', ticklocs)
82+
6783
set(gca(), 'yticklabels', ['PG3', 'PG5', 'PG7', 'PG9'])
6884

6985
# set the yticks to use axes coords on the y axis
70-
set(yticks, 'transform', ax.transAxes)
86+
ax.set_yticks(ticklocs)
87+
for tick in ax.yaxis.get_major_ticks():
88+
tick.label1.set_transform(ax.transAxes)
89+
tick.label2.set_transform(ax.transAxes)
90+
tick.tick1line.set_transform(ax.transAxes)
91+
tick.tick2line.set_transform(ax.transAxes)
92+
tick.gridline.set_transform(ax.transAxes)
93+
94+
7195
xlabel('time (s)')
7296

7397

examples/psd_demo.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
1#!/usr/bin/env python
22
# python
33

44
from matplotlib.matlab import *

src/_image.cpp

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
1+
#include <iostream>
22
#include <fstream>
33
#include <cmath>
44
#include <cstdio>
@@ -184,6 +184,7 @@ Image_resize(ImageObject *image, PyObject* args) {
184184
image->colsOut = numcols;
185185
image->rowsOut = numrows;
186186

187+
187188
size_t NUMBYTES(numrows * numcols * image->BPP);
188189
agg::int8u *buffer = new agg::int8u[NUMBYTES];
189190
image->rbufOut = new agg::rendering_buffer;
@@ -378,10 +379,12 @@ Image_dealloc(ImageObject *self)
378379
{
379380
PyObject_Del(self);
380381

381-
delete [] self->bufferIn;
382-
delete self->rbufOut;
383-
delete [] self->bufferOut;
384-
delete self->rbufIn;
382+
delete [] self->bufferIn; self->bufferIn = NULL;
383+
delete self->rbufIn; self->rbufIn=NULL;
384+
385+
delete self->rbufOut; self->rbufOut = NULL;
386+
delete [] self->bufferOut; self->bufferOut=NULL;
387+
385388

386389
}
387390

@@ -544,18 +547,33 @@ _image_fromfile(PyObject *self, PyObject *args) {
544547
}
545548

546549
char _image_fromarray__doc__[] =
547-
"fromarray(A)\n"
550+
"fromarray(A, isoutput)\n"
548551
"\n"
549552
"Load the image from a Numeric or numarray array\n"
553+
"By default this function fills the input buffer, which can subsequently\n"
554+
"be resampled using resize. If isoutput=1, fill the output buffer.\n"
555+
"This is used to support raw pixel images w/o resampling"
550556
;
551557
static PyObject *
552-
_image_fromarray(PyObject *self, PyObject *args) {
558+
_image_fromarray(PyObject *self, PyObject *args, PyObject *kwargs) {
553559

554560
PyObject *x;
555561
PyArrayObject *A;
562+
563+
int isoutput=0;
564+
565+
/* // my kwargs is segfaulting
566+
static char *kwlist[] = {"A", "isoutput", NULL};
567+
568+
569+
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|i", kwlist,
570+
&x, &isoutput))
571+
return NULL;
556572
557-
if (!PyArg_ParseTuple(args, "O", &x))
573+
*/
574+
if (!PyArg_ParseTuple(args, "Oi", &x, &isoutput))
558575
return NULL;
576+
559577

560578
//rank 2 or 3
561579
A = (PyArrayObject *) PyArray_ContiguousFromObject(x, PyArray_DOUBLE, 2, 3);
@@ -642,6 +660,20 @@ _image_fromarray(PyObject *self, PyObject *args) {
642660
return NULL;
643661
}
644662
Py_XDECREF(A);
663+
664+
if (isoutput) {
665+
// make the output buffer point to the input buffer
666+
667+
imo->rowsOut = imo->rowsIn;
668+
imo->colsOut = imo->colsIn;
669+
670+
imo->bufferOut = new agg::int8u[NUMBYTES];
671+
imo->rbufOut = new agg::rendering_buffer;
672+
imo->rbufOut->attach(imo->bufferOut, imo->colsOut, imo->rowsOut, imo->colsOut * imo->BPP);
673+
674+
for (size_t i=0; i<NUMBYTES; i++)
675+
*(imo->bufferOut +i) = *(imo->bufferIn +i);
676+
}
645677
return (PyObject *)imo;
646678
}
647679

src/_transforms.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,15 +564,20 @@ Transformation::set_offset(const Py::Tuple & args) {
564564
args.verify_length(2);
565565

566566
Py::SeqBase<Py::Object> xy = args[0];
567-
567+
//std::cout << "checking args" << std::endl;
568568
if (!check(args[1]))
569569
throw Py::TypeError("Transformation::set_offset(xy,trans) requires trans to be a Transformation instance");
570-
570+
571+
//std::cout << "getting x,y" << std::endl;
572+
571573
_usingOffset = 1;
572574
_xo = Py::Float(xy[0]);
573575
_yo = Py::Float(xy[1]);
576+
//std::cout << "casting" << std::endl;
574577
_transOffset = static_cast<Transformation*>(args[1].ptr());
578+
//std::cout << "increffing" << std::endl;
575579
Py_INCREF(_transOffset);
580+
//std::cout << "returning" << std::endl;
576581
return Py::Object();
577582
}
578583

0 commit comments

Comments
 (0)