Commit a5f61380 authored by Guido van Rossum's avatar Guido van Rossum

Got rid the bogus cache code and fix some unchecked errors.

parent ebc8c51c
...@@ -22,7 +22,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -22,7 +22,7 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/ ******************************************************************/
/* IMGFILE module - Interface to sgi libimg */ /* IMGFILE module - Interface to sgi libimage */
/* XXX This modele should be done better at some point. It should return /* 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 ** an object of image file class, and have routines to manipulate these
...@@ -35,16 +35,20 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ...@@ -35,16 +35,20 @@ OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#include "modsupport.h" #include "modsupport.h"
#include <gl/image.h> #include <gl/image.h>
#include <errno.h>
/* #include "sigtype.h" */ static object * ImgfileError; /* Exception we raise for various trouble */
static object * ImgfileError;
static char gfname[1024]; /* The image library does not always call the error hander :-(,
static IMAGE *image; therefore we have a global variable indicating that it was called.
It is cleared by imgfile_open(). */
static int error_called; static int error_called;
/* The error handler */
static imgfile_error(str) static imgfile_error(str)
char *str; char *str;
{ {
...@@ -53,27 +57,32 @@ static imgfile_error(str) ...@@ -53,27 +57,32 @@ static imgfile_error(str)
return; /* To imglib, which will return a failure indictaor */ return; /* To imglib, which will return a failure indictaor */
} }
static
/* Open an image file and return a pointer to it.
Make sure we raise an exception if we fail. */
static IMAGE *
imgfile_open(fname) imgfile_open(fname)
char *fname; char *fname;
{ {
IMAGE *image;
i_seterror(imgfile_error); i_seterror(imgfile_error);
error_called = 0; error_called = 0;
if ( image != NULL && strcmp(fname, gfname) != 0 ) { errno = 0;
iclose(image); if ( (image = iopen(fname, "r")) == NULL ) {
image = NULL;
gfname[0] = '\0';
}
if ( (image=iopen(fname, "r")) == NULL ) {
/* Error may already be set by imgfile_error */ /* Error may already be set by imgfile_error */
if ( !error_called ) if ( !error_called ) {
err_setstr(ImgfileError, "Cannot open image file"); if (errno)
return 0; err_errno(ImgfileError);
else
err_setstr(ImgfileError, "Can't open image file");
}
return NULL;
} }
strcpy(gfname, fname); return image;
return 1;
} }
static object * static object *
imgfile_read(self, args) imgfile_read(self, args)
object *self; object *self;
...@@ -86,33 +95,44 @@ imgfile_read(self, args) ...@@ -86,33 +95,44 @@ imgfile_read(self, args)
long *idatap; long *idatap;
static short rs[8192], gs[8192], bs[8192]; static short rs[8192], gs[8192], bs[8192];
int x, y; int x, y;
IMAGE *image;
if ( !getargs(args, "s", &fname) ) if ( !getargs(args, "s", &fname) )
return 0; return NULL;
if ( !imgfile_open(fname) ) if ( (image = imgfile_open(fname)) == NULL )
return NULL; return NULL;
if ( image->colormap != CM_NORMAL ) { if ( image->colormap != CM_NORMAL ) {
iclose(image);
err_setstr(ImgfileError, "Can only handle CM_NORMAL images"); err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
return NULL; return NULL;
} }
if ( BPP(image->type) != 1 ) { if ( BPP(image->type) != 1 ) {
err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1"); iclose(image);
err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
return NULL; return NULL;
} }
xsize = image->xsize; xsize = image->xsize;
ysize = image->ysize; ysize = image->ysize;
zsize = image->zsize; zsize = image->zsize;
if ( zsize != 1 && zsize != 3) { if ( zsize != 1 && zsize != 3) {
iclose(image);
err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
return NULL; return NULL;
} }
if ( xsize > 8192 ) {
iclose(image);
err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
return NULL;
}
if ( zsize == 3 ) zsize = 4; if ( zsize == 3 ) zsize = 4;
rv = newsizedstringobject(NULL, xsize*ysize*zsize); rv = newsizedstringobject((char *)NULL, xsize*ysize*zsize);
if ( rv == NULL ) if ( rv == NULL ) {
iclose(image);
return NULL; return NULL;
}
cdatap = getstringvalue(rv); cdatap = getstringvalue(rv);
idatap = (long *)cdatap; idatap = (long *)cdatap;
for ( y=0; y < ysize && !error_called; y++ ) { for ( y=0; y < ysize && !error_called; y++ ) {
...@@ -138,6 +158,7 @@ imgfile_read(self, args) ...@@ -138,6 +158,7 @@ imgfile_read(self, args)
return rv; return rv;
} }
static object * static object *
imgfile_readscaled(self, args) imgfile_readscaled(self, args)
object *self; object *self;
...@@ -153,35 +174,46 @@ imgfile_readscaled(self, args) ...@@ -153,35 +174,46 @@ imgfile_readscaled(self, args)
int xwtd, ywtd, xorig, yorig; int xwtd, ywtd, xorig, yorig;
float xfac, yfac; float xfac, yfac;
int cnt; int cnt;
IMAGE *image;
if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) ) if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) )
return 0; return NULL;
if ( !imgfile_open(fname) ) if ( (image = imgfile_open(fname)) == NULL )
return NULL; return NULL;
if ( image->colormap != CM_NORMAL ) { if ( image->colormap != CM_NORMAL ) {
iclose(image);
err_setstr(ImgfileError, "Can only handle CM_NORMAL images"); err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
return NULL; return NULL;
} }
if ( BPP(image->type) != 1 ) { if ( BPP(image->type) != 1 ) {
err_setstr(ImgfileError, "Cannot handle imgfiles with bpp!=1"); iclose(image);
err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
return NULL; return NULL;
} }
xsize = image->xsize; xsize = image->xsize;
ysize = image->ysize; ysize = image->ysize;
zsize = image->zsize; zsize = image->zsize;
if ( zsize != 1 && zsize != 3) { if ( zsize != 1 && zsize != 3) {
iclose(image);
err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
return NULL; return NULL;
} }
if ( xsize > 8192 ) {
iclose(image);
err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
return NULL;
}
if ( zsize == 3 ) zsize = 4; if ( zsize == 3 ) zsize = 4;
rv = newsizedstringobject(NULL, xwtd*ywtd*zsize); rv = newsizedstringobject(NULL, xwtd*ywtd*zsize);
if ( rv == NULL ) {
iclose(image);
return NULL;
}
xfac = (float)xsize/(float)xwtd; xfac = (float)xsize/(float)xwtd;
yfac = (float)ysize/(float)ywtd; yfac = (float)ysize/(float)ywtd;
if ( rv == NULL )
return NULL;
cdatap = getstringvalue(rv); cdatap = getstringvalue(rv);
idatap = (long *)cdatap; idatap = (long *)cdatap;
for ( y=0; y < ywtd && !error_called; y++ ) { for ( y=0; y < ywtd && !error_called; y++ ) {
...@@ -203,6 +235,7 @@ imgfile_readscaled(self, args) ...@@ -203,6 +235,7 @@ imgfile_readscaled(self, args)
} }
} }
} }
iclose(image);
if ( error_called ) { if ( error_called ) {
DECREF(rv); DECREF(rv);
return NULL; return NULL;
...@@ -210,6 +243,7 @@ imgfile_readscaled(self, args) ...@@ -210,6 +243,7 @@ imgfile_readscaled(self, args)
return rv; return rv;
} }
static object * static object *
imgfile_getsizes(self, args) imgfile_getsizes(self, args)
object *self; object *self;
...@@ -217,21 +251,19 @@ imgfile_getsizes(self, args) ...@@ -217,21 +251,19 @@ imgfile_getsizes(self, args)
{ {
char *fname; char *fname;
object *rv; object *rv;
IMAGE *image;
if ( !getargs(args, "s", &fname) ) if ( !getargs(args, "s", &fname) )
return 0;
if ( !imgfile_open(fname) )
return NULL; return NULL;
rv = newtupleobject(3);
if ( rv == NULL ) if ( (image = imgfile_open(fname)) == NULL )
return NULL; return NULL;
settupleitem(rv, 0, newintobject(image->xsize)); rv = mkvalue("(iii)", image->xsize, image->ysize, image->zsize);
settupleitem(rv, 1, newintobject(image->ysize)); iclose(image);
settupleitem(rv, 2, newintobject(image->zsize));
return rv; return rv;
} }
static object * static object *
imgfile_write(self, args) imgfile_write(self, args)
object *self; object *self;
...@@ -249,23 +281,32 @@ imgfile_write(self, args) ...@@ -249,23 +281,32 @@ imgfile_write(self, args)
if ( !getargs(args, "(ss#iii)", if ( !getargs(args, "(ss#iii)",
&fname, &cdatap, &len, &xsize, &ysize, &zsize) ) &fname, &cdatap, &len, &xsize, &ysize, &zsize) )
return 0; return NULL;
if ( zsize != 1 && zsize != 3 ) { if ( zsize != 1 && zsize != 3 ) {
err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels"); err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
return 0; return NULL;
} }
if ( (zsize == 1 && len != xsize*ysize) || if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
(zsize == 3 && len != xsize*ysize*4) ) {
err_setstr(ImgfileError, "Data does not match sizes"); err_setstr(ImgfileError, "Data does not match sizes");
return 0; return NULL;
}
if ( xsize > 8192 ) {
err_setstr(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); image =iopen(fname, "w", RLE(1), 3, xsize, ysize, zsize);
if ( image == 0 ) { if ( image == 0 ) {
if ( ! error_called ) if ( ! error_called ) {
err_setstr(ImgfileError, "Cannot create image file"); if (errno)
return 0; err_errno(ImgfileError);
else
err_setstr(ImgfileError, "Can't create image file");
}
return NULL;
} }
idatap = (long *)cdatap; idatap = (long *)cdatap;
...@@ -292,18 +333,19 @@ imgfile_write(self, args) ...@@ -292,18 +333,19 @@ imgfile_write(self, args)
} }
iclose(image); iclose(image);
if ( error_called ) if ( error_called )
return 0; return NULL;
INCREF(None); INCREF(None);
return None; return None;
} }
static struct methodlist imgfile_methods[] = { static struct methodlist imgfile_methods[] = {
{ "getsizes", imgfile_getsizes }, { "getsizes", imgfile_getsizes },
{ "read", imgfile_read }, { "read", imgfile_read },
{ "readscaled", imgfile_readscaled }, { "readscaled", imgfile_readscaled },
{ "write", imgfile_write }, { "write", imgfile_write },
{ 0, 0 } { NULL, NULL } /* Sentinel */
}; };
...@@ -314,6 +356,6 @@ initimgfile() ...@@ -314,6 +356,6 @@ initimgfile()
m = initmodule("imgfile", imgfile_methods); m = initmodule("imgfile", imgfile_methods);
d = getmoduledict(m); d = getmoduledict(m);
ImgfileError = newstringobject("imgfile.error"); ImgfileError = newstringobject("imgfile.error");
if ( ImgfileError == NULL || dictinsert(d,"error",ImgfileError) ) if ( ImgfileError == NULL || dictinsert(d, "error", ImgfileError) )
fatal("can't define imgfile.error"); fatal("can't define imgfile.error");
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment