mirror of
https://github.com/python/cpython.git
synced 2025-11-03 03:22:27 +00:00
Renamed.
This commit is contained in:
parent
c357325663
commit
7f33e403a4
1 changed files with 430 additions and 404 deletions
|
|
@ -40,11 +40,9 @@ PERFORMANCE OF THIS SOFTWARE.
|
||||||
** Warning: this module is very non-reentrant (esp. the readscaled stuff)
|
** Warning: this module is very non-reentrant (esp. the readscaled stuff)
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include "allobjects.h"
|
#include "Python.h"
|
||||||
#include "modsupport.h"
|
|
||||||
|
|
||||||
#include <gl/image.h>
|
#include <gl/image.h>
|
||||||
#include <errno.h>
|
|
||||||
|
|
||||||
#include "/usr/people/4Dgifts/iristools/include/izoom.h"
|
#include "/usr/people/4Dgifts/iristools/include/izoom.h"
|
||||||
|
|
||||||
|
|
@ -55,7 +53,7 @@ extern void filterzoom();
|
||||||
extern void putrow();
|
extern void putrow();
|
||||||
extern void getrow();
|
extern void getrow();
|
||||||
|
|
||||||
static object * ImgfileError; /* Exception we raise for various trouble */
|
static PyObject * ImgfileError; /* Exception we raise for various trouble */
|
||||||
|
|
||||||
static int top_to_bottom; /* True if we want top-to-bottom images */
|
static int top_to_bottom; /* True if we want top-to-bottom images */
|
||||||
|
|
||||||
|
|
@ -70,11 +68,11 @@ static int error_called;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
imgfile_error(str)
|
imgfile_error(str)
|
||||||
char *str;
|
char *str;
|
||||||
{
|
{
|
||||||
err_setstr(ImgfileError, str);
|
PyErr_SetString(ImgfileError, str);
|
||||||
error_called = 1;
|
error_called = 1;
|
||||||
return; /* To imglib, which will return a failure indicator */
|
return; /* To imglib, which will return a failure indicator */
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -83,124 +81,129 @@ imgfile_error(str)
|
||||||
|
|
||||||
static IMAGE *
|
static IMAGE *
|
||||||
imgfile_open(fname)
|
imgfile_open(fname)
|
||||||
char *fname;
|
char *fname;
|
||||||
{
|
{
|
||||||
IMAGE *image;
|
IMAGE *image;
|
||||||
i_seterror(imgfile_error);
|
i_seterror(imgfile_error);
|
||||||
error_called = 0;
|
error_called = 0;
|
||||||
errno = 0;
|
errno = 0;
|
||||||
if ( (image = iopen(fname, "r")) == NULL ) {
|
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 ) {
|
||||||
if (errno)
|
if (errno)
|
||||||
err_errno(ImgfileError);
|
PyErr_SetFromErrno(ImgfileError);
|
||||||
else
|
else
|
||||||
err_setstr(ImgfileError, "Can't open image file");
|
PyErr_SetString(ImgfileError,
|
||||||
}
|
"Can't open image file");
|
||||||
return NULL;
|
}
|
||||||
}
|
return NULL;
|
||||||
return image;
|
|
||||||
}
|
|
||||||
|
|
||||||
static object *
|
|
||||||
imgfile_ttob(self, args)
|
|
||||||
object *self;
|
|
||||||
object *args;
|
|
||||||
{
|
|
||||||
int newval;
|
|
||||||
object *rv;
|
|
||||||
|
|
||||||
if (!getargs(args, "i", &newval))
|
|
||||||
return NULL;
|
|
||||||
rv = newintobject(top_to_bottom);
|
|
||||||
top_to_bottom = newval;
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
static object *
|
|
||||||
imgfile_read(self, args)
|
|
||||||
object *self;
|
|
||||||
object *args;
|
|
||||||
{
|
|
||||||
char *fname;
|
|
||||||
object *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 ( !getargs(args, "s", &fname) )
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if ( (image = imgfile_open(fname)) == NULL )
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if ( image->colormap != CM_NORMAL ) {
|
|
||||||
iclose(image);
|
|
||||||
err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if ( BPP(image->type) != 1 ) {
|
|
||||||
iclose(image);
|
|
||||||
err_setstr(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);
|
|
||||||
err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if ( xsize > 8192 ) {
|
|
||||||
iclose(image);
|
|
||||||
err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if ( zsize == 3 ) zsize = 4;
|
|
||||||
rv = newsizedstringobject((char *)NULL, xsize*ysize*zsize);
|
|
||||||
if ( rv == NULL ) {
|
|
||||||
iclose(image);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
cdatap = getstringvalue(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; 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);
|
|
||||||
}
|
}
|
||||||
}
|
return image;
|
||||||
iclose(image);
|
}
|
||||||
if ( error_called ) {
|
|
||||||
DECREF(rv);
|
static PyObject *
|
||||||
return NULL;
|
imgfile_ttob(self, args)
|
||||||
}
|
PyObject *self;
|
||||||
return rv;
|
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; 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iclose(image);
|
||||||
|
if ( error_called ) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
static IMAGE *glob_image;
|
static IMAGE *glob_image;
|
||||||
|
|
@ -209,334 +212,357 @@ static int glob_width, glob_z, glob_ysize;
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xs_get(buf, y)
|
xs_get(buf, y)
|
||||||
short *buf;
|
short *buf;
|
||||||
int y;
|
int y;
|
||||||
{
|
{
|
||||||
if (top_to_bottom)
|
if (top_to_bottom)
|
||||||
getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
|
getrow(glob_image, buf, (glob_ysize-1-y), glob_z);
|
||||||
else
|
else
|
||||||
getrow(glob_image, buf, y, glob_z);
|
getrow(glob_image, buf, y, glob_z);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xs_put_c(buf, y)
|
xs_put_c(buf, y)
|
||||||
short *buf;
|
short *buf;
|
||||||
int y;
|
int y;
|
||||||
{
|
{
|
||||||
char *datap = (char *)glob_datap + y*glob_width;
|
char *datap = (char *)glob_datap + y*glob_width;
|
||||||
int width = glob_width;
|
int width = glob_width;
|
||||||
|
|
||||||
while ( width-- )
|
while ( width-- )
|
||||||
*datap++ = (*buf++) & 0xff;
|
*datap++ = (*buf++) & 0xff;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xs_put_0(buf, y)
|
xs_put_0(buf, y)
|
||||||
short *buf;
|
short *buf;
|
||||||
int y;
|
int y;
|
||||||
{
|
{
|
||||||
long *datap = glob_datap + y*glob_width;
|
long *datap = glob_datap + y*glob_width;
|
||||||
int width = glob_width;
|
int width = glob_width;
|
||||||
|
|
||||||
while ( width-- )
|
while ( width-- )
|
||||||
*datap++ = (*buf++) & 0xff;
|
*datap++ = (*buf++) & 0xff;
|
||||||
}
|
}
|
||||||
static void
|
static void
|
||||||
xs_put_12(buf, y)
|
xs_put_12(buf, y)
|
||||||
short *buf;
|
short *buf;
|
||||||
int y;
|
int y;
|
||||||
{
|
{
|
||||||
long *datap = glob_datap + y*glob_width;
|
long *datap = glob_datap + y*glob_width;
|
||||||
int width = glob_width;
|
int width = glob_width;
|
||||||
|
|
||||||
while ( width-- )
|
while ( width-- )
|
||||||
*datap++ |= ((*buf++) & 0xff) << (glob_z*8);
|
*datap++ |= ((*buf++) & 0xff) << (glob_z*8);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
|
xscale(image, xsize, ysize, zsize, datap, xnew, ynew, fmode, blur)
|
||||||
IMAGE *image;
|
IMAGE *image;
|
||||||
int xsize, ysize, zsize;
|
int xsize, ysize, zsize;
|
||||||
long *datap;
|
long *datap;
|
||||||
int xnew, ynew;
|
int xnew, ynew;
|
||||||
int fmode;
|
int fmode;
|
||||||
double blur;
|
double blur;
|
||||||
{
|
{
|
||||||
glob_image = image;
|
glob_image = image;
|
||||||
glob_datap = datap;
|
glob_datap = datap;
|
||||||
glob_width = xnew;
|
glob_width = xnew;
|
||||||
glob_ysize = ysize;
|
glob_ysize = ysize;
|
||||||
if ( zsize == 1 ) {
|
if ( zsize == 1 ) {
|
||||||
glob_z = 0;
|
glob_z = 0;
|
||||||
filterzoom(xs_get, xs_put_c, xsize, ysize, xnew, ynew, fmode, blur);
|
filterzoom(xs_get, xs_put_c, xsize, ysize,
|
||||||
} else {
|
xnew, ynew, fmode, blur);
|
||||||
glob_z = 0;
|
} else {
|
||||||
filterzoom(xs_get, xs_put_0, xsize, ysize, xnew, ynew, fmode, blur);
|
glob_z = 0;
|
||||||
glob_z = 1;
|
filterzoom(xs_get, xs_put_0, xsize, ysize,
|
||||||
filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
|
xnew, ynew, fmode, blur);
|
||||||
glob_z = 2;
|
glob_z = 1;
|
||||||
filterzoom(xs_get, xs_put_12, xsize, ysize, xnew, ynew, fmode, blur);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static object *
|
static PyObject *
|
||||||
imgfile_readscaled(self, args)
|
imgfile_readscaled(self, args)
|
||||||
object *self;
|
PyObject *self;
|
||||||
object *args;
|
PyObject *args;
|
||||||
{
|
{
|
||||||
char *fname;
|
char *fname;
|
||||||
object *rv;
|
PyObject *rv;
|
||||||
int xsize, ysize, zsize;
|
int xsize, ysize, zsize;
|
||||||
char *cdatap;
|
char *cdatap;
|
||||||
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;
|
||||||
int xwtd, ywtd, xorig, yorig;
|
int xwtd, ywtd, xorig, yorig;
|
||||||
float xfac, yfac;
|
float xfac, yfac;
|
||||||
int cnt;
|
int cnt;
|
||||||
IMAGE *image;
|
IMAGE *image;
|
||||||
char *filter;
|
char *filter;
|
||||||
double blur;
|
double blur;
|
||||||
int extended;
|
int extended;
|
||||||
int fmode = 0;
|
int fmode = 0;
|
||||||
int yfirst, ylast, ystep;
|
int yfirst, ylast, ystep;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Parse args. Funny, since arg 4 and 5 are optional
|
** Parse args. Funny, since arg 4 and 5 are optional
|
||||||
** (filter name and blur factor). Also, 4 or 5 arguments indicates
|
** (filter name and blur factor). Also, 4 or 5 arguments indicates
|
||||||
** extended scale algorithm in stead of simple-minded pixel drop/dup.
|
** extended scale algorithm in stead of simple-minded pixel drop/dup.
|
||||||
*/
|
*/
|
||||||
extended = 0;
|
extended = 0;
|
||||||
cnt = gettuplesize(args);
|
cnt = PyTuple_Size(args);
|
||||||
if ( cnt == 5 ) {
|
if ( cnt == 5 ) {
|
||||||
extended = 1;
|
extended = 1;
|
||||||
if ( !getargs(args, "(siisd)", &fname, &xwtd, &ywtd, &filter, &blur) )
|
if ( !PyArg_Parse(args, "(siisd)",
|
||||||
return NULL;
|
&fname, &xwtd, &ywtd, &filter, &blur) )
|
||||||
} else if ( cnt == 4 ) {
|
return NULL;
|
||||||
extended = 1;
|
} else if ( cnt == 4 ) {
|
||||||
if ( !getargs(args, "(siis)", &fname, &xwtd, &ywtd, &filter) )
|
extended = 1;
|
||||||
return NULL;
|
if ( !PyArg_Parse(args, "(siis)",
|
||||||
blur = 1.0;
|
&fname, &xwtd, &ywtd, &filter) )
|
||||||
} else if ( !getargs(args, "(sii)", &fname, &xwtd, &ywtd) )
|
return NULL;
|
||||||
return NULL;
|
blur = 1.0;
|
||||||
|
} else if ( !PyArg_Parse(args, "(sii)", &fname, &xwtd, &ywtd) )
|
||||||
|
return NULL;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
** Check parameters, open file and check type, rows, etc.
|
** Check parameters, open file and check type, rows, etc.
|
||||||
*/
|
*/
|
||||||
if ( extended ) {
|
if ( extended ) {
|
||||||
if ( strcmp(filter, "impulse") == 0 ) fmode = IMPULSE;
|
if ( strcmp(filter, "impulse") == 0 )
|
||||||
else if ( strcmp( filter, "box") == 0 ) fmode = BOX;
|
fmode = IMPULSE;
|
||||||
else if ( strcmp( filter, "triangle") == 0 ) fmode = TRIANGLE;
|
else if ( strcmp( filter, "box") == 0 )
|
||||||
else if ( strcmp( filter, "quadratic") == 0 ) fmode = QUADRATIC;
|
fmode = BOX;
|
||||||
else if ( strcmp( filter, "gaussian") == 0 ) fmode = GAUSSIAN;
|
else if ( strcmp( filter, "triangle") == 0 )
|
||||||
else {
|
fmode = TRIANGLE;
|
||||||
err_setstr(ImgfileError, "Unknown filter type");
|
else if ( strcmp( filter, "quadratic") == 0 )
|
||||||
return NULL;
|
fmode = QUADRATIC;
|
||||||
|
else if ( strcmp( filter, "gaussian") == 0 )
|
||||||
|
fmode = GAUSSIAN;
|
||||||
|
else {
|
||||||
|
PyErr_SetString(ImgfileError, "Unknown filter type");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if ( (image = imgfile_open(fname)) == NULL )
|
if ( (image = imgfile_open(fname)) == NULL )
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
if ( image->colormap != CM_NORMAL ) {
|
if ( image->colormap != CM_NORMAL ) {
|
||||||
iclose(image);
|
iclose(image);
|
||||||
err_setstr(ImgfileError, "Can only handle CM_NORMAL images");
|
PyErr_SetString(ImgfileError,
|
||||||
return NULL;
|
"Can only handle CM_NORMAL images");
|
||||||
}
|
return NULL;
|
||||||
if ( BPP(image->type) != 1 ) {
|
}
|
||||||
iclose(image);
|
if ( BPP(image->type) != 1 ) {
|
||||||
err_setstr(ImgfileError, "Can't handle imgfiles with bpp!=1");
|
iclose(image);
|
||||||
return NULL;
|
PyErr_SetString(ImgfileError,
|
||||||
}
|
"Can't handle imgfiles with bpp!=1");
|
||||||
xsize = image->xsize;
|
return NULL;
|
||||||
ysize = image->ysize;
|
}
|
||||||
zsize = image->zsize;
|
xsize = image->xsize;
|
||||||
if ( zsize != 1 && zsize != 3) {
|
ysize = image->ysize;
|
||||||
iclose(image);
|
zsize = image->zsize;
|
||||||
err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
|
if ( zsize != 1 && zsize != 3) {
|
||||||
return NULL;
|
iclose(image);
|
||||||
}
|
PyErr_SetString(ImgfileError,
|
||||||
if ( xsize > 8192 ) {
|
"Can only handle 1 or 3 byte pixels");
|
||||||
iclose(image);
|
return NULL;
|
||||||
err_setstr(ImgfileError, "Can't handle image with > 8192 columns");
|
}
|
||||||
return NULL;
|
if ( xsize > 8192 ) {
|
||||||
}
|
iclose(image);
|
||||||
|
PyErr_SetString(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 = PyString_FromStringAndSize(NULL, xwtd*ywtd*zsize);
|
||||||
if ( rv == NULL ) {
|
if ( rv == NULL ) {
|
||||||
iclose(image);
|
iclose(image);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
xfac = (float)xsize/(float)xwtd;
|
xfac = (float)xsize/(float)xwtd;
|
||||||
yfac = (float)ysize/(float)ywtd;
|
yfac = (float)ysize/(float)ywtd;
|
||||||
cdatap = getstringvalue(rv);
|
cdatap = PyString_AsString(rv);
|
||||||
idatap = (long *)cdatap;
|
idatap = (long *)cdatap;
|
||||||
|
|
||||||
if ( extended ) {
|
if ( extended ) {
|
||||||
xscale(image, xsize, ysize, zsize, idatap, xwtd, ywtd, fmode, blur);
|
xscale(image, xsize, ysize, zsize,
|
||||||
} else {
|
idatap, xwtd, ywtd, fmode, blur);
|
||||||
if (top_to_bottom) {
|
|
||||||
yfirst = ywtd-1;
|
|
||||||
ylast = -1;
|
|
||||||
ystep = -1;
|
|
||||||
} else {
|
} else {
|
||||||
yfirst = 0;
|
if (top_to_bottom) {
|
||||||
ylast = ywtd;
|
yfirst = ywtd-1;
|
||||||
ystep = 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; 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
iclose(image);
|
||||||
|
if ( error_called ) {
|
||||||
|
Py_DECREF(rv);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
return rv;
|
||||||
|
}
|
||||||
|
|
||||||
|
static PyObject *
|
||||||
|
imgfile_getsizes(self, args)
|
||||||
|
PyObject *self;
|
||||||
|
PyObject *args;
|
||||||
|
{
|
||||||
|
char *fname;
|
||||||
|
PyObject *rv;
|
||||||
|
IMAGE *image;
|
||||||
|
|
||||||
|
if ( !PyArg_Parse(args, "s", &fname) )
|
||||||
|
return NULL;
|
||||||
|
|
||||||
|
if ( (image = imgfile_open(fname)) == NULL )
|
||||||
|
return NULL;
|
||||||
|
rv = Py_BuildValue("(iii)", image->xsize, 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 ) {
|
for ( y=yfirst; y != ylast && !error_called; y += ystep ) {
|
||||||
yorig = (int)(y*yfac);
|
if ( zsize == 1 ) {
|
||||||
if ( zsize == 1 ) {
|
for( x=0; x<xsize; x++ )
|
||||||
getrow(image, rs, yorig, 0);
|
rs[x] = *cdatap++;
|
||||||
for(x=0; x<xwtd; x++ ) {
|
putrow(image, rs, y, 0);
|
||||||
*cdatap++ = rs[(int)(x*xfac)];
|
} 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);
|
||||||
}
|
}
|
||||||
} 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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
iclose(image);
|
||||||
iclose(image);
|
if ( error_called )
|
||||||
if ( error_called ) {
|
return NULL;
|
||||||
DECREF(rv);
|
Py_INCREF(Py_None);
|
||||||
return NULL;
|
return Py_None;
|
||||||
}
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
static object *
|
|
||||||
imgfile_getsizes(self, args)
|
|
||||||
object *self;
|
|
||||||
object *args;
|
|
||||||
{
|
|
||||||
char *fname;
|
|
||||||
object *rv;
|
|
||||||
IMAGE *image;
|
|
||||||
|
|
||||||
if ( !getargs(args, "s", &fname) )
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if ( (image = imgfile_open(fname)) == NULL )
|
|
||||||
return NULL;
|
|
||||||
rv = mkvalue("(iii)", image->xsize, image->ysize, image->zsize);
|
|
||||||
iclose(image);
|
|
||||||
return rv;
|
|
||||||
}
|
|
||||||
|
|
||||||
static object *
|
|
||||||
imgfile_write(self, args)
|
|
||||||
object *self;
|
|
||||||
object *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 ( !getargs(args, "(ss#iii)",
|
|
||||||
&fname, &cdatap, &len, &xsize, &ysize, &zsize) )
|
|
||||||
return NULL;
|
|
||||||
|
|
||||||
if ( zsize != 1 && zsize != 3 ) {
|
|
||||||
err_setstr(ImgfileError, "Can only handle 1 or 3 byte pixels");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if ( len != xsize * ysize * (zsize == 1 ? 1 : 4) ) {
|
|
||||||
err_setstr(ImgfileError, "Data does not match sizes");
|
|
||||||
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);
|
|
||||||
if ( image == 0 ) {
|
|
||||||
if ( ! error_called ) {
|
|
||||||
if (errno)
|
|
||||||
err_errno(ImgfileError);
|
|
||||||
else
|
|
||||||
err_setstr(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<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;
|
|
||||||
INCREF(None);
|
|
||||||
return None;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static struct methodlist imgfile_methods[] = {
|
static PyMethodDef imgfile_methods[] = {
|
||||||
{ "getsizes", imgfile_getsizes },
|
{ "getsizes", imgfile_getsizes },
|
||||||
{ "read", imgfile_read },
|
{ "read", imgfile_read },
|
||||||
{ "readscaled", imgfile_readscaled, 1},
|
{ "readscaled", imgfile_readscaled, 1},
|
||||||
{ "write", imgfile_write },
|
{ "write", imgfile_write },
|
||||||
{ "ttob", imgfile_ttob },
|
{ "ttob", imgfile_ttob },
|
||||||
{ NULL, NULL } /* Sentinel */
|
{ NULL, NULL } /* Sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
void
|
void
|
||||||
initimgfile()
|
initimgfile()
|
||||||
{
|
{
|
||||||
object *m, *d;
|
PyObject *m, *d;
|
||||||
m = initmodule("imgfile", imgfile_methods);
|
m = Py_InitModule("imgfile", imgfile_methods);
|
||||||
d = getmoduledict(m);
|
d = PyModule_GetDict(m);
|
||||||
ImgfileError = newstringobject("imgfile.error");
|
ImgfileError = PyString_FromString("imgfile.error");
|
||||||
if ( ImgfileError == NULL || dictinsert(d, "error", ImgfileError) )
|
if ( ImgfileError == NULL
|
||||||
fatal("can't define imgfile.error");
|
|| PyDict_SetItemString(d, "error", ImgfileError) )
|
||||||
|
Py_FatalError("can't define imgfile.error");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue