diff options
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/Setup.dist | 2 | ||||
-rw-r--r-- | Modules/_bsddb.c | 1 | ||||
-rw-r--r-- | Modules/_cursesmodule.c | 4 | ||||
-rw-r--r-- | Modules/_struct.c | 2 | ||||
-rw-r--r-- | Modules/_tkinter.c | 6 | ||||
-rw-r--r-- | Modules/datetimemodule.c | 18 | ||||
-rw-r--r-- | Modules/imageop.c | 785 | ||||
-rw-r--r-- | Modules/parsermodule.c | 35 | ||||
-rw-r--r-- | Modules/rgbimgmodule.c | 780 |
9 files changed, 41 insertions, 1592 deletions
diff --git a/Modules/Setup.dist b/Modules/Setup.dist index 86b7963..7e098a8 100644 --- a/Modules/Setup.dist +++ b/Modules/Setup.dist @@ -218,8 +218,6 @@ _symtable symtablemodule.c # These represent audio samples or images as strings: #audioop audioop.c # Operations on audio samples -#imageop imageop.c # Operations on images -#rgbimg rgbimgmodule.c # Read SGI RGB image files (but coded portably) # Note that the _md5 and _sha modules are normally only built if the diff --git a/Modules/_bsddb.c b/Modules/_bsddb.c index aa706e7..ef0c7f3 100644 --- a/Modules/_bsddb.c +++ b/Modules/_bsddb.c @@ -1724,6 +1724,7 @@ DB_get_both(DBObject* self, PyObject* args, PyObject* kwargs) CHECK_DB_NOT_CLOSED(self); if (!make_key_dbt(self, keyobj, &key, NULL)) return NULL; + CLEAR_DBT(data); if ( !make_dbt(dataobj, &data) || !checkTxnObj(txnobj, &txn) ) { diff --git a/Modules/_cursesmodule.c b/Modules/_cursesmodule.c index a6a6ba3..918e22d 100644 --- a/Modules/_cursesmodule.c +++ b/Modules/_cursesmodule.c @@ -1299,7 +1299,7 @@ static PyObject * PyCursesWindow_RedrawLine(PyCursesWindowObject *self, PyObject *args) { int beg, num; - if (!PyArg_ParseTuple(args,"ii;beg,num", &beg, &num)) + if (!PyArg_ParseTuple(args, "ii;beg,num", &beg, &num)) return NULL; return PyCursesCheckERR(wredrawln(self->win,beg,num), "redrawln"); } @@ -1533,7 +1533,7 @@ static PyMethodDef PyCursesWindow_Methods[] = { {"overwrite", (PyCFunction)PyCursesWindow_Overwrite, METH_VARARGS}, {"putwin", (PyCFunction)PyCursesWindow_PutWin, METH_VARARGS}, - {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine}, + {"redrawln", (PyCFunction)PyCursesWindow_RedrawLine, METH_VARARGS}, {"redrawwin", (PyCFunction)PyCursesWindow_redrawwin, METH_NOARGS}, {"refresh", (PyCFunction)PyCursesWindow_Refresh, METH_VARARGS}, #ifndef STRICT_SYSV_CURSES diff --git a/Modules/_struct.c b/Modules/_struct.c index b9c29d5..08b3102 100644 --- a/Modules/_struct.c +++ b/Modules/_struct.c @@ -1817,7 +1817,7 @@ static struct PyMethodDef s_methods[] = { {"pack", s_pack, METH_VARARGS, s_pack__doc__}, {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, {"unpack", s_unpack, METH_O, s_unpack__doc__}, - {"unpack_from", (PyCFunction)s_unpack_from, METH_KEYWORDS, + {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, s_unpack_from__doc__}, {NULL, NULL} /* sentinel */ }; diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c index 790a1be..12490b0 100644 --- a/Modules/_tkinter.c +++ b/Modules/_tkinter.c @@ -2668,8 +2668,8 @@ static PyMethodDef Tkapp_methods[] = { {"willdispatch", Tkapp_WillDispatch, METH_NOARGS}, {"wantobjects", Tkapp_WantObjects, METH_VARARGS}, - {"call", Tkapp_Call, METH_OLDARGS}, - {"globalcall", Tkapp_GlobalCall, METH_OLDARGS}, + {"call", Tkapp_Call, METH_VARARGS}, + {"globalcall", Tkapp_GlobalCall, METH_VARARGS}, {"eval", Tkapp_Eval, METH_VARARGS}, {"globaleval", Tkapp_GlobalEval, METH_VARARGS}, {"evalfile", Tkapp_EvalFile, METH_VARARGS}, @@ -2690,7 +2690,7 @@ static PyMethodDef Tkapp_methods[] = {"exprboolean", Tkapp_ExprBoolean, METH_VARARGS}, {"splitlist", Tkapp_SplitList, METH_VARARGS}, {"split", Tkapp_Split, METH_VARARGS}, - {"merge", Tkapp_Merge, METH_OLDARGS}, + {"merge", Tkapp_Merge, METH_VARARGS}, {"createcommand", Tkapp_CreateCommand, METH_VARARGS}, {"deletecommand", Tkapp_DeleteCommand, METH_VARARGS}, #ifdef HAVE_CREATEFILEHANDLER diff --git a/Modules/datetimemodule.c b/Modules/datetimemodule.c index 5539659..d90a83c 100644 --- a/Modules/datetimemodule.c +++ b/Modules/datetimemodule.c @@ -2603,7 +2603,7 @@ static PyMethodDef date_methods[] = { {"ctime", (PyCFunction)date_ctime, METH_NOARGS, PyDoc_STR("Return ctime() style string.")}, - {"strftime", (PyCFunction)date_strftime, METH_KEYWORDS, + {"strftime", (PyCFunction)date_strftime, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("format -> strftime() style string.")}, {"timetuple", (PyCFunction)date_timetuple, METH_NOARGS, @@ -2628,7 +2628,7 @@ static PyMethodDef date_methods[] = { PyDoc_STR("Return the day of the week represented by the date.\n" "Monday == 0 ... Sunday == 6")}, - {"replace", (PyCFunction)date_replace, METH_KEYWORDS, + {"replace", (PyCFunction)date_replace, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Return date with new specified fields.")}, {"__reduce__", (PyCFunction)date_reduce, METH_NOARGS, @@ -3375,7 +3375,7 @@ static PyMethodDef time_methods[] = { PyDoc_STR("Return string in ISO 8601 format, HH:MM:SS[.mmmmmm]" "[+HH:MM].")}, - {"strftime", (PyCFunction)time_strftime, METH_KEYWORDS, + {"strftime", (PyCFunction)time_strftime, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("format -> strftime() style string.")}, {"utcoffset", (PyCFunction)time_utcoffset, METH_NOARGS, @@ -3387,7 +3387,7 @@ static PyMethodDef time_methods[] = { {"dst", (PyCFunction)time_dst, METH_NOARGS, PyDoc_STR("Return self.tzinfo.dst(self).")}, - {"replace", (PyCFunction)time_replace, METH_KEYWORDS, + {"replace", (PyCFunction)time_replace, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Return time with new specified fields.")}, {"__reduce__", (PyCFunction)time_reduce, METH_NOARGS, @@ -4411,7 +4411,7 @@ static PyMethodDef datetime_methods[] = { /* Class methods: */ {"now", (PyCFunction)datetime_now, - METH_KEYWORDS | METH_CLASS, + METH_VARARGS | METH_KEYWORDS | METH_CLASS, PyDoc_STR("[tz] -> new datetime with tz's local day and time.")}, {"utcnow", (PyCFunction)datetime_utcnow, @@ -4419,7 +4419,7 @@ static PyMethodDef datetime_methods[] = { PyDoc_STR("Return a new datetime representing UTC day and time.")}, {"fromtimestamp", (PyCFunction)datetime_fromtimestamp, - METH_KEYWORDS | METH_CLASS, + METH_VARARGS | METH_KEYWORDS | METH_CLASS, PyDoc_STR("timestamp[, tz] -> tz's local time from POSIX timestamp.")}, {"utcfromtimestamp", (PyCFunction)datetime_utcfromtimestamp, @@ -4456,7 +4456,7 @@ static PyMethodDef datetime_methods[] = { {"utctimetuple", (PyCFunction)datetime_utctimetuple, METH_NOARGS, PyDoc_STR("Return UTC time tuple, compatible with time.localtime().")}, - {"isoformat", (PyCFunction)datetime_isoformat, METH_KEYWORDS, + {"isoformat", (PyCFunction)datetime_isoformat, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("[sep] -> string in ISO 8601 format, " "YYYY-MM-DDTHH:MM:SS[.mmmmmm][+HH:MM].\n\n" "sep is used to separate the year from the time, and " @@ -4471,10 +4471,10 @@ static PyMethodDef datetime_methods[] = { {"dst", (PyCFunction)datetime_dst, METH_NOARGS, PyDoc_STR("Return self.tzinfo.dst(self).")}, - {"replace", (PyCFunction)datetime_replace, METH_KEYWORDS, + {"replace", (PyCFunction)datetime_replace, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("Return datetime with new specified fields.")}, - {"astimezone", (PyCFunction)datetime_astimezone, METH_KEYWORDS, + {"astimezone", (PyCFunction)datetime_astimezone, METH_VARARGS | METH_KEYWORDS, PyDoc_STR("tz -> convert to local time in new timezone tz\n")}, {"__reduce__", (PyCFunction)datetime_reduce, METH_NOARGS, diff --git a/Modules/imageop.c b/Modules/imageop.c deleted file mode 100644 index 92f805a..0000000 --- a/Modules/imageop.c +++ /dev/null @@ -1,785 +0,0 @@ - -/* imageopmodule - Various operations on pictures */ - -#ifdef sun -#define signed -#endif - -#include "Python.h" - -#if SIZEOF_INT == 4 -typedef int Py_Int32; -typedef unsigned int Py_UInt32; -#else -#if SIZEOF_LONG == 4 -typedef long Py_Int32; -typedef unsigned long Py_UInt32; -#else -#error "No 4-byte integral type" -#endif -#endif - -#define CHARP(cp, xmax, x, y) ((char *)(cp+y*xmax+x)) -#define SHORTP(cp, xmax, x, y) ((short *)(cp+2*(y*xmax+x))) -#define LONGP(cp, xmax, x, y) ((Py_Int32 *)(cp+4*(y*xmax+x))) - -static PyObject *ImageopError; -static PyObject *ImageopDict; - -/* If this function returns true (the default if anything goes wrong), we're - behaving in a backward-compatible way with respect to how multi-byte pixels - are stored in the strings. The code in this module was originally written - for an SGI which is a big-endian system, and so the old code assumed that - 4-byte integers hold the R, G, and B values in a particular order. - However, on little-endian systems the order is reversed, and so not - actually compatible with what gl.lrectwrite and imgfile expect. - (gl.lrectwrite and imgfile are also SGI-specific, however, it is - conceivable that the data handled here comes from or goes to an SGI or that - it is otherwise used in the expectation that the byte order in the strings - is as specified.) - - The function returns the value of the module variable - "backward_compatible", or 1 if the variable does not exist or is not an - int. - */ - -static int -imageop_backward_compatible(void) -{ - static PyObject *bcos; - PyObject *bco; - long rc; - - if (ImageopDict == NULL) /* "cannot happen" */ - return 1; - if (bcos == NULL) { - /* cache string object for future use */ - bcos = PyString_FromString("backward_compatible"); - if (bcos == NULL) - return 1; - } - bco = PyDict_GetItem(ImageopDict, bcos); - if (bco == NULL) - return 1; - if (!PyInt_Check(bco)) - return 1; - rc = PyInt_AsLong(bco); - if (PyErr_Occurred()) { - /* not an integer, or too large, or something */ - PyErr_Clear(); - rc = 1; - } - return rc != 0; /* convert to values 0, 1 */ -} - -static PyObject * -imageop_crop(PyObject *self, PyObject *args) -{ - char *cp, *ncp; - short *nsp; - Py_Int32 *nlp; - int len, size, x, y, newx1, newx2, newy1, newy2; - int ix, iy, xstep, ystep; - PyObject *rv; - - if ( !PyArg_ParseTuple(args, "s#iiiiiii", &cp, &len, &size, &x, &y, - &newx1, &newy1, &newx2, &newy2) ) - return 0; - - if ( size != 1 && size != 2 && size != 4 ) { - PyErr_SetString(ImageopError, "Size should be 1, 2 or 4"); - return 0; - } - if ( len != size*x*y ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - xstep = (newx1 < newx2)? 1 : -1; - ystep = (newy1 < newy2)? 1 : -1; - - rv = PyString_FromStringAndSize(NULL, - (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size); - if ( rv == 0 ) - return 0; - ncp = (char *)PyString_AsString(rv); - nsp = (short *)ncp; - nlp = (Py_Int32 *)ncp; - newy2 += ystep; - newx2 += xstep; - for( iy = newy1; iy != newy2; iy+=ystep ) { - for ( ix = newx1; ix != newx2; ix+=xstep ) { - if ( iy < 0 || iy >= y || ix < 0 || ix >= x ) { - if ( size == 1 ) - *ncp++ = 0; - else - *nlp++ = 0; - } else { - if ( size == 1 ) - *ncp++ = *CHARP(cp, x, ix, iy); - else if ( size == 2 ) - *nsp++ = *SHORTP(cp, x, ix, iy); - else - *nlp++ = *LONGP(cp, x, ix, iy); - } - } - } - return rv; -} - -static PyObject * -imageop_scale(PyObject *self, PyObject *args) -{ - char *cp, *ncp; - short *nsp; - Py_Int32 *nlp; - int len, size, x, y, newx, newy; - int ix, iy; - int oix, oiy; - PyObject *rv; - - if ( !PyArg_ParseTuple(args, "s#iiiii", - &cp, &len, &size, &x, &y, &newx, &newy) ) - return 0; - - if ( size != 1 && size != 2 && size != 4 ) { - PyErr_SetString(ImageopError, "Size should be 1, 2 or 4"); - return 0; - } - if ( len != size*x*y ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, newx*newy*size); - if ( rv == 0 ) - return 0; - ncp = (char *)PyString_AsString(rv); - nsp = (short *)ncp; - nlp = (Py_Int32 *)ncp; - for( iy = 0; iy < newy; iy++ ) { - for ( ix = 0; ix < newx; ix++ ) { - oix = ix * x / newx; - oiy = iy * y / newy; - if ( size == 1 ) - *ncp++ = *CHARP(cp, x, oix, oiy); - else if ( size == 2 ) - *nsp++ = *SHORTP(cp, x, oix, oiy); - else - *nlp++ = *LONGP(cp, x, oix, oiy); - } - } - return rv; -} - -/* Note: this routine can use a bit of optimizing */ - -static PyObject * -imageop_tovideo(PyObject *self, PyObject *args) -{ - int maxx, maxy, x, y, len; - int i; - unsigned char *cp, *ncp; - int width; - PyObject *rv; - - - if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &width, &maxx, &maxy) ) - return 0; - - if ( width != 1 && width != 4 ) { - PyErr_SetString(ImageopError, "Size should be 1 or 4"); - return 0; - } - if ( maxx*maxy*width != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, len); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - if ( width == 1 ) { - memcpy(ncp, cp, maxx); /* Copy first line */ - ncp += maxx; - for (y=1; y<maxy; y++) { /* Interpolate other lines */ - for(x=0; x<maxx; x++) { - i = y*maxx + x; - *ncp++ = ((int)cp[i] + (int)cp[i-maxx]) >> 1; - } - } - } else { - memcpy(ncp, cp, maxx*4); /* Copy first line */ - ncp += maxx*4; - for (y=1; y<maxy; y++) { /* Interpolate other lines */ - for(x=0; x<maxx; x++) { - i = (y*maxx + x)*4 + 1; - *ncp++ = 0; /* Skip alfa comp */ - *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1; - i++; - *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1; - i++; - *ncp++ = ((int)cp[i] + (int)cp[i-4*maxx]) >> 1; - } - } - } - return rv; -} - -static PyObject * -imageop_grey2mono(PyObject *self, PyObject *args) -{ - int tres, x, y, len; - unsigned char *cp, *ncp; - unsigned char ovalue; - PyObject *rv; - int i, bit; - - - if ( !PyArg_ParseTuple(args, "s#iii", &cp, &len, &x, &y, &tres) ) - return 0; - - if ( x*y != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, (len+7)/8); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - bit = 0x80; - ovalue = 0; - for ( i=0; i < len; i++ ) { - if ( (int)cp[i] > tres ) - ovalue |= bit; - bit >>= 1; - if ( bit == 0 ) { - *ncp++ = ovalue; - bit = 0x80; - ovalue = 0; - } - } - if ( bit != 0x80 ) - *ncp++ = ovalue; - return rv; -} - -static PyObject * -imageop_grey2grey4(PyObject *self, PyObject *args) -{ - int x, y, len; - unsigned char *cp, *ncp; - unsigned char ovalue; - PyObject *rv; - int i; - int pos; - - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - if ( x*y != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, (len+1)/2); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - pos = 0; - ovalue = 0; - for ( i=0; i < len; i++ ) { - ovalue |= ((int)cp[i] & 0xf0) >> pos; - pos += 4; - if ( pos == 8 ) { - *ncp++ = ovalue; - ovalue = 0; - pos = 0; - } - } - if ( pos != 0 ) - *ncp++ = ovalue; - return rv; -} - -static PyObject * -imageop_grey2grey2(PyObject *self, PyObject *args) -{ - int x, y, len; - unsigned char *cp, *ncp; - unsigned char ovalue; - PyObject *rv; - int i; - int pos; - - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - if ( x*y != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, (len+3)/4); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - pos = 0; - ovalue = 0; - for ( i=0; i < len; i++ ) { - ovalue |= ((int)cp[i] & 0xc0) >> pos; - pos += 2; - if ( pos == 8 ) { - *ncp++ = ovalue; - ovalue = 0; - pos = 0; - } - } - if ( pos != 0 ) - *ncp++ = ovalue; - return rv; -} - -static PyObject * -imageop_dither2mono(PyObject *self, PyObject *args) -{ - int sum, x, y, len; - unsigned char *cp, *ncp; - unsigned char ovalue; - PyObject *rv; - int i, bit; - - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - if ( x*y != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, (len+7)/8); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - bit = 0x80; - ovalue = 0; - sum = 0; - for ( i=0; i < len; i++ ) { - sum += cp[i]; - if ( sum >= 256 ) { - sum -= 256; - ovalue |= bit; - } - bit >>= 1; - if ( bit == 0 ) { - *ncp++ = ovalue; - bit = 0x80; - ovalue = 0; - } - } - if ( bit != 0x80 ) - *ncp++ = ovalue; - return rv; -} - -static PyObject * -imageop_dither2grey2(PyObject *self, PyObject *args) -{ - int x, y, len; - unsigned char *cp, *ncp; - unsigned char ovalue; - PyObject *rv; - int i; - int pos; - int sum = 0, nvalue; - - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - if ( x*y != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, (len+3)/4); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - pos = 1; - ovalue = 0; - for ( i=0; i < len; i++ ) { - sum += cp[i]; - nvalue = sum & 0x180; - sum -= nvalue; - ovalue |= nvalue >> pos; - pos += 2; - if ( pos == 9 ) { - *ncp++ = ovalue; - ovalue = 0; - pos = 1; - } - } - if ( pos != 0 ) - *ncp++ = ovalue; - return rv; -} - -static PyObject * -imageop_mono2grey(PyObject *self, PyObject *args) -{ - int v0, v1, x, y, len, nlen; - unsigned char *cp, *ncp; - PyObject *rv; - int i, bit; - - if ( !PyArg_ParseTuple(args, "s#iiii", &cp, &len, &x, &y, &v0, &v1) ) - return 0; - - nlen = x*y; - if ( (nlen+7)/8 != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - bit = 0x80; - for ( i=0; i < nlen; i++ ) { - if ( *cp & bit ) - *ncp++ = v1; - else - *ncp++ = v0; - bit >>= 1; - if ( bit == 0 ) { - bit = 0x80; - cp++; - } - } - return rv; -} - -static PyObject * -imageop_grey22grey(PyObject *self, PyObject *args) -{ - int x, y, len, nlen; - unsigned char *cp, *ncp; - PyObject *rv; - int i, pos, value = 0, nvalue; - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - nlen = x*y; - if ( (nlen+3)/4 != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - pos = 0; - for ( i=0; i < nlen; i++ ) { - if ( pos == 0 ) { - value = *cp++; - pos = 8; - } - pos -= 2; - nvalue = (value >> pos) & 0x03; - *ncp++ = nvalue | (nvalue << 2) | - (nvalue << 4) | (nvalue << 6); - } - return rv; -} - -static PyObject * -imageop_grey42grey(PyObject *self, PyObject *args) -{ - int x, y, len, nlen; - unsigned char *cp, *ncp; - PyObject *rv; - int i, pos, value = 0, nvalue; - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - nlen = x*y; - if ( (nlen+1)/2 != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - pos = 0; - for ( i=0; i < nlen; i++ ) { - if ( pos == 0 ) { - value = *cp++; - pos = 8; - } - pos -= 4; - nvalue = (value >> pos) & 0x0f; - *ncp++ = nvalue | (nvalue << 4); - } - return rv; -} - -static PyObject * -imageop_rgb2rgb8(PyObject *self, PyObject *args) -{ - int x, y, len, nlen; - unsigned char *cp; - unsigned char *ncp; - PyObject *rv; - int i, r, g, b; - int backward_compatible = imageop_backward_compatible(); - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - nlen = x*y; - if ( nlen*4 != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - for ( i=0; i < nlen; i++ ) { - /* Bits in source: aaaaaaaa BBbbbbbb GGGggggg RRRrrrrr */ - if (backward_compatible) { - Py_UInt32 value = * (Py_UInt32 *) cp; - cp += 4; - r = (int) ((value & 0xff) / 255. * 7. + .5); - g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5); - b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5); - } else { - cp++; /* skip alpha channel */ - b = (int) (*cp++ / 255. * 3. + .5); - g = (int) (*cp++ / 255. * 7. + .5); - r = (int) (*cp++ / 255. * 7. + .5); - } - *ncp++ = (unsigned char)((r<<5) | (b<<3) | g); - } - return rv; -} - -static PyObject * -imageop_rgb82rgb(PyObject *self, PyObject *args) -{ - int x, y, len, nlen; - unsigned char *cp; - unsigned char *ncp; - PyObject *rv; - int i, r, g, b; - unsigned char value; - int backward_compatible = imageop_backward_compatible(); - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - nlen = x*y; - if ( nlen != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen*4); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - for ( i=0; i < nlen; i++ ) { - /* Bits in source: RRRBBGGG - ** Red and Green are multiplied by 36.5, Blue by 85 - */ - value = *cp++; - r = (value >> 5) & 7; - g = (value ) & 7; - b = (value >> 3) & 3; - r = (r<<5) | (r<<3) | (r>>1); - g = (g<<5) | (g<<3) | (g>>1); - b = (b<<6) | (b<<4) | (b<<2) | b; - if (backward_compatible) { - Py_UInt32 nvalue = r | (g<<8) | (b<<16); - * (Py_UInt32 *) ncp = nvalue; - ncp += 4; - } else { - *ncp++ = 0; - *ncp++ = b; - *ncp++ = g; - *ncp++ = r; - } - } - return rv; -} - -static PyObject * -imageop_rgb2grey(PyObject *self, PyObject *args) -{ - int x, y, len, nlen; - unsigned char *cp; - unsigned char *ncp; - PyObject *rv; - int i, r, g, b; - int nvalue; - int backward_compatible = imageop_backward_compatible(); - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - nlen = x*y; - if ( nlen*4 != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - for ( i=0; i < nlen; i++ ) { - if (backward_compatible) { - Py_UInt32 value = * (Py_UInt32 *) cp; - cp += 4; - r = (int) ((value & 0xff) / 255. * 7. + .5); - g = (int) (((value >> 8) & 0xff) / 255. * 7. + .5); - b = (int) (((value >> 16) & 0xff) / 255. * 3. + .5); - } else { - cp++; /* skip alpha channel */ - b = *cp++; - g = *cp++; - r = *cp++; - } - nvalue = (int)(0.30*r + 0.59*g + 0.11*b); - if ( nvalue > 255 ) nvalue = 255; - *ncp++ = (unsigned char)nvalue; - } - return rv; -} - -static PyObject * -imageop_grey2rgb(PyObject *self, PyObject *args) -{ - int x, y, len, nlen; - unsigned char *cp; - unsigned char *ncp; - PyObject *rv; - int i; - unsigned char value; - int backward_compatible = imageop_backward_compatible(); - - if ( !PyArg_ParseTuple(args, "s#ii", &cp, &len, &x, &y) ) - return 0; - - nlen = x*y; - if ( nlen != len ) { - PyErr_SetString(ImageopError, "String has incorrect length"); - return 0; - } - - rv = PyString_FromStringAndSize(NULL, nlen*4); - if ( rv == 0 ) - return 0; - ncp = (unsigned char *)PyString_AsString(rv); - - for ( i=0; i < nlen; i++ ) { - value = *cp++; - if (backward_compatible) { - * (Py_UInt32 *) ncp = (Py_UInt32) value | ((Py_UInt32) value << 8 ) | ((Py_UInt32) value << 16); - ncp += 4; - } else { - *ncp++ = 0; - *ncp++ = value; - *ncp++ = value; - *ncp++ = value; - } - } - return rv; -} - -/* -static object * -imageop_mul(object *self, object *args) -{ - char *cp, *ncp; - int len, size, x, y; - object *rv; - int i; - - if ( !getargs(args, "(s#iii)", &cp, &len, &size, &x, &y) ) - return 0; - - if ( size != 1 && size != 4 ) { - err_setstr(ImageopError, "Size should be 1 or 4"); - return 0; - } - if ( len != size*x*y ) { - err_setstr(ImageopError, "String has incorrect length"); - return 0; - } - - rv = newsizedstringobject(NULL, XXXX); - if ( rv == 0 ) - return 0; - ncp = (char *)getstringvalue(rv); - - - for ( i=0; i < len; i += size ) { - } - return rv; -} -*/ - -static PyMethodDef imageop_methods[] = { - { "crop", imageop_crop, METH_VARARGS }, - { "scale", imageop_scale, METH_VARARGS }, - { "grey2mono", imageop_grey2mono, METH_VARARGS }, - { "grey2grey2", imageop_grey2grey2, METH_VARARGS }, - { "grey2grey4", imageop_grey2grey4, METH_VARARGS }, - { "dither2mono", imageop_dither2mono, METH_VARARGS }, - { "dither2grey2", imageop_dither2grey2, METH_VARARGS }, - { "mono2grey", imageop_mono2grey, METH_VARARGS }, - { "grey22grey", imageop_grey22grey, METH_VARARGS }, - { "grey42grey", imageop_grey42grey, METH_VARARGS }, - { "tovideo", imageop_tovideo, METH_VARARGS }, - { "rgb2rgb8", imageop_rgb2rgb8, METH_VARARGS }, - { "rgb82rgb", imageop_rgb82rgb, METH_VARARGS }, - { "rgb2grey", imageop_rgb2grey, METH_VARARGS }, - { "grey2rgb", imageop_grey2rgb, METH_VARARGS }, - { 0, 0 } -}; - - -PyMODINIT_FUNC -initimageop(void) -{ - PyObject *m; - m = Py_InitModule("imageop", imageop_methods); - if (m == NULL) - return; - ImageopDict = PyModule_GetDict(m); - ImageopError = PyErr_NewException("imageop.error", NULL, NULL); - if (ImageopError != NULL) - PyDict_SetItemString(ImageopDict, "error", ImageopError); -} diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c index f795088..8289173 100644 --- a/Modules/parsermodule.c +++ b/Modules/parsermodule.c @@ -1403,7 +1403,7 @@ validate_small_stmt(node *tree) /* compound_stmt: - * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef + * if_stmt | while_stmt | for_stmt | try_stmt | funcdef | classdef | decorated */ static int validate_compound_stmt(node *tree) @@ -1422,7 +1422,8 @@ validate_compound_stmt(node *tree) || (ntype == for_stmt) || (ntype == try_stmt) || (ntype == funcdef) - || (ntype == classdef)) + || (ntype == classdef) + || (ntype == decorated)) res = validate_node(tree); else { res = 0; @@ -1432,7 +1433,6 @@ validate_compound_stmt(node *tree) return (res); } - static int validate_yield_or_testlist(node *tree) { @@ -2366,28 +2366,40 @@ validate_decorators(node *tree) /* funcdef: * - * -6 -5 -4 -3 -2 -1 - * [decorators] 'def' NAME parameters ':' suite + * -5 -4 -3 -2 -1 + * 'def' NAME parameters ':' suite */ static int validate_funcdef(node *tree) { int nch = NCH(tree); int ok = (validate_ntype(tree, funcdef) - && ((nch == 5) || (nch == 6)) + && (nch == 5) && validate_name(RCHILD(tree, -5), "def") && validate_ntype(RCHILD(tree, -4), NAME) && validate_colon(RCHILD(tree, -2)) && validate_parameters(RCHILD(tree, -3)) && validate_suite(RCHILD(tree, -1))); - - if (ok && (nch == 6)) - ok = validate_decorators(CHILD(tree, 0)); - return ok; } +/* decorated + * decorators (classdef | funcdef) + */ +static int +validate_decorated(node *tree) +{ + int nch = NCH(tree); + int ok = (validate_ntype(tree, decorated) + && (nch == 2) + && validate_decorators(RCHILD(tree, -2)) + && (validate_funcdef(RCHILD(tree, -1)) + || validate_class(RCHILD(tree, -1))) + ); + return ok; +} + static int validate_lambdef(node *tree) { @@ -2731,6 +2743,9 @@ validate_node(node *tree) case classdef: res = validate_class(tree); break; + case decorated: + res = validate_decorated(tree); + break; /* * "Trivial" parse tree nodes. * (Why did I call these trivial?) diff --git a/Modules/rgbimgmodule.c b/Modules/rgbimgmodule.c deleted file mode 100644 index 0f9ee71..0000000 --- a/Modules/rgbimgmodule.c +++ /dev/null @@ -1,780 +0,0 @@ -/* - * fastimg - - * Faster reading and writing of image files. - * - * This code should work on machines with any byte order. - * - * Could someone make this run real fast using multiple processors - * or how about using memory mapped files to speed it up? - * - * Paul Haeberli - 1991 - * - * Changed to return sizes. - * Sjoerd Mullender - 1993 - * Changed to incorporate into Python. - * Sjoerd Mullender - 1993 - */ -#include "Python.h" - -#if SIZEOF_INT == 4 -typedef int Py_Int32; -typedef unsigned int Py_UInt32; -#else -#if SIZEOF_LONG == 4 -typedef long Py_Int32; -typedef unsigned long Py_UInt32; -#else -#error "No 4-byte integral type" -#endif -#endif - -#include <string.h> - -/* - * from image.h - * - */ -typedef struct { - unsigned short imagic; /* stuff saved on disk . . */ - unsigned short type; - unsigned short dim; - unsigned short xsize; - unsigned short ysize; - unsigned short zsize; - Py_UInt32 min; - Py_UInt32 max; - Py_UInt32 wastebytes; - char name[80]; - Py_UInt32 colormap; - - Py_Int32 file; /* stuff used in core only */ - unsigned short flags; - short dorev; - short x; - short y; - short z; - short cnt; - unsigned short *ptr; - unsigned short *base; - unsigned short *tmpbuf; - Py_UInt32 offset; - Py_UInt32 rleend; /* for rle images */ - Py_UInt32 *rowstart; /* for rle images */ - Py_Int32 *rowsize; /* for rle images */ -} IMAGE; - -#define IMAGIC 0732 - -#define TYPEMASK 0xff00 -#define BPPMASK 0x00ff -#define ITYPE_VERBATIM 0x0000 -#define ITYPE_RLE 0x0100 -#define ISRLE(type) (((type) & 0xff00) == ITYPE_RLE) -#define ISVERBATIM(type) (((type) & 0xff00) == ITYPE_VERBATIM) -#define BPP(type) ((type) & BPPMASK) -#define RLE(bpp) (ITYPE_RLE | (bpp)) -#define VERBATIM(bpp) (ITYPE_VERBATIM | (bpp)) -/* - * end of image.h stuff - * - */ - -#define RINTLUM (79) -#define GINTLUM (156) -#define BINTLUM (21) - -#define ILUM(r,g,b) ((int)(RINTLUM*(r)+GINTLUM*(g)+BINTLUM*(b))>>8) - -#define OFFSET_R 3 /* this is byte order dependent */ -#define OFFSET_G 2 -#define OFFSET_B 1 -#define OFFSET_A 0 - -#define CHANOFFSET(z) (3-(z)) /* this is byte order dependent */ - -static void expandrow(unsigned char *, unsigned char *, int); -static void setalpha(unsigned char *, int); -static void copybw(Py_Int32 *, int); -static void interleaverow(unsigned char*, unsigned char*, int, int); -static int compressrow(unsigned char *, unsigned char *, int, int); -static void lumrow(unsigned char *, unsigned char *, int); - -#ifdef ADD_TAGS -#define TAGLEN (5) -#else -#define TAGLEN (0) -#endif - -static PyObject *ImgfileError; - -static int reverse_order; - -#ifdef ADD_TAGS -/* - * addlongimgtag - - * this is used to extract image data from core dumps. - * - */ -static void -addlongimgtag(Py_UInt32 *dptr, int xsize, int ysize) -{ - dptr = dptr + (xsize * ysize); - dptr[0] = 0x12345678; - dptr[1] = 0x59493333; - dptr[2] = 0x69434222; - dptr[3] = xsize; - dptr[4] = ysize; -} -#endif - -/* - * byte order independent read/write of shorts and longs. - * - */ -static unsigned short -getshort(FILE *inf) -{ - unsigned char buf[2]; - - fread(buf, 2, 1, inf); - return (buf[0] << 8) + (buf[1] << 0); -} - -static Py_UInt32 -getlong(FILE *inf) -{ - unsigned char buf[4]; - - fread(buf, 4, 1, inf); - return (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + (buf[3] << 0); -} - -static void -putshort(FILE *outf, unsigned short val) -{ - unsigned char buf[2]; - - buf[0] = (val >> 8); - buf[1] = (val >> 0); - fwrite(buf, 2, 1, outf); -} - -static int -putlong(FILE *outf, Py_UInt32 val) -{ - unsigned char buf[4]; - - buf[0] = (unsigned char) (val >> 24); - buf[1] = (unsigned char) (val >> 16); - buf[2] = (unsigned char) (val >> 8); - buf[3] = (unsigned char) (val >> 0); - return (int)fwrite(buf, 4, 1, outf); -} - -static void -readheader(FILE *inf, IMAGE *image) -{ - memset(image ,0, sizeof(IMAGE)); - image->imagic = getshort(inf); - image->type = getshort(inf); - image->dim = getshort(inf); - image->xsize = getshort(inf); - image->ysize = getshort(inf); - image->zsize = getshort(inf); -} - -static int -writeheader(FILE *outf, IMAGE *image) -{ - IMAGE t; - - memset(&t, 0, sizeof(IMAGE)); - fwrite(&t, sizeof(IMAGE), 1, outf); - fseek(outf, 0, SEEK_SET); - putshort(outf, image->imagic); - putshort(outf, image->type); - putshort(outf, image->dim); - putshort(outf, image->xsize); - putshort(outf, image->ysize); - putshort(outf, image->zsize); - putlong(outf, image->min); - putlong(outf, image->max); - putlong(outf, 0); - return (int)fwrite("no name", 8, 1, outf); -} - -static int -writetab(FILE *outf, /*unsigned*/ Py_Int32 *tab, int len) -{ - int r = 0; - - while(len) { - r = putlong(outf, *tab++); - len--; - } - return r; -} - -static void -readtab(FILE *inf, /*unsigned*/ Py_Int32 *tab, int len) -{ - while(len) { - *tab++ = getlong(inf); - len--; - } -} - -/* - * sizeofimage - - * return the xsize and ysize of an iris image file. - * - */ -static PyObject * -sizeofimage(PyObject *self, PyObject *args) -{ - char *name; - IMAGE image; - FILE *inf; - - if (!PyArg_ParseTuple(args, "s:sizeofimage", &name)) - return NULL; - - inf = fopen(name, "rb"); - if (!inf) { - PyErr_SetString(ImgfileError, "can't open image file"); - return NULL; - } - readheader(inf, &image); - fclose(inf); - if (image.imagic != IMAGIC) { - PyErr_SetString(ImgfileError, - "bad magic number in image file"); - return NULL; - } - return Py_BuildValue("(ii)", image.xsize, image.ysize); -} - -/* - * longimagedata - - * read in a B/W RGB or RGBA iris image file and return a - * pointer to an array of longs. - * - */ -static PyObject * -longimagedata(PyObject *self, PyObject *args) -{ - char *name; - unsigned char *base, *lptr; - unsigned char *rledat = NULL, *verdat = NULL; - Py_Int32 *starttab = NULL, *lengthtab = NULL; - FILE *inf = NULL; - IMAGE image; - int y, z, tablen; - int xsize, ysize, zsize; - int bpp, rle, cur, badorder; - int rlebuflen; - PyObject *rv = NULL; - - if (!PyArg_ParseTuple(args, "s:longimagedata", &name)) - return NULL; - - inf = fopen(name,"rb"); - if (!inf) { - PyErr_SetString(ImgfileError, "can't open image file"); - return NULL; - } - readheader(inf,&image); - if (image.imagic != IMAGIC) { - PyErr_SetString(ImgfileError, - "bad magic number in image file"); - goto finally; - } - rle = ISRLE(image.type); - bpp = BPP(image.type); - if (bpp != 1) { - PyErr_SetString(ImgfileError, - "image must have 1 byte per pix chan"); - goto finally; - } - xsize = image.xsize; - ysize = image.ysize; - zsize = image.zsize; - if (rle) { - tablen = ysize * zsize * sizeof(Py_Int32); - starttab = (Py_Int32 *)malloc(tablen); - lengthtab = (Py_Int32 *)malloc(tablen); - rlebuflen = (int) (1.05 * xsize +10); - rledat = (unsigned char *)malloc(rlebuflen); - if (!starttab || !lengthtab || !rledat) { - PyErr_NoMemory(); - goto finally; - } - - fseek(inf, 512, SEEK_SET); - readtab(inf, starttab, ysize*zsize); - readtab(inf, lengthtab, ysize*zsize); - - /* check data order */ - cur = 0; - badorder = 0; - for(y = 0; y < ysize; y++) { - for(z = 0; z < zsize; z++) { - if (starttab[y + z * ysize] < cur) { - badorder = 1; - break; - } - cur = starttab[y +z * ysize]; - } - if (badorder) - break; - } - - fseek(inf, 512 + 2 * tablen, SEEK_SET); - cur = 512 + 2 * tablen; - rv = PyString_FromStringAndSize((char *)NULL, - (xsize * ysize + TAGLEN) * sizeof(Py_Int32)); - if (rv == NULL) - goto finally; - - base = (unsigned char *) PyString_AsString(rv); -#ifdef ADD_TAGS - addlongimgtag(base,xsize,ysize); -#endif - if (badorder) { - for (z = 0; z < zsize; z++) { - lptr = base; - if (reverse_order) - lptr += (ysize - 1) * xsize - * sizeof(Py_UInt32); - for (y = 0; y < ysize; y++) { - int idx = y + z * ysize; - if (cur != starttab[idx]) { - fseek(inf,starttab[idx], - SEEK_SET); - cur = starttab[idx]; - } - if (lengthtab[idx] > rlebuflen) { - PyErr_SetString(ImgfileError, - "rlebuf is too small"); - Py_DECREF(rv); - rv = NULL; - goto finally; - } - fread(rledat, lengthtab[idx], 1, inf); - cur += lengthtab[idx]; - expandrow(lptr, rledat, 3-z); - if (reverse_order) - lptr -= xsize - * sizeof(Py_UInt32); - else - lptr += xsize - * sizeof(Py_UInt32); - } - } - } else { - lptr = base; - if (reverse_order) - lptr += (ysize - 1) * xsize - * sizeof(Py_UInt32); - for (y = 0; y < ysize; y++) { - for(z = 0; z < zsize; z++) { - int idx = y + z * ysize; - if (cur != starttab[idx]) { - fseek(inf, starttab[idx], - SEEK_SET); - cur = starttab[idx]; - } - fread(rledat, lengthtab[idx], 1, inf); - cur += lengthtab[idx]; - expandrow(lptr, rledat, 3-z); - } - if (reverse_order) - lptr -= xsize * sizeof(Py_UInt32); - else - lptr += xsize * sizeof(Py_UInt32); - } - } - if (zsize == 3) - setalpha(base, xsize * ysize); - else if (zsize < 3) - copybw((Py_Int32 *) base, xsize * ysize); - } - else { - rv = PyString_FromStringAndSize((char *) 0, - (xsize*ysize+TAGLEN)*sizeof(Py_Int32)); - if (rv == NULL) - goto finally; - - base = (unsigned char *) PyString_AsString(rv); -#ifdef ADD_TAGS - addlongimgtag(base, xsize, ysize); -#endif - verdat = (unsigned char *)malloc(xsize); - if (!verdat) { - Py_CLEAR(rv); - goto finally; - } - - fseek(inf, 512, SEEK_SET); - for (z = 0; z < zsize; z++) { - lptr = base; - if (reverse_order) - lptr += (ysize - 1) * xsize - * sizeof(Py_UInt32); - for (y = 0; y < ysize; y++) { - fread(verdat, xsize, 1, inf); - interleaverow(lptr, verdat, 3-z, xsize); - if (reverse_order) - lptr -= xsize * sizeof(Py_UInt32); - else - lptr += xsize * sizeof(Py_UInt32); - } - } - if (zsize == 3) - setalpha(base, xsize * ysize); - else if (zsize < 3) - copybw((Py_Int32 *) base, xsize * ysize); - } - finally: - if (starttab) - free(starttab); - if (lengthtab) - free(lengthtab); - if (rledat) - free(rledat); - if (verdat) - free(verdat); - fclose(inf); - return rv; -} - -/* static utility functions for longimagedata */ - -static void -interleaverow(unsigned char *lptr, unsigned char *cptr, int z, int n) -{ - lptr += z; - while (n--) { - *lptr = *cptr++; - lptr += 4; - } -} - -static void -copybw(Py_Int32 *lptr, int n) -{ - while (n >= 8) { - lptr[0] = 0xff000000 + (0x010101 * (lptr[0] & 0xff)); - lptr[1] = 0xff000000 + (0x010101 * (lptr[1] & 0xff)); - lptr[2] = 0xff000000 + (0x010101 * (lptr[2] & 0xff)); - lptr[3] = 0xff000000 + (0x010101 * (lptr[3] & 0xff)); - lptr[4] = 0xff000000 + (0x010101 * (lptr[4] & 0xff)); - lptr[5] = 0xff000000 + (0x010101 * (lptr[5] & 0xff)); - lptr[6] = 0xff000000 + (0x010101 * (lptr[6] & 0xff)); - lptr[7] = 0xff000000 + (0x010101 * (lptr[7] & 0xff)); - lptr += 8; - n -= 8; - } - while (n--) { - *lptr = 0xff000000 + (0x010101 * (*lptr&0xff)); - lptr++; - } -} - -static void -setalpha(unsigned char *lptr, int n) -{ - while (n >= 8) { - lptr[0 * 4] = 0xff; - lptr[1 * 4] = 0xff; - lptr[2 * 4] = 0xff; - lptr[3 * 4] = 0xff; - lptr[4 * 4] = 0xff; - lptr[5 * 4] = 0xff; - lptr[6 * 4] = 0xff; - lptr[7 * 4] = 0xff; - lptr += 4 * 8; - n -= 8; - } - while (n--) { - *lptr = 0xff; - lptr += 4; - } -} - -static void -expandrow(unsigned char *optr, unsigned char *iptr, int z) -{ - unsigned char pixel, count; - - optr += z; - while (1) { - pixel = *iptr++; - if (!(count = (pixel & 0x7f))) - return; - if (pixel & 0x80) { - while (count >= 8) { - optr[0 * 4] = iptr[0]; - optr[1 * 4] = iptr[1]; - optr[2 * 4] = iptr[2]; - optr[3 * 4] = iptr[3]; - optr[4 * 4] = iptr[4]; - optr[5 * 4] = iptr[5]; - optr[6 * 4] = iptr[6]; - optr[7 * 4] = iptr[7]; - optr += 8 * 4; - iptr += 8; - count -= 8; - } - while (count--) { - *optr = *iptr++; - optr += 4; - } - } - else { - pixel = *iptr++; - while (count >= 8) { - optr[0 * 4] = pixel; - optr[1 * 4] = pixel; - optr[2 * 4] = pixel; - optr[3 * 4] = pixel; - optr[4 * 4] = pixel; - optr[5 * 4] = pixel; - optr[6 * 4] = pixel; - optr[7 * 4] = pixel; - optr += 8 * 4; - count -= 8; - } - while (count--) { - *optr = pixel; - optr += 4; - } - } - } -} - -/* - * longstoimage - - * copy an array of longs to an iris image file. Each long - * represents one pixel. xsize and ysize specify the dimensions of - * the pixel array. zsize specifies what kind of image file to - * write out. if zsize is 1, the luminance of the pixels are - * calculated, and a single channel black and white image is saved. - * If zsize is 3, an RGB image file is saved. If zsize is 4, an - * RGBA image file is saved. - * - */ -static PyObject * -longstoimage(PyObject *self, PyObject *args) -{ - unsigned char *lptr; - char *name; - int xsize, ysize, zsize; - FILE *outf = NULL; - IMAGE image; - int tablen, y, z, pos, len; - Py_Int32 *starttab = NULL, *lengthtab = NULL; - unsigned char *rlebuf = NULL; - unsigned char *lumbuf = NULL; - int rlebuflen; - Py_ssize_t goodwrite; - PyObject *retval = NULL; - - if (!PyArg_ParseTuple(args, "s#iiis:longstoimage", &lptr, &len, - &xsize, &ysize, &zsize, &name)) - return NULL; - - goodwrite = 1; - outf = fopen(name, "wb"); - if (!outf) { - PyErr_SetString(ImgfileError, "can't open output file"); - return NULL; - } - tablen = ysize * zsize * sizeof(Py_Int32); - - starttab = (Py_Int32 *)malloc(tablen); - lengthtab = (Py_Int32 *)malloc(tablen); - rlebuflen = (int) (1.05 * xsize + 10); - rlebuf = (unsigned char *)malloc(rlebuflen); - lumbuf = (unsigned char *)malloc(xsize * sizeof(Py_Int32)); - if (!starttab || !lengthtab || !rlebuf || !lumbuf) { - PyErr_NoMemory(); - goto finally; - } - - memset(&image, 0, sizeof(IMAGE)); - image.imagic = IMAGIC; - image.type = RLE(1); - if (zsize>1) - image.dim = 3; - else - image.dim = 2; - image.xsize = xsize; - image.ysize = ysize; - image.zsize = zsize; - image.min = 0; - image.max = 255; - goodwrite *= writeheader(outf, &image); - pos = 512 + 2 * tablen; - fseek(outf, pos, SEEK_SET); - if (reverse_order) - lptr += (ysize - 1) * xsize * sizeof(Py_UInt32); - for (y = 0; y < ysize; y++) { - for (z = 0; z < zsize; z++) { - if (zsize == 1) { - lumrow(lptr, lumbuf, xsize); - len = compressrow(lumbuf, rlebuf, - CHANOFFSET(z), xsize); - } else { - len = compressrow(lptr, rlebuf, - CHANOFFSET(z), xsize); - } - if(len > rlebuflen) { - PyErr_SetString(ImgfileError, - "rlebuf is too small"); - goto finally; - } - goodwrite *= fwrite(rlebuf, len, 1, outf); - starttab[y + z * ysize] = pos; - lengthtab[y + z * ysize] = len; - pos += len; - } - if (reverse_order) - lptr -= xsize * sizeof(Py_UInt32); - else - lptr += xsize * sizeof(Py_UInt32); - } - - fseek(outf, 512, SEEK_SET); - goodwrite *= writetab(outf, starttab, ysize*zsize); - goodwrite *= writetab(outf, lengthtab, ysize*zsize); - if (goodwrite) { - Py_INCREF(Py_None); - retval = Py_None; - } else - PyErr_SetString(ImgfileError, "not enough space for image"); - - finally: - fclose(outf); - free(starttab); - free(lengthtab); - free(rlebuf); - free(lumbuf); - return retval; -} - -/* static utility functions for longstoimage */ - -static void -lumrow(unsigned char *rgbptr, unsigned char *lumptr, int n) -{ - lumptr += CHANOFFSET(0); - while (n--) { - *lumptr = ILUM(rgbptr[OFFSET_R], - rgbptr[OFFSET_G], - rgbptr[OFFSET_B]); - lumptr += 4; - rgbptr += 4; - } -} - -static int -compressrow(unsigned char *lbuf, unsigned char *rlebuf, int z, int cnt) -{ - unsigned char *iptr, *ibufend, *sptr, *optr; - short todo, cc; - Py_Int32 count; - - lbuf += z; - iptr = lbuf; - ibufend = iptr + cnt * 4; - optr = rlebuf; - - while(iptr < ibufend) { - sptr = iptr; - iptr += 8; - while ((iptr<ibufend) && - ((iptr[-8]!=iptr[-4]) ||(iptr[-4]!=iptr[0]))) - { - iptr += 4; - } - iptr -= 8; - count = (iptr - sptr) / 4; - while (count) { - todo = count > 126 ? 126 : (short)count; - count -= todo; - *optr++ = 0x80 | todo; - while (todo > 8) { - optr[0] = sptr[0 * 4]; - optr[1] = sptr[1 * 4]; - optr[2] = sptr[2 * 4]; - optr[3] = sptr[3 * 4]; - optr[4] = sptr[4 * 4]; - optr[5] = sptr[5 * 4]; - optr[6] = sptr[6 * 4]; - optr[7] = sptr[7 * 4]; - optr += 8; - sptr += 8 * 4; - todo -= 8; - } - while (todo--) { - *optr++ = *sptr; - sptr += 4; - } - } - sptr = iptr; - cc = *iptr; - iptr += 4; - while ((iptr < ibufend) && (*iptr == cc)) - iptr += 4; - count = (iptr - sptr) / 4; - while (count) { - todo = count > 126 ? 126 : (short)count; - count -= todo; - *optr++ = (unsigned char) todo; - *optr++ = (unsigned char) cc; - } - } - *optr++ = 0; - return optr - (unsigned char *)rlebuf; -} - -static PyObject * -ttob(PyObject *self, PyObject *args) -{ - int order, oldorder; - - if (!PyArg_ParseTuple(args, "i:ttob", &order)) - return NULL; - oldorder = reverse_order; - reverse_order = order; - return PyInt_FromLong(oldorder); -} - -static PyMethodDef -rgbimg_methods[] = { - {"sizeofimage", sizeofimage, METH_VARARGS}, - {"longimagedata", longimagedata, METH_VARARGS}, - {"longstoimage", longstoimage, METH_VARARGS}, - {"ttob", ttob, METH_VARARGS}, - {NULL, NULL} /* sentinel */ -}; - - -PyMODINIT_FUNC -initrgbimg(void) -{ - PyObject *m, *d; - m = Py_InitModule("rgbimg", rgbimg_methods); - if (m == NULL) - return; - - if (PyErr_Warn(PyExc_DeprecationWarning, - "the rgbimg module is deprecated")) - return; - - d = PyModule_GetDict(m); - ImgfileError = PyErr_NewException("rgbimg.error", NULL, NULL); - if (ImgfileError != NULL) - PyDict_SetItemString(d, "error", ImgfileError); -} |