diff --git a/Tests/test_imagepath.py b/Tests/test_imagepath.py index beadff1..a0dee28 100644 --- a/Tests/test_imagepath.py +++ b/Tests/test_imagepath.py @@ -47,3 +47,8 @@ def test_path(): assert_equal(list(p), [(0.0, 1.0)]) p = ImagePath.Path(array.array("f", [0, 1]).tostring()) assert_equal(list(p), [(0.0, 1.0)]) + +def test_getbbox(): + for coords in (0,1): + p = ImagePath.Path(coords) + assert_equal(p.getbbox(), (0.0, 0.0, 0.0, 0.0)) diff --git a/path.c b/path.c index 871da93..059e738 100644 --- a/path.c +++ b/path.c @@ -57,7 +57,7 @@ alloc_array(Py_ssize_t count) PyErr_NoMemory(); return NULL; } - xy = malloc(2 * count * sizeof(double) + 1); + xy = calloc(2 * count + 1, sizeof(double)); if (!xy) PyErr_NoMemory(); return xy; @@ -336,18 +336,27 @@ path_getbbox(PyPathObject* self, PyObject* args) xy = self->xy; - x0 = x1 = xy[0]; - y0 = y1 = xy[1]; - - for (i = 1; i < self->count; i++) { - if (xy[i+i] < x0) - x0 = xy[i+i]; - if (xy[i+i] > x1) - x1 = xy[i+i]; - if (xy[i+i+1] < y0) - y0 = xy[i+i+1]; - if (xy[i+i+1] > y1) - y1 = xy[i+i+1]; + if (self->count == 0) { + x0 = x1 = 0; + y0 = y1 = 0; + } else { + x0 = x1 = xy[0]; + y0 = y1 = xy[1]; + + for (i = 1; i < self->count; i++) { + if (xy[i + i] < x0) { + x0 = xy[i + i]; + } + if (xy[i + i] > x1) { + x1 = xy[i + i]; + } + if (xy[i + i + 1] < y0) { + y0 = xy[i + i + 1]; + } + if (xy[i + i + 1] > y1) { + y1 = xy[i + i + 1]; + } + } } return Py_BuildValue("dddd", x0, y0, x1, y1);