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

Skip to content

Commit b6cd1ec

Browse files
committed
fixed image clipping bug
svn path=/trunk/matplotlib/; revision=1845
1 parent 46032b7 commit b6cd1ec

6 files changed

Lines changed: 28 additions & 143 deletions

File tree

lib/matplotlib/axes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ def cla(self):
664664
horizontalalignment='center',
665665
)
666666
self.title.set_transform(self.transAxes)
667+
self.title.set_clip_box(None)
667668

668669
self._set_artist_props(self.title)
669670

lib/matplotlib/backends/backend_agg.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ def __init__(self, width, height, dpi):
129129
self.texmanager = TexManager()
130130

131131
self.bbox = lbwh_to_bbox(0,0, self.width, self.height)
132-
133132

134133
def draw_arc(self, gcEdge, rgbFace, x, y, width, height, angle1, angle2):
135134
"""
@@ -241,7 +240,7 @@ def get_text_width_height(self, s, prop, ismath, rgb=(0,0,0)):
241240
Z = self.texmanager.get_rgba(s, size, dpi, rgb)
242241
m,n,tmp = Z.shape
243242
return n,m
244-
243+
245244
if ismath:
246245
width, height, fonts = math_parse_s_ft2font(
247246
s, self.dpi.get(), prop.get_size_in_points())
@@ -267,7 +266,6 @@ def draw_tex(self, gc, x, y, s, prop, angle):
267266

268267
key = s, size, dpi, rgb, angle
269268
im = self.texd.get(key)
270-
271269
if im is None:
272270
Z = self.texmanager.get_rgba(s, size, dpi, rgb)
273271
if flip:
@@ -289,7 +287,10 @@ def func(x):
289287
im.flipud_out()
290288
self.texd[key] = im
291289

292-
self.draw_image(x, self.height-y, im, self.bbox)
290+
cliprect = gc.get_clip_rectangle()
291+
if cliprect is None: bbox = None
292+
else: bbox = lbwh_to_bbox(*cliprect)
293+
self.draw_image(x, self.height-y, im, bbox)
293294

294295
def get_canvas_width_height(self):
295296
'return the canvas width and height in display coords'

lib/matplotlib/backends/backend_ps.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1087,6 +1087,7 @@ def print_figure(self, outfile, dpi=72,
10871087
latexh.close()
10881088

10891089
command = 'latex -interaction=nonstopmode "%s"' % texfile
1090+
10901091
verbose.report(command, 'debug-annoying')
10911092
stdin, stdout, stderr = os.popen3(command)
10921093
verbose.report(stdout.read(), 'debug-annoying')
@@ -1099,9 +1100,16 @@ def print_figure(self, outfile, dpi=72,
10991100
os.remove(epsfile)
11001101
if ext.startswith('.ep'):
11011102
dpi = rcParams['ps.distiller.res']
1102-
command = 'gs -dBATCH -dNOPAUSE -dSAFER -r%d \
1103+
1104+
if sys.platform == 'win32':
1105+
command = 'gswin32c -dBATCH -dNOPAUSE -dSAFER -r%d \
1106+
-sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
1107+
-sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
1108+
else:
1109+
command = 'gs -dBATCH -dNOPAUSE -dSAFER -r%d \
11031110
-sDEVICE=epswrite -dLanguageLevel=2 -dEPSFitPage \
11041111
-sOutputFile="%s" "%s"'% (dpi, epsfile, psfile)
1112+
11051113
verbose.report(command, 'debug-annoying')
11061114
stdin, stdout, stderr = os.popen3(command)
11071115
verbose.report(stdout.read(), 'debug-annoying')

lib/matplotlib/texmanager.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ def get_rgba(self, tex, fontsize=10, dpi=80, rgb=(0,0,0)):
274274
"""
275275
Return tex string as an rgba array
276276
"""
277+
277278
# dvipng assumes a constant background, whereas we want to
278279
# overlay these rasters with antialiasing over arbitrary
279280
# backgrounds that may have other figure elements under them.
@@ -301,14 +302,12 @@ def get_rgba(self, tex, fontsize=10, dpi=80, rgb=(0,0,0)):
301302
r,g,b = rgb
302303
key = tex, dpi, tuple(rgb)
303304
Z = self.arrayd.get(key)
304-
305+
305306
if Z is None:
306307
# force=True to skip cacheing while debugging
307308
pngfile = self.make_png(tex, dpi, force=False)
308309
X = readpng(pngfile)
309-
310-
v
311-
ers = self.get_dvipng_version()
310+
vers = self.get_dvipng_version()
312311
#print 'dvipng version', vers
313312
if vers<'1.6':
314313
# hack the alpha channel as described in comment above
@@ -328,6 +327,7 @@ def get_rgba(self, tex, fontsize=10, dpi=80, rgb=(0,0,0)):
328327
Z[:,:,3] = alpha
329328

330329
self.arrayd[key] = Z
330+
331331
return Z
332332

333333
def get_dvipng_version(self):

lib/matplotlib/text.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,7 @@ def draw(self, renderer):
341341
x, y = self._transform.xy_tup((x, y))
342342
if renderer.flipy():
343343
y = canvash-y
344+
344345
renderer.draw_tex(gc, x, y, line,
345346
self._fontproperties, angle)
346347
return

src/_backend_agg.cpp

Lines changed: 8 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -659,6 +659,12 @@ RendererAgg::bbox_to_rect(const Py::Object& o) {
659659

660660
void
661661
RendererAgg::set_clip_from_bbox(const Py::Object& o) {
662+
663+
// do not puut this in the else below. We want to unconditionally
664+
// clear the clip
665+
theRasterizer->reset_clipping();
666+
rendererBase->reset_clipping(true);
667+
662668
if (o.ptr() != Py_None) { //using clip
663669
// Bbox::check(args[0]) failing; something about cross module?
664670
// set the clip rectangle
@@ -670,9 +676,9 @@ RendererAgg::set_clip_from_bbox(const Py::Object& o) {
670676
double r = clipbox->ur_api()->x_api()->val() ;
671677
double t = clipbox->ur_api()->y_api()->val() ; ;
672678
theRasterizer->clip_box(l, height-t, r, height-b);
679+
rendererBase->clip_box((int)l, (int)(height-t), (int)r, (int)(height-b));
673680
}
674681

675-
676682
}
677683

678684

@@ -1591,150 +1597,18 @@ RendererAgg::draw_text(const Py::Tuple& args) {
15911597

15921598
}
15931599

1594-
/*
1595-
Py::Object
1596-
RendererAgg::draw_image(const Py::Tuple& args) {
1597-
_VERBOSE("RendererAgg::draw_image");
1598-
theRasterizer->reset_clipping();
1599-
args.verify_length(5);
1600-
1601-
float x = Py::Float(args[0]);
1602-
float y = Py::Float(args[1]);
1603-
Image *image = static_cast<Image*>(args[2].ptr());
1604-
std::string origin = Py::String(args[3]);
1605-
1606-
if (origin!="lower" && origin!="upper")
1607-
throw Py::ValueError(Printf("origin must be upper|lower; found %s", origin.c_str()).str());
1608-
1609-
bool isUpper = origin=="upper";
1610-
1611-
size_t ind = 0;
1612-
size_t thisx, thisy;
1613-
float oy = isUpper ? y : height-y;
1614-
1615-
float minx(0), maxx(width), miny(0), maxy(height);
1616-
1617-
if (args[4].ptr() != Py_None) {
1618-
Bbox* bbox = static_cast<Bbox*>(args[4].ptr());
1619-
minx = bbox->ll_api()->x_api()->val();
1620-
maxy = height-bbox->ll_api()->y_api()->val();
1621-
maxx = bbox->ur_api()->x_api()->val();
1622-
miny = height-bbox->ur_api()->y_api()->val();
1623-
}
1624-
1625-
//if (isUpper) oy -= image->rowsOut; //start at top
1626-
for (size_t j=0; j<image->rowsOut; j++) {
1627-
thisy = (size_t)(isUpper ? oy+j : oy-j-0.5);
1628-
if (thisy<miny || thisy>=maxy) {
1629-
ind += 4*image->colsOut;
1630-
continue;
1631-
}
1632-
for (size_t i=0; i<image->colsOut; i++) {
1633-
thisx = (size_t)(i+x);
1634-
if (thisx<minx || thisx>=maxx) {
1635-
ind += 4;
1636-
continue;
1637-
}
1638-
1639-
pixfmt::color_type p;
1640-
1641-
p.r = *(image->bufferOut+ind++);
1642-
p.g = *(image->bufferOut+ind++);
1643-
p.b = *(image->bufferOut+ind++);
1644-
p.a = *(image->bufferOut+ind++);
1645-
1646-
pixFmt->blend_pixel(thisx, thisy, p, p.a);
1647-
}
1648-
}
1649-
1650-
return Py::Object();
1651-
1652-
}
1653-
*/
1654-
1655-
/*
1656-
Py::Object
1657-
RendererAgg::draw_image(const Py::Tuple& args) {
1658-
_VERBOSE("RendererAgg::draw_image");
1659-
theRasterizer->reset_clipping();
1660-
args.verify_length(4);
1661-
1662-
float x = Py::Float(args[0]);
1663-
float y = Py::Float(args[1]);
1664-
Image *image = static_cast<Image*>(args[2].ptr());
1665-
1666-
size_t ind = 0;
1667-
size_t thisx, thisy;
1668-
float oy = height-y;
1669-
1670-
float minx(0), maxx(width), miny(0), maxy(height);
1671-
1672-
if (args[3].ptr() != Py_None) {
1673-
Bbox* bbox = static_cast<Bbox*>(args[3].ptr());
1674-
minx = bbox->ll_api()->x_api()->val();
1675-
maxy = height-bbox->ll_api()->y_api()->val();
1676-
maxx = bbox->ur_api()->x_api()->val();
1677-
miny = height-bbox->ur_api()->y_api()->val();
1678-
}
1679-
1680-
1681-
1682-
//if (isUpper) oy -= image->rowsOut; //start at top
1683-
1684-
for (size_t j=0; j<image->rowsOut; j++) {
1685-
thisy = (size_t)(oy-j-0.5);
1686-
if (thisy<miny || thisy>=maxy) {
1687-
ind += 4*image->colsOut;
1688-
continue;
1689-
}
1690-
for (size_t i=0; i<image->colsOut; i++) {
1691-
thisx = (size_t)(i+x);
1692-
if (thisx<minx || thisx>=maxx) {
1693-
ind += 4;
1694-
continue;
1695-
}
1696-
1697-
pixfmt::color_type p;
1698-
1699-
p.r = *(image->bufferOut+ind++);
1700-
p.g = *(image->bufferOut+ind++);
1701-
p.b = *(image->bufferOut+ind++);
1702-
p.a = *(image->bufferOut+ind++);
1703-
1704-
pixFmt->blend_pixel(thisx, thisy, p, p.a);
1705-
}
1706-
}
1707-
1708-
return Py::Object();
1709-
1710-
}
1711-
1712-
*/
1713-
17141600

17151601
Py::Object
17161602
RendererAgg::draw_image(const Py::Tuple& args) {
17171603
_VERBOSE("RendererAgg::draw_image");
1718-
theRasterizer->reset_clipping();
17191604
args.verify_length(4);
17201605

17211606
float x = Py::Float(args[0]);
17221607
float y = Py::Float(args[1]);
17231608
Image *image = static_cast<Image*>(args[2].ptr());
17241609

1725-
1726-
float minx(0), maxx(width), miny(0), maxy(height);
1727-
1728-
if (args[3].ptr() != Py_None) {
1729-
Bbox* bbox = static_cast<Bbox*>(args[3].ptr());
1730-
minx = bbox->ll_api()->x_api()->val();
1731-
maxy = height-bbox->ll_api()->y_api()->val();
1732-
maxx = bbox->ur_api()->x_api()->val();
1733-
miny = height-bbox->ur_api()->y_api()->val();
1734-
//theRasterizer->clip_box(minx, miny, maxx, maxy);
1735-
}
1610+
set_clip_from_bbox(args[3]);
17361611

1737-
17381612
pixfmt pixf(*(image->rbufOut));
17391613

17401614

0 commit comments

Comments
 (0)