Mercurial > resize-gd
annotate resize-gd.c @ 13:c4834b4f4b54
added backslashes to dashes in manpage
author | meillo@marmaro.de |
---|---|
date | Sat, 14 Jun 2008 19:14:27 +0200 |
parents | 6b8e8fcd6d4d |
children | 6f5c3a02e4d5 |
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; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
92 gdImagePtr im_in; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
93 gdImagePtr im_out; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
94 FILE* in; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
95 FILE* out; |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
96 char* c; |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
97 struct size sizeopt, sizeimg, sizedest; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
98 |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
99 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
|
100 version(); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
101 exit(0); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
102 } |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
103 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
|
104 version(); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
105 usage(); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
106 exit(0); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
107 } |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
108 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
|
109 usage(); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
110 exit(1); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
111 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
112 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
113 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
|
114 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
|
115 usage(); |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
116 exit(2); |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
117 } |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
118 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
119 /* 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
|
120 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
|
121 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
|
122 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
|
123 sizeopt.h = atoi(c + 1); |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
124 } else { |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
125 sizeopt.h = -1; /* keep aspect ratio */ |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
126 } |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
127 |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
128 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
129 /* process images */ |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
130 for (i = 2; i < argc; i++) { |
10 | 131 /* printf("processing file '%s'\n", argv[i]); */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
132 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
133 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
134 type = Png; |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
135 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
136 type = Jpg; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
137 } else { |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
138 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
|
139 continue; |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
140 } |
7
c0045d8d3ce2
improved comment at begin of source; some cleanups
meillo@marmaro.de
parents:
5
diff
changeset
|
141 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
142 /* load image */ |
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
143 in = fopen(argv[i], "rb"); |
10 | 144 if (in == NULL) { |
145 fprintf(stderr, "unable to open '%s' for reading\n", argv[i]); | |
146 continue; | |
147 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
148 if (type == Png) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
149 im_in = gdImageCreateFromPng(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
150 } else if (type == Jpg) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
151 im_in = gdImageCreateFromJpeg(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
152 } |
10 | 153 if (im_in == NULL) { |
154 fprintf(stderr, "unable to load image '%s'\n", argv[i]); | |
155 continue; | |
156 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
157 fclose(in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
158 |
2
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
159 /* calculate target size */ |
8e71b54b6e1e
made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents:
1
diff
changeset
|
160 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
|
161 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
|
162 sizedest = calcsize(sizeopt, sizeimg, 0); |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
163 |
5
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
164 /* 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
|
165 if (sizedest.w == sizeimg.w && sizedest.h == sizeimg.h) { |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
166 gdImageDestroy(im_in); |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
167 continue; |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
168 } |
8e2804fe30bc
skipps now too small images; added --version and --help
meillo@marmaro.de
parents:
3
diff
changeset
|
169 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
170 /* 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
|
171 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); |
10 | 172 if (im_out == NULL) { |
173 fprintf(stderr, "unable to allocate memory for resized image\n"); | |
174 exit(3); | |
175 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
176 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
|
177 |
1
7a8f72b27dc3
added keep aspect ratio; validated size
meillo@localhost.localdomain
parents:
0
diff
changeset
|
178 /* write image */ |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
179 out = fopen(argv[i], "wb"); |
10 | 180 if (out == NULL) { |
181 fprintf(stderr, "unable to open '%s' for writing\n", argv[i]); | |
182 continue; | |
183 } | |
0
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
184 if (type == Png) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
185 gdImagePng(im_out, out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
186 } else if (type == Jpg) { |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
187 gdImageJpeg(im_out, out, -1); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
188 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
189 fclose(out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
190 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
191 gdImageDestroy(im_in); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
192 gdImageDestroy(im_out); |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
193 } |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
194 |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
195 return 0; |
8c94239b3b3f
initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff
changeset
|
196 } |