resize-gd
view resize-gd.c @ 6:e020f26c2c6a
added manpage
author | meillo@marmaro.de |
---|---|
date | Sat, 14 Jun 2008 12:19:35 +0200 |
parents | 35a50e57b6f5 |
children | c0045d8d3ce2 |
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 */
20 #define PROGRAM "resize-gd"
21 #define VERSION "0.1"
23 enum {
24 Png,
25 Jpg
26 };
28 struct size {
29 int w;
30 int h;
31 };
34 void
35 version() {
36 printf("%s version: %s\n", PROGRAM, VERSION);
37 }
40 void
41 usage() {
42 printf("\
43 usage: %s <size> <imagefiles> [...]\n\
44 (keeps aspect ratio, does not enlarge images)\n\
45 %s <width>x<height> <imagefiles> [...]\n\
46 (resizes to that size, enlarges if needed)\n\
47 ", PROGRAM, PROGRAM);
48 }
51 struct size
52 calcsize(struct size opt, struct size img, int enlarge) {
53 struct size result;
55 if (opt.h <= 0) {
56 /* keep aspect ratio */
57 if (!enlarge && opt.w > img.w && opt.w > img.h) {
58 opt.w = (img.w > img.h) ? img.w : img.h;
59 }
60 if (img.w > img.h) {
61 result.w = opt.w;
62 result.h = opt.w * (1.0 * img.h / img.w);
63 } else {
64 result.h = opt.w;
65 result.w = opt.w * (1.0 * img.w / img.h);
66 }
67 } else {
68 result = opt;
69 }
70 return result;
71 }
74 int
75 validsize(char* sp) {
76 while (isdigit(*sp)) {
77 sp++;
78 }
79 if (*sp == 'x' && isdigit(*(sp+1))) {
80 sp++;
81 while (isdigit(*sp)) {
82 sp++;
83 }
84 }
85 if (*sp != '\0') {
86 return 0;
87 }
88 return 1;
89 }
92 int
93 main(int argc, char* argv[]) {
94 int i;
95 int type;
96 gdImagePtr im_in;
97 gdImagePtr im_out;
98 FILE* in;
99 FILE* out;
100 char* c;
101 struct size sizeopt, sizeimg, sizedest;
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 }
112 if (argc < 3) {
113 usage();
114 exit(1);
115 }
117 if (!validsize(argv[1])) {
118 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
119 usage();
120 exit(2);
121 }
123 /* parse width and height */
124 sizeopt.w = atoi(argv[1]);
125 c = strstr(argv[1], "x");
126 if (c && c[1] != '\0') {
127 sizeopt.h = atoi(c + 1);
128 } else {
129 sizeopt.h = -1; /* keep aspect ratio */
130 }
133 /* process images */
134 for (i = 2; i < argc; i++) {
135 printf("processing file '%s'\n", argv[i]);
137 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
138 type = Png;
139 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
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 /* load image */
146 in = fopen(argv[i], "rb");
148 if (type == Png) {
149 im_in = gdImageCreateFromPng(in);
150 } else if (type == Jpg) {
151 im_in = gdImageCreateFromJpeg(in);
152 }
153 /*
154 if ((im_in = gdImageCreateFromPng(in)) != NULL) {
155 type = Png;
156 } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) {
157 type = Jpg;
158 } else {
159 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
160 continue;
161 }
162 */
164 fclose(in);
166 /* calculate target size */
167 sizeimg.w = im_in->sx;
168 sizeimg.h = im_in->sy;
169 sizedest = calcsize(sizeopt, sizeimg, 0);
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 }
177 /* copy-resize the image */
178 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
179 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
181 /* write image */
182 out = fopen(argv[i], "wb");
183 if (type == Png) {
184 gdImagePng(im_out, out);
185 } else if (type == Jpg) {
186 gdImageJpeg(im_out, out, -1);
187 }
188 fclose(out);
190 gdImageDestroy(im_in);
191 gdImageDestroy(im_out);
192 }
194 return 0;
195 }