resize-gd

changeset 5:8e2804fe30bc

skipps now too small images; added --version and --help
author meillo@marmaro.de
date Sat, 14 Jun 2008 12:18:06 +0200
parents aa24986b8969
children e020f26c2c6a
files resize-gd.c
diffstat 1 files changed, 28 insertions(+), 5 deletions(-) [+]
line diff
     1.1 --- a/resize-gd.c	Sat Jun 14 12:00:23 2008 +0200
     1.2 +++ b/resize-gd.c	Sat Jun 14 12:18:06 2008 +0200
     1.3 @@ -17,6 +17,8 @@
     1.4  #include <ctype.h>
     1.5  #include "gd.h" /* Bring in the gd library functions */
     1.6  
     1.7 +#define PROGRAM "resize-gd"
     1.8 +#define VERSION "0.1"
     1.9  
    1.10  enum {
    1.11  	Png,
    1.12 @@ -30,13 +32,19 @@
    1.13  
    1.14  
    1.15  void
    1.16 +version() {
    1.17 +	printf("%s version: %s\n", PROGRAM, VERSION);
    1.18 +}
    1.19 +
    1.20 +
    1.21 +void
    1.22  usage() {
    1.23 -	puts("\
    1.24 -usage: resize-gd <size> <imagefiles> [...]\n\
    1.25 +	printf("\
    1.26 +usage: %s <size> <imagefiles> [...]\n\
    1.27           (keeps aspect ratio, does not enlarge images)\n\
    1.28 -       resize-gd <width>x<height> <imagefiles> [...]\n\
    1.29 +       %s <width>x<height> <imagefiles> [...]\n\
    1.30           (resizes to that size, enlarges if needed)\n\
    1.31 -");
    1.32 +", PROGRAM, PROGRAM);
    1.33  }
    1.34  
    1.35  
    1.36 @@ -92,6 +100,15 @@
    1.37  	char* c;
    1.38  	struct size sizeopt, sizeimg, sizedest;
    1.39  
    1.40 +	if (argc == 2 && strcmp(argv[1], "--version") == 0) {
    1.41 +		version();
    1.42 +		exit(0);
    1.43 +	}
    1.44 +	if (argc == 2 && strcmp(argv[1], "--help") == 0) {
    1.45 +		version();
    1.46 +		usage();
    1.47 +		exit(0);
    1.48 +	}
    1.49  	if (argc < 3) {
    1.50  		usage();
    1.51  		exit(1);
    1.52 @@ -100,7 +117,7 @@
    1.53  	if (!validsize(argv[1])) {
    1.54  		fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
    1.55  		usage();
    1.56 -		exit(3);
    1.57 +		exit(2);
    1.58  	}
    1.59  
    1.60  	/* parse width and height */
    1.61 @@ -151,6 +168,12 @@
    1.62  		sizeimg.h = im_in->sy;
    1.63  		sizedest = calcsize(sizeopt, sizeimg, 0);
    1.64  
    1.65 +		/* skip images that dont need to be resized */
    1.66 +		if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) {
    1.67 +			gdImageDestroy(im_in);
    1.68 +			continue;
    1.69 +		}
    1.70 +
    1.71  		/* copy-resize the image */
    1.72  		im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
    1.73  		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);