comparison resize-gd.c @ 5:8e2804fe30bc

skipps now too small images; added --version and --help
author meillo@marmaro.de
date Sat, 14 Jun 2008 12:18:06 +0200
parents 35a50e57b6f5
children c0045d8d3ce2
comparison
equal deleted inserted replaced
4:aa24986b8969 5:8e2804fe30bc
15 #include <stdlib.h> /* for atoi() */ 15 #include <stdlib.h> /* for atoi() */
16 #include <string.h> 16 #include <string.h>
17 #include <ctype.h> 17 #include <ctype.h>
18 #include "gd.h" /* Bring in the gd library functions */ 18 #include "gd.h" /* Bring in the gd library functions */
19 19
20 #define PROGRAM "resize-gd"
21 #define VERSION "0.1"
20 22
21 enum { 23 enum {
22 Png, 24 Png,
23 Jpg 25 Jpg
24 }; 26 };
28 int h; 30 int h;
29 }; 31 };
30 32
31 33
32 void 34 void
35 version() {
36 printf("%s version: %s\n", PROGRAM, VERSION);
37 }
38
39
40 void
33 usage() { 41 usage() {
34 puts("\ 42 printf("\
35 usage: resize-gd <size> <imagefiles> [...]\n\ 43 usage: %s <size> <imagefiles> [...]\n\
36 (keeps aspect ratio, does not enlarge images)\n\ 44 (keeps aspect ratio, does not enlarge images)\n\
37 resize-gd <width>x<height> <imagefiles> [...]\n\ 45 %s <width>x<height> <imagefiles> [...]\n\
38 (resizes to that size, enlarges if needed)\n\ 46 (resizes to that size, enlarges if needed)\n\
39 "); 47 ", PROGRAM, PROGRAM);
40 } 48 }
41 49
42 50
43 struct size 51 struct size
44 calcsize(struct size opt, struct size img, int enlarge) { 52 calcsize(struct size opt, struct size img, int enlarge) {
90 FILE* in; 98 FILE* in;
91 FILE* out; 99 FILE* out;
92 char* c; 100 char* c;
93 struct size sizeopt, sizeimg, sizedest; 101 struct size sizeopt, sizeimg, sizedest;
94 102
103 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
104 version();
105 exit(0);
106 }
107 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
108 version();
109 usage();
110 exit(0);
111 }
95 if (argc < 3) { 112 if (argc < 3) {
96 usage(); 113 usage();
97 exit(1); 114 exit(1);
98 } 115 }
99 116
100 if (!validsize(argv[1])) { 117 if (!validsize(argv[1])) {
101 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n"); 118 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
102 usage(); 119 usage();
103 exit(3); 120 exit(2);
104 } 121 }
105 122
106 /* parse width and height */ 123 /* parse width and height */
107 sizeopt.w = atoi(argv[1]); 124 sizeopt.w = atoi(argv[1]);
108 c = strstr(argv[1], "x"); 125 c = strstr(argv[1], "x");
149 /* calculate target size */ 166 /* calculate target size */
150 sizeimg.w = im_in->sx; 167 sizeimg.w = im_in->sx;
151 sizeimg.h = im_in->sy; 168 sizeimg.h = im_in->sy;
152 sizedest = calcsize(sizeopt, sizeimg, 0); 169 sizedest = calcsize(sizeopt, sizeimg, 0);
153 170
171 /* skip images that dont need to be resized */
172 if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) {
173 gdImageDestroy(im_in);
174 continue;
175 }
176
154 /* copy-resize the image */ 177 /* copy-resize the image */
155 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); 178 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); 179 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
157 180
158 /* write image */ 181 /* write image */