resize-gd

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 source
1 /* compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c */
3 #include <stdio.h>
4 #include <stdlib.h> /* for atoi() */
5 #include <string.h>
6 #include "gd.h" /* Bring in the gd library functions */
9 enum {
10 Png,
11 Jpg,
12 };
15 int main(int argc, char* argv[]) {
16 int i;
17 int w, h;
18 int x, y;
19 int type;
20 gdImagePtr im_in;
21 gdImagePtr im_out;
22 FILE* in;
23 FILE* out;
25 if (argc < 3) {
26 puts("usage: resize-gd <width>x<height> <imagefiles>");
27 exit(1);
28 }
30 /* parse width and height */
31 w = atoi(argv[1]);
32 h = atoi(strstr(argv[1], "x") + 1);
33 printf("w: %d h: %d\n", w, h);
35 /* process images */
36 for (i = 2; i < argc; i++) {
37 printf("processing file '%s'\n", argv[i]);
39 if (strcmp(&(argv[i][strlen(argv[i])-4]), ".png") == 0) {
40 type = Png;
41 } else if (strcmp(&(argv[i][strlen(argv[i])-4]), ".jpg") == 0) {
42 type = Jpg;
43 } else {
44 fprintf(stderr, "unknown filetype\n");
45 exit(2);
46 }
48 /* Load a large png to shrink in the smaller one */
49 in = fopen(argv[i], "rb");
50 if (type == Png) {
51 im_in = gdImageCreateFromPng(in);
52 } else if (type == Jpg) {
53 im_in = gdImageCreateFromJpeg(in);
54 }
55 fclose(in);
57 im_out = gdImageCreateTrueColor(w, h);
59 /* Now copy the large image */
60 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
62 /* write out */
63 out = fopen(argv[i], "wb");
64 if (type == Png) {
65 gdImagePng(im_out, out);
66 } else if (type == Jpg) {
67 gdImageJpeg(im_out, out, -1);
68 }
69 fclose(out);
71 gdImageDestroy(im_in);
72 gdImageDestroy(im_out);
73 }
75 return 0;
76 }