changeset 1:7a8f72b27dc3

added keep aspect ratio; validated size
author meillo@localhost.localdomain
date Sat, 14 Jun 2008 09:08:05 +0200
parents 8c94239b3b3f
children 8e71b54b6e1e
files resize-gd.c
diffstat 1 files changed, 73 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/resize-gd.c	Fri Jun 13 14:18:52 2008 +0200
+++ b/resize-gd.c	Sat Jun 14 09:08:05 2008 +0200
@@ -1,8 +1,20 @@
-/* compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c  */
+/*
+ * compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c
+ * build-depends: libgd2-noxpm-dev | libgd2-dev
+ * depends: libgd2-noxpm | libgd2-xpm
+ *
+ *
+ *
+ * http://www.libgd.org/ImageCreation
+ * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html
+ * http://netpbm.sourceforge.net/
+ *
+ */
 
 #include <stdio.h>
 #include <stdlib.h>  /* for atoi() */
 #include <string.h>
+#include <ctype.h>
 #include "gd.h" /* Bring in the gd library functions */
 
 
@@ -15,51 +27,98 @@
 int main(int argc, char* argv[]) {
 	int i;
 	int w, h;
+	int width, height;
 	int x, y;
 	int type;
 	gdImagePtr im_in;
 	gdImagePtr im_out;
 	FILE* in;
 	FILE* out;
+	char* sep;
 
 	if (argc < 3) {
-		puts("usage: resize-gd <width>x<height> <imagefiles>");
+		puts("usage: resize-gd <width>x<height> <imagefiles>\n\
+       resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
 		exit(1);
 	}
 
 	/* parse width and height */
-	w = atoi(argv[1]);
-	h = atoi(strstr(argv[1], "x") + 1);
-	printf("w: %d  h: %d\n", w, h);
+	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);
+	} else {
+		height = -1;  /* keep aspect ratio */
+	}
+
 
 	/* process images */
 	for (i = 2; i < argc; i++) {
 		printf("processing file '%s'\n", argv[i]);
 
-		if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) {
+		if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
 			type = Png;
-		} else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) {
+		} else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
 			type = Jpg;
 		} else {
-			fprintf(stderr, "unknown filetype\n");
-			exit(2);
+			fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
+			continue;
 		}
+		/* load image */
+		in = fopen(argv[i], "rb");
 
-		/* Load a large png to shrink in the smaller one */
-		in = fopen(argv[i], "rb");
 		if (type == Png) {
 			im_in = gdImageCreateFromPng(in);
 		} else if (type == Jpg) {
 			im_in = gdImageCreateFromJpeg(in);
 		}
+		/*
+		if ((im_in = gdImageCreateFromPng(in)) != NULL) {
+			type = Png;
+		} else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) {
+			type = Jpg;
+		} else {
+			fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
+			continue;
+		}
+		*/
+
 		fclose(in);
 
-		im_out = gdImageCreateTrueColor(w, h);
+		/* 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;
+		}
 
-		/* Now copy the large image */
+		/* copy-resize the image */
+		im_out = gdImageCreateTrueColor(w, h);
 		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
 
-		/* write out */
+		/* write image */
 		out = fopen(argv[i], "wb");
 		if (type == Png) {
 			gdImagePng(im_out, out);