resize-gd

diff resize-gd.c @ 0:8c94239b3b3f

initial commit (provides basic funcionality)
author meillo@localhost.localdomain
date Fri, 13 Jun 2008 14:18:52 +0200
parents
children 7a8f72b27dc3
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/resize-gd.c	Fri Jun 13 14:18:52 2008 +0200
     1.3 @@ -0,0 +1,76 @@
     1.4 +/* compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c  */
     1.5 +
     1.6 +#include <stdio.h>
     1.7 +#include <stdlib.h>  /* for atoi() */
     1.8 +#include <string.h>
     1.9 +#include "gd.h" /* Bring in the gd library functions */
    1.10 +
    1.11 +
    1.12 +enum {
    1.13 +	Png,
    1.14 +	Jpg,
    1.15 +};
    1.16 +
    1.17 +
    1.18 +int main(int argc, char* argv[]) {
    1.19 +	int i;
    1.20 +	int w, h;
    1.21 +	int x, y;
    1.22 +	int type;
    1.23 +	gdImagePtr im_in;
    1.24 +	gdImagePtr im_out;
    1.25 +	FILE* in;
    1.26 +	FILE* out;
    1.27 +
    1.28 +	if (argc < 3) {
    1.29 +		puts("usage: resize-gd <width>x<height> <imagefiles>");
    1.30 +		exit(1);
    1.31 +	}
    1.32 +
    1.33 +	/* parse width and height */
    1.34 +	w = atoi(argv[1]);
    1.35 +	h = atoi(strstr(argv[1], "x") + 1);
    1.36 +	printf("w: %d  h: %d\n", w, h);
    1.37 +
    1.38 +	/* process images */
    1.39 +	for (i = 2; i < argc; i++) {
    1.40 +		printf("processing file '%s'\n", argv[i]);
    1.41 +
    1.42 +		if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) {
    1.43 +			type = Png;
    1.44 +		} else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) {
    1.45 +			type = Jpg;
    1.46 +		} else {
    1.47 +			fprintf(stderr, "unknown filetype\n");
    1.48 +			exit(2);
    1.49 +		}
    1.50 +
    1.51 +		/* Load a large png to shrink in the smaller one */
    1.52 +		in = fopen(argv[i], "rb");
    1.53 +		if (type == Png) {
    1.54 +			im_in = gdImageCreateFromPng(in);
    1.55 +		} else if (type == Jpg) {
    1.56 +			im_in = gdImageCreateFromJpeg(in);
    1.57 +		}
    1.58 +		fclose(in);
    1.59 +
    1.60 +		im_out = gdImageCreateTrueColor(w, h);
    1.61 +
    1.62 +		/* Now copy the large image */
    1.63 +		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
    1.64 +
    1.65 +		/* write out */
    1.66 +		out = fopen(argv[i], "wb");
    1.67 +		if (type == Png) {
    1.68 +			gdImagePng(im_out, out);
    1.69 +		} else if (type == Jpg) {
    1.70 +			gdImageJpeg(im_out, out, -1);
    1.71 +		}
    1.72 +		fclose(out);
    1.73 +
    1.74 +		gdImageDestroy(im_in);
    1.75 +		gdImageDestroy(im_out);
    1.76 +	}
    1.77 +
    1.78 +	return 0;
    1.79 +}