Skip to content
Permalink
Browse files

Remove broken custom Python imageObj constructor code

  • Loading branch information...
geographika committed Oct 10, 2018
1 parent f3edb87 commit 275b91f6cdfa2e0646f4f95ee9aef2e657011542
Showing with 3 additions and 178 deletions.
  1. +0 −171 mapscript/python/pyextend.i
  2. +2 −2 mapscript/python/tests/cases/image_test.py
  3. +1 −5 mapscript/swiginc/image.i
@@ -147,183 +147,12 @@ def fromstring(data, mappath=None):

}

/****************************************************************************
* Support for bridging Python file-like objects and GD through IOCtx
***************************************************************************/

/******************************************************************************
* Extensions to imageObj
*****************************************************************************/

%extend imageObj {

/* New imageObj constructor taking an optional PyFile-ish object
* argument.
*
* Furthermore, we are defaulting width and height so that in case
* anyone wants to swig mapscript with the -keyword option, they can
* omit width and height. Work done as part of Bugzilla issue 550. */

imageObj(PyObject *arg1=Py_None, PyObject *arg2=Py_None,
PyObject *input_format=Py_None, PyObject *input_resolution=Py_None, PyObject *input_defresolution=Py_None)
{
#ifdef FORCE_BROKEN_GD_CODE
imageObj *image=NULL;
outputFormatObj *format=NULL;
int width;
int height;
double resolution, defresolution;
PyObject *pybytes;
rendererVTableObj *renderer = NULL;
rasterBufferObj *rb = NULL;

unsigned char PNGsig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
unsigned char JPEGsig[3] = {255, 216, 255};

resolution = defresolution = MS_DEFAULT_RESOLUTION;

if ((PyInt_Check(arg1) && PyInt_Check(arg2)) || PyString_Check(arg1))
{
if (input_format == Py_None) {
format = msCreateDefaultOutputFormat(NULL, "GD/GIF", "gdgif");
if (format == NULL)
format = msCreateDefaultOutputFormat(NULL, "GD/PNG", "gdpng");

if (format)
msInitializeRendererVTable(format);
}
else if (PyString_Check(input_format)) {
format = msCreateDefaultOutputFormat(NULL,
PyString_AsString(input_format),
NULL);
}
else {
if ((SWIG_ConvertPtr(input_format, (void **) &format,
SWIGTYPE_p_outputFormatObj,
SWIG_POINTER_EXCEPTION | 0 )) == -1)
{
msSetError(MS_IMGERR, "Can't convert format pointer",
"imageObj()");
return NULL;
}
}

if (format == NULL) {
msSetError(MS_IMGERR, "Could not create output format",
"imageObj()");
return NULL;
}
}

if (PyFloat_Check(input_resolution))
resolution = PyFloat_AsDouble(input_resolution);
if (PyFloat_Check(input_defresolution))
defresolution = PyFloat_AsDouble(input_defresolution);

if (PyInt_Check(arg1) && PyInt_Check(arg2))
{
/* Create from width, height, format/driver */
width = (int) PyInt_AsLong(arg1);
height = (int) PyInt_AsLong(arg2);

image = msImageCreate(width, height, format, NULL, NULL, resolution, defresolution, NULL);
return image;
}

/* Is arg1 a filename? */
else if (PyString_Check(arg1))
{
renderer = format->vtable;
rb = (rasterBufferObj*)calloc(1,sizeof(rasterBufferObj));

if (!rb) {
msSetError(MS_MEMERR, NULL, "imageObj()");
return NULL;
}

if ( (renderer->loadImageFromFile(PyString_AsString(arg1), rb)) == MS_FAILURE)
return NULL;

image = msImageCreate(rb->width, rb->height, format, NULL, NULL,
resolution, defresolution, NULL);
renderer->mergeRasterBuffer(image, rb, 1.0, 0, 0, 0, 0, rb->width, rb->height);

msFreeRasterBuffer(rb);
free(rb);

return image;
}

/* Is a file-like object */
else if (arg1 != Py_None)
{

if (PyObject_HasAttrString(arg1, "seek"))
{
/* Detect image format */
pybytes = PyObject_CallMethod(arg1, "read", "i", 8);
PyObject_CallMethod(arg1, "seek", "i", 0);

if (memcmp(PyString_AsString(pybytes),"GIF8",4)==0)
{
%#ifdef USE_GD_GIF
image = createImageObjFromPyFile(arg1, "GD/GIF");
%#else
msSetError(MS_MISCERR, "Unable to load GIF image.",
"imageObj()");
%#endif
}
else if (memcmp(PyString_AsString(pybytes),PNGsig,8)==0)
{
%#ifdef USE_GD_PNG
image = createImageObjFromPyFile(arg1, "GD/PNG");
%#else
msSetError(MS_MISCERR, "Unable to load PNG image.",
"imageObj()");
%#endif
}
else if (memcmp(PyString_AsString(pybytes),JPEGsig,3)==0)
{
%#ifdef USE_GD_JPEG
image = createImageObjFromPyFile(arg1, "GD/JPEG");
%#else
msSetError(MS_MISCERR, "Unable to load JPEG image.",
"imageObj()");
%#endif
}
else
{
msSetError(MS_MISCERR, "Failed to detect image format. Likely cause is invalid image or improper filemode. On windows, Python files should be opened in 'rb' mode.", "imageObj()");
}

return image;

}
else /* such as a url handle */
{
/* If there is no seek method, we absolutely must
have a driver name */
if (!PyString_Check(arg2))
{
msSetError(MS_MISCERR, "A driver name absolutely must accompany file objects which do not have a seek() method", "imageObj()");
return NULL;
}
return (imageObj *) createImageObjFromPyFile(arg1,
PyString_AsString(arg2));
}
}
else
{
msSetError(MS_IMGERR, "Failed to create image",
"imageObj()");
return NULL;
}
#else
msSetError(MS_IMGERR, "imageObj() is severely broken and should not be used","imageObj()");
return NULL;
#endif
}

/* ======================================================================
write()

@@ -62,14 +62,14 @@ def testSaveToString(self):

class ImageObjTestCase(unittest.TestCase):

def xtestConstructor(self):
def testConstructor(self):
"""imageObj constructor works"""
imgobj = mapscript.imageObj(10, 10)
assert imgobj.thisown == 1
assert imgobj.height == 10
assert imgobj.width == 10

def xtestConstructorWithFormat(self):
def testConstructorWithFormat(self):
"""imageObj with an optional driver works"""
driver = 'AGG/PNG'
format = mapscript.outputFormatObj(driver)
@@ -30,10 +30,7 @@

%extend imageObj {

/* imageObj constructor now takes filename as an optional argument.
* If the target language is Python, we ignore this constructor and
* instead use the one in python/pymodule.i. */
#ifndef SWIGPYTHON
/* imageObj constructor now takes filename as an optional argument. */
imageObj(int width, int height, outputFormatObj *input_format=NULL,
const char *file=NULL,
double resolution=MS_DEFAULT_RESOLUTION, double defresolution=MS_DEFAULT_RESOLUTION)
@@ -81,7 +78,6 @@
image = msImageCreate(width, height, format, NULL, NULL, resolution, defresolution, NULL);
return image;
}
#endif

~imageObj()
{

0 comments on commit 275b91f

Please sign in to comment.
You can’t perform that action at this time.