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

Skip to content

Commit 9564d80

Browse files
committed
Merge pull request #3009 from solvents/issue_2963
BUG: Showing a BboxImage can cause a segmentation fault
2 parents 4a54114 + 747a780 commit 9564d80

5 files changed

Lines changed: 32 additions & 3 deletions

File tree

lib/matplotlib/image.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,8 @@ def make_image(self, renderer, magnification=1.0):
11541154
im.set_resample(self._resample)
11551155

11561156
l, b, r, t = self.get_window_extent(renderer).extents # bbox.extents
1157-
widthDisplay = round(r) - round(l)
1158-
heightDisplay = round(t) - round(b)
1157+
widthDisplay = abs(round(r) - round(l))
1158+
heightDisplay = abs(round(t) - round(b))
11591159
widthDisplay *= magnification
11601160
heightDisplay *= magnification
11611161

@@ -1183,11 +1183,14 @@ def draw(self, renderer, *args, **kwargs):
11831183
# todo: we should be able to do some cacheing here
11841184
image_mag = renderer.get_image_magnification()
11851185
im = self.make_image(renderer, image_mag)
1186-
l, b, r, t = self.get_window_extent(renderer).extents
1186+
x0, y0, x1, y1 = self.get_window_extent(renderer).extents
11871187
gc = renderer.new_gc()
11881188
self._set_gc_clip(gc)
11891189
gc.set_alpha(self.get_alpha())
11901190
#gc.set_clip_path(self.get_clip_path())
1191+
1192+
l = np.min([x0, x1])
1193+
b = np.min([y0, y1])
11911194
renderer.draw_image(gc, round(l), round(b), im)
11921195
gc.restore()
11931196

Binary file not shown.
19.2 KB
Loading

lib/matplotlib/tests/test_image.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import numpy as np
77

88
from matplotlib.testing.decorators import image_comparison, knownfailureif, cleanup
9+
from matplotlib.image import BboxImage, imread
10+
from matplotlib.transforms import Bbox
911
from matplotlib import rcParams
1012
import matplotlib.pyplot as plt
1113
from nose.tools import assert_raises
@@ -315,6 +317,24 @@ def test_rasterize_dpi():
315317
rcParams['savefig.dpi'] = 10
316318

317319

320+
@image_comparison(baseline_images=['bbox_image_inverted'],
321+
extensions=['png', 'pdf'])
322+
def test_bbox_image_inverted():
323+
# This is just used to produce an image to feed to BboxImage
324+
fig = plt.figure()
325+
axes = fig.add_subplot(111)
326+
axes.plot([1, 2, 3])
327+
328+
im_buffer = io.BytesIO()
329+
fig.savefig(im_buffer)
330+
im_buffer.seek(0)
331+
image = imread(im_buffer)
332+
333+
bbox_im = BboxImage(Bbox([[100, 100], [0, 0]]))
334+
bbox_im.set_data(image)
335+
axes.add_artist(bbox_im)
336+
337+
318338
if __name__=='__main__':
319339
import nose
320340
nose.runmodule(argv=['-s','--with-doctest'], exit=False)

src/_image.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,12 @@ Image::resize(const Py::Tuple& args, const Py::Dict& kwargs)
374374
int numcols = Py::Int(args[0]);
375375
int numrows = Py::Int(args[1]);
376376

377+
if (numcols < 0 || numrows < 0)
378+
{
379+
throw Py::RuntimeError(
380+
"Width and height must have non-negative values");
381+
}
382+
377383
colsOut = numcols;
378384
rowsOut = numrows;
379385

0 commit comments

Comments
 (0)