resize-gd

view resize-gd.c @ 4:aa24986b8969

replaces simple Makefile with a good one
author meillo@marmaro.de
date Sat, 14 Jun 2008 12:00:23 +0200
parents 8e71b54b6e1e
children 8e2804fe30bc
line source
1 /*
2 * compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c
3 * build-depends: libgd2-noxpm-dev | libgd2-dev
4 * depends: libgd2-noxpm | libgd2-xpm
5 *
6 *
7 *
8 * http://www.libgd.org/ImageCreation
9 * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html
10 * http://netpbm.sourceforge.net/
11 *
12 */
14 #include <stdio.h>
15 #include <stdlib.h> /* for atoi() */
16 #include <string.h>
17 #include <ctype.h>
18 #include "gd.h" /* Bring in the gd library functions */
21 enum {
22 Png,
23 Jpg
24 };
26 struct size {
27 int w;
28 int h;
29 };
32 void
33 usage() {
34 puts("\
35 usage: resize-gd <size> <imagefiles> [...]\n\
36 (keeps aspect ratio, does not enlarge images)\n\
37 resize-gd <width>x<height> <imagefiles> [...]\n\
38 (resizes to that size, enlarges if needed)\n\
39 ");
40 }
43 struct size
44 calcsize(struct size opt, struct size img, int enlarge) {
45 struct size result;
47 if (opt.h <= 0) {
48 /* keep aspect ratio */
49 if (!enlarge && opt.w > img.w && opt.w > img.h) {
50 opt.w = (img.w > img.h) ? img.w : img.h;
51 }
52 if (img.w > img.h) {
53 result.w = opt.w;
54 result.h = opt.w * (1.0 * img.h / img.w);
55 } else {
56 result.h = opt.w;
57 result.w = opt.w * (1.0 * img.w / img.h);
58 }
59 } else {
60 result = opt;
61 }
62 return result;
63 }
66 int
67 validsize(char* sp) {
68 while (isdigit(*sp)) {
69 sp++;
70 }
71 if (*sp == 'x' && isdigit(*(sp+1))) {
72 sp++;
73 while (isdigit(*sp)) {
74 sp++;
75 }
76 }
77 if (*sp != '\0') {
78 return 0;
79 }
80 return 1;
81 }
84 int
85 main(int argc, char* argv[]) {
86 int i;
87 int type;
88 gdImagePtr im_in;
89 gdImagePtr im_out;
90 FILE* in;
91 FILE* out;
92 char* c;
93 struct size sizeopt, sizeimg, sizedest;
95 if (argc < 3) {
96 usage();
97 exit(1);
98 }
100 if (!validsize(argv[1])) {
101 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
102 usage();
103 exit(3);
104 }
106 /* parse width and height */
107 sizeopt.w = atoi(argv[1]);
108 c = strstr(argv[1], "x");
109 if (c && c[1] != '\0') {
110 sizeopt.h = atoi(c + 1);
111 } else {
112 sizeopt.h = -1; /* keep aspect ratio */
113 }
116 /* process images */
117 for (i = 2; i < argc; i++) {
118 printf("processing file '%s'\n", argv[i]);
120 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
121 type = Png;
122 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
123 type = Jpg;
124 } else {
125 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
126 continue;
127 }
128 /* load image */
129 in = fopen(argv[i], "rb");
131 if (type == Png) {
132 im_in = gdImageCreateFromPng(in);
133 } else if (type == Jpg) {
134 im_in = gdImageCreateFromJpeg(in);
135 }
136 /*
137 if ((im_in = gdImageCreateFromPng(in)) != NULL) {
138 type = Png;
139 } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) {
140 type = Jpg;
141 } else {
142 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
143 continue;
144 }
145 */
147 fclose(in);
149 /* calculate target size */
150 sizeimg.w = im_in->sx;
151 sizeimg.h = im_in->sy;
152 sizedest = calcsize(sizeopt, sizeimg, 0);
154 /* copy-resize the image */
155 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
156 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
158 /* write image */
159 out = fopen(argv[i], "wb");
160 if (type == Png) {
161 gdImagePng(im_out, out);
162 } else if (type == Jpg) {
163 gdImageJpeg(im_out, out, -1);
164 }
165 fclose(out);
167 gdImageDestroy(im_in);
168 gdImageDestroy(im_out);
169 }
171 return 0;
172 }