Annotation of researchv10dc/vol2/Faces/How/matte.c, revision 1.1.1.1

1.1       root        1: #include <u.h>
                      2: #include <gnot.h>
                      3: #include <event.h>
                      4: 
                      5: Bitmap display;
                      6: Font *defont;
                      7: int w,h;
                      8: 
                      9: main(argc,argv)
                     10: char *argv[];
                     11: {
                     12:        Bitmap *b,*m;
                     13:        int fd;
                     14:        ginit();
                     15:        einit();
                     16:        if (argc != 4) {
                     17:                fprint(2,"usage: matte w h file\n");
                     18:                exit(0);
                     19:        }
                     20:        w = atoi(argv[1]);
                     21:        h = atoi(argv[2]);
                     22:        if ((fd = open(argv[3],0)) < 0) {
                     23:                fprint(2,"can't open %s\n",argv[3]);
                     24:                exit(0);
                     25:        }
                     26:        b = balloc(Rect(0,0,w,h),3);
                     27:        m = balloc(Rect(0,0,w,h),1);
                     28:        bitblt(m,m->rect.min,m,m->rect,F);
                     29:        myread(fd,b->base,w*h);
                     30:        close(fd);
                     31:        dispfs(b,m);
                     32:        edit(b,m,argv[3]);
                     33: }
                     34: 
                     35: dispfs(b,m)
                     36: Bitmap *b,*m;
                     37: {
                     38:        Bitmap *r;
                     39:        r = balloc(b->rect,b->ldepth);
                     40:        bitblt(r,r->rect.min,b,b->rect,S);
                     41:        bitblt(r,r->rect.min,m,m->rect,S&D);
                     42:        fs(r);
                     43:        bitblt(&display,display.rect.min,r,r->rect,S);
                     44:        bfree(r);
                     45: }
                     46: 
                     47: char *fsizes[] = {"4", "7", "12", "25", "80", "150", 0};
                     48: Menu sizes = {fsizes};
                     49: 
                     50: enum {REMOVE, RESTORE, WRITE, EXIT};
                     51: char *fmenu[] = {"remove", "restore", "write", "exit", 0};
                     52: Menu commands = {fmenu};
                     53: 
                     54: #define DR(n)  raddp(Rect(0,0,n,n),display.rect.min)
                     55: 
                     56: edit(b,m,file)
                     57: Bitmap *b,*m;
                     58: char *file;
                     59: {
                     60:        Event *e;
                     61:        Point p;
                     62:        Bitmap *res,*brush=0;
                     63:        int i,fd,size=25,wannago = 0;
                     64:        int mode=0;
                     65:        while (1) {
                     66:                switch ((e = getevent())->type) {
                     67:                case MOUSE:
                     68:                        if (mouse.buttons&1) {
                     69:                                p = sub(mouse.xy,Pt(size/2,size/2));
                     70:                                docursor(0);
                     71:                                bitblt(&display,p,&display,DR(size),F-mode);
                     72:                                docursor(size);
                     73:                                bitblt(m,sub(p,display.rect.min),m,Rect(0,0,size,size),mode);
                     74:                        }
                     75:                        else if (mouse.buttons&2) {
                     76:                                if ((i = menuhit(&sizes,2)) == -1)
                     77:                                        break;
                     78:                                size = atoi(fsizes[i]);
                     79:                                docursor(size);
                     80:                        }
                     81:                        else if (mouse.buttons&4)
                     82:                                switch (menuhit(&commands,3)) {
                     83:                                case -1:
                     84:                                        break;
                     85:                                case REMOVE:
                     86:                                        mode = 0;
                     87:                                        docursor(0);
                     88:                                        dispfs(b,m);
                     89:                                        docursor(size);
                     90:                                        break;
                     91:                                case RESTORE:
                     92:                                        mode = F;
                     93:                                        docursor(0);
                     94:                                        bitblt(&display,display.rect.min,m,m->rect,notS);
                     95:                                        docursor(size);
                     96:                                        break;
                     97:                                case WRITE:
                     98:                                        res = balloc(Rect(0,0,w,h),3);
                     99: /*
                    100:                                        bitblt(res,Pt(0,0),b,b->rect,S);
                    101:                                        bitblt(res,Pt(0,0),m,m->rect,S&D);
                    102: */
                    103:                                        bitblt(res,Pt(0,0),m,m->rect,S);
                    104:                                        if ((fd = create("matte",1,0666)) < 0) {
                    105:                                                fprint("can't create matte file\n");
                    106:                                                continue;
                    107:                                        }
                    108:                                        mywrite(fd,res->base,w*h);
                    109:                                        close(fd);
                    110:                                        bfree(res);
                    111:                                        break;
                    112:                                case EXIT:
                    113:                                        if (wannago)
                    114:                                                exit(0);
                    115:                                        else {
                    116:                                                wannago = 1;
                    117:                                                continue;
                    118:                                        }
                    119:                                        break;
                    120:                                }
                    121:                        wannago = 0;
                    122:                        docursor(size);
                    123:                default:
                    124:                        break;
                    125:                }
                    126:        }
                    127: }
                    128: 
                    129: docursor(n)
                    130: {
                    131:        static Point pt;
                    132:        static size;
                    133:        if (size != 0)
                    134:                cursoroff();
                    135:        bitblt(&display,sub(pt,Pt(size/2,size/2)),&display,DR(size),notD);
                    136:        pt = mouse.xy;
                    137:        size = n;
                    138:        bitblt(&display,sub(pt,Pt(size/2,size/2)),&display,DR(size),notD);
                    139:        if (size != 0)
                    140:                cursoron();
                    141: }      
                    142: 
                    143: ushort prop[1024];
                    144: uchar bright[] = {192,128,64,0};
                    145: 
                    146: fs(b)
                    147: Bitmap *b;
                    148: {
                    149:        int i,j,w,h,save,next,e,x;
                    150:        uchar *p;
                    151:        ushort *q;
                    152:        w = b->rect.max.x;
                    153:        h = b->rect.max.y;
                    154:        for (i = 0; i < h; i++) {
                    155:                p = (uchar *) addr(b,Pt(0,i));
                    156:                save = next = 0;
                    157:                q = prop;
                    158:                for (j = 0; j < w; j++) {
                    159:                        x = *p + *q + next;
                    160:                        if (x > 255)
                    161:                                x = 255;
                    162:                        e = x&0x3f;
                    163:                        next = (e + (e<<1))>>3;
                    164:                        *q++ = save + next;
                    165:                        save = e>>2;
                    166:                        *p++ = ~x;
                    167:                }
                    168:        }
                    169: }
                    170: 
                    171: myread(fd,buf,n)
                    172: uchar *buf;
                    173: {
                    174:        int i;
                    175:        do {
                    176:                i = read(fd,buf,n);
                    177:                if(i<=0)
                    178:                        return;         /* let it overrun */
                    179:                buf += i;
                    180:                n -= i;
                    181:        } while (n > 0);
                    182: }
                    183: 
                    184: mywrite(fd,buf,n)
                    185: uchar *buf;
                    186: {
                    187:        int i;
                    188:        do {
                    189:                i = write(fd,buf,(n>4096)?4096:n);
                    190:                if (i<=0) {
                    191:                        fprint(2,"bad write of %d\n",n);
                    192:                        exit(1);
                    193:                }
                    194:                buf += i;
                    195:                n -= i;
                    196:        } while (n > 0);
                    197: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.