From mboxrd@z Thu Jan 1 00:00:00 1970 Message-Id: <200007161850.OAA270930@f04n07.cac.psu.edu> From: schwartz@bio.cse.psu.edu (Scott Schwartz) Subject: [9fans] blend demo Date: Sun, 16 Jul 2000 15:01:02 -0400 To: 9fans@cse.psu.edu MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit Topicbox-Message-UUID: deb7364e-eac8-11e9-9e20-41e7f4b1d025 Here's a little program I've been using to exercise alpha channel blending. It draws a bunch of overlapping circles in various colors on various backgrounds. I'm not sure the displayed results are correct, but maybe that's an artifact of my 8-bit display, and it's pretty slow, but debugging aside (I'm running with some patches to libdraw from Russ), I thought it would make a nice little example of the new graphics model. # To unbundle, run this file echo blend.c sed 's/.//' >blend.c <<'//GO.SYSIN DD blend.c' -#include -#include -#include -#include - -#define RGBA rgba -#define MIN(a,b) ((a)<(b)?(a):(b)) - -int rgba(int r, int g, int b, int a) -{ - int c; - - r = (r*a)/255; - g = (g*a)/255; - b = (b*a)/255; - c= (r<<24) | (g<<16) | (b<<8) | (a); - return c; -} - -enum { Nbg = 3 }; -Image *fg[2][2][3]; -Image *bg[Nbg]; - -void main(int argc, char *argv[]) -{ - Event e; - Rectangle r1 = Rect(0,0,1,1); - Image **i; - - USED(argc, argv); - - if (initdraw(nil, nil, "blend") < 0) { - fprint(2, "initdraw failed: %r\n"); - exits("initdraw"); - } - einit(Emouse|Ekeyboard); - - bg[0] = allocimage(display, r1, RGBA32, 1, DBlack); - bg[1] = allocimage(display, r1, RGBA32, 1, DWhite); - bg[2] = allocimage(display, r1, RGBA32, 1, RGBA(0,0,0,64)); - - // Pure CMY - i = fg[0][0]; - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,255,0,255)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,255,255)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,255,255)); - // 50% CMY - i = fg[0][1]; - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,255,0,127)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,255,127)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,255,127)); - // Pure RGB - i = fg[1][0]; - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,0,255)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,0,255)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,0,255,255)); - // 50% RGB - i = fg[1][1]; - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(255,0,0,127)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,255,0,127)); - *i++ = allocimage(display, r1, RGBA32, 1, RGBA(0,0,255,127)); - - eresized(0); - for (;;) { - switch(event(&e)){ - case Ekeyboard: - switch(e.kbdc){ - case 'q': - case '\04': - exits(nil); - } - break; - case Emouse: - if(e.mouse.buttons){ - } - break; - } - } - exits(nil); -} - -void spots(Point o, int R, Image *bg, Image **fg) -{ - Rectangle r = Rpt(o, addpt(o, Pt(3*R, 3*R))); - - draw(screen, r, bg, nil, ZP); - - fillellipse(screen, addpt(o,Pt(1*R,2*R)), R,R, fg[0], ZP); - fillellipse(screen, addpt(o,Pt(2*R,2*R)), R,R, fg[1], ZP); - fillellipse(screen, addpt(o,Pt(1.5*R,1*R)), R,R, fg[2], ZP); -} - -void redraw(Image *screen) -{ - Rectangle r = screen->r; - int dX = r.max.x-r.min.x; - int dY = r.max.y-r.min.y; - int R = MIN(dX,dY)/4/3; - int x, y; - - fprint(2, "s=%R, R=%d\n", screen->r, R); - // draw(screen, r, bg[2], nil, ZP); - - for (x=0; x<4; ++x) - for (y=0; y<4; ++y) - spots(addpt(r.min, Pt(x*3*R, y*3*R)), R, - bg[(x<2)+(y<2)], fg[y%2][x%2]); - //flushimage(display, 1); -} - -void -eresized(int new) -{ - if(new && getwindow(display, Refnone) < 0){ - fprint(2, "can't reattach to window: %r\n"); - exits("attach"); - } - redraw(screen); -} //GO.SYSIN DD blend.c