Mercurial > resize-gd
comparison resize-gd.c @ 10:6b8e8fcd6d4d
added error handling
author | meillo@marmaro.de |
---|---|
date | Sat, 14 Jun 2008 18:08:03 +0200 |
parents | c0045d8d3ce2 |
children | 6f5c3a02e4d5 |
comparison
equal
deleted
inserted
replaced
9:a3547175f466 | 10:6b8e8fcd6d4d |
---|---|
12 #include <string.h> | 12 #include <string.h> |
13 #include <ctype.h> | 13 #include <ctype.h> |
14 #include "gd.h" | 14 #include "gd.h" |
15 | 15 |
16 #define PROGRAM "resize-gd" | 16 #define PROGRAM "resize-gd" |
17 #define VERSION "0.1" | 17 #define VERSION "0.2" |
18 | 18 |
19 enum { | 19 enum { |
20 Png, | 20 Png, |
21 Jpg | 21 Jpg |
22 }; | 22 }; |
126 } | 126 } |
127 | 127 |
128 | 128 |
129 /* process images */ | 129 /* process images */ |
130 for (i = 2; i < argc; i++) { | 130 for (i = 2; i < argc; i++) { |
131 printf("processing file '%s'\n", argv[i]); | 131 /* printf("processing file '%s'\n", argv[i]); */ |
132 | 132 |
133 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { | 133 if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) { |
134 type = Png; | 134 type = Png; |
135 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { | 135 } else if (strcmp(argv[i]+strlen(argv[i])-4, ".jpg") == 0) { |
136 type = Jpg; | 136 type = Jpg; |
139 continue; | 139 continue; |
140 } | 140 } |
141 | 141 |
142 /* load image */ | 142 /* load image */ |
143 in = fopen(argv[i], "rb"); | 143 in = fopen(argv[i], "rb"); |
144 if (in == NULL) { | |
145 fprintf(stderr, "unable to open '%s' for reading\n", argv[i]); | |
146 continue; | |
147 } | |
144 if (type == Png) { | 148 if (type == Png) { |
145 im_in = gdImageCreateFromPng(in); | 149 im_in = gdImageCreateFromPng(in); |
146 } else if (type == Jpg) { | 150 } else if (type == Jpg) { |
147 im_in = gdImageCreateFromJpeg(in); | 151 im_in = gdImageCreateFromJpeg(in); |
152 } | |
153 if (im_in == NULL) { | |
154 fprintf(stderr, "unable to load image '%s'\n", argv[i]); | |
155 continue; | |
148 } | 156 } |
149 fclose(in); | 157 fclose(in); |
150 | 158 |
151 /* calculate target size */ | 159 /* calculate target size */ |
152 sizeimg.w = im_in->sx; | 160 sizeimg.w = im_in->sx; |
159 continue; | 167 continue; |
160 } | 168 } |
161 | 169 |
162 /* copy-resize the image */ | 170 /* copy-resize the image */ |
163 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); | 171 im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h); |
172 if (im_out == NULL) { | |
173 fprintf(stderr, "unable to allocate memory for resized image\n"); | |
174 exit(3); | |
175 } | |
164 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); | 176 gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy); |
165 | 177 |
166 /* write image */ | 178 /* write image */ |
167 out = fopen(argv[i], "wb"); | 179 out = fopen(argv[i], "wb"); |
180 if (out == NULL) { | |
181 fprintf(stderr, "unable to open '%s' for writing\n", argv[i]); | |
182 continue; | |
183 } | |
168 if (type == Png) { | 184 if (type == Png) { |
169 gdImagePng(im_out, out); | 185 gdImagePng(im_out, out); |
170 } else if (type == Jpg) { | 186 } else if (type == Jpg) { |
171 gdImageJpeg(im_out, out, -1); | 187 gdImageJpeg(im_out, out, -1); |
172 } | 188 } |