Mercurial > resize-gd
annotate resize-gd.c @ 17:159402d54c29 default tip
replaced atoi with strtol, which is more secure
author | meillo@marmaro.de |
---|---|
date | Tue, 22 Jul 2008 10:07:27 +0200 |
parents | c50716420346 |
children |
rev | line source |
---|---|
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
1 /* |
7
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
2 * resize-gd - resizes images using the gd-library |
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
3 * |
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
4 * Copyright 2008 by markus schnalke <meillo@marmaro.de> |
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
5 * |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
6 * build-depends: libgd2-noxpm-dev | libgd2-dev |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
7 * depends: libgd2-noxpm | libgd2-xpm |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
8 */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
9 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
10 #include <stdio.h> |
7
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
11 #include <stdlib.h> |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
12 #include <string.h> |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
13 #include <ctype.h> |
7
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
14 #include "gd.h" |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
15 |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
16 #define PROGRAM "resize-gd" |
10 | 17 #define VERSION "0.2" |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
18 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
19 enum { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
20 Png, |
3 | 21 Jpg |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
22 }; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
23 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
24 struct size { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
25 int w; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
26 int h; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
27 }; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
28 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
29 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
30 void |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
31 version() { |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
32 printf("%s version: %s\n", PROGRAM, VERSION); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
33 } |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
34 |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
35 |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
36 void |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
37 usage() { |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
38 printf("\ |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
39 usage: %s <size> <imagefiles> [...]\n\ |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
40 (keeps aspect ratio, does not enlarge images)\n\ |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
41 %s <width>x<height> <imagefiles> [...]\n\ |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
42 (resizes to that size, enlarges if needed)\n\ |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
43 ", PROGRAM, PROGRAM); |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
44 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
45 |
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 struct size |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
48 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
|
49 struct size result; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
50 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
51 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
|
52 /* keep aspect ratio */ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
53 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
|
54 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
|
55 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
56 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
|
57 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
|
58 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
|
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.h = opt.w; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
61 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
|
62 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
63 } else { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
64 result = opt; |
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 return result; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
67 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
68 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
69 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
70 int |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
71 validsize(char* sp) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
72 while (isdigit(*sp)) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
73 sp++; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
74 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
75 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
|
76 sp++; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
77 while (isdigit(*sp)) { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
78 sp++; |
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 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
81 if (*sp != '\0') { |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
82 return 0; |
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 return 1; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
85 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
86 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
87 |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
88 int |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
89 main(int argc, char* argv[]) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
90 int i; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
91 int type; |
15
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
92 int success = 0; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
93 gdImagePtr im_in; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
94 gdImagePtr im_out; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
95 FILE* in; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
96 FILE* out; |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
97 char* c; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
98 struct size sizeopt, sizeimg, sizedest; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
99 |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
100 if (argc == 2 && strcmp(argv[1], "--version") == 0) { |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
101 version(); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
102 exit(0); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
103 } |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
104 if (argc == 2 && strcmp(argv[1], "--help") == 0) { |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
105 version(); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
106 usage(); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
107 exit(0); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
108 } |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
109 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
|
110 usage(); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
111 exit(1); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
112 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
113 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
114 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
|
115 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
|
116 usage(); |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
117 exit(2); |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
118 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
119 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
120 /* parse width and height */ |
17
159402d54c29
replaced atoi with strtol, which is more secure
meillo@marmaro.de
parents:
15
diff
changeset
|
121 sizeopt.w = (int) strtol(argv[1], (char**)NULL, 10); |
14
6f5c3a02e4d5
filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents:
10
diff
changeset
|
122 c = strchr(argv[1], 'x'); |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
123 if (c && c[1] != '\0') { |
17
159402d54c29
replaced atoi with strtol, which is more secure
meillo@marmaro.de
parents:
15
diff
changeset
|
124 sizeopt.h = (int) strtol(c+1, (char**)NULL, 10); |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
125 } else { |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
126 sizeopt.h = -1; /* keep aspect ratio */ |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
127 } |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
128 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
129 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
130 /* process images */ |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
131 for (i = 2; i < argc; i++) { |
10 | 132 /* printf("processing file '%s'\n", argv[i]); */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
133 |
14
6f5c3a02e4d5
filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents:
10
diff
changeset
|
134 c = strrchr(argv[i], '.'); |
6f5c3a02e4d5
filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents:
10
diff
changeset
|
135 if (strcmp(c, ".png") == 0 || strcmp(c, ".PNG") == 0) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
136 type = Png; |
14
6f5c3a02e4d5
filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents:
10
diff
changeset
|
137 } else if (strcmp(c, ".jpg") == 0 || strcmp(c, ".JPG") == 0 || strcmp(c, ".jpeg") == 0 || strcmp(c, ".JPEG") == 0) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
138 type = Jpg; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
139 } else { |
14
6f5c3a02e4d5
filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents:
10
diff
changeset
|
140 fprintf(stderr, "'%s' has unknown filetype. Filename must end with '.jpg', '.jpeg' or '.png'.\n", argv[i]); |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
141 continue; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
142 } |
7
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
143 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
144 /* load image */ |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
145 in = fopen(argv[i], "rb"); |
10 | 146 if (in == NULL) { |
147 fprintf(stderr, "unable to open '%s' for reading\n", argv[i]); | |
148 continue; | |
149 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
150 if (type == Png) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
151 im_in = gdImageCreateFromPng(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
152 } else if (type == Jpg) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
153 im_in = gdImageCreateFromJpeg(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
154 } |
10 | 155 if (im_in == NULL) { |
156 fprintf(stderr, "unable to load image '%s'\n", argv[i]); | |
157 continue; | |
158 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
159 fclose(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
160 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
161 /* calculate target size */ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
162 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
|
163 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
|
164 sizedest = calcsize(sizeopt, sizeimg, 0); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
165 |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
166 /* skip images that dont need to be resized */ |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
167 if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) { |
15
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
168 success++; |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
169 gdImageDestroy(im_in); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
170 continue; |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
171 } |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
172 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
173 /* 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
|
174 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); |
10 | 175 if (im_out == NULL) { |
176 fprintf(stderr, "unable to allocate memory for resized image\n"); | |
177 exit(3); | |
178 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
179 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
|
180 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
181 /* write image */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
182 out = fopen(argv[i], "wb"); |
10 | 183 if (out == NULL) { |
184 fprintf(stderr, "unable to open '%s' for writing\n", argv[i]); | |
185 continue; | |
186 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
187 if (type == Png) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
188 gdImagePng(im_out, out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
189 } else if (type == Jpg) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
190 gdImageJpeg(im_out, out, -1); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
191 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
192 fclose(out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
193 |
15
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
194 success++; |
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
195 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
196 gdImageDestroy(im_in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
197 gdImageDestroy(im_out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
198 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
199 |
15
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
200 if (success == 0) { |
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
201 return 4; /* no resizing was successful */ |
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
202 } else { |
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
203 return 0; |
c50716420346
new exit code (4) if all files failed in resizing
meillo@marmaro.de
parents:
14
diff
changeset
|
204 } |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
205 } |