Skip to content

Commit de48109

Browse files
committed
Fix blitting in Qt backends (which need ARGB, not RGBA)
svn path=/branches/v0_91_maint/; revision=5120
1 parent c7b2759 commit de48109

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

lib/matplotlib/backends/backend_qt4agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ def paintEvent( self, e ):
112112
w, h = int(bbox.width()), int(bbox.height())
113113
l, t = bbox.ll().x().get(), bbox.ur().y().get()
114114
reg = self.copy_from_bbox(bbox)
115-
stringBuffer = reg.to_string()
115+
stringBuffer = reg.to_string_argb()
116116
qImage = QtGui.QImage(stringBuffer, w, h, QtGui.QImage.Format_ARGB32)
117117
pixmap = QtGui.QPixmap.fromImage(qImage)
118118
p = QtGui.QPainter( self )

lib/matplotlib/backends/backend_qtagg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ def paintEvent( self, e ):
118118
w, h = int(bbox.width()), int(bbox.height())
119119
l, t = bbox.ll().x().get(), bbox.ur().y().get()
120120
reg = self.copy_from_bbox(bbox)
121-
stringBuffer = reg.to_string()
121+
stringBuffer = reg.to_string_argb()
122122
qImage = qt.QImage(stringBuffer, w, h, 32, None, 0, qt.QImage.IgnoreEndian)
123123
self.pixmap.convertFromImage(qImage, qt.QPixmap.Color)
124124
p.drawPixmap(qt.QPoint(l, self.renderer.height-t), self.pixmap)

src/_backend_agg.cpp

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,33 @@ Py::Object BufferRegion::to_string(const Py::Tuple &args) {
224224
return Py::String(PyString_FromStringAndSize((const char*)aggbuf.data,aggbuf.height*aggbuf.stride), true);
225225
}
226226

227+
Py::Object BufferRegion::to_string_argb(const Py::Tuple &args) {
228+
// owned=true to prevent memory leak
229+
Py_ssize_t length;
230+
char* pix;
231+
char* begin;
232+
char* end;
233+
char tmp;
234+
235+
PyObject* str = PyString_FromStringAndSize((const char*)aggbuf.data, aggbuf.height*aggbuf.stride);
236+
if (PyString_AsStringAndSize(str, &begin, &length)) {
237+
throw Py::TypeError("Could not create memory for blit");
238+
}
239+
240+
pix = begin;
241+
end = begin + (aggbuf.height * aggbuf.stride);
242+
while (pix != end) {
243+
// Convert rgba to argb
244+
tmp = pix[3];
245+
pix[3] = pix[2];
246+
pix[2] = pix[1];
247+
pix[1] = pix[0];
248+
pix[0] = pix[3];
249+
pix += 4;
250+
}
227251

252+
return Py::String(str, true);
253+
}
228254

229255

230256
const size_t
@@ -2612,7 +2638,8 @@ void BufferRegion::init_type() {
26122638

26132639
add_varargs_method("to_string", &BufferRegion::to_string,
26142640
"to_string()");
2615-
2641+
add_varargs_method("to_string_argb", &BufferRegion::to_string_argb,
2642+
"to_string_argb()");
26162643
}
26172644

26182645

src/_backend_agg.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class SafeSnap {
6767
SafeSnap() : first(true), xsnap(0.0), lastx(0.0), lastxsnap(0.0),
6868
ysnap(0.0), lasty(0.0), lastysnap(0.0) {}
6969
SnapData snap (const float& x, const float& y);
70-
70+
7171
private:
7272
bool first;
7373
float xsnap, lastx, lastxsnap, ysnap, lasty, lastysnap;
@@ -85,6 +85,7 @@ class BufferRegion : public Py::PythonExtension<BufferRegion> {
8585
agg::rect rect;
8686
bool freemem;
8787
Py::Object to_string(const Py::Tuple &args);
88+
Py::Object to_string_argb(const Py::Tuple &args);
8889

8990
static void init_type(void);
9091
virtual ~BufferRegion() {
@@ -125,7 +126,7 @@ class GCAgg {
125126
double dashOffset;
126127
double *dasha;
127128

128-
129+
129130
protected:
130131
agg::rgba get_color(const Py::Object& gc);
131132
double points_to_pixels( const Py::Object& points);
@@ -141,7 +142,7 @@ class GCAgg {
141142

142143

143144
//struct AMRenderer {
144-
//
145+
//
145146
//}
146147

147148
// the renderer

0 commit comments

Comments
 (0)