Mercurial > resize-gd
annotate 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 |
rev | line source |
---|---|
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
1 /* |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
2 * compile with: gcc -lgd -lpng -lz -ljpeg -lm resize-gd.c |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
3 * build-depends: libgd2-noxpm-dev | libgd2-dev |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
4 * depends: libgd2-noxpm | libgd2-xpm |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
5 * |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
6 * |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
7 * |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
8 * http://www.libgd.org/ImageCreation |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
9 * http://cpan.uwinnipeg.ca/htdocs/Image-Resize/Image/Resize.pm.html |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
10 * http://netpbm.sourceforge.net/ |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
11 * |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
12 */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
13 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
14 #include <stdio.h> |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
15 #include <stdlib.h> /* for atoi() */ |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
16 #include <string.h> |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
17 #include <ctype.h> |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
18 #include "gd.h" /* Bring in the gd library functions */ |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
19 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
20 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
21 enum { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
22 Png, |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
23 Jpg, |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
24 }; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
25 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
26 struct size { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
27 int w; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
28 int h; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
29 }; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
30 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
31 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
32 void |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
33 usage() { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
34 puts("\ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
35 usage: resize-gd <size> <imagefiles> [...]\n\ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
36 (keeps aspect ratio, does not enlarge images)\n\ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
37 resize-gd <width>x<height> <imagefiles> [...]\n\ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
38 (resizes to that size, enlarges if needed)\n\ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
39 "); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
40 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
41 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
42 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
43 struct size |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
44 calcsize(struct size opt, struct size img, int enlarge) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
45 struct size result; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
46 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
47 if (opt.h <= 0) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
48 /* keep aspect ratio */ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
49 if (!enlarge && opt.w > img.w && opt.w > img.h) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
50 opt.w = (img.w > img.h) ? img.w : img.h; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
51 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
52 if (img.w > img.h) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
53 result.w = opt.w; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
54 result.h = opt.w * (1.0 * img.h / img.w); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
55 } else { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
56 result.h = opt.w; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
57 result.w = opt.w * (1.0 * img.w / img.h); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
58 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
59 } else { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
60 result = opt; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
61 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
62 return result; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
63 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
64 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
65 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
66 int |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
67 validsize(char* sp) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
68 while (isdigit(*sp)) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
69 sp++; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
70 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
71 if (*sp == 'x' && isdigit(*(sp+1))) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
72 sp++; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
73 while (isdigit(*sp)) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
74 sp++; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
75 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
76 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
77 if (*sp != '\0') { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
78 return 0; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
79 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
80 return 1; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
81 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
82 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
83 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
84 int |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
85 main(int argc, char* argv[]) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
86 int i; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
87 int w, h; |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
88 int width, height; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
89 int x, y; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
90 int type; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
91 gdImagePtr im_in; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
92 gdImagePtr im_out; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
93 FILE* in; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
94 FILE* out; |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
95 char* c; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
96 struct size sizeopt, sizeimg, sizedest; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
97 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
98 if (argc < 3) { |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
99 usage(); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
100 exit(1); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
101 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
102 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
103 if (!validsize(argv[1])) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
104 fprintf(stderr, "Invalid form of size. Has to be <size> or <width>x<height>.\n"); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
105 usage(); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
106 exit(3); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
107 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
108 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
109 /* parse width and height */ |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
110 sizeopt.w = atoi(argv[1]); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
111 c = strstr(argv[1], "x"); |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
112 if (c && c[1] != '\0') { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
113 sizeopt.h = atoi(c + 1); |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
114 } else { |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
115 sizeopt.h = -1; /* keep aspect ratio */ |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
116 } |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
117 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
118 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
119 /* process images */ |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
120 for (i = 2; i < argc; i++) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
121 printf("processing file '%s'\n", argv[i]); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
122 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
123 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
124 type = Png; |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
125 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
126 type = Jpg; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
127 } else { |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
128 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
129 continue; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
130 } |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
131 /* load image */ |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
132 in = fopen(argv[i], "rb"); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
133 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
134 if (type == Png) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
135 im_in = gdImageCreateFromPng(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
136 } else if (type == Jpg) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
137 im_in = gdImageCreateFromJpeg(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
138 } |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
139 /* |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
140 if ((im_in = gdImageCreateFromPng(in)) != NULL) { |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
141 type = Png; |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
142 } else if ((im_in = gdImageCreateFromJpeg(in)) != NULL) { |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
143 type = Jpg; |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
144 } else { |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
145 fprintf(stderr, "'%s' has unknown filetype. Filename must end with (lowercase) '.png' or '.jpg'.\n", argv[i]); |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
146 continue; |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
147 } |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
148 */ |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
149 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
150 fclose(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
151 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
152 /* calculate target size */ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
153 sizeimg.w = im_in->sx; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
154 sizeimg.h = im_in->sy; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
155 sizedest = calcsize(sizeopt, sizeimg, 0); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
156 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
157 /* copy-resize the image */ |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
158 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
159 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
160 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
161 /* write image */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
162 out = fopen(argv[i], "wb"); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
163 if (type == Png) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
164 gdImagePng(im_out, out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
165 } else if (type == Jpg) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
166 gdImageJpeg(im_out, out, -1); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
167 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
168 fclose(out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
169 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
170 gdImageDestroy(im_in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
171 gdImageDestroy(im_out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
172 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
173 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
174 return 0; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
175 } |