resize-gd

view 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
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 w, h;
88 int width, height;
89 int x, y;
90 int type;
91 gdImagePtr im_in;
92 gdImagePtr im_out;
93 FILE* in;
94 FILE* out;
95 char* c;
96 struct size sizeopt, sizeimg, sizedest;
98 if (argc < 3) {
99 usage();
100 exit(1);
101 }
103 if (!validsize(argv[1])) {
104 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
105 usage();
106 exit(3);
107 }
109 /* parse width and height */
110 sizeopt.w = atoi(argv[1]);
111 c = strstr(argv[1], "x");
112 if (c && c[1] != '\0') {
113 sizeopt.h = atoi(c + 1);
114 } else {
115 sizeopt.h = -1; /* keep aspect ratio */
116 }
119 /* process images */
120 for (i = 2; i < argc; i++) {
121 printf("processing file '%s'\n", argv[i]);
123 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
124 type = Png;
125 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
126 type = Jpg;
127 } else {
128 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
129 continue;
130 }
131 /* load image */
132 in = fopen(argv[i], "rb");
134 if (type == Png) {
135 im_in = gdImageCreateFromPng(in);
136 } else if (type == Jpg) {
137 im_in = gdImageCreateFromJpeg(in);
138 }
139 /*
140 if ((im_in = gdImageCreateFromPng(in)) != NULL) {
141 type = Png;
142 } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) {
143 type = Jpg;
144 } else {
145 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
146 continue;
147 }
148 */
150 fclose(in);
152 /* calculate target size */
153 sizeimg.w = im_in->sx;
154 sizeimg.h = im_in->sy;
155 sizedest = calcsize(sizeopt, sizeimg, 0);
157 /* copy-resize the image */
158 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
159 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
161 /* write image */
162 out = fopen(argv[i], "wb");
163 if (type == Png) {
164 gdImagePng(im_out, out);
165 } else if (type == Jpg) {
166 gdImageJpeg(im_out, out, -1);
167 }
168 fclose(out);
170 gdImageDestroy(im_in);
171 gdImageDestroy(im_out);
172 }
174 return 0;
175 }