shithub: oledsaver

ref: b545233a9b54502f276f70a167f2b7f71c54161f
dir: /bitart.c/

View raw version
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <memdraw.h>
#include "bitart.h"

int
ob0(int x, int y)
{
	return ((-((-y) ^ (-x))) % (-((x - y) / (x / x)))) % 12;
}

int
ob1(int x, int y)
{
	return ((-((y^23)|(x%2)))-(((~y)*(~y))%((-x)+(y*x))))%7;
}

int
ob2(int x, int y)
{
	return ((-((x+23)-(-x)))%(~(~(14-y))))%6;
}

int
ob3(int x, int y)
{
	return ((-(-(x|x)))%(((-x)^(y+x))/((y/x)-(x^x))))%3;
}

int
ob4(int x, int y)
{
	return (((~(x/y))*(-(-y)))-(-((y-y)|(-x))))%11;
}

int
ob5(int x, int y)
{
	return (((-(y|y))/((-y)+(y&x)))/(((y*y)&(~x))+((x/y)&(13&x))))%4;
}

int
drand(int, int)
{
	return ntruerand(2);
}

/* drawing order iterators */

Memimage*
topdown(drawfunc df, Memimage *img)
{
	Rectangle r;
	
	r.min.x = 0;
	r.min.y = 0;
	r.max.x = 1;
	r.max.y = 1;	

	int y, x, fd;
	for(y = 0; y < h; y++){
		// successive writes will fail if do not close/open
		fd = open(path, OWRITE);
		if(fd < 0){
			sysfatal("could not open kbdoled file → %r");
		}


		for(x = 0; x < w; x++){
			Memimage *todraw = memblack;
			if(df(x, y))
				todraw = memwhite;
		
			r.min.x = x;
			r.min.y = y;

			r.max.x = r.min.x+1;
			r.max.y = r.min.y+1;

			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);

		}
		img = writeuncompressed(fd, img);
		
		// flush
		fd = close(fd);
		if(fd < 0)
			sysfatal("close failed → %r");
		sleep(ms);
	}
	return img;
}

Memimage*
bottomup(drawfunc df, Memimage *img)
{
	Rectangle r;
	
	r.min.x = 0;
	r.min.y = 0;
	r.max.x = 1;
	r.max.y = 1;	

	int y, x, fd;
	for(y = h-1; y >=0; y--){
		// successive writes will fail if do not close/open
		fd = open(path, OWRITE);
		if(fd < 0){
			sysfatal("could not open kbdoled file → %r");
		}


		for(x = 0; x < w; x++){
			Memimage *todraw = memblack;
			if(df(x, y))
				todraw = memwhite;
		
			r.min.x = x;
			r.min.y = y;

			r.max.x = r.min.x+1;
			r.max.y = r.min.y+1;

			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);

		}
		img = writeuncompressed(fd, img);
		
		// flush
		fd = close(fd);
		if(fd < 0)
			sysfatal("close failed → %r");
		sleep(ms);
	}
	return img;
}

Memimage*
leftright(drawfunc df, Memimage *img)
{
	Rectangle r;
	
	r.min.x = 0;
	r.min.y = 0;
	r.max.x = 1;
	r.max.y = 1;	

	int y, x, fd;
	for(x = 0; x < w; x++){
		// successive writes will fail if do not close/open
		fd = open(path, OWRITE);
		if(fd < 0){
			sysfatal("could not open kbdoled file → %r");
		}


		for(y = 0; y < h; y++){
			Memimage *todraw = memblack;
			if(df(x, y))
				todraw = memwhite;
		
			r.min.x = x;
			r.min.y = y;

			r.max.x = r.min.x+1;
			r.max.y = r.min.y+1;

			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);

		}
		img = writeuncompressed(fd, img);
		
		// flush
		fd = close(fd);
		if(fd < 0)
			sysfatal("close failed → %r");
		sleep(ms);
	}
	return img;
}

Memimage*
rightleft(drawfunc df, Memimage *img)
{
	Rectangle r;
	
	r.min.x = 0;
	r.min.y = 0;
	r.max.x = 1;
	r.max.y = 1;	

	int y, x, fd;
	for(x = w-1; x >= 0; x--){
		// successive writes will fail if do not close/open
		fd = open(path, OWRITE);
		if(fd < 0){
			sysfatal("could not open kbdoled file → %r");
		}


		for(y = 0; y < h; y++){
			Memimage *todraw = memblack;
			if(df(x, y))
				todraw = memwhite;
		
			r.min.x = x;
			r.min.y = y;

			r.max.x = r.min.x+1;
			r.max.y = r.min.y+1;

			memimagedraw(img, r, todraw, ZP, nil, ZP, SoverD);

		}
		img = writeuncompressed(fd, img);
		
		// flush
		fd = close(fd);
		if(fd < 0)
			sysfatal("close failed → %r");
		sleep(ms);
	}
	return img;
}

// unloadimage will replace data in the image
// so we build a new image as we unbuild the current one
// free the old image
// return the new image ptr
Memimage*
writeuncompressed(int fd, Memimage *m)
{
	char chanstr[32];
	int bpl, y, j;
	uchar *buf;
	Memimage *out;
	Rectangle r;
	
	out = allocmemimage(Rect(0, 0, w, h), GREY1);

	if(chantostr(chanstr, m->chan) == nil)
		sysfatal("can't convert channel descriptor: %r");
	fprint(fd, "%11s %11d %11d %11d %11d ",
		chanstr, m->r.min.x, m->r.min.y, m->r.max.x, m->r.max.y);

	bpl = bytesperline(m->r, m->depth);
	buf = malloc(bpl);
	if(buf == nil)
		sysfatal("malloc failed: %r");
	for(y=m->r.min.y; y<m->r.max.y; y++){
		r = Rect(m->r.min.x, y, m->r.max.x, y+1);
		j = unloadmemimage(m, r, buf, bpl);
		loadmemimage(out, r, buf, bpl);
		if(j != bpl)
			sysfatal("image unload failed → %r");

		if(write(fd, buf, bpl) != bpl)
			sysfatal("wu write failed → %r");
	}
	
	free(buf);
	freememimage(m);
	
	return out;
}