diff options
author | Guido van Rossum <guido@python.org> | 1992-12-14 16:59:51 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1992-12-14 16:59:51 (GMT) |
commit | 5f59d6018e1b49c3bc581a0f69aa2bb1c88ae52b (patch) | |
tree | 08018b39686e5a4d0d045916b1677b254cde9451 /Modules/imageop.c | |
parent | 8de83e041cff919ebf7150c5c9a5e0e00e7d9dbe (diff) | |
download | cpython-5f59d6018e1b49c3bc581a0f69aa2bb1c88ae52b.zip cpython-5f59d6018e1b49c3bc581a0f69aa2bb1c88ae52b.tar.gz cpython-5f59d6018e1b49c3bc581a0f69aa2bb1c88ae52b.tar.bz2 |
* mymalloc.h: always allocate one extra byte, since some malloc's
return NULL for malloc(0) or realloc(p, 0). (This should be done
differently than wasting one byte, but alas...)
* Moved "add'l libraries" option in Makefile to an earlier place.
* Remove argument compatibility hacks (b) and (c).
* Add grey2mono, dither2mono and mono2grey to imageop.
* Dup the fd in socket.fromfd().
* Added new modules mpz, md5 (by JH, requiring GNU MP 1.2). Affects
Makefile and config.c.
* socketmodule.c: added socket.fromfd(fd, family, type, [proto]),
converted socket() to use of getargs().
Diffstat (limited to 'Modules/imageop.c')
-rw-r--r-- | Modules/imageop.c | 134 |
1 files changed, 133 insertions, 1 deletions
diff --git a/Modules/imageop.c b/Modules/imageop.c index 8cd0128..c575c25 100644 --- a/Modules/imageop.c +++ b/Modules/imageop.c @@ -62,7 +62,8 @@ imageop_crop(self, args) xstep = (newx1 < newx2)? 1 : -1; ystep = (newy1 < newy2)? 1 : -1; - rv = newsizedstringobject(NULL, (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size); + rv = newsizedstringobject(NULL, + (abs(newx2-newx1)+1)*(abs(newy2-newy1)+1)*size); if ( rv == 0 ) return 0; ncp = (char *)getstringvalue(rv); @@ -123,6 +124,134 @@ imageop_scale(self, args) return rv; } +static object * +imageop_grey2mono(self, args) + object *self; + object *args; +{ + int tres, x, y, len; + unsigned char *cp, *ncp; + unsigned char ovalue; + object *rv; + int i, bit; + + + if ( !getargs(args, "(s#iii)", &cp, &len, &x, &y, &tres) ) + return 0; + + if ( x*y != len ) { + err_setstr(ImageopError, "String has incorrect length"); + return 0; + } + + rv = newsizedstringobject(NULL, (len+7)/8); + if ( rv == 0 ) + return 0; + ncp = (unsigned char *)getstringvalue(rv); + + bit = 0x80; + ovalue = 0; + for ( i=0; i < len; i++ ) { + if ( cp[i] > tres ) + ovalue |= bit; + bit >>= 1; + if ( bit == 0 ) { + *ncp++ = ovalue; + bit = 0x80; + ovalue = 0; + } + } + if ( bit != 0x80 ) + *ncp++ = ovalue; + return rv; +} + +static object * +imageop_dither2mono(self, args) + object *self; + object *args; +{ + int sum, x, y, len; + unsigned char *cp, *ncp; + unsigned char ovalue; + object *rv; + int i, bit; + + + if ( !getargs(args, "(s#ii)", &cp, &len, &x, &y) ) + return 0; + + if ( x*y != len ) { + err_setstr(ImageopError, "String has incorrect length"); + return 0; + } + + rv = newsizedstringobject(NULL, (len+7)/8); + if ( rv == 0 ) + return 0; + ncp = (unsigned char *)getstringvalue(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 object * +imageop_mono2grey(self, args) + object *self; + object *args; +{ + int v0, v1, x, y, len, nlen; + unsigned char *cp, *ncp; + unsigned char ovalue; + object *rv; + int i, bit, value; + + if ( !getargs(args, "(s#iiii)", &cp, &len, &x, &y, &v0, &v1) ) + return 0; + + nlen = x*y; + if ( (nlen+7)/8 != len ) { + err_setstr(ImageopError, "String has incorrect length"); + return 0; + } + + rv = newsizedstringobject(NULL, nlen); + if ( rv == 0 ) + return 0; + ncp = (unsigned char *)getstringvalue(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 object * imageop_mul(self, args) @@ -161,6 +290,9 @@ imageop_mul(self, args) static struct methodlist imageop_methods[] = { { "crop", imageop_crop }, { "scale", imageop_scale }, + { "grey2mono", imageop_grey2mono }, + { "dither2mono", imageop_dither2mono }, + { "mono2grey", imageop_mono2grey }, { 0, 0 } }; |