#include #include #include Image *px; uchar sq[128*128]; uchar bayer[8][8] = { {0, 128, 32, 160, 8, 136, 40, 168}, {192, 64, 224, 96, 200, 72, 232, 104}, {48, 176, 16, 144, 56, 184, 24, 152}, {240, 112, 208, 80, 248, 120, 216, 88}, {12, 140, 44, 172, 4, 132, 36, 164}, {204, 76, 236, 108, 196, 68, 228, 100}, {60, 188, 28, 156, 52, 180, 20, 148}, {252, 124, 220, 92, 244, 116, 212, 84} }; void dither(void){ Rectangle r; int x, y; uchar p; r.min.y = screen->r.min.y; for(y = 0; y < 128; y++){ r.max.y = r.min.y + 4; r.min.x = screen->r.min.x; for(x = 0; x < 128; x++){ r.max.x = r.min.x + 4; p = 255 + (sq[y << 7 | x] < bayer[x & 7][y & 7]); loadimage(px, px->r, &p, 1); draw(screen, r, px, nil, ZP); flushimage(display, 1); sleep(1); r.min.x = r.max.x; } r.min.y = r.max.y; } } void sillywalk(int n, float d){ Rectangle r; int c; if(sq[n]) return; sq[n] = 255.0 * (sin(d) * 0.5 + 0.5); loadimage(px, px->r, sq + n, 1); r = rectaddpt(Rect(0, 0, 4, 4), screen->r.min); r = rectaddpt(r, Pt(n << 2 & 508, n >> 5 & 508)); draw(screen, r, px, nil, ZP); flushimage(display, 1); sleep(1); c = 0; switch(lrand() & 3){ case 0: again: sillywalk(n - 128 & 16383, d + 0.01); if(++c == 4) break; case 1: sillywalk(n + 1 & 16383, d + 0.01); if(++c == 4) break; case 2: sillywalk(n + 128 & 16383, d + 0.01); if(++c == 4) break; case 3: sillywalk(n - 1 & 16383, d + 0.01); if(++c == 4) break; goto again; } } void main(int, char**){ ulong i; i = truerand(); print("0x%.8ux\n", i); srand(i); if(initdraw(nil, nil, "bayer walk") < 0) sysfatal("initdraw: %r"); px = allocimage(display, Rect(0, 0, 1, 1), CMAP8, 1, DNofill); if(px == nil) sysfatal("allocimage: %r"); sillywalk(lrand() & 16383, 0.01); dither(); sleep(9999990); exits(nil); }