changeset 2:8e71b54b6e1e

made separate functions for calcsize and usage; pictures are not enlarged by default
author meillo@marmaro.de
date Sat, 14 Jun 2008 10:15:07 +0200
parents 7a8f72b27dc3
children 35a50e57b6f5
files resize-gd.c
diffstat 1 files changed, 78 insertions(+), 38 deletions(-) [+]
line wrap: on
line diff
--- a/resize-gd.c	Sat Jun 14 09:08:05 2008 +0200
+++ b/resize-gd.c	Sat Jun 14 10:15:07 2008 +0200
@@ -23,8 +23,66 @@
 	Jpg,
 };
 
+struct size {
+	int w;
+	int h;
+};
 
-int main(int argc, char* argv[]) {
+
+void
+usage() {
+	puts("\
+usage: resize-gd <size> <imagefiles> [...]\n\
+         (keeps aspect ratio, does not enlarge images)\n\
+       resize-gd <width>x<height> <imagefiles> [...]\n\
+         (resizes to that size, enlarges if needed)\n\
+");
+}
+
+
+struct size
+calcsize(struct size opt, struct size img, int enlarge) {
+	struct size result;
+
+	if (opt.h <= 0) {
+		/* keep aspect ratio */
+		if (!enlarge && opt.w > img.w && opt.w > img.h) {
+			opt.w = (img.w > img.h) ? img.w : img.h;
+		}
+		if (img.w > img.h) {
+			result.w = opt.w;
+			result.h = opt.w * (1.0 * img.h / img.w);
+		} else {
+			result.h = opt.w;
+			result.w = opt.w * (1.0 * img.w / img.h);
+		}
+	} else {
+		result = opt;
+	}
+	return result;
+}
+
+
+int
+validsize(char* sp) {
+	while (isdigit(*sp)) {
+		sp++;
+	}
+	if (*sp == 'x' && isdigit(*(sp+1))) {
+		sp++;
+		while (isdigit(*sp)) {
+			sp++;
+		}
+	}
+	if (*sp != '\0') {
+		return 0;
+	}
+	return 1;
+}
+
+
+int
+main(int argc, char* argv[]) {
 	int i;
 	int w, h;
 	int width, height;
@@ -34,36 +92,27 @@
 	gdImagePtr im_out;
 	FILE* in;
 	FILE* out;
-	char* sep;
+	char* c;
+	struct size sizeopt, sizeimg, sizedest;
 
 	if (argc < 3) {
-		puts("usage: resize-gd <width>x<height> <imagefiles>\n\
-       resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
+		usage();
 		exit(1);
 	}
 
+	if (!validsize(argv[1])) {
+		fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
+		usage();
+		exit(3);
+	}
+
 	/* parse width and height */
-	sep = argv[1];
-	while (isdigit(*sep)) {
-		sep++;
-	}
-	if (*sep == 'x' && isdigit(*(sep+1))) {
-		sep++;
-		while (isdigit(*sep)) {
-			sep++;
-		}
-	}
-	if (*sep != '\0') {
-		fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
-		exit(3);
-	}
-
-	width = atoi(argv[1]);
-	sep = strstr(argv[1], "x");
-	if (sep && sep[1] != '\0') {
-		height = atoi(sep + 1);
+	sizeopt.w = atoi(argv[1]);
+	c = strstr(argv[1], "x");
+	if (c && c[1] != '\0') {
+		sizeopt.h = atoi(c + 1);
 	} else {
-		height = -1;  /* keep aspect ratio */
+		sizeopt.h = -1;  /* keep aspect ratio */
 	}
 
 
@@ -100,22 +149,13 @@
 
 		fclose(in);
 
-		/* for keeping aspect ratio */
-		if (height <= 0) {
-			if (im_in->sx > im_in->sy) {
-				w = width;
-				h = width * (1.0 * im_in->sy / im_in->sx);
-			} else {
-				h = width;
-				w = width * (1.0 * im_in->sx / im_in->sy);
-			}
-		} else {
-			w = width;
-			h = height;
-		}
+		/* calculate target size */
+		sizeimg.w = im_in->sx;
+		sizeimg.h = im_in->sy;
+		sizedest = calcsize(sizeopt, sizeimg, 0);
 
 		/* copy-resize the image */
-		im_out = gdImageCreateTrueColor(w, h);
+		im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
 		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
 
 		/* write image */