modify issue3738

This commit is contained in:
高辛格 2019-07-05 14:09:34 +08:00
parent ea5b30d212
commit a27c43a8d5
2 changed files with 92 additions and 21 deletions

View File

@ -807,3 +807,26 @@ class TestImageDraw(PillowTestCase):
operation, mode operation, mode
) )
self.assert_image_similar(im, Image.open(expected), 1) self.assert_image_similar(im, Image.open(expected), 1)
def test_xy(self):
im = hopper()
draw = ImageDraw.Draw(im)
xys = [
[X0, Y0, X1, Y1],
[[X0, Y0], [X1, Y1]],
[(X0, Y0), (X1, Y1)],
(X0, Y0, X1, Y1),
([X0, Y0], [X1, Y1]),
((X0, Y0), (X1, Y1)),
]
for xy in xys:
draw.arc(xy, 0, 90)
draw.chord(xy, 0, 90)
draw.pieslice(xy, 0, 90)
draw.ellipse(xy)
draw.line(xy)
draw.point(xy)
draw.polygon(xy)
draw.rectangle(xy)

View File

@ -106,6 +106,63 @@ path_dealloc(PyPathObject* path)
PyObject_Del(path); PyObject_Del(path);
} }
static int
get_one_item(PyObject *op, double *x)
{
if (PyFloat_Check(op)) {
*x = PyFloat_AS_DOUBLE(op);
return 1;
} else if (PyInt_Check(op)) {
*x = (float) PyInt_AS_LONG(op);
return 1;
} else if (PyNumber_Check(op)) {
*x = PyFloat_AsDouble(op);
return 1;
} else {
return 0;
}
}
static int
get_two_item(PyObject *op, double *x, double *y) {
PyObject *o;
int success;
if (PySequence_Check(op) && PyObject_Length(op) > 1) {
if (PyList_Check(op)) {
o = PyList_GET_ITEM(op, 0);
if (!get_one_item(o, x))
return 0;
o = PyList_GET_ITEM(op, 1);
if (!get_one_item(o, y))
return 0;
return 1;
} else if (PyTuple_Check(op)) {
o = PyTuple_GET_ITEM(op, 0);
if (!get_one_item(o, x))
return 0;
o = PyTuple_GET_ITEM(op, 1);
if (!get_one_item(o, y))
return 0;
return 1;
} else {
o = PySequence_GetItem(op, 0);
success = get_one_item(o, x);
Py_DECREF(o);
if (!success)
return 0;
o = PySequence_GetItem(op, 1);
success = get_one_item(o, y);
Py_DECREF(o);
if (!success)
return 0;
return 1;
}
}
return 0;
}
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
/* Helpers */ /* Helpers */
/* -------------------------------------------------------------------- */ /* -------------------------------------------------------------------- */
@ -168,17 +225,14 @@ PyPath_Flatten(PyObject* data, double **pxy)
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
double x, y; double x, y;
PyObject *op = PyList_GET_ITEM(data, i); PyObject *op = PyList_GET_ITEM(data, i);
if (PyFloat_Check(op)) if (get_one_item(op, &x)) {
xy[j++] = PyFloat_AS_DOUBLE(op); xy[j++] = x;
else if (PyInt_Check(op)) } else if (get_two_item(op, &x, &y)) {
xy[j++] = (float) PyInt_AS_LONG(op);
else if (PyNumber_Check(op))
xy[j++] = PyFloat_AsDouble(op);
else if (PyArg_ParseTuple(op, "dd", &x, &y)) {
xy[j++] = x; xy[j++] = x;
xy[j++] = y; xy[j++] = y;
} else { } else {
free(xy); free(xy);
PyErr_SetString(PyExc_TypeError, "wrong type of coordinates");
return -1; return -1;
} }
} }
@ -186,17 +240,14 @@ PyPath_Flatten(PyObject* data, double **pxy)
for (i = 0; i < n; i++) { for (i = 0; i < n; i++) {
double x, y; double x, y;
PyObject *op = PyTuple_GET_ITEM(data, i); PyObject *op = PyTuple_GET_ITEM(data, i);
if (PyFloat_Check(op)) if (get_one_item(op, &x)) {
xy[j++] = PyFloat_AS_DOUBLE(op); xy[j++] = x;
else if (PyInt_Check(op)) } else if (get_two_item(op, &x, &y)) {
xy[j++] = (float) PyInt_AS_LONG(op);
else if (PyNumber_Check(op))
xy[j++] = PyFloat_AsDouble(op);
else if (PyArg_ParseTuple(op, "dd", &x, &y)) {
xy[j++] = x; xy[j++] = x;
xy[j++] = y; xy[j++] = y;
} else { } else {
free(xy); free(xy);
PyErr_SetString(PyExc_TypeError, "wrong type of coordinates");
return -1; return -1;
} }
} }
@ -215,18 +266,15 @@ PyPath_Flatten(PyObject* data, double **pxy)
return -1; return -1;
} }
} }
if (PyFloat_Check(op)) if (get_one_item(op, &x)) {
xy[j++] = PyFloat_AS_DOUBLE(op); xy[j++] = x;
else if (PyInt_Check(op)) } else if (get_two_item(op, &x, &y)) {
xy[j++] = (float) PyInt_AS_LONG(op);
else if (PyNumber_Check(op))
xy[j++] = PyFloat_AsDouble(op);
else if (PyArg_ParseTuple(op, "dd", &x, &y)) {
xy[j++] = x; xy[j++] = x;
xy[j++] = y; xy[j++] = y;
} else { } else {
Py_DECREF(op); Py_DECREF(op);
free(xy); free(xy);
PyErr_SetString(PyExc_TypeError, "wrong type of coordinates");
return -1; return -1;
} }
Py_DECREF(op); Py_DECREF(op);