Skip to content

Commit

Permalink
解决了内存泄漏的问题,原因是复制出来的图像的引用计数忘了减去1。
Browse files Browse the repository at this point in the history
  • Loading branch information
cedricporter committed Aug 13, 2012
1 parent c101b07 commit fc4ce80
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions EffectLab/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ typedef struct {
} ImagingObject;



/* 弧度变形 */
static int _radian_warp(PyObject *func, double x, double y, double *xnew, double *ynew)
{
double phi,radius,radius2;
Expand All @@ -82,13 +84,16 @@ static int _radian_warp(PyObject *func, double x, double y, double *xnew, double
{
return 0;
}
Py_DECREF(ret);

*xnew = radius * cos(phi);
*ynew = radius * sin(phi);

return 1;
}


/* 基于平移的变形 */
static int _xy_warp(PyObject *func, double x, double y, double *xnew, double *ynew)
{
PyObject *ret;
Expand All @@ -100,10 +105,12 @@ static int _xy_warp(PyObject *func, double x, double y, double *xnew, double *yn
{
return 0;
}
Py_DECREF(ret);

return 1;
}


/* 镜头变形效果 */
static PyObject* _lens_warp(PyObject *self, PyObject *args, int (*warp)(PyObject*, double, double, double *, double*))
{
Expand All @@ -120,7 +127,7 @@ static PyObject* _lens_warp(PyObject *self, PyObject *args, int (*warp)(PyObject
double xnew, ynew;
UINT8 *pixel;
int i3, j3;
PyObject *func, *ret;
PyObject *func;
PyObject *empty_color;
int er, eg, eb, ea;

Expand All @@ -134,6 +141,7 @@ static PyObject* _lens_warp(PyObject *self, PyObject *args, int (*warp)(PyObject
return NULL;
}
imIn = core->image;
Py_DECREF(core);

/* copy a new image */
newimage = PyObject_CallMethod(image, "copy", NULL);
Expand All @@ -142,6 +150,7 @@ static PyObject* _lens_warp(PyObject *self, PyObject *args, int (*warp)(PyObject
return NULL;
}
imOut = core->image;
Py_DECREF(core);

if (!PyArg_ParseTuple(empty_color, "iiii", &er, &eg, &eb, &ea))
{
Expand Down Expand Up @@ -182,23 +191,6 @@ static PyObject* _lens_warp(PyObject *self, PyObject *args, int (*warp)(PyObject
/* ----------- */
warp(func, xx, yy, &xnew, &ynew);

/* if ((ret = PyObject_CallFunction(func, "dd", xx, yy)) == NULL) */
/* { */
/* return NULL; */
/* } */
/* if (!PyArg_ParseTuple(ret, "dd", &xnew, &ynew)) */
/* { */
/* return NULL; */
/* } */

/* radius2 = xx * xx + yy * yy; */
/* radius = sqrt(radius2); */
/* phi = atan2(yy, xx); */

/* radius = sqrt(radius); */
/* xnew = radius * cos(phi); */
/* ynew = radius * sin(phi); */

/* ------------------------- */

i2 = 0.5 * width * (xnew + 1);
Expand Down Expand Up @@ -240,14 +232,17 @@ static PyObject* _lens_warp(PyObject *self, PyObject *args, int (*warp)(PyObject

static PyObject* lens_warp(PyObject *self, PyObject *args)
{
return _lens_warp(self, args, _xy_warp);
PyObject *img = _lens_warp(self, args, _xy_warp);
return img;
}

static PyObject* radian_warp(PyObject *self, PyObject *args)
{
return _lens_warp(self, args, _radian_warp);
PyObject *img = _lens_warp(self, args, _radian_warp);
return img;
}

/* 波浪效果 */
static PyObject* wave_warp(PyObject *self, PyObject *args)
{
PyObject *image;
Expand All @@ -263,7 +258,7 @@ static PyObject* wave_warp(PyObject *self, PyObject *args)
double xnew, ynew;
UINT8 *pixel;
int i3, j3;
PyObject *ret, *empty_color;
PyObject *empty_color;
double dw = 1, dh=0.3;
double radian;
double offset;
Expand All @@ -280,6 +275,7 @@ static PyObject* wave_warp(PyObject *self, PyObject *args)
return NULL;
}
imIn = core->image;
Py_DECREF(core);

/* copy a new image */
newimage = PyObject_CallMethod(image, "copy", NULL);
Expand All @@ -288,6 +284,7 @@ static PyObject* wave_warp(PyObject *self, PyObject *args)
return NULL;
}
imOut = core->image;
Py_DECREF(core);

if (!PyArg_ParseTuple(empty_color, "iiii", &er, &eg, &eb, &ea))
{
Expand Down Expand Up @@ -366,6 +363,7 @@ static PyObject* wave_warp(PyObject *self, PyObject *args)
return (PyObject *)newimage;
}


static PyMethodDef CoreMethods[] = {
{"lens_warp", lens_warp, METH_VARARGS, "Global warp"},
{"radian_warp", radian_warp, METH_VARARGS, "Radian Warp"},
Expand Down

0 comments on commit fc4ce80

Please sign in to comment.