MapServer / MapServer-import

3 stars 2 forks source link

Create an imageObj instance from a FILE* #550

Closed tbonfort closed 12 years ago

tbonfort commented 12 years ago

Reporter: sgillies@frii.com Date: 2004/02/01 - 03:53

Would be great to be able to create an imageObj from an open file.
Happily, Python's built-in File object is implemented entirely on
FILE* from the C standard library, making the following possible:

    file = open('foo.png', 'rb')
    imgobj = imageObj(0, 0, None, file)

The trio of dummy arguments to imageObj above are necessary to
avoid breaking the existing API.

Any Python object which implements the File interface including
StringIO, result of urllib.urlopen() etc should be equally good
stream arguments.  I think we'll find this useful for the other
flavors of MapScript, but I am going to hold off on writing the
Perl typemap until a Perl user or two shows interest.
tbonfort commented 12 years ago

Author: sgillies@frii.com Date: 2004/02/01 - 04:03

A Perl typemap would probably use PerlIO_exportFile() but there appear to be
more issues involved (predictably) than with Python.  Will leave it for the
future.

The Python typemap is simple:

// Type mapping for grabbing a FILE * from Python
%typemap(python,in) FILE * {
    if (!PyFile_Check($source)) {
        PyErr_SetString(PyExc_TypeError, "");
        return NULL;
    }
    $target = PyFile_AsFile($source);
}
tbonfort commented 12 years ago

Author: sgillies@frii.com Date: 2004/02/02 - 05:39

I still haven't mastered GD's IOCtx API and can't burn a lot of time writing
Python _and_ Perl typemaps for it, so took the easy way out and wrote code
using the Python C API only.  Will revisit this at some point if there is
enough interest from Perl users.

Perl users will benefit from two optional arguments to the imageObj constructor
which allows these use cases

    $imgobj = mapscript::imageObj(100, 100);                   # default format
    $imgobj = mapscript::imageObj(100, 100, "GD/JPEG");        # JPEG
    $imgobj = mapscript::imageObj(0, 0, undef, "foo.png");     # from file

Python gets these and more.  Here are examples from the unit tests:

    imgobj = mapscript.imageObj(10, 10)
    imgobj = mapscript.imageObj(10, 10, "GD/PNG")
    imgobj = mapscript.imageObj(0, 0, None, "foo.png")

    f = file("foo.png", "rb")
    imgobj = mapscript.imageObj(0, 0, "GD/PNG", f)   # Must specify driver 

    import StringIO
    f = file("foo.png", "rb")
    s = StringIO.StringIO(f.read())  # store imagery in memory
    f.close()
    imgobj = mapscript.imageObj(0, 0, "GD/PNG", s)   # Must specify driver

    import urllib
    u = urllib.urlopen("http://host/foo.png")
    imgobj = mapscript.imageObj(0, 0, "GD/PNG", u)   # Must specify driver