changeset 10:6b8e8fcd6d4d

added error handling
author meillo@marmaro.de
date Sat, 14 Jun 2008 18:08:03 +0200
parents a3547175f466
children 7cc162bbff25
files resize-gd.c
diffstat 1 files changed, 18 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/resize-gd.c	Sat Jun 14 18:07:51 2008 +0200
+++ b/resize-gd.c	Sat Jun 14 18:08:03 2008 +0200
@@ -14,7 +14,7 @@
 #include "gd.h"
 
 #define PROGRAM "resize-gd"
-#define VERSION "0.1"
+#define VERSION "0.2"
 
 enum {
 	Png,
@@ -128,7 +128,7 @@
 
 	/* process images */
 	for (i = 2; i < argc; i++) {
-		printf("processing file '%s'\n", argv[i]);
+		/* printf("processing file '%s'\n", argv[i]); */
 
 		if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
 			type = Png;
@@ -141,11 +141,19 @@
 
 		/* load image */
 		in = fopen(argv[i], "rb");
+		if (in == NULL) {
+			fprintf(stderr, "unable to open '%s' for reading\n", argv[i]);
+			continue;
+		}
 		if (type == Png) {
 			im_in = gdImageCreateFromPng(in);
 		} else if (type == Jpg) {
 			im_in = gdImageCreateFromJpeg(in);
 		}
+		if (im_in == NULL) {
+			fprintf(stderr, "unable to load image '%s'\n", argv[i]);
+			continue;
+		}
 		fclose(in);
 
 		/* calculate target size */
@@ -161,10 +169,18 @@
 
 		/* copy-resize the image */
 		im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
+		if (im_out == NULL) {
+			fprintf(stderr, "unable to allocate memory for resized image\n");
+			exit(3);
+		}
 		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
 
 		/* write image */
 		out = fopen(argv[i], "wb");
+		if (out == NULL) {
+			fprintf(stderr, "unable to open '%s' for writing\n", argv[i]);
+			continue;
+		}
 		if (type == Png) {
 			gdImagePng(im_out, out);
 		} else if (type == Jpg) {