# HG changeset patch # User meillo@marmaro.de # Date 1213459683 -7200 # Node ID 6b8e8fcd6d4d86dab36787646c2641c200ebeba0 # Parent a3547175f466c202f1104ad473cb2ff92c44000f added error handling diff -r a3547175f466 -r 6b8e8fcd6d4d resize-gd.c --- 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) {