resize-gd

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 diff
     1.1 --- a/resize-gd.c	Sat Jun 14 18:07:51 2008 +0200
     1.2 +++ b/resize-gd.c	Sat Jun 14 18:08:03 2008 +0200
     1.3 @@ -14,7 +14,7 @@
     1.4  #include "gd.h"
     1.5  
     1.6  #define PROGRAM "resize-gd"
     1.7 -#define VERSION "0.1"
     1.8 +#define VERSION "0.2"
     1.9  
    1.10  enum {
    1.11  	Png,
    1.12 @@ -128,7 +128,7 @@
    1.13  
    1.14  	/* process images */
    1.15  	for (i = 2; i < argc; i++) {
    1.16 -		printf("processing file '%s'\n", argv[i]);
    1.17 +		/* printf("processing file '%s'\n", argv[i]); */
    1.18  
    1.19  		if (strcmp(argv[i]+strlen(argv[i])-4, ".png") == 0) {
    1.20  			type = Png;
    1.21 @@ -141,11 +141,19 @@
    1.22  
    1.23  		/* load image */
    1.24  		in = fopen(argv[i], "rb");
    1.25 +		if (in == NULL) {
    1.26 +			fprintf(stderr, "unable to open '%s' for reading\n", argv[i]);
    1.27 +			continue;
    1.28 +		}
    1.29  		if (type == Png) {
    1.30  			im_in = gdImageCreateFromPng(in);
    1.31  		} else if (type == Jpg) {
    1.32  			im_in = gdImageCreateFromJpeg(in);
    1.33  		}
    1.34 +		if (im_in == NULL) {
    1.35 +			fprintf(stderr, "unable to load image '%s'\n", argv[i]);
    1.36 +			continue;
    1.37 +		}
    1.38  		fclose(in);
    1.39  
    1.40  		/* calculate target size */
    1.41 @@ -161,10 +169,18 @@
    1.42  
    1.43  		/* copy-resize the image */
    1.44  		im_out = gdImageCreateTrueColor(sizedest.w, sizedest.h);
    1.45 +		if (im_out == NULL) {
    1.46 +			fprintf(stderr, "unable to allocate memory for resized image\n");
    1.47 +			exit(3);
    1.48 +		}
    1.49  		gdImageCopyResampled(im_out, im_in, 0, 0, 0, 0, im_out->sx, im_out->sy, im_in->sx, im_in->sy);
    1.50  
    1.51  		/* write image */
    1.52  		out = fopen(argv[i], "wb");
    1.53 +		if (out == NULL) {
    1.54 +			fprintf(stderr, "unable to open '%s' for writing\n", argv[i]);
    1.55 +			continue;
    1.56 +		}
    1.57  		if (type == Png) {
    1.58  			gdImagePng(im_out, out);
    1.59  		} else if (type == Jpg) {