resize-gd

view resize-gd.c @ 16:9674ada28cfe

Added tag 0.2 for changeset c50716420346
author meillo@marmaro.de
date Wed, 25 Jun 2008 15:03:00 +0200
parents 6f5c3a02e4d5
children 159402d54c29
line source
1 /*
2 * resize-gd - resizes images using the gd-library
3 *
4 * Copyright 2008 by markus schnalke <meillo@marmaro.de>
5 *
6 * build-depends: libgd2-noxpm-dev | libgd2-dev
7 * depends: libgd2-noxpm | libgd2-xpm
8 */
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include <ctype.h>
14 #include "gd.h"
16 #define PROGRAM "resize-gd"
17 #define VERSION "0.2"
19 enum {
20 Png,
21 Jpg
22 };
24 struct size {
25 int w;
26 int h;
27 };
30 void
31 version() {
32 printf("%s version: %s\n", PROGRAM, VERSION);
33 }
36 void
37 usage() {
38 printf("\
39 usage: %s <size> <imagefiles> [...]\n\
40 (keeps aspect ratio, does not enlarge images)\n\
41 %s <width>x<height> <imagefiles> [...]\n\
42 (resizes to that size, enlarges if needed)\n\
43 ", PROGRAM, PROGRAM);
44 }
47 struct size
48 calcsize(struct size opt, struct size img, int enlarge) {
49 struct size result;
51 if (opt.h <= 0) {
52 /* keep aspect ratio */
53 if (!enlarge && opt.w > img.w && opt.w > img.h) {
54 opt.w = (img.w > img.h) ? img.w : img.h;
55 }
56 if (img.w > img.h) {
57 result.w = opt.w;
58 result.h = opt.w * (1.0 * img.h / img.w);
59 } else {
60 result.h = opt.w;
61 result.w = opt.w * (1.0 * img.w / img.h);
62 }
63 } else {
64 result = opt;
65 }
66 return result;
67 }
70 int
71 validsize(char* sp) {
72 while (isdigit(*sp)) {
73 sp++;
74 }
75 if (*sp == 'x' && isdigit(*(sp+1))) {
76 sp++;
77 while (isdigit(*sp)) {
78 sp++;
79 }
80 }
81 if (*sp != '\0') {
82 return 0;
83 }
84 return 1;
85 }
88 int
89 main(int argc, char* argv[]) {
90 int i;
91 int type;
92 int success = 0;
93 gdImagePtr im_in;
94 gdImagePtr im_out;
95 FILE* in;
96 FILE* out;
97 char* c;
98 struct size sizeopt, sizeimg, sizedest;
100 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
101 version();
102 exit(0);
103 }
104 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
105 version();
106 usage();
107 exit(0);
108 }
109 if (argc < 3) {
110 usage();
111 exit(1);
112 }
114 if (!validsize(argv[1])) {
115 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
116 usage();
117 exit(2);
118 }
120 /* parse width and height */
121 sizeopt.w = atoi(argv[1]);
122 c = strchr(argv[1], 'x');
123 if (c && c[1] != '\0') {
124 sizeopt.h = atoi(c + 1);
125 } else {
126 sizeopt.h = -1; /* keep aspect ratio */
127 }
130 /* process images */
131 for (i = 2; i < argc; i++) {
132 /* printf("processing file '%s'\n", argv[i]); */
134 c = strrchr(argv[i], '.');
135 if (strcmp(c, ".png") == 0 || strcmp(c, ".PNG") == 0) {
136 type = Png;
137 } else if (strcmp(c, ".jpg") == 0 || strcmp(c, ".JPG") == 0 || strcmp(c, ".jpeg") == 0 || strcmp(c, ".JPEG") == 0) {
138 type = Jpg;
139 } else {
140 fprintf(stderr, "'%s' has unknown filetype. Filename must end with '.jpg', '.jpeg' or '.png'.\n", argv[i]);
141 continue;
142 }
144 /* load image */
145 in = fopen(argv[i], "rb");
146 if (in == NULL) {
147 fprintf(stderr, "unable to open '%s' for reading\n", argv[i]);
148 continue;
149 }
150 if (type == Png) {
151 im_in = gdImageCreateFromPng(in);
152 } else if (type == Jpg) {
153 im_in = gdImageCreateFromJpeg(in);
154 }
155 if (im_in == NULL) {
156 fprintf(stderr, "unable to load image '%s'\n", argv[i]);
157 continue;
158 }
159 fclose(in);
161 /* calculate target size */
162 sizeimg.w = im_in->sx;
163 sizeimg.h = im_in->sy;
164 sizedest = calcsize(sizeopt, sizeimg, 0);
166 /* skip images that dont need to be resized */
167 if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) {
168 success++;
169 gdImageDestroy(im_in);
170 continue;
171 }
173 /* copy-resize the image */
174 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
175 if (im_out == NULL) {
176 fprintf(stderr, "unable to allocate memory for resized image\n");
177 exit(3);
178 }
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 (out == NULL) {
184 fprintf(stderr, "unable to open '%s' for writing\n", argv[i]);
185 continue;
186 }
187 if (type == Png) {
188 gdImagePng(im_out, out);
189 } else if (type == Jpg) {
190 gdImageJpeg(im_out, out, -1);
191 }
192 fclose(out);
194 success++;
196 gdImageDestroy(im_in);
197 gdImageDestroy(im_out);
198 }
200 if (success == 0) {
201 return 4; /* no resizing was successful */
202 } else {
203 return 0;
204 }
205 }