/*
	bits.l
	95/04/17
*/


/*
	95/04/11
#f bits.h

typedef unsigned long  int   dword;
typedef unsigned short int    word;
typedef unsigned char         byte;

#define Putg(bp,x,y,pg) ( GDS_put_block( \
	(bp)->b,x,y,(x)+(bp)->wx-1,(y)+(bp)->wy-1,pg,PSET) \
	)

typedef struct {
	signed short int wx,wy;
	word s;
	byte *b;
} BITS;

#end
*/

/*
	bits.c
	95/04/11 95/04/12
	lcc -cn -c bits
*/

--
#include <malloc.h>
#include "gds.h"
#include "bits.h"

-bitsini.c

BITS *bitsini( int x, int y ){

	BITS *b;

	if( x < 0 || x > 640 || y < 0 || y > 400 ) return NULL;
	b = (BITS *) malloc(sizeof(BITS));
	if( b == NULL ) return NULL;
	/* b->s = ( ( x * y / 8 ) + ( ( x * y ) % 8 ? 1 : 0 ) ); */
	b->s = ( x * y + 7 ) / 8;
	b->b = (byte *) malloc( b->s * 4 );
	if( b->b == NULL ) return NULL;
	b->wx = x;
	b->wy = y;
	return b;
}

--
#include "bits.h"

-bitschg.c

int bitschg( BITS *b, BITS *n, int x, int y ){

	int size;

	size = ( x * y + 7 ) / 8;
	if( b->s < size ) return -1;
	n->s = size;
	n->b = b->b;
	n->wx = x;
	n->wy = y;
	return 0;
}

-setbitg.c

int setbitg( BITS *b, int x, int y, byte c ){

	dword d;
	int i;
	byte k;

	if( x >= b->wx || y >= b->wy ) return -1;
	d = x + y * b->wx;
	k = ( 0x80 >> ( d % 8 ));
	d /= 8;
	for( i = 0 ; i < 4 ; i++ ){
		if( c & ( 1 << i ) ){
			*( b->b + b->s * i + d ) |= k;
		} else {
			*( b->b + b->s * i + d ) &= ~ k;
		}
	}
	return 0;
}

-clbitg.c

void clbitg( BITS *b ){

	/*
	int i;

	for( i = 0 ; i < ( b->s * 4 ) ; i++ ) *( b->b + i ) = 0;
	*/
	int i;
	dword *d;
	for( d = (dword *)b->b, i = b->s ; i-- > 0 ; ) *d++ = 0L;
}

