GIF reading fixes

This commit is contained in:
jaseg 2014-01-30 20:11:59 +01:00
parent dd677c6fa7
commit 5a77d3a0e0
2 changed files with 14 additions and 6 deletions

View file

@ -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;

View file

@ -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);