resize-gd
view resize-gd.c @ 8:6f0826fdec50
Added tag 0.1 for changeset c0045d8d3ce21d3a33e789ac68283168a72a613e
author | meillo@marmaro.de |
---|---|
date | Sat, 14 Jun 2008 12:33:51 +0200 |
parents | 8e2804fe30bc |
children | 6b8e8fcd6d4d |
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.1"
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 gdImagePtr im_in;
93 gdImagePtr im_out;
94 FILE* in;
95 FILE* out;
96 char* c;
97 struct size sizeopt, sizeimg, sizedest;
99 if (argc == 2 && strcmp(argv[1], "--version") == 0) {
100 version();
101 exit(0);
102 }
103 if (argc == 2 && strcmp(argv[1], "--help") == 0) {
104 version();
105 usage();
106 exit(0);
107 }
108 if (argc < 3) {
109 usage();
110 exit(1);
111 }
113 if (!validsize(argv[1])) {
114 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n");
115 usage();
116 exit(2);
117 }
119 /* parse width and height */
120 sizeopt.w = atoi(argv[1]);
121 c = strstr(argv[1], "x");
122 if (c && c[1] != '\0') {
123 sizeopt.h = atoi(c + 1);
124 } else {
125 sizeopt.h = -1; /* keep aspect ratio */
126 }
129 /* process images */
130 for (i = 2; i < argc; i++) {
131 printf("processing file '%s'\n", argv[i]);
133 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
134 type = Png;
135 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) {
136 type = Jpg;
137 } else {
138 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]);
139 continue;
140 }
142 /* load image */
143 in = fopen(argv[i], "rb");
144 if (type == Png) {
145 im_in = gdImageCreateFromPng(in);
146 } else if (type == Jpg) {
147 im_in = gdImageCreateFromJpeg(in);
148 }
149 fclose(in);
151 /* calculate target size */
152 sizeimg.w = im_in->sx;
153 sizeimg.h = im_in->sy;
154 sizedest = calcsize(sizeopt, sizeimg, 0);
156 /* skip images that dont need to be resized */
157 if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) {
158 gdImageDestroy(im_in);
159 continue;
160 }
162 /* copy-resize the image */
163 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
164 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
166 /* write image */
167 out = fopen(argv[i], "wb");
168 if (type == Png) {
169 gdImagePng(im_out, out);
170 } else if (type == Jpg) {
171 gdImageJpeg(im_out, out, -1);
172 }
173 fclose(out);
175 gdImageDestroy(im_in);
176 gdImageDestroy(im_out);
177 }
179 return 0;
180 }