* 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().
This commit is contained in:
Guido van Rossum 1992-12-14 16:59:51 +00:00
parent 8de83e041c
commit 5f59d6018e
6 changed files with 2176 additions and 4 deletions

View file

@ -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 }
};