| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | /* IMGFILE module - Interface to sgi libimage */ | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-07-16 12:04:32 +00:00
										 |  |  | /* XXX This module should be done better at some point. It should return
 | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | ** 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). | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | ** | 
					
						
							|  |  |  | ** Warning: this module is very non-reentrant (esp. the readscaled stuff) | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #include <gl/image.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-01-19 15:33:13 +00:00
										 |  |  | #include "/usr/people/4Dgifts/iristools/include/izoom.h"
 | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-09 18:49:42 +00:00
										 |  |  | /* 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(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyObject * ImgfileError; /* Exception we raise for various trouble */ | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | static int top_to_bottom;	/* True if we want top-to-bottom images */ | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | /* 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(). */ | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-03-23 18:21:32 +00:00
										 |  |  | static int error_called; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* The error handler */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:43:35 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_error(char *str) | 
					
						
							| 
									
										
										
										
											1992-03-23 18:21:32 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	PyErr_SetString(ImgfileError, str); | 
					
						
							|  |  |  | 	error_called = 1; | 
					
						
							|  |  |  | 	return;	/* To imglib, which will return a failure indicator */ | 
					
						
							| 
									
										
										
										
											1992-03-23 18:21:32 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Open an image file and return a pointer to it.
 | 
					
						
							|  |  |  |    Make sure we raise an exception if we fail. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static IMAGE * | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_open(char *fname) | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_ttob(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	int newval; | 
					
						
							|  |  |  | 	PyObject *rv; | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	if (!PyArg_ParseTuple(args, "i:ttob", &newval)) | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 	rv = PyInt_FromLong(top_to_bottom); | 
					
						
							|  |  |  | 	top_to_bottom = newval; | 
					
						
							|  |  |  | 	return rv; | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_read(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	if ( !PyArg_ParseTuple(args, "s:read", &fname) ) | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	if ( (image = imgfile_open(fname)) == NULL ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		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; x<xsize; x++ ) | 
					
						
							|  |  |  | 				*cdatap++ = rs[x]; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			getrow(image, rs, y, 0); | 
					
						
							|  |  |  | 			getrow(image, gs, y, 1); | 
					
						
							|  |  |  | 			getrow(image, bs, y, 2); | 
					
						
							|  |  |  | 			for(x=0; x<xsize; x++ ) | 
					
						
							|  |  |  | 				*idatap++ = (rs[x] & 0xff)  | | 
					
						
							|  |  |  | 					((gs[x] & 0xff)<<8) | | 
					
						
							|  |  |  | 					((bs[x] & 0xff)<<16); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	iclose(image); | 
					
						
							|  |  |  | 	if ( error_called ) { | 
					
						
							|  |  |  | 		Py_DECREF(rv); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return rv; | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1993-06-17 12:35:49 +00:00
										 |  |  | static IMAGE *glob_image; | 
					
						
							|  |  |  | static long *glob_datap; | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | static int glob_width, glob_z, glob_ysize; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:43:35 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | xs_get(short *buf, int y) | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	if (top_to_bottom) | 
					
						
							|  |  |  | 		getrow(glob_image, buf, (glob_ysize-1-y), glob_z); | 
					
						
							|  |  |  | 	else | 
					
						
							|  |  |  | 		getrow(glob_image, buf, y, glob_z); | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:43:35 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | xs_put_c(short *buf, int y) | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	char *datap = (char *)glob_datap + y*glob_width; | 
					
						
							|  |  |  | 	int width = glob_width; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	while ( width-- ) | 
					
						
							|  |  |  | 		*datap++ = (*buf++) & 0xff; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-05 23:43:35 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | xs_put_0(short *buf, int y) | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	long *datap = glob_datap + y*glob_width; | 
					
						
							|  |  |  | 	int width = glob_width; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	while ( width-- ) | 
					
						
							|  |  |  | 		*datap++ = (*buf++) & 0xff; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-05 23:43:35 +00:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | xs_put_12(short *buf, int y) | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	long *datap = glob_datap + y*glob_width; | 
					
						
							|  |  |  | 	int width = glob_width; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	while ( width-- ) | 
					
						
							|  |  |  | 		*datap++ |= ((*buf++) & 0xff) << (glob_z*8); | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | xscale(IMAGE *image, int xsize, int ysize, int zsize, | 
					
						
							|  |  |  |        long *datap, int xnew, int ynew, int fmode, double blur) | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	glob_image = image; | 
					
						
							|  |  |  | 	glob_datap = datap; | 
					
						
							|  |  |  | 	glob_width = xnew; | 
					
						
							|  |  |  | 	glob_ysize = ysize; | 
					
						
							|  |  |  | 	if ( zsize == 1 ) { | 
					
						
							|  |  |  | 		glob_z = 0; | 
					
						
							|  |  |  | 		filterzoom(xs_get, xs_put_c, xsize, ysize, | 
					
						
							|  |  |  | 			   xnew, ynew, fmode, blur); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		glob_z = 0; | 
					
						
							|  |  |  | 		filterzoom(xs_get, xs_put_0, xsize, ysize, | 
					
						
							|  |  |  | 			   xnew, ynew, fmode, blur); | 
					
						
							|  |  |  | 		glob_z = 1; | 
					
						
							|  |  |  | 		filterzoom(xs_get, xs_put_12, xsize, ysize, | 
					
						
							|  |  |  | 			   xnew, ynew, fmode, blur); | 
					
						
							|  |  |  | 		glob_z = 2; | 
					
						
							|  |  |  | 		filterzoom(xs_get, xs_put_12, xsize, ysize, | 
					
						
							|  |  |  | 			   xnew, ynew, fmode, blur); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_readscaled(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	char *fname; | 
					
						
							|  |  |  | 	PyObject *rv; | 
					
						
							|  |  |  | 	int xsize, ysize, zsize; | 
					
						
							|  |  |  | 	char *cdatap; | 
					
						
							|  |  |  | 	long *idatap; | 
					
						
							|  |  |  | 	static short rs[8192], gs[8192], bs[8192]; | 
					
						
							|  |  |  | 	int x, y; | 
					
						
							|  |  |  | 	int xwtd, ywtd, xorig, yorig; | 
					
						
							|  |  |  | 	float xfac, yfac; | 
					
						
							|  |  |  | 	IMAGE *image; | 
					
						
							|  |  |  | 	char *filter; | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	double blur = 1.0; | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	int extended; | 
					
						
							|  |  |  | 	int fmode = 0; | 
					
						
							|  |  |  | 	int yfirst, ylast, ystep; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	** Parse args. Funny, since arg 4 and 5 are optional | 
					
						
							|  |  |  | 	** (filter name and blur factor). Also, 4 or 5 arguments indicates | 
					
						
							|  |  |  | 	** extended scale algorithm in stead of simple-minded pixel drop/dup. | 
					
						
							|  |  |  | 	*/ | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	extended = PyTuple_Size(args) >= 4; | 
					
						
							|  |  |  | 	if ( !PyArg_ParseTuple(args, "sii|sd", | 
					
						
							|  |  |  | 			       &fname, &xwtd, &ywtd, &filter, &blur) ) | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	** Check parameters, open file and check type, rows, etc. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 	if ( extended ) { | 
					
						
							|  |  |  | 		if ( strcmp(filter, "impulse") == 0 ) | 
					
						
							|  |  |  | 			fmode = IMPULSE; | 
					
						
							|  |  |  | 		else if ( strcmp( filter, "box") == 0 ) | 
					
						
							|  |  |  | 			fmode = BOX; | 
					
						
							|  |  |  | 		else if ( strcmp( filter, "triangle") == 0 ) | 
					
						
							|  |  |  | 			fmode = TRIANGLE; | 
					
						
							|  |  |  | 		else if ( strcmp( filter, "quadratic") == 0 ) | 
					
						
							|  |  |  | 			fmode = QUADRATIC; | 
					
						
							|  |  |  | 		else if ( strcmp( filter, "gaussian") == 0 ) | 
					
						
							|  |  |  | 			fmode = GAUSSIAN; | 
					
						
							|  |  |  | 		else { | 
					
						
							|  |  |  | 			PyErr_SetString(ImgfileError, "Unknown filter type"); | 
					
						
							|  |  |  | 			return NULL; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	if ( (image = imgfile_open(fname)) == NULL ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	if ( zsize == 3 ) zsize = 4; | 
					
						
							|  |  |  | 	rv = PyString_FromStringAndSize(NULL, xwtd*ywtd*zsize); | 
					
						
							|  |  |  | 	if ( rv == NULL ) { | 
					
						
							|  |  |  | 		iclose(image); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1993-10-11 12:54:31 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1997-02-14 22:59:58 +00:00
										 |  |  | 	PyFPE_START_PROTECT("readscaled", return 0) | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	xfac = (float)xsize/(float)xwtd; | 
					
						
							|  |  |  | 	yfac = (float)ysize/(float)ywtd; | 
					
						
							| 
									
										
										
										
											1997-03-14 04:32:50 +00:00
										 |  |  | 	PyFPE_END_PROTECT(yfac) | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		for ( y=yfirst; y != ylast && !error_called; y += ystep ) { | 
					
						
							|  |  |  | 			yorig = (int)(y*yfac); | 
					
						
							|  |  |  | 			if ( zsize == 1 ) { | 
					
						
							|  |  |  | 				getrow(image, rs, yorig, 0); | 
					
						
							|  |  |  | 				for(x=0; x<xwtd; x++ ) { | 
					
						
							|  |  |  | 					*cdatap++ = rs[(int)(x*xfac)];	 | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				getrow(image, rs, yorig, 0); | 
					
						
							|  |  |  | 				getrow(image, gs, yorig, 1); | 
					
						
							|  |  |  | 				getrow(image, bs, yorig, 2); | 
					
						
							|  |  |  | 				for(x=0; x<xwtd; x++ ) { | 
					
						
							|  |  |  | 					xorig = (int)(x*xfac); | 
					
						
							|  |  |  | 					*idatap++ = (rs[xorig] & 0xff)  | | 
					
						
							|  |  |  | 						((gs[xorig] & 0xff)<<8) | | 
					
						
							|  |  |  | 						((bs[xorig] & 0xff)<<16); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											1993-01-19 15:17:13 +00:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	iclose(image); | 
					
						
							|  |  |  | 	if ( error_called ) { | 
					
						
							|  |  |  | 		Py_DECREF(rv); | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return rv; | 
					
						
							| 
									
										
										
										
											1992-06-03 17:06:36 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_getsizes(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	char *fname; | 
					
						
							|  |  |  | 	PyObject *rv; | 
					
						
							|  |  |  | 	IMAGE *image; | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	if ( !PyArg_ParseTuple(args, "s:getsizes", &fname) ) | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	if ( (image = imgfile_open(fname)) == NULL ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							|  |  |  | 	rv = Py_BuildValue("(iii)", image->xsize, image->ysize, image->zsize); | 
					
						
							|  |  |  | 	iclose(image); | 
					
						
							|  |  |  | 	return rv; | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 17:04:33 +00:00
										 |  |  | imgfile_write(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	if ( !PyArg_ParseTuple(args, "ss#iii:write", | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 			  &fname, &cdatap, &len, &xsize, &ysize, &zsize) ) | 
					
						
							|  |  |  | 		return NULL; | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	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; | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	idatap = (long *)cdatap; | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  |      | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	if (top_to_bottom) { | 
					
						
							|  |  |  | 		yfirst = ysize-1; | 
					
						
							|  |  |  | 		ylast = -1; | 
					
						
							|  |  |  | 		ystep = -1; | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  | 	} else { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 		yfirst = 0; | 
					
						
							|  |  |  | 		ylast = ysize; | 
					
						
							|  |  |  | 		ystep = 1; | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	for ( y=yfirst; y != ylast && !error_called; y += ystep ) { | 
					
						
							|  |  |  | 		if ( zsize == 1 ) { | 
					
						
							|  |  |  | 			for( x=0; x<xsize; x++ ) | 
					
						
							|  |  |  | 				rs[x] = *cdatap++; | 
					
						
							|  |  |  | 			putrow(image, rs, y, 0); | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			for( x=0; x<xsize; x++ ) { | 
					
						
							|  |  |  | 				rgb = *idatap++; | 
					
						
							|  |  |  | 				r = rgb & 0xff; | 
					
						
							|  |  |  | 				g = (rgb >> 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; | 
					
						
							| 
									
										
										
										
											1992-08-20 11:54:27 +00:00
										 |  |  |      | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1992-09-03 20:41:22 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | static PyMethodDef imgfile_methods[] = { | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	{ "getsizes",	imgfile_getsizes, METH_VARARGS }, | 
					
						
							|  |  |  | 	{ "read",	imgfile_read, METH_VARARGS }, | 
					
						
							| 
									
										
										
										
											2002-01-17 23:15:58 +00:00
										 |  |  | 	{ "readscaled",	imgfile_readscaled, METH_VARARGS}, | 
					
						
							| 
									
										
										
										
											2002-03-31 15:43:28 +00:00
										 |  |  | 	{ "write",	imgfile_write, METH_VARARGS }, | 
					
						
							|  |  |  | 	{ "ttob",	imgfile_ttob, METH_VARARGS }, | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	{ NULL,		NULL } /* Sentinel */ | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							| 
									
										
										
										
											2000-07-21 06:00:07 +00:00
										 |  |  | initimgfile(void) | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	PyObject *m, *d; | 
					
						
							|  |  |  | 	m = Py_InitModule("imgfile", imgfile_methods); | 
					
						
							| 
									
										
										
										
											2006-01-19 06:09:39 +00:00
										 |  |  | 	if (m == NULL) | 
					
						
							|  |  |  | 		return; | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 	d = PyModule_GetDict(m); | 
					
						
							| 
									
										
										
										
											1997-10-01 04:29:29 +00:00
										 |  |  | 	ImgfileError = PyErr_NewException("imgfile.error", NULL, NULL); | 
					
						
							|  |  |  | 	if (ImgfileError != NULL) | 
					
						
							|  |  |  | 		PyDict_SetItemString(d, "error", ImgfileError); | 
					
						
							| 
									
										
										
										
											1992-03-04 16:40:03 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											1996-12-20 21:56:08 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 |