extend -b and -c to -B and -C with optional α parameter Reference: /n/atom/patch/applied2013/alphacrop Date: Wed Jun 19 03:22:35 CES 2013 Signed-off-by: quanstro@quanstro.net --- /sys/src/cmd/crop.c Wed Jun 19 03:22:29 2013 +++ /sys/src/cmd/crop.c Wed Jun 19 03:22:29 2013 @@ -16,7 +16,7 @@ void usage(void) { - fprint(2, "usage: crop [-b rgb] [-c rgb] [-i ±inset | -r R | -x ±inset | -y ±inset] [-t tx ty] [imagefile]\n"); + fprint(2, "usage: crop [-b rgb] [-B rgbα] [-c rgb] [-C rgbα] [-i ±inset | -r R | -x ±inset | -y ±inset] [-t tx ty] [imagefile]\n"); fprint(2, "\twhere R is a rectangle: minx miny maxx maxy\n"); fprint(2, "\twhere rgb is a color: red green blue\n"); exits("usage"); @@ -35,7 +35,7 @@ } Rectangle -crop(Memimage *m, ulong c) +crop(Memimage *m, ulong c, ulong bg) { Memimage *n; int x, y, bpl, wpl; @@ -52,6 +52,7 @@ n = allocmemimage(m->r, RGBA32); if(n == nil) sysfatal("can't allocate temporary image: %r"); + memfillcolor(n, bg); memimagedraw(n, n->r, m, m->r.min, nil, ZP, S); m = n; } @@ -85,7 +86,7 @@ void main(int argc, char *argv[]) { - int fd, mode, red, green, blue; + int fd, mode, red, green, blue, α; Rectangle r, rparam; Point t; Memimage *m, *new; @@ -102,51 +103,55 @@ ARGBEGIN{ case 'b': - if(bg != 0) - usage(); - red = getint(ARGF())&0xFF; - green = getint(ARGF())&0xFF; - blue = getint(ARGF())&0xFF; - bg = (red<<24)|(green<<16)|(blue<<8)|0xFF; + case 'B': + red = getint(EARGF(usage()))&0xFF; + green = getint(EARGF(usage()))&0xFF; + blue = getint(EARGF(usage()))&0xFF; + α = 0xff; + if(ARGC() == 'B') + α = getint(EARGF(usage()))&0xFF; + bg = red<<24 | green<<16 | blue<<8 | α; break; case 'c': - if(cropval != 0) - usage(); - red = getint(ARGF())&0xFF; - green = getint(ARGF())&0xFF; - blue = getint(ARGF())&0xFF; - cropval = (red<<24)|(green<<16)|(blue<<8)|0xFF; + case 'C': + red = getint(EARGF(usage()))&0xFF; + green = getint(EARGF(usage()))&0xFF; + blue = getint(EARGF(usage()))&0xFF; + α = 0xff; + if(ARGC() == 'C') + α = getint(EARGF(usage()))&0xFF; + cropval = red<<24 | green<<16 | blue<<8 | α; break; case 'i': if(mode != None) usage(); mode = Inset; - rparam.min.x = getint(ARGF()); + rparam.min.x = getint(EARGF(usage())); break; case 'x': if(mode != None && mode != Insetxy) usage(); mode = Insetxy; - rparam.min.x = getint(ARGF()); + rparam.min.x = getint(EARGF(usage())); break; case 'y': if(mode != None && mode != Insetxy) usage(); mode = Insetxy; - rparam.min.y = getint(ARGF()); + rparam.min.y = getint(EARGF(usage())); break; case 'r': if(mode != None) usage(); mode = Set; - rparam.min.x = getint(ARGF()); - rparam.min.y = getint(ARGF()); - rparam.max.x = getint(ARGF()); - rparam.max.y = getint(ARGF()); + rparam.min.x = getint(EARGF(usage())); + rparam.min.y = getint(EARGF(usage())); + rparam.max.x = getint(EARGF(usage())); + rparam.max.y = getint(EARGF(usage())); break; case 't': - t.x = getint(ARGF()); - t.y = getint(ARGF()); + t.x = getint(EARGF(usage())); + t.y = getint(EARGF(usage())); break; default: usage(); @@ -172,7 +177,7 @@ r = m->r; if(cropval != 0){ - r = crop(m, cropval); + r = crop(m, cropval, bg); m->clipr = r; } @@ -196,12 +201,8 @@ new = allocmemimage(r, m->chan); if(new == nil) sysfatal("can't allocate new image: %r"); - if(bg != 0) - memfillcolor(new, bg); - else - memfillcolor(new, 0x000000FF); - - memimagedraw(new, m->clipr, m, m->clipr.min, nil, ZP, S); + memfillcolor(new, bg); + memimagedraw(new, m->clipr, m, m->clipr.min, nil, ZP, SatopD); dw = byteaddr(new, ZP) - byteaddr(new, t); new->r = rectaddpt(new->r, t); new->zero += dw;