shithub: vcrop

Download patch

ref: f36ef4b2a79306f34b825947a2a85229e4b8aa6f
parent: b3d399a6aad96527b79dfe254ee11eb54df9ee4a
author: phil9 <telephil9@gmail.com>
date: Fri Dec 17 06:13:25 EST 2021

clamp cropped area to image bounds

	garbage was being captured when the crop rectangle was outside of image
	bounds. We now ensure that the cropped area is constrained within image
	rectangle.

--- a/vcrop.c
+++ b/vcrop.c
@@ -44,15 +44,24 @@
 crop(Mouse *m)
 {
 	Rectangle r;
+	Point o;
 	Image *i;
 
 	r = egetrect(1, m);
 	if(eqrect(r, ZR) || badrect(r) || (Dx(r)<Threshold && Dy(r)<Threshold))
 		return;
-	i = allocimage(display, Rect(0,0,Dx(r),Dy(r)), screen->chan, 0, DNofill);
+	o = subpt(r.min, screen->r.min);
+	o = addpt(n->r.min, o);
+	o = subpt(o, addpt(n->r.min, pos));
+	/* clamp rect to image bounds */
+	if(o.x < n->r.min.x) o.x = n->r.min.x;
+	if(o.y < n->r.min.y) o.y = n->r.min.y;
+	if((o.x + Dx(r)) > n->r.max.x) r.max.x = r.min.x + n->r.max.x - o.x;
+	if((o.y + Dy(r)) > n->r.max.y) r.max.y = r.min.y + n->r.max.y - o.y;
+	i = allocimage(display, r, screen->chan, 0, DNofill);
 	if(i==nil)
 		sysfatal("allocimage: %r");
-	draw(i, i->r, screen, nil, r.min);
+	draw(i, i->r, n, nil, o);
 	if(p)
 		freeimage(p);
 	p = n;