resize-gd

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, 75 insertions(+), 35 deletions(-) [+]
line diff
     1.1 --- a/resize-gd.c	Sat Jun 14 09:08:05 2008 +0200
     1.2 +++ b/resize-gd.c	Sat Jun 14 10:15:07 2008 +0200
     1.3 @@ -23,8 +23,66 @@
     1.4  	Jpg,
     1.5  };
     1.6  
     1.7 +struct size {
     1.8 +	int w;
     1.9 +	int h;
    1.10 +};
    1.11  
    1.12 -int main(int argc, char* argv[]) {
    1.13 +
    1.14 +void
    1.15 +usage() {
    1.16 +	puts("\
    1.17 +usage: resize-gd <size> <imagefiles> [...]\n\
    1.18 +         (keeps aspect ratio, does not enlarge images)\n\
    1.19 +       resize-gd <width>x<height> <imagefiles> [...]\n\
    1.20 +         (resizes to that size, enlarges if needed)\n\
    1.21 +");
    1.22 +}
    1.23 +
    1.24 +
    1.25 +struct size
    1.26 +calcsize(struct size opt, struct size img, int enlarge) {
    1.27 +	struct size result;
    1.28 +
    1.29 +	if (opt.h <= 0) {
    1.30 +		/* keep aspect ratio */
    1.31 +		if (!enlarge && opt.w > img.w && opt.w > img.h) {
    1.32 +			opt.w = (img.w > img.h) ? img.w : img.h;
    1.33 +		}
    1.34 +		if (img.w > img.h) {
    1.35 +			result.w = opt.w;
    1.36 +			result.h = opt.w * (1.0 * img.h / img.w);
    1.37 +		} else {
    1.38 +			result.h = opt.w;
    1.39 +			result.w = opt.w * (1.0 * img.w / img.h);
    1.40 +		}
    1.41 +	} else {
    1.42 +		result = opt;
    1.43 +	}
    1.44 +	return result;
    1.45 +}
    1.46 +
    1.47 +
    1.48 +int
    1.49 +validsize(char* sp) {
    1.50 +	while (isdigit(*sp)) {
    1.51 +		sp++;
    1.52 +	}
    1.53 +	if (*sp == 'x' && isdigit(*(sp+1))) {
    1.54 +		sp++;
    1.55 +		while (isdigit(*sp)) {
    1.56 +			sp++;
    1.57 +		}
    1.58 +	}
    1.59 +	if (*sp != '\0') {
    1.60 +		return 0;
    1.61 +	}
    1.62 +	return 1;
    1.63 +}
    1.64 +
    1.65 +
    1.66 +int
    1.67 +main(int argc, char* argv[]) {
    1.68  	int i;
    1.69  	int w, h;
    1.70  	int width, height;
    1.71 @@ -34,36 +92,27 @@
    1.72  	gdImagePtr im_out;
    1.73  	FILE* in;
    1.74  	FILE* out;
    1.75 -	char* sep;
    1.76 +	char* c;
    1.77 +	struct size sizeopt, sizeimg, sizedest;
    1.78  
    1.79  	if (argc < 3) {
    1.80 -		puts("usage: resize-gd <width>x<height> <imagefiles>\n\
    1.81 -       resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
    1.82 +		usage();
    1.83  		exit(1);
    1.84  	}
    1.85  
    1.86 -	/* parse width and height */
    1.87 -	sep = argv[1];
    1.88 -	while (isdigit(*sep)) {
    1.89 -		sep++;
    1.90 -	}
    1.91 -	if (*sep == 'x' && isdigit(*(sep+1))) {
    1.92 -		sep++;
    1.93 -		while (isdigit(*sep)) {
    1.94 -			sep++;
    1.95 -		}
    1.96 -	}
    1.97 -	if (*sep != '\0') {
    1.98 +	if (!validsize(argv[1])) {
    1.99  		fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
   1.100 +		usage();
   1.101  		exit(3);
   1.102  	}
   1.103  
   1.104 -	width = atoi(argv[1]);
   1.105 -	sep = strstr(argv[1], "x");
   1.106 -	if (sep && sep[1] != '\0') {
   1.107 -		height = atoi(sep + 1);
   1.108 +	/* parse width and height */
   1.109 +	sizeopt.w = atoi(argv[1]);
   1.110 +	c = strstr(argv[1], "x");
   1.111 +	if (c && c[1] != '\0') {
   1.112 +		sizeopt.h = atoi(c + 1);
   1.113  	} else {
   1.114 -		height = -1;  /* keep aspect ratio */
   1.115 +		sizeopt.h = -1;  /* keep aspect ratio */
   1.116  	}
   1.117  
   1.118  
   1.119 @@ -100,22 +149,13 @@
   1.120  
   1.121  		fclose(in);
   1.122  
   1.123 -		/* for keeping aspect ratio */
   1.124 -		if (height <= 0) {
   1.125 -			if (im_in->sx > im_in->sy) {
   1.126 -				w = width;
   1.127 -				h = width * (1.0 * im_in->sy / im_in->sx);
   1.128 -			} else {
   1.129 -				h = width;
   1.130 -				w = width * (1.0 * im_in->sx / im_in->sy);
   1.131 -			}
   1.132 -		} else {
   1.133 -			w = width;
   1.134 -			h = height;
   1.135 -		}
   1.136 +		/* calculate target size */
   1.137 +		sizeimg.w = im_in->sx;
   1.138 +		sizeimg.h = im_in->sy;
   1.139 +		sizedest = calcsize(sizeopt, sizeimg, 0);
   1.140  
   1.141  		/* copy-resize the image */
   1.142 -		im_out = gdImageCreateTrueColor(w, h);
   1.143 +		im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
   1.144  		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
   1.145  
   1.146  		/* write image */