shithub: pain

Download patch

ref: ba2794f02b1f69c0b7c2310f967c308d17471c78
parent: 2135d47a94469871de61d2efe2e9dbf8993c583d
author: jmq <jmq@jmq.sh>
date: Mon Dec 9 23:53:27 EST 2024

resize working on +pos

--- a/main.c
+++ b/main.c
@@ -157,7 +157,6 @@
 
 	if (ZoomedImage == nil) {
 		ZoomedImage = allocimage(display, ZoomedSize, RGBA32, 1, DNofill);
-		print("ZoomedSize: %R\n", ZoomedSize);
 		if (ZoomedImage == nil)
 			sysfatal("failed to allocate ZoomedImage: %r");
 		sr = 1;
--- a/resize.c
+++ b/resize.c
@@ -13,7 +13,7 @@
 {
 	float d, f;
 	Point p;
-	Rectangle r;
+	Rectangle r, tr;
 	int sh, i;
 	Image * t, * ci;
 
@@ -28,7 +28,12 @@
 	sh = Dy(si->r);
 	// Allocate temporary image with the size of the dimensions
 	// we are supposed to write on
-	if ((t = allocimage(display, dr, si->chan, 1, DNofill)) == nil) {
+	tr = dr;
+	if (Dx(dr) < Dx(si->r))
+		tr.max.x = tr.min.x + Dx(si->r);
+	if (Dy(dr) < Dy(si->r))
+		tr.max.y = tr.min.y + Dy(si->r);
+	if ((t = allocimage(display, tr, si->chan, 1, DNofill)) == nil) {
 		werrstr("allocimage: %r");
 		return -1;
 	}
@@ -38,7 +43,7 @@
 	//
 	// For this first scan, we will be using a vertical line of width 1, and height
 	// sh (so the source image's height)
-	if ((ci = allocimage(display, Rect(0, 0, 1, sh), si->chan, 1, DNofill)) == nil) {
+	if ((ci = allocimage(display, Rect(0, 0, 1, Dy(tr)), si->chan, 1, DNofill)) == nil) {
 		werrstr("allocimage: %r");
 		freeimage(t);
 		return -1;
@@ -51,12 +56,13 @@
 
 	// Tall rectangle where the ci image will be drawn in
 	// the temporary image
-	r.min = dr.min;
-	r.max.y = dr.max.y;
-	r.max.x = r.min.x + MAX(1.0, 1/scale);	// It wouldn't make much sense to have
+	r.min = tr.min;
+	r.max.y = tr.max.y;
+	r.max.x = r.min.x + MAX(1.0, scale);	// It wouldn't make much sense to have
 											// a horizontal width of less than 1
-	print("t->r = %R\n", t->r);
-	print("ci->r = %R\n", ci->r);
+	// print("t->r = %R\n", t->r);
+	// print("ci->r = %R\n", ci->r);
+	// print("dr = %R\n", dr);
 	i = r.max.x - r.min.x;
 	d = p.x;
 	f = r.min.x;
@@ -63,22 +69,22 @@
 	// Scan the source image with a vertical cursor image and apply it to the
 	// temporary image. We want to do this until we try to scan outside the
 	// source image, or we try to write outside of the target image.
-	while (r.min.x < dr.max.x || p.x < (si->r.max.x/scale)) {
+	while (r.min.x < tr.max.x) {
 		// If we are expanding an image, one scan on the source image will result on
 		// multiple draws on the target image. Conversively, if we are shrinking an
 		// image, for every line on the target image will we will skip over multiple
 		// lines on the source image.
 		
-		print("f = %f, p = %P, r = %R\n", scale, p, r);
+		// print("f = %f, p = %P, r = %R\n", scale, p, r);
 		// Move the ci one 1/scale space to the right
 		draw(ci, ci->r, si, nil, p);
-		d += 1/scale;
+		d += MAX(1/scale, 1);
 		p.x = d;
 		
 		// Move the draw rectangle for the temporary image scale
 		// rectangles to the right
 		draw(t, r, ci, nil, ZP);
-		f += scale;
+		f += MAX(scale, 1);
 		r.min.x = f;
 		r.max.x = r.min.x + i;
 	}
@@ -93,7 +99,7 @@
 	// to do it vertically
 	
 	// Make our horizontal scan image
-	if ((ci = allocimage(display, Rect(0, 0, Dx(dr), 1), si->chan, 1, DNofill)) == nil) {
+	if ((ci = allocimage(display, Rect(0, 0, Dx(tr), 1), si->chan, 1, DNofill)) == nil) {
 		werrstr("allocimage: %r");
 		freeimage(t);
 		return -1;
@@ -100,20 +106,20 @@
 	}
 
 	// Location of the cursor relative to the source image's sp
-	p.y = dr.min.y;
 	p.x = (int)(((float)dr.min.x)/scale);
+	p.y = (int)(((float)dr.min.y)/scale);
 	p = addpt(p, sp);
 
 	// Wide rectangle where the ci image will be drawn in
 	// the temporary image
-	r.min = dr.min;
-	r.max.x = dr.max.x;
+	r.min = tr.min;
+	r.max.x = tr.max.x;
 	r.max.y = r.min.y + MAX(1.0, scale);
-	
+
 	i = r.max.y - r.min.y;
 	d = p.y;
-	draw(di, t->r, t, nil, ZP);
-	while (0) {
+	f = r.min.y;
+	while (r.min.y < dr.max.y) {
 		// If we are expanding an image, one scan on the source image will result on
 		// multiple draws on the target image. Conversively, if we are shrinking an
 		// image, for every line on the target image will we will skip over multiple
@@ -121,15 +127,15 @@
 		
 		// Move the ci one 1/scale space to the right
 		draw(ci, ci->r, t, nil, p);
-		d += 1/scale;
+		d += MAX(1/scale, 1);
 		p.y = d;
 		
 		// Move the draw rectangle for the temporary image scale
 		// rectangles to the right
 		draw(di, r, ci, nil, ZP);
-		r.max.y += i;
-		r.min.y += i;
-		print("f = %f, p = %P, r = %R\n", scale, p, r);
+		f += MAX(scale, 1);
+		r.min.y = f;
+		r.max.y = r.min.y + i;
 	}
 
 	freeimage(t);
--