/*********************************************************** Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, The Netherlands. All Rights Reserved Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation, and that the names of Stichting Mathematisch Centrum or CWI or Corporation for National Research Initiatives or CNRI not be used in advertising or publicity pertaining to distribution of the software without specific, written prior permission. While CWI is the initial source for this software, a modified version is made available by the Corporation for National Research Initiatives (CNRI) at the Internet address ftp://ftp.python.org. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ******************************************************************/ /* IMGFILE module - Interface to sgi libimage */ /* XXX This modele should be done better at some point. It should return ** an object of image file class, and have routines to manipulate these ** image files in a neater way (so you can get rgb images off a greyscale ** file, for instance, or do a straight display without having to get the ** image bits into python, etc). ** ** Warning: this module is very non-reentrant (esp. the readscaled stuff) */ #include "Python.h" #include #include "/usr/people/4Dgifts/iristools/include/izoom.h" /* Bunch of missing extern decls; keep gcc -Wall happy... */ extern void i_seterror(); extern void iclose(); extern void filterzoom(); extern void putrow(); extern void getrow(); static PyObject * ImgfileError; /* Exception we raise for various trouble */ static int top_to_bottom; /* True if we want top-to-bottom images */ /* The image library does not always call the error hander :-(, therefore we have a global variable indicating that it was called. It is cleared by imgfile_open(). */ static int error_called; /* The error handler */ static void imgfile_error(str) char *str; { PyErr_SetString(ImgfileError, str); error_called = 1; return; /* To imglib, which will return a failure indicator */ } /* Open an image file and return a pointer to it. Make sure we raise an exception if we fail. */ static IMAGE * imgfile_open(fname) char *fname; { IMAGE *image; i_seterror(imgfile_error); error_called = 0; errno = 0; if ( (image = iopen(fname, "r")) == NULL ) { /* Error may already be set by imgfile_error */ if ( !error_called ) { if (errno) PyErr_SetFromErrno(ImgfileError); else PyErr_SetString(ImgfileError, "Can't open image file"); } return NULL; } return image; } static PyObject * imgfile_ttob(self, args) PyObject *self; PyObject *args; { int newval; PyObject *rv; if (!PyArg_Parse(args, "i", &newval)) return NULL; rv = PyInt_FromLong(top_to_bottom); top_to_bottom = newval; return rv; } static PyObject * imgfile_read(self, args) PyObject *self; PyObject *args; { char *fname; PyObject *rv; int xsize, ysize, zsize; char *cdatap; long *idatap; static short rs[8192], gs[8192], bs[8192]; int x, y; IMAGE *image; int yfirst, ylast, ystep; if ( !PyArg_Parse(args, "s", &fname) ) return NULL; if ( (image = imgfile_open(fname)) == NULL ) return NULL; if ( image->colormap != CM_NORMAL ) { iclose(image); PyErr_SetString(ImgfileError, "Can only handle CM_NORMAL images"); return NULL; } if ( BPP(image->type) != 1 ) { iclose(image); PyErr_SetString(ImgfileError, "Can't handle imgfiles with bpp!=1"); return NULL; } xsize = image->xsize; ysize = image->ysize; zsize = image->zsize; if ( zsize != 1 && zsize != 3) { iclose(image); PyErr_SetString(ImgfileError, "Can only handle 1 or 3 byte pixels"); return NULL; } if ( xsize > 8192 ) { iclose(image); PyErr_SetString(ImgfileError, "Can't handle image with > 8192 columns"); return NULL; } if ( zsize == 3 ) zsize = 4; rv = PyString_FromStringAndSize((char *)NULL, xsize*ysize*zsize); if ( rv == NULL ) { iclose(image); return NULL; } cdatap = PyString_AsString(rv); idatap = (long *)cdatap; if (top_to_bottom) { yfirst = ysize-1; ylast = -1; ystep = -1; } else { yfirst = 0; ylast = ysize; ystep = 1; } for ( y=yfirst; y != ylast && !error_called; y += ystep ) { if ( zsize == 1 ) { getrow(image, rs, y, 0); for(x=0; xcolormap != CM_NORMAL ) { iclose(image); PyErr_SetString(ImgfileError, "Can only handle CM_NORMAL images"); return NULL; } if ( BPP(image->type) != 1 ) { iclose(image); PyErr_SetString(ImgfileError, "Can't handle imgfiles with bpp!=1"); return NULL; } xsize = image->xsize; ysize = image->ysize; zsize = image->zsize; if ( zsize != 1 && zsize != 3) { iclose(image); PyErr_SetString(ImgfileError, "Can only handle 1 or 3 byte pixels"); return NULL; } if ( xsize > 8192 ) { iclose(image); PyErr_SetString(ImgfileError, "Can't handle image with > 8192 columns"); return NULL; } if ( zsize == 3 ) zsize = 4; rv = PyString_FromStringAndSize(NULL, xwtd*ywtd*zsize); if ( rv == NULL ) { iclose(image); return NULL; } PyFPE_START_PROTECT("readscaled", return 0) xfac = (float)xsize/(float)xwtd; yfac = (float)ysize/(float)ywtd; PyFPE_END_PROTECT(yfac) cdatap = PyString_AsString(rv); idatap = (long *)cdatap; if ( extended ) { xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur); } else { if (top_to_bottom) { yfirst = ywtd-1; ylast = -1; ystep = -1; } else { yfirst = 0; ylast = ywtd; ystep = 1; } for ( y=yfirst; y != ylast && !error_called; y += ystep ) { yorig = (int)(y*yfac); if ( zsize == 1 ) { getrow(image, rs, yorig, 0); for(x=0; xxsize, image->ysize, image->zsize); iclose(image); return rv; } static PyObject * imgfile_write(self, args) PyObject *self; PyObject *args; { IMAGE *image; char *fname; int xsize, ysize, zsize, len; char *cdatap; long *idatap; short rs[8192], gs[8192], bs[8192]; short r, g, b; long rgb; int x, y; int yfirst, ylast, ystep; if ( !PyArg_Parse(args, "(ss#iii)", &fname, &cdatap, &len, &xsize, &ysize, &zsize) ) return NULL; if ( zsize != 1 && zsize != 3 ) { PyErr_SetString(ImgfileError, "Can only handle 1 or 3 byte pixels"); return NULL; } if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) { PyErr_SetString(ImgfileError, "Data does not match sizes"); return NULL; } if ( xsize > 8192 ) { PyErr_SetString(ImgfileError, "Can't handle image with > 8192 columns"); return NULL; } error_called = 0; errno = 0; image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize); if ( image == 0 ) { if ( ! error_called ) { if (errno) PyErr_SetFromErrno(ImgfileError); else PyErr_SetString(ImgfileError, "Can't create image file"); } return NULL; } idatap = (long *)cdatap; if (top_to_bottom) { yfirst = ysize-1; ylast = -1; ystep = -1; } else { yfirst = 0; ylast = ysize; ystep = 1; } for ( y=yfirst; y != ylast && !error_called; y += ystep ) { if ( zsize == 1 ) { for( x=0; x> 8 ) & 0xff; b = (rgb >> 16 ) & 0xff; rs[x] = r; gs[x] = g; bs[x] = b; } putrow(image, rs, y, 0); putrow(image, gs, y, 1); putrow(image, bs, y, 2); } } iclose(image); if ( error_called ) return NULL; Py_INCREF(Py_None); return Py_None; } static PyMethodDef imgfile_methods[] = { { "getsizes", imgfile_getsizes }, { "read", imgfile_read }, { "readscaled", imgfile_readscaled, 1}, { "write", imgfile_write }, { "ttob", imgfile_ttob }, { NULL, NULL } /* Sentinel */ }; void initimgfile() { PyObject *m, *d; m = Py_InitModule("imgfile", imgfile_methods); d = PyModule_GetDict(m); ImgfileError = PyString_FromString("imgfile.error"); if ( ImgfileError == NULL || PyDict_SetItemString(d, "error", ImgfileError) ) Py_FatalError("can't define imgfile.error"); }