comparison resize-gd.c @ 2:8e71b54b6e1e

made separate functions for calcsize and usage; pictures are not enlarged by default
author meillo@marmaro.de
date Sat, 14 Jun 2008 10:15:07 +0200
parents 7a8f72b27dc3
children 35a50e57b6f5
comparison
equal deleted inserted replaced
1:7a8f72b27dc3 2:8e71b54b6e1e
21 enum { 21 enum {
22 Png, 22 Png,
23 Jpg, 23 Jpg,
24 }; 24 };
25 25
26 struct size {
27 int w;
28 int h;
29 };
26 30
27 int main(int argc, char* argv[]) { 31
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 }
41
42
43 struct size
44 calcsize(struct size opt, struct size img, int enlarge) {
45 struct size result;
46
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 }
64
65
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 }
82
83
84 int
85 main(int argc, char* argv[]) {
28 int i; 86 int i;
29 int w, h; 87 int w, h;
30 int width, height; 88 int width, height;
31 int x, y; 89 int x, y;
32 int type; 90 int type;
33 gdImagePtr im_in; 91 gdImagePtr im_in;
34 gdImagePtr im_out; 92 gdImagePtr im_out;
35 FILE* in; 93 FILE* in;
36 FILE* out; 94 FILE* out;
37 char* sep; 95 char* c;
96 struct size sizeopt, sizeimg, sizedest;
38 97
39 if (argc < 3) { 98 if (argc < 3) {
40 puts("usage: resize-gd <width>x<height> <imagefiles>\n\ 99 usage();
41 resize-gd <size> <imagefiles> (keeps aspect ratio)\n");
42 exit(1); 100 exit(1);
43 } 101 }
44 102
45 /* parse width and height */ 103 if (!validsize(argv[1])) {
46 sep = argv[1];
47 while (isdigit(*sep)) {
48 sep++;
49 }
50 if (*sep == 'x' && isdigit(*(sep+1))) {
51 sep++;
52 while (isdigit(*sep)) {
53 sep++;
54 }
55 }
56 if (*sep != '\0') {
57 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n"); 104 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
105 usage();
58 exit(3); 106 exit(3);
59 } 107 }
60 108
61 width = atoi(argv[1]); 109 /* parse width and height */
62 sep = strstr(argv[1], "x"); 110 sizeopt.w = atoi(argv[1]);
63 if (sep && sep[1] != '\0') { 111 c = strstr(argv[1], "x");
64 height = atoi(sep + 1); 112 if (c && c[1] != '\0') {
113 sizeopt.h = atoi(c + 1);
65 } else { 114 } else {
66 height = -1; /* keep aspect ratio */ 115 sizeopt.h = -1; /* keep aspect ratio */
67 } 116 }
68 117
69 118
70 /* process images */ 119 /* process images */
71 for (i = 2; i < argc; i++) { 120 for (i = 2; i < argc; i++) {
98 } 147 }
99 */ 148 */
100 149
101 fclose(in); 150 fclose(in);
102 151
103 /* for keeping aspect ratio */ 152 /* calculate target size */
104 if (height <= 0) { 153 sizeimg.w = im_in->sx;
105 if (im_in->sx > im_in->sy) { 154 sizeimg.h = im_in->sy;
106 w = width; 155 sizedest = calcsize(sizeopt, sizeimg, 0);
107 h = width * (1.0 * im_in->sy / im_in->sx);
108 } else {
109 h = width;
110 w = width * (1.0 * im_in->sx / im_in->sy);
111 }
112 } else {
113 w = width;
114 h = height;
115 }
116 156
117 /* copy-resize the image */ 157 /* copy-resize the image */
118 im_out = gdImageCreateTrueColor(w, h); 158 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
119 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); 159 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
120 160
121 /* write image */ 161 /* write image */
122 out = fopen(argv[i], "wb"); 162 out = fopen(argv[i], "wb");
123 if (type == Png) { 163 if (type == Png) {