code: 9ferno

ref: 6bb619c8db2867ddd9cd19c0aec05065f5ee0cae
dir: /libdraw/bytesperline.c/

View raw version
#include "lib9.h"
#include "draw.h"

static
int
unitsperline(Rectangle r, int d, int bitsperunit)
{
	u32 l, t;

	if(r.min.x >= 0){
		l = (r.max.x*d+bitsperunit-1)/bitsperunit;
		l -= (r.min.x*d)/bitsperunit;
	}else{			/* make positive before divide */
		t = (-r.min.x*d+bitsperunit-1)/bitsperunit;
		l = t+(r.max.x*d+bitsperunit-1)/bitsperunit;
	}
	return l;
}

int
wordsperline(Rectangle r, int d)
{
	return unitsperline(r, d, 8*sizeof(u32));
}

int
bytesperline(Rectangle r, int d)
{
	return unitsperline(r, d, 8);
}