annotate resize-gd.c @ 14:6f5c3a02e4d5

filename suffix is now detected in lower or upper case letters
author meillo@marmaro.de
date Sun, 15 Jun 2008 15:50:59 +0200
parents 6b8e8fcd6d4d
children c50716420346
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
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
35a50e57b6f5 removes some compiler warnings
meillo@marmaro.de
parents: 2
diff changeset
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]);
14
6f5c3a02e4d5 filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents: 10
diff changeset
121 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
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
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
131 /* printf("processing file '%s'\n", argv[i]); */
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
132
14
6f5c3a02e4d5 filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents: 10
diff changeset
133 c = strrchr(argv[i], '.');
6f5c3a02e4d5 filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents: 10
diff changeset
134 if (strcmp(c, ".png") == 0 || strcmp(c, ".PNG") == 0) {
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
135 type = Png;
14
6f5c3a02e4d5 filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents: 10
diff changeset
136 } 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
137 type = Jpg;
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
138 } else {
14
6f5c3a02e4d5 filename suffix is now detected in lower or upper case letters
meillo@marmaro.de
parents: 10
diff changeset
139 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
140 continue;
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
141 }
7
c0045d8d3ce2 improved comment at begin of source; some cleanups
meillo@marmaro.de
parents: 5
diff changeset
142
1
7a8f72b27dc3 added keep aspect ratio; validated size
meillo@localhost.localdomain
parents: 0
diff changeset
143 /* load image */
7a8f72b27dc3 added keep aspect ratio; validated size
meillo@localhost.localdomain
parents: 0
diff changeset
144 in = fopen(argv[i], "rb");
10
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
145 if (in == NULL) {
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
146 fprintf(stderr, "unable to open '%s' for reading\n", argv[i]);
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
147 continue;
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
148 }
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
149 if (type == Png) {
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
150 im_in = gdImageCreateFromPng(in);
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
151 } else if (type == Jpg) {
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
152 im_in = gdImageCreateFromJpeg(in);
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
153 }
10
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
154 if (im_in == NULL) {
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
155 fprintf(stderr, "unable to load image '%s'\n", argv[i]);
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
156 continue;
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
157 }
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
158 fclose(in);
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
159
2
8e71b54b6e1e made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents: 1
diff changeset
160 /* calculate target size */
8e71b54b6e1e made separate functions for calcsize and usage; pictures are not enlarged by default
meillo@marmaro.de
parents: 1
diff changeset
161 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
162 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
163 sizedest = calcsize(sizeopt, sizeimg, 0);
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
164
5
8e2804fe30bc skipps now too small images; added --version and --help
meillo@marmaro.de
parents: 3
diff changeset
165 /* 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
166 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
167 gdImageDestroy(im_in);
8e2804fe30bc skipps now too small images; added --version and --help
meillo@marmaro.de
parents: 3
diff changeset
168 continue;
8e2804fe30bc skipps now too small images; added --version and --help
meillo@marmaro.de
parents: 3
diff changeset
169 }
8e2804fe30bc skipps now too small images; added --version and --help
meillo@marmaro.de
parents: 3
diff changeset
170
1
7a8f72b27dc3 added keep aspect ratio; validated size
meillo@localhost.localdomain
parents: 0
diff changeset
171 /* 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
172 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
10
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
173 if (im_out == NULL) {
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
174 fprintf(stderr, "unable to allocate memory for resized image\n");
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
175 exit(3);
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
176 }
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
177 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
178
1
7a8f72b27dc3 added keep aspect ratio; validated size
meillo@localhost.localdomain
parents: 0
diff changeset
179 /* write image */
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
180 out = fopen(argv[i], "wb");
10
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
181 if (out == NULL) {
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
182 fprintf(stderr, "unable to open '%s' for writing\n", argv[i]);
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
183 continue;
6b8e8fcd6d4d added error handling
meillo@marmaro.de
parents: 7
diff changeset
184 }
0
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
185 if (type == Png) {
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
186 gdImagePng(im_out, out);
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
187 } else if (type == Jpg) {
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
188 gdImageJpeg(im_out, out, -1);
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
189 }
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
190 fclose(out);
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 gdImageDestroy(im_in);
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
193 gdImageDestroy(im_out);
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
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
196 return 0;
8c94239b3b3f initial commit (provides basic funcionality)
meillo@localhost.localdomain
parents:
diff changeset
197 }