resize-gd

changeset 0:8c94239b3b3f

initial commit (provides basic funcionality)
author meillo@localhost.localdomain
date Fri, 13 Jun 2008 14:18:52 +0200
parents
children 7a8f72b27dc3
files Makefile resize-gd.c
diffstat 2 files changed, 78 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Makefile	Fri Jun 13 14:18:52 2008 +0200
     1.3 @@ -0,0 +1,2 @@
     1.4 +resize-gd: resize-gd.c
     1.5 +	gcc -o resize-gd -lgd -lpng -lz -ljpeg  -lm resize-gd.c
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/resize-gd.c	Fri Jun 13 14:18:52 2008 +0200
     2.3 @@ -0,0 +1,76 @@
     2.4 +/* compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c  */
     2.5 +
     2.6 +#include <stdio.h>
     2.7 +#include <stdlib.h>  /* for atoi() */
     2.8 +#include <string.h>
     2.9 +#include "gd.h" /* Bring in the gd library functions */
    2.10 +
    2.11 +
    2.12 +enum {
    2.13 +	Png,
    2.14 +	Jpg,
    2.15 +};
    2.16 +
    2.17 +
    2.18 +int main(int argc, char* argv[]) {
    2.19 +	int i;
    2.20 +	int w, h;
    2.21 +	int x, y;
    2.22 +	int type;
    2.23 +	gdImagePtr im_in;
    2.24 +	gdImagePtr im_out;
    2.25 +	FILE* in;
    2.26 +	FILE* out;
    2.27 +
    2.28 +	if (argc < 3) {
    2.29 +		puts("usage: resize-gd <width>x<height> <imagefiles>");
    2.30 +		exit(1);
    2.31 +	}
    2.32 +
    2.33 +	/* parse width and height */
    2.34 +	w = atoi(argv[1]);
    2.35 +	h = atoi(strstr(argv[1], "x") + 1);
    2.36 +	printf("w: %d  h: %d\n", w, h);
    2.37 +
    2.38 +	/* process images */
    2.39 +	for (i = 2; i < argc; i++) {
    2.40 +		printf("processing file '%s'\n", argv[i]);
    2.41 +
    2.42 +		if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) {
    2.43 +			type = Png;
    2.44 +		} else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) {
    2.45 +			type = Jpg;
    2.46 +		} else {
    2.47 +			fprintf(stderr, "unknown filetype\n");
    2.48 +			exit(2);
    2.49 +		}
    2.50 +
    2.51 +		/* Load a large png to shrink in the smaller one */
    2.52 +		in = fopen(argv[i], "rb");
    2.53 +		if (type == Png) {
    2.54 +			im_in = gdImageCreateFromPng(in);
    2.55 +		} else if (type == Jpg) {
    2.56 +			im_in = gdImageCreateFromJpeg(in);
    2.57 +		}
    2.58 +		fclose(in);
    2.59 +
    2.60 +		im_out = gdImageCreateTrueColor(w, h);
    2.61 +
    2.62 +		/* Now copy the large image */
    2.63 +		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
    2.64 +
    2.65 +		/* write out */
    2.66 +		out = fopen(argv[i], "wb");
    2.67 +		if (type == Png) {
    2.68 +			gdImagePng(im_out, out);
    2.69 +		} else if (type == Jpg) {
    2.70 +			gdImageJpeg(im_out, out, -1);
    2.71 +		}
    2.72 +		fclose(out);
    2.73 +
    2.74 +		gdImageDestroy(im_in);
    2.75 +		gdImageDestroy(im_out);
    2.76 +	}
    2.77 +
    2.78 +	return 0;
    2.79 +}