view 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 wrap: on
line source

/* compile with: gcc -lgd -lpng -lz -ljpeg  -lm resize-gd.c  */

#include <stdio.h>
#include <stdlib.h>  /* for atoi() */
#include <string.h>
#include "gd.h" /* Bring in the gd library functions */


enum {
	Png,
	Jpg,
};


int main(int argc, char* argv[]) {
	int i;
	int w, h;
	int x, y;
	int type;
	gdImagePtr im_in;
	gdImagePtr im_out;
	FILE* in;
	FILE* out;

	if (argc < 3) {
		puts("usage: resize-gd <width>x<height> <imagefiles>");
		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);

	/* 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) {
			type = Png;
		} else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) {
			type = Jpg;
		} else {
			fprintf(stderr, "unknown filetype\n");
			exit(2);
		}

		/* 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);
		}
		fclose(in);

		im_out = gdImageCreateTrueColor(w, h);

		/* Now copy the large image */
		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);

		/* write out */
		out = fopen(argv[i], "wb");
		if (type == Png) {
			gdImagePng(im_out, out);
		} else if (type == Jpg) {
			gdImageJpeg(im_out, out, -1);
		}
		fclose(out);

		gdImageDestroy(im_in);
		gdImageDestroy(im_out);
	}

	return 0;
}