From 5a77d3a0e0a24c2c637aec0195919a13d42ac769 Mon Sep 17 00:00:00 2001
From: jaseg <jaseg@jaseg.net>
Date: Thu, 30 Jan 2014 20:11:59 +0100
Subject: GIF reading fixes

---
 host/gif.c  | 12 ++++++++++--
 host/main.c |  8 ++++----
 2 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/host/gif.c b/host/gif.c
index ac77bd8..c62a5e2 100644
--- a/host/gif.c
+++ b/host/gif.c
@@ -14,7 +14,8 @@ int gif_buffer_input (GifFileType *gif, GifByteType *dest, int n){
 	readBuffer_t *rb = gif->UserData;
 	if(rb->index+n > rb->length)
 		n = rb->length - rb->index;
-	memcpy(dest, rb->data, n);
+	memcpy(dest, rb->data + rb->index, n);
+	rb->index += n;
 	return n;
 }
 
@@ -38,6 +39,7 @@ void color_fill(color_t *dest, color_t src, size_t size){
 }
 
 gifAnimationState_t *gif_read(uint8_t *buf, size_t buflength){
+	color_t *fb = 0;
 	gifAnimationState_t *st = malloc(sizeof(gifAnimationState_t));
 	if(!st){
 		fprintf(stderr, "Failed to allocate %lu bytes\n", sizeof(*st));
@@ -47,6 +49,12 @@ gifAnimationState_t *gif_read(uint8_t *buf, size_t buflength){
 	readBuffer_t readBuf = {buf, 0, buflength};
 	int err = 0;
 	GifFileType *gif = DGifOpen(&readBuf, gif_buffer_input, &err);
+	if(err){
+		fprintf(stderr, "Could not open GIF: %s\n", GifErrorString(err));
+		goto error;
+	}
+
+	err = DGifSlurp(gif);
 	if(err){
 		fprintf(stderr, "Could not read GIF data: %s\n", GifErrorString(err));
 		goto error;
@@ -57,7 +65,7 @@ gifAnimationState_t *gif_read(uint8_t *buf, size_t buflength){
 		fprintf(stderr, "Invalid 0*0px gif\n");
 		goto error;
 	}
-	color_t *fb = calloc(framesize, sizeof(color_t));
+	fb = calloc(framesize, sizeof(color_t));
 	if(!fb){
 		fprintf(stderr, "Failed to allocate framebuffer for GIF (%lu bytes)\n", framesize*sizeof(color_t));
 		goto error;
diff --git a/host/main.c b/host/main.c
index a8c5231..aa782aa 100644
--- a/host/main.c
+++ b/host/main.c
@@ -390,7 +390,7 @@ int main(int argc, char **argv){
 				fprintf(stderr, "Error opening gif file from argument (\"%s\"): Cannot allocate %lu bytes.\n", argv[1], newsize);
 				goto error;
 			}
-			read += fread(buf+size, 1, newsize, f);
+			read += fread(buf+size, 1, READ_INC, f);
 			size = newsize;
 		}while(read == size);
 		fb = framebuffer_render_gif(buf, read, &gifstate, &delay);
@@ -401,13 +401,13 @@ int main(int argc, char **argv){
 	}
 
 	for(;;){ /* Never gonna give you up, never gonna let you down! */
-		printf("\033[2J");
-
 		if(!fb){
-			fprintf(stderr, "Error rendering text.\n");
+			fprintf(stderr, "Error rendering.\n");
 			goto error;
 		}
 
+		printf("\033[2J");
+
 		console_render_buffer(fb);
 		printf("\n");
 		usleep(delay*1000);
-- 
cgit