Skip to content

Commit 6b592d9

Browse files
committed
Get wxagg extension working again. Factor out the new Bbox conversion
code into agg_py_transforms.h svn path=/branches/transforms/; revision=4159
1 parent 55bf082 commit 6b592d9

6 files changed

Lines changed: 53 additions & 99 deletions

File tree

src/_backend_agg.cpp

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -273,33 +273,6 @@ RendererAgg::RendererAgg(unsigned int width, unsigned int height, double dpi,
273273

274274
};
275275

276-
bool
277-
RendererAgg::bbox_to_rect(const Py::Object& bbox_obj, double* l, double* b, double* r, double* t) {
278-
PyArrayObject* bbox = NULL;
279-
280-
if (bbox_obj.ptr() != Py_None) {
281-
bbox = (PyArrayObject*) PyArray_FromObject(bbox_obj.ptr(), PyArray_DOUBLE, 2, 2);
282-
283-
if (!bbox || PyArray_NDIM(bbox) != 2 || PyArray_DIM(bbox, 0) != 2 || PyArray_DIM(bbox, 1) != 2) {
284-
Py_XDECREF(bbox);
285-
throw Py::TypeError
286-
("Expected a Bbox object.");
287-
}
288-
289-
*l = *(double*)PyArray_GETPTR2(bbox, 0, 0);
290-
double _b = *(double*)PyArray_GETPTR2(bbox, 0, 1);
291-
*r = *(double*)PyArray_GETPTR2(bbox, 1, 0);
292-
double _t = *(double*)PyArray_GETPTR2(bbox, 1, 1);
293-
*b = height - _t;
294-
*t = height - _b;
295-
296-
Py_XDECREF(bbox);
297-
return true;
298-
}
299-
300-
return false;
301-
}
302-
303276
template<class R>
304277
void
305278
RendererAgg::set_clipbox(const Py::Object& cliprect, R rasterizer) {
@@ -308,7 +281,7 @@ RendererAgg::set_clipbox(const Py::Object& cliprect, R rasterizer) {
308281
_VERBOSE("RendererAgg::set_clipbox");
309282

310283
double l, b, r, t;
311-
if (bbox_to_rect(cliprect, &l, &b, &r, &t)) {
284+
if (py_convert_bbox(cliprect.ptr(), l, b, r, t)) {
312285
rasterizer->clip_box((int)l, (int)b, (int)r, (int)t);
313286
}
314287

@@ -408,7 +381,7 @@ RendererAgg::copy_from_bbox(const Py::Tuple& args) {
408381

409382
Py::Object box_obj = args[0];
410383
double l, b, r, t;
411-
if (!bbox_to_rect(box_obj, &l, &b, &r, &t))
384+
if (!py_convert_bbox(box_obj.ptr(), l, b, r, t))
412385
throw Py::TypeError("Invalid bbox provided to copy_from_bbox");
413386

414387
agg::rect rect((int)l, (int)b, (int)r, (int)t);

src/_backend_agg.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,6 @@ class RendererAgg: public Py::PythonExtension<RendererAgg> {
215215
double points_to_pixels_snapto( const Py::Object& points);
216216
agg::rgba rgb_to_color(const Py::SeqBase<Py::Object>& rgb, double alpha);
217217
facepair_t _get_rgba_face(const Py::Object& rgbFace, double alpha);
218-
bool bbox_to_rect(const Py::Object& bbox, double* l, double* b, double* r, double* t);
219218
template<class R>
220219
void set_clipbox(const Py::Object& cliprect, R rasterizer);
221220
bool render_clippath(const Py::Object& clippath, const agg::trans_affine& clippath_trans);

src/_gtkagg.cpp

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "_backend_agg.h"
1515
#define PY_ARRAY_TYPES_PREFIX NumPy
1616
#include "numpy/arrayobject.h"
17+
#include "agg_py_transforms.h"
1718

1819
// the extension module
1920
class _gtkagg_module : public Py::ExtensionModule<_gtkagg_module>
@@ -71,31 +72,12 @@ class _gtkagg_module : public Py::ExtensionModule<_gtkagg_module>
7172
else {
7273
//bbox is not None; copy the image in the bbox
7374
PyObject* clipbox = args[2].ptr();
74-
PyArrayObject* bbox = NULL;
7575
double l, b, r, t;
7676

77-
try {
78-
bbox = (PyArrayObject*) PyArray_FromObject(clipbox, PyArray_DOUBLE, 2, 2);
79-
80-
if (!bbox || bbox->nd != 2 || bbox->dimensions[0] != 2 || bbox->dimensions[1] != 2) {
81-
throw Py::TypeError
82-
("Argument 3 to agg_to_gtk_drawable must be a Bbox object.");
83-
}
84-
85-
l = *(double*)PyArray_GETPTR2(bbox, 0, 0);
86-
b = *(double*)PyArray_GETPTR2(bbox, 0, 1);
87-
r = *(double*)PyArray_GETPTR2(bbox, 1, 0);
88-
t = *(double*)PyArray_GETPTR2(bbox, 1, 1);
89-
90-
Py_XDECREF(bbox);
91-
bbox = NULL;
92-
} catch (...) {
93-
Py_XDECREF(bbox);
94-
bbox = NULL;
95-
throw;
77+
if (!py_convert_bbox(clipbox, l, b, r, t)) {
78+
throw Py::TypeError
79+
("Argument 3 to agg_to_gtk_drawable must be a Bbox object.");
9680
}
97-
//std::cout << b << " "
98-
// << t << " ";
9981

10082
destx = (int)l;
10183
desty = srcheight-(int)t;
@@ -118,11 +100,8 @@ class _gtkagg_module : public Py::ExtensionModule<_gtkagg_module>
118100
agg::rect_base<int> region(destx, desty, (int)r, srcheight-(int)b);
119101
destrb.copy_from(*aggRenderer->renderingBuffer, &region,
120102
-destx, -desty);
121-
122-
123103
}
124104

125-
126105
/*std::cout << desty << " "
127106
<< destheight << " "
128107
<< srcheight << std::endl;*/

src/_tkagg.cpp

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "agg_basics.h"
1616
#include "_backend_agg.h"
17+
#include "agg_py_transforms.h"
1718

1819
extern "C" {
1920
#ifdef __APPLE__
@@ -85,29 +86,8 @@ PyAggImagePhoto(ClientData clientdata, Tcl_Interp* interp,
8586

8687
/* check for bbox/blitting */
8788
bboxo = (PyObject*)atol(argv[4]);
88-
if (bboxo != Py_None) {
89+
if (py_convert_bbox(bboxo, l, b, r, t)) {
8990
has_bbox = true;
90-
PyArrayObject* bbox = NULL;
91-
try {
92-
bbox = (PyArrayObject*) PyArray_FromObject(bboxo, PyArray_DOUBLE, 2, 2);
93-
94-
if (!bbox || bbox->nd != 2 || bbox->dimensions[0] != 2 || bbox->dimensions[1] != 2) {
95-
throw Py::TypeError
96-
("Argument 3 to agg_to_gtk_drawable must be a Bbox object.");
97-
}
98-
99-
l = *(double*)PyArray_GETPTR2(bbox, 0, 0);
100-
b = *(double*)PyArray_GETPTR2(bbox, 0, 1);
101-
r = *(double*)PyArray_GETPTR2(bbox, 1, 0);
102-
t = *(double*)PyArray_GETPTR2(bbox, 1, 1);
103-
104-
Py_XDECREF(bbox);
105-
bbox = NULL;
106-
} catch (...) {
107-
Py_XDECREF(bbox);
108-
bbox = NULL;
109-
throw;
110-
}
11191

11292
destx = (int)l;
11393
desty = srcheight-(int)t;

src/_wxagg.cpp

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,18 @@
4646

4747
#include "agg_basics.h"
4848
#include "_backend_agg.h"
49-
#include "_transforms.h"
5049
#include "agg_pixfmt_rgba.h"
5150
#include "util/agg_color_conv_rgb8.h"
51+
#include "agg_py_transforms.h"
5252

5353
#include <wx/image.h>
5454
#include <wx/bitmap.h>
5555
#include <wx/wxPython/wxPython.h>
5656

5757

5858
// forward declarations
59-
static wxImage *convert_agg2image(RendererAgg *aggRenderer, Bbox *clipbox);
60-
static wxBitmap *convert_agg2bitmap(RendererAgg *aggRenderer, Bbox *clipbox);
59+
static wxImage *convert_agg2image(RendererAgg *aggRenderer, Py::Object clipbox);
60+
static wxBitmap *convert_agg2bitmap(RendererAgg *aggRenderer, Py::Object clipbox);
6161

6262

6363
// the extension module
@@ -94,9 +94,7 @@ class _wxagg_module : public Py::ExtensionModule<_wxagg_module>
9494
RendererAgg* aggRenderer = static_cast<RendererAgg*>(
9595
args[0].getAttr("_renderer").ptr());
9696

97-
Bbox *clipbox = NULL;
98-
if (args[1].ptr() != Py_None)
99-
clipbox = static_cast<Bbox*>(args[1].ptr());
97+
Py::Object clipbox = args[1];
10098

10199
// convert the buffer
102100
wxImage *image = convert_agg2image(aggRenderer, clipbox);
@@ -118,9 +116,7 @@ class _wxagg_module : public Py::ExtensionModule<_wxagg_module>
118116
RendererAgg* aggRenderer = static_cast<RendererAgg*>(
119117
args[0].getAttr("_renderer").ptr());
120118

121-
Bbox *clipbox = NULL;
122-
if (args[1].ptr() != Py_None)
123-
clipbox = static_cast<Bbox*>(args[1].ptr());
119+
Py::Object clipbox = args[1];
124120

125121
// convert the buffer
126122
wxBitmap *bitmap = convert_agg2bitmap(aggRenderer, clipbox);
@@ -141,7 +137,7 @@ class _wxagg_module : public Py::ExtensionModule<_wxagg_module>
141137
// Implementation Functions
142138
//
143139

144-
static wxImage *convert_agg2image(RendererAgg *aggRenderer, Bbox *clipbox)
140+
static wxImage *convert_agg2image(RendererAgg *aggRenderer, Py::Object clipbox)
145141
{
146142
int srcWidth = 1;
147143
int srcHeight = 1;
@@ -150,19 +146,16 @@ static wxImage *convert_agg2image(RendererAgg *aggRenderer, Bbox *clipbox)
150146
bool deleteSrcBuffer = false;
151147
agg::int8u *srcBuffer = NULL;
152148

153-
if (clipbox == NULL) {
149+
double l, b, r, t;
150+
151+
if (!py_convert_bbox(clipbox.ptr(), l, b, r, t)) {
154152
// Convert everything: rgba => rgb -> image
155153
srcBuffer = aggRenderer->pixBuffer;
156154
srcWidth = (int) aggRenderer->get_width();
157155
srcHeight = (int) aggRenderer->get_height();
158156
srcStride = (int) aggRenderer->get_width()*4;
159157
} else {
160158
// Convert a region: rgba => clipped rgba => rgb -> image
161-
double l = clipbox->ll_api()->x_api()->val() ;
162-
double b = clipbox->ll_api()->y_api()->val();
163-
double r = clipbox->ur_api()->x_api()->val() ;
164-
double t = clipbox->ur_api()->y_api()->val() ;
165-
166159
srcWidth = (int) (r-l);
167160
srcHeight = (int) (t-b);
168161
srcStride = srcWidth*4;
@@ -230,7 +223,7 @@ static wxImage *convert_agg2image(RendererAgg *aggRenderer, Bbox *clipbox)
230223
}
231224

232225

233-
static wxBitmap *convert_agg2bitmap(RendererAgg *aggRenderer, Bbox *clipbox)
226+
static wxBitmap *convert_agg2bitmap(RendererAgg *aggRenderer, Py::Object clipbox)
234227
{
235228
// Convert everything: rgba => rgb -> image => bitmap
236229
// Convert a region: rgba => clipped rgba => rgb -> image => bitmap

src/agg_py_transforms.h

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "CXX/Objects.hxx"
88
#include "agg_trans_affine.h"
99

10-
1110
/** A helper function to convert from a Numpy affine transformation matrix
1211
* to an agg::trans_affine.
1312
*/
@@ -20,10 +19,10 @@ agg::trans_affine py_to_agg_transformation_matrix(const Py::Object& obj, bool er
2019
matrix = (PyArrayObject*) PyArray_FromObject(obj.ptr(), PyArray_DOUBLE, 2, 2);
2120
if (!matrix)
2221
throw std::exception();
23-
if (matrix->nd == 2 || matrix->dimensions[0] == 3 || matrix->dimensions[1] == 3) {
24-
size_t stride0 = matrix->strides[0];
25-
size_t stride1 = matrix->strides[1];
26-
char* row0 = matrix->data;
22+
if (PyArray_NDIM(matrix) == 2 || PyArray_DIM(matrix, 0) == 3 || PyArray_DIM(matrix, 1) == 3) {
23+
size_t stride0 = PyArray_STRIDE(matrix, 0);
24+
size_t stride1 = PyArray_STRIDE(matrix, 1);
25+
char* row0 = PyArray_BYTES(matrix);
2726
char* row1 = row0 + stride0;
2827

2928
double a = *(double*)(row0);
@@ -55,4 +54,35 @@ agg::trans_affine py_to_agg_transformation_matrix(const Py::Object& obj, bool er
5554
return agg::trans_affine();
5655
}
5756

57+
bool py_convert_bbox(PyObject* bbox_obj, double& l, double& b, double& r, double& t) {
58+
PyArrayObject* bbox = NULL;
59+
60+
if (bbox_obj == Py_None)
61+
return false;
62+
63+
try {
64+
bbox = (PyArrayObject*) PyArray_FromObject(bbox_obj, PyArray_DOUBLE, 2, 2);
65+
66+
if (!bbox || bbox->nd != 2 || bbox->dimensions[0] != 2 || bbox->dimensions[1] != 2) {
67+
throw Py::TypeError
68+
("Argument 3 to agg_to_gtk_drawable must be a Bbox object.");
69+
}
70+
71+
l = *(double*)PyArray_GETPTR2(bbox, 0, 0);
72+
b = *(double*)PyArray_GETPTR2(bbox, 0, 1);
73+
r = *(double*)PyArray_GETPTR2(bbox, 1, 0);
74+
t = *(double*)PyArray_GETPTR2(bbox, 1, 1);
75+
76+
Py_XDECREF(bbox);
77+
bbox = NULL;
78+
return true;
79+
} catch (...) {
80+
Py_XDECREF(bbox);
81+
bbox = NULL;
82+
throw;
83+
}
84+
85+
return false;
86+
}
87+
5888
#endif // __AGG_PY_TRANSFORMS_H__

0 commit comments

Comments
 (0)