[mapserver-dev] Re: Python method getImageGDString for 3.7 mapscript

Sean Gillies sgillies at i3.com
Mon Aug 19 11:05:37 EDT 2002


Sorry!

Forgot to attach the code.  I've put it inside the imageObj
methods in mapscript.i.

Sean Gillies wrote:
> Last week I submitted code for a Python getImageString method
> that Steve Lime added to the CVS.
> 
> I've upgraded the code so that it works with the developing
> mapserver and gd-2.0.1.  You'll notice that the code looks
> almost exactly like msSaveImageGD().
> 
> Have not tested it with GIF or WBMP, but don't see anything
> wrong with my calls to gdImageGifPtr() or gdImageWBMPPtr().
> 
> Soon I'll be able to contribute a Python getImageGDALString.
> 
> Please let me know if you think this is useful code.
> 
> cheers,

-- 
Sean Gillies             Applications Developer
i-cubed : information integration & imaging LLC
970-482-4400 voice
970-482-4499 fax              http://www.i3.com
-------------- next part --------------
// ------------------------------------------------------------------------
// Method getImageGDString renders the imageObj into image data and returns
// it as a string. Inspired by and used like the msSaveImageGD() method. 
// Python only at this time. 
//
// Questions and comments to Sean Gillies <sgillies at i3.com>

#ifdef SWIGPYTHON
    PyObject *getImageGDString() {
        unsigned char *imgbytes;
        int size;
        PyObject *imgstring;

#if GD2_VERS > 1
        if (self->format->imagemode == MS_IMAGEMODE_RGBA) {
            gdImageSaveAlpha(self->img.gd, 1);
        }
        else if (self->format->imagemode == MS_IMAGEMODE_RGB) {
            gdImageSaveAlpha(self->img.gd, 0);
        }
#endif

        if (strcasecmp("ON", msGetOutputFormatOption(self->format, "INTERLACE", "ON" )) == 0) {
            gdImageInterlace(self->img.gd, 1);
        }
        if (self->format->transparent) {
            gdImageColorTransparent(self->img.gd, 0);
        }
        if (strcasecmp(self->format->driver, "gd/gif") == 0) {
#ifdef USE_GD_GIF
            imgbytes = gdImageGifPtr(self->img.gd, &size);
#else
            msSetError(MS_MISCERR, "GIF output is not available.", "getImageGDString()");
            return(MS_FAILURE);
#endif
        }
        else if (strcasecmp(self->format->driver, "gd/png") == 0) {
#ifdef USE_GD_PNG
            imgbytes = gdImagePngPtr(self->img.gd, &size);
#else
            msSetError(MS_MISCERR, "PNG output is not available.", "getImageGDString()");
            return(MS_FAILURE);
#endif
        }
        else if (strcasecmp(self->format->driver, "gd/jpeg") == 0) {
#ifdef USE_GD_JPEG
            imgbytes = gdImageJpegPtr(self->img.gd, &size, atoi(msGetOutputFormatOption(self->format, "QUALITY", "75" )));
#else
            msSetError(MS_MISCERR, "JPEG output is not available.", "getImageGDString()");
            return(MS_FAILURE);
#endif
        }
        else if (strcasecmp(self->format->driver, "gd/wbmp") == 0) {
#ifdef USE_GD_WBMP
            imgbytes = gdImageWBMPPtr(self->img.gd, &size, 1);
#else
            msSetError(MS_MISCERR, "WBMP output is not available.", "getImageGDString()");
            return(MS_FAILURE);
#endif
        }
        else {
            msSetError(MS_MISCERR, "Unknown output image type driver: %s.", "getImageGDString()", self->format->driver );
            return(MS_FAILURE);
        }

        // Create a Python string from the (char *) imgbytes.
        // The gdImage*Ptr functions return a size for just this purpose.
        imgstring = PyString_FromStringAndSize(imgbytes, size);

        // The gd docs recommend gdFree()
        gdFree(imgbytes);

        return imgstring;
    }
#endif


More information about the mapserver-dev mailing list