git: 9front

Download patch

ref: fcb6e41f5990077bcda51d8c369ae23b2fcff0ec
parent: 4577848db478cdbf1073f9365d36cfa70892afe3
author: cinap_lenrek <cinap_lenrek@felloff.net>
date: Fri Feb 7 21:33:29 EST 2014

jpg: center image in screen

when using the jpg(1) programs interactively, draw the
image in the center of it. this avoids wasting space
with always on borders on small windows and looks better
when used in fullscreen.

--- a/sys/src/cmd/jpg/bmp.c
+++ b/sys/src/cmd/jpg/bmp.c
@@ -24,6 +24,16 @@
 
 Rawimage** readbmp(int fd, int colorspace);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -35,9 +45,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/gif.c
+++ b/sys/src/cmd/jpg/gif.c
@@ -26,14 +26,14 @@
 Rectangle
 imager(void)
 {
-	Rectangle r;
+	Point p1, p2;
 
 	if(allims==nil || allims[0]==nil)
 		return screen->r;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(allims[0]->r);
-	r.max.y = r.min.y+Dy(allims[0]->r);
-	return r;
+
+	p1 = addpt(divpt(subpt(allims[0]->r.max, allims[0]->r.min), 2), allims[0]->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(allims[0]->r, subpt(p2, p1));
 }
 
 void
--- a/sys/src/cmd/jpg/jpg.c
+++ b/sys/src/cmd/jpg/jpg.c
@@ -26,6 +26,16 @@
 
 char	*show(int, char*, int);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -37,9 +47,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/png.c
+++ b/sys/src/cmd/jpg/png.c
@@ -23,6 +23,16 @@
 
 char	*show(int, char*, int);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -34,9 +44,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	draw(screen, r, image, nil, image->r.min);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/ppm.c
+++ b/sys/src/cmd/jpg/ppm.c
@@ -22,6 +22,16 @@
 
 char	*show(int, char*);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -33,9 +43,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/tga.c
+++ b/sys/src/cmd/jpg/tga.c
@@ -24,6 +24,16 @@
 
 Rawimage** readtga(int fd);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -35,9 +45,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/tif.c
+++ b/sys/src/cmd/jpg/tif.c
@@ -22,6 +22,16 @@
 int init(void);
 char *show(int, char *, int);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -31,9 +41,7 @@
 		sysfatal("getwindow: %r");
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x + Dx(image->r);
-	r.max.y = r.min.y + Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/v210.c
+++ b/sys/src/cmd/jpg/v210.c
@@ -24,6 +24,16 @@
 
 Rawimage** readV210(int fd, int colorspace);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -35,9 +45,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--- a/sys/src/cmd/jpg/yuv.c
+++ b/sys/src/cmd/jpg/yuv.c
@@ -24,6 +24,16 @@
 
 Rawimage** readyuv(int fd, int colorspace);
 
+Rectangle
+imager(Image *i)
+{
+	Point p1, p2;
+
+	p1 = addpt(divpt(subpt(i->r.max, i->r.min), 2), i->r.min);
+	p2 = addpt(divpt(subpt(screen->clipr.max, screen->clipr.min), 2), screen->clipr.min);
+	return rectaddpt(i->r, subpt(p2, p1));
+}
+
 void
 eresized(int new)
 {
@@ -35,9 +45,7 @@
 	}
 	if(image == nil)
 		return;
-	r = insetrect(screen->clipr, Edge+Border);
-	r.max.x = r.min.x+Dx(image->r);
-	r.max.y = r.min.y+Dy(image->r);
+	r = imager(image);
 	border(screen, r, -Border, nil, ZP);
 	drawop(screen, r, image, nil, image->r.min, S);
 	flushimage(display, 1);
--