From 3b4cf74399e26b68ff8ba77cdb56e91e93494cd1 Mon Sep 17 00:00:00 2001
From: David Maciejak <david.maciejak@gmail.com>
Date: Mon, 24 Feb 2014 20:31:24 +0800
Subject: [PATCH] Added webp return error code

---
 wrlib/load_webp.c |   18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/wrlib/load_webp.c b/wrlib/load_webp.c
index d69a107..9364332 100644
--- a/wrlib/load_webp.c
+++ b/wrlib/load_webp.c
@@ -58,7 +58,6 @@ RLoadWEBP(const char *file_name)
 		return NULL;
 	}
 
-
 	if (!(buffer[0] == 'R' &&
 	      buffer[1] == 'I' &&
 	      buffer[2] == 'F' &&
@@ -78,12 +77,12 @@ RLoadWEBP(const char *file_name)
 		return NULL;
 	}
 
-
 	fseek(file, 0, SEEK_END);
 	raw_data_size = ftell(file);
 
 	if (raw_data_size <= 0) {
 		fprintf(stderr, "Failed to find the WEBP image size\n");
+		RErrorCode = RERR_BADIMAGEFILE;
 		return NULL;
 	}
 
@@ -92,7 +91,8 @@ RLoadWEBP(const char *file_name)
 	raw_data = (uint8_t *) malloc(raw_data_size);
 
 	if (!raw_data) {
-		fprintf(stderr, "Failed to allocate enought buffer for WEBP\n");
+		fprintf(stderr, "Failed to allocate enough buffer for WEBP\n");
+		RErrorCode = RERR_NOMEMORY;
 		return NULL;
 	}
 
@@ -100,27 +100,33 @@ RLoadWEBP(const char *file_name)
 
 	if (r != raw_data_size) {
 		fprintf(stderr, "Failed to read WEBP\n");
+		RErrorCode = RERR_READ;
 		return NULL;
 	}
 
 	if (WebPGetFeatures(raw_data, raw_data_size, &features) !=
 	    VP8_STATUS_OK) {
 		fprintf(stderr, "WebPGetFeatures has failed\n");
+		RErrorCode = RERR_BADIMAGEFILE;
 		return NULL;
 	}
 
 	if (features.has_alpha) {
 		image = RCreateImage(features.width, features.height, True);
-		if (!image)
+		if (!image) {
+			RErrorCode = RERR_NOMEMORY;
 			return NULL;
+		}
 		ret =
 		    WebPDecodeRGBAInto(raw_data, raw_data_size, image->data,
 				       features.width * features.height * 4,
 				       features.width * 4);
 	} else {
 		image = RCreateImage(features.width, features.height, False);
-		if (!image)
+		if (!image) {
+			RErrorCode = RERR_NOMEMORY;
 			return NULL;
+		}
 		ret =
 		    WebPDecodeRGBInto(raw_data, raw_data_size, image->data,
 				      features.width * features.height * 3,
@@ -129,6 +135,8 @@ RLoadWEBP(const char *file_name)
 
 	if (!ret) {
 		fprintf(stderr, "Failed to decode WEBP\n");
+		RErrorCode = RERR_BADIMAGEFILE;
+		RReleaseImage(image);
 		return NULL;
 	}
 
-- 
1.7.10.4

