shithub: vcrop

Download patch

ref: 4ac8bd4ae86f086d26012320b30ac04ce20e9c94
author: telephil9 <[email protected]>
date: Thu Nov 5 04:40:45 EST 2020

Initial import

--- /dev/null
+++ b/LICENSE
@@ -1,0 +1,21 @@
+MIT License
+
+Copyright (c) 2020 phil9
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in all
+copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+SOFTWARE.
--- /dev/null
+++ b/mkfile
@@ -1,0 +1,7 @@
+</$objtype/mkfile
+
+CFLAGS=-FTVw
+TARG=vcrop
+OFILES=vcrop.$O
+
+</sys/src/cmd/mkone
--- /dev/null
+++ b/vcrop.c
@@ -1,0 +1,140 @@
+#include <u.h>
+#include <libc.h>
+#include <draw.h>
+#include <event.h>
+#include <keyboard.h>
+
+Image *bg;
+Image *p;
+Image *n;
+char  *menustr[] = { "crop", "undo", "save", "exit", 0 };
+Menu   menu = { menustr };
+enum { Mcrop, Mundo, Msave, Mexit };
+
+void
+eresized(int new)
+{
+	if(new && getwindow(display, Refnone)<0)
+		sysfatal("cannot reattach: %r");
+	draw(screen, screen->r, bg, nil, ZP);
+	draw(screen, screen->r, n, nil, n->r.min);
+}
+
+void
+crop(Mouse *m)
+{
+	Rectangle r;
+	Image *i;
+
+	r = egetrect(1, m);
+	if(eqrect(r, ZR) || badrect(r))
+		return;
+	i = allocimage(display, Rect(0,0,Dx(r),Dy(r)), screen->chan, 0, DNofill);
+	if(i==nil)
+		sysfatal("allocimage: %r");
+	draw(i, i->r, screen, nil, r.min);
+	if(p)
+		freeimage(p);
+	p = n;
+	n = i;
+	eresized(0);
+}
+
+void
+save(Mouse *m)
+{
+	char buf[255];
+	int i, fd;
+
+	i = eenter("Save as:", buf, sizeof buf, m);
+	if(i<0)
+		return;
+	fd = create(buf, OWRITE, 0644);
+	if(fd<0)
+		sysfatal("create: %r");
+	i = writeimage(fd, n, 0);
+	if(i<0)
+		sysfatal("writeimage: %r");
+	close(fd);
+}
+
+void
+undo(void)
+{
+	if(p==nil)
+		return;
+	freeimage(n);
+	n = p;
+	p = nil;
+	eresized(0);
+}
+
+void
+menu3hit(Mouse *m)
+{
+	int i;
+
+	i = emenuhit(3, m, &menu);
+	switch(i){
+	case Mcrop:
+		m->buttons = 1;
+		crop(m);
+		break;
+	case Mundo:
+		undo();
+		break;
+	case Msave:
+		save(m);
+		break;
+	case Mexit:
+		exits(nil);
+	}
+}
+
+void
+usage(char *n)
+{
+	fprint(2, "usage: %s [image]\n", n);
+	exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+	Event ev;
+	int e, fd;
+
+	if(argc > 2)
+		usage(argv[0]);
+	fd = 0;
+	if(argc==2){
+		fd = open(argv[1], OREAD);
+		if(fd<0)
+			sysfatal("open: %r");
+	}
+	if(initdraw(nil, nil, "vcrop")<0)
+		sysfatal("initdraw: %r");
+	einit(Emouse|Ekeyboard);
+	bg = allocimage(display, Rect(0,0,1,1), screen->chan, 1, 0xCCCCCCFF);
+	n = readimage(display, fd, 0);
+	if(n==nil)
+		sysfatal("readimage: %r");
+	close(fd);
+	p = nil;
+	eresized(0);
+	for(;;){
+		e = event(&ev);
+		switch(e){
+		case Emouse:
+			if(ev.mouse.buttons==1)
+				crop(&ev.mouse);
+			else if(ev.mouse.buttons==4)
+				menu3hit(&ev.mouse);
+			break;
+		case Ekeyboard:
+			if(ev.kbdc==Kdel)
+				exits(nil);
+			break;
+		}
+	}
+}