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

Skip to content

Reduce coupling between _tkagg and _backend_agg modules #3778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 11, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions lib/matplotlib/backends/tkagg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import six
from six.moves import tkinter as Tk

import numpy as np

from matplotlib.backends import _tkagg

def blit(photoimage, aggimage, bbox=None, colormode=1):
Expand All @@ -13,15 +15,19 @@ def blit(photoimage, aggimage, bbox=None, colormode=1):
bbox_array = bbox.__array__()
else:
bbox_array = None
data = np.asarray(aggimage)
try:
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
tk.call(
"PyAggImagePhoto", photoimage,
id(data), colormode, id(bbox_array))
except Tk.TclError:
try:
try:
_tkagg.tkinit(tk.interpaddr(), 1)
except AttributeError:
_tkagg.tkinit(id(tk), 0)
tk.call("PyAggImagePhoto", photoimage, id(aggimage), colormode, id(bbox_array))
tk.call("PyAggImagePhoto", photoimage,
id(data), colormode, id(bbox_array))
except (ImportError, AttributeError, Tk.TclError):
raise

Expand Down
29 changes: 15 additions & 14 deletions setupext.py
Original file line number Diff line number Diff line change
Expand Up @@ -852,23 +852,24 @@ def check(self):
self.__class__.found_external = False
return str(e) + ' Using local copy.'

def add_flags(self, ext):
def add_flags(self, ext, add_sources=True):
if self.found_external:
pkg_config.setup_extension(ext, 'libagg')
else:
ext.include_dirs.append('extern/agg24/include')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same point about agg24 here, too

agg_sources = [
'agg_bezier_arc.cpp',
'agg_curves.cpp',
'agg_image_filters.cpp',
'agg_trans_affine.cpp',
'agg_vcgen_contour.cpp',
'agg_vcgen_dash.cpp',
'agg_vcgen_stroke.cpp',
'agg_vpgen_segmentator.cpp'
]
ext.sources.extend(
os.path.join('extern', 'agg24', 'src', x) for x in agg_sources)
if add_sources:
agg_sources = [
'agg_bezier_arc.cpp',
'agg_curves.cpp',
'agg_image_filters.cpp',
'agg_trans_affine.cpp',
'agg_vcgen_contour.cpp',
'agg_vcgen_dash.cpp',
'agg_vcgen_stroke.cpp',
'agg_vpgen_segmentator.cpp'
]
ext.sources.extend(
os.path.join('extern', 'agg24', 'src', x) for x in agg_sources)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this supposed to be "agg24-svn" now?



class FreeType(SetupPackage):
Expand Down Expand Up @@ -1298,7 +1299,7 @@ def get_extension(self):
ext = make_extension('matplotlib.backends._tkagg', sources)
self.add_flags(ext)
Numpy().add_flags(ext)
LibAgg().add_flags(ext)
LibAgg().add_flags(ext, add_sources=False)
return ext

def query_tcltk(self):
Expand Down
23 changes: 22 additions & 1 deletion src/_backend_agg_wrapper.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
#include "_backend_agg_wrapper.h"
#include "mplutils.h"
#include "py_converters.h"
#include "_backend_agg.h"

typedef struct
{
PyObject_HEAD;
RendererAgg *x;
Py_ssize_t shape[3];
Py_ssize_t strides[3];
Py_ssize_t suboffsets[3];
} PyRendererAgg;

typedef struct
{
PyObject_HEAD;
BufferRegion *x;
Py_ssize_t shape[3];
Py_ssize_t strides[3];
Py_ssize_t suboffsets[3];
} PyBufferRegion;


/**********************************************************************
* BufferRegion
Expand Down
29 changes: 0 additions & 29 deletions src/_backend_agg_wrapper.h

This file was deleted.

41 changes: 21 additions & 20 deletions src/_tkagg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
#include <cstdio>
#include <sstream>

#include "agg_basics.h"
#include "_backend_agg_wrapper.h"
#include "py_converters.h"

extern "C"
Expand Down Expand Up @@ -50,14 +48,14 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
{
Tk_PhotoHandle photo;
Tk_PhotoImageBlock block;
PyObject *aggo;
PyObject *bufferobj;

// vars for blitting
PyObject *bboxo;

size_t aggl, bboxl;
bool has_bbox;
agg::int8u *destbuffer;
uint8_t *destbuffer;
int destx, desty, destwidth, destheight, deststride;
//unsigned long tmp_ptr;

Expand All @@ -84,12 +82,17 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
Tcl_AppendResult(interp, "error casting pointer", (char *)NULL);
return TCL_ERROR;
}
aggo = (PyObject *)aggl;

// TODO: This is really brittle and will break when RendererAgg
// comes in multiple flavors
RendererAgg *aggRenderer = ((PyRendererAgg *)(aggo))->x;
int srcheight = (int)aggRenderer->get_height();
bufferobj = (PyObject *)aggl;

numpy::array_view<uint8_t, 3> buffer;
try {
buffer = numpy::array_view<uint8_t, 3>(bufferobj);
} catch (...) {
Tcl_AppendResult(interp, "buffer is of wrong type", (char *)NULL);
PyErr_Clear();
return TCL_ERROR;
}
int srcheight = buffer.dim(0);

/* XXX insert aggRenderer type check */

Expand Down Expand Up @@ -127,13 +130,11 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
return TCL_ERROR;
}

agg::rendering_buffer destrbuf;
destrbuf.attach(destbuffer, destwidth, destheight, deststride);
pixfmt destpf(destrbuf);
renderer_base destrb(destpf);

agg::rect_base<int> region(destx, desty, (int)rect.x2, srcheight - (int)rect.y1);
destrb.copy_from(aggRenderer->renderingBuffer, &region, -destx, -desty);
for (int i = 0; i < destheight; ++i) {
memcpy(destbuffer + (deststride * i),
&buffer(i + desty, destx, 0),
deststride);
}
} else {
has_bbox = false;
destbuffer = NULL;
Expand Down Expand Up @@ -170,10 +171,10 @@ static int PyAggImagePhoto(ClientData clientdata, Tcl_Interp *interp, int argc,
delete[] destbuffer;

} else {
block.width = aggRenderer->get_width();
block.height = aggRenderer->get_height();
block.width = buffer.dim(1);
block.height = buffer.dim(0);
block.pitch = (int)block.width * nval;
block.pixelPtr = aggRenderer->pixBuffer;
block.pixelPtr = buffer.data();

/* Clear current contents */
Tk_PhotoBlank(photo);
Expand Down