Working on gif handling. Not working yet.

libgif does not yet read all parts of the file
This commit is contained in:
jaseg 2014-01-10 12:34:25 +01:00
parent 4c84439c10
commit 1e72c82212
4 changed files with 68 additions and 20 deletions

View file

@ -37,7 +37,7 @@ void color_fill(color_t *dest, color_t src, size_t size){
memcpy(dest+i, dest, (size-i)*sizeof(color_t));
}
gifAnimationState_t *gif_read(uint8_t *buf, unsigned int buflength){
gifAnimationState_t *gif_read(uint8_t *buf, size_t buflength){
gifAnimationState_t *st = malloc(sizeof(gifAnimationState_t));
if(!st){
fprintf(stderr, "Failed to allocate %lu bytes\n", sizeof(*st));
@ -59,7 +59,7 @@ gifAnimationState_t *gif_read(uint8_t *buf, unsigned int buflength){
}
color_t *fb = calloc(framesize, sizeof(color_t));
if(!fb){
fprintf(stderr, "Failed to allocate framebuffer for GIF (%d bytes)\n", gif->SWidth*gif->SHeight);
fprintf(stderr, "Failed to allocate framebuffer for GIF (%lu bytes)\n", framesize*sizeof(color_t));
goto error;
}
if(gif->SColorMap){ /* Initially fill framebuffer with background color */
@ -73,6 +73,11 @@ gifAnimationState_t *gif_read(uint8_t *buf, unsigned int buflength){
st->gif = gif;
st->idx = 0;
st->frame = malloc(sizeof(framebuffer_t));
if(!st->frame){
fprintf(stderr, "Failed to allocate %lu bytes\n", sizeof(framebuffer_t));
goto error;
}
st->frame->data = fb;
st->frame->w = gif->SWidth;
st->frame->h = gif->SHeight;
@ -93,7 +98,7 @@ void gifAnimationState_free(gifAnimationState_t *st){
* state internal state, initialize as NULL
* delay is filled with this frame's delay value in milliseconds (if it is not NULL). -1 means "No delay given".
* Small note: I mean, rendering a GIF in python is not quite trivial, either (about 20loc). But come on, this is just ridiculous. */
framebuffer_t *gif_render(uint8_t *buf, unsigned int buflength, gifAnimationState_t **state, int *delay){
framebuffer_t *framebuffer_render_gif(uint8_t *buf, size_t buflength, gifAnimationState_t **state, int *delay){
gifAnimationState_t *st;
if(*state == NULL){
/* On first invocation, parse gif and store it in the state struct */

View file

@ -6,8 +6,8 @@
typedef struct _gifAnimationState gifAnimationState_t;
gifAnimationState_t *gif_read(uint8_t *buf, unsigned int buflength);
framebuffer_t* gif_render(uint8_t *buf, unsigned int buflength, gifAnimationState_t **state, int *delay);
gifAnimationState_t *gif_read(uint8_t *buf, size_t buflength);
framebuffer_t* framebuffer_render_gif(uint8_t *buf, size_t buflength, gifAnimationState_t **state, int *delay);
void gifAnimationState_free(gifAnimationState_t *st);
#endif//__GIF_H__

View file

@ -1,7 +1,8 @@
#include "main.h"
#include "font.h"
#include "color.h"
#include "font.h"
#include "gif.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
@ -345,34 +346,72 @@ void console_render_buffer(framebuffer_t *fb){
}
int main(int argc, char **argv){
if(argc < 2){
fprintf(stderr, "No input text given\n");
if(argc != 2){
fprintf(stderr, "No or too much input text given\n");
return 1;
}
FILE *f = fopen("unifont.bdf", "r");
if(!f){
FILE *fontfile = fopen("unifont.bdf", "r");
if(!fontfile){
fprintf(stderr, "Error opening font file: %s\n", strerror(errno));
return 1;
}
glyph_t* glyph_table[BLP_SIZE];
if(read_bdf(f, glyph_table, BLP_SIZE)){
if(read_bdf(fontfile, glyph_table, BLP_SIZE)){
fprintf(stderr, "Error reading font file.\n");
return 1;
}
fclose(fontfile);
for(;;){
printf("\033[2J");
for(unsigned int i=1; i<argc; i++){
framebuffer_t *fb = framebuffer_render_text(argv[i], glyph_table, BLP_SIZE);
if(!fb){
fprintf(stderr, "Error rendering text.\n");
int delay = 20;
framebuffer_t *fb;
gifAnimationState_t *gifstate = NULL;
char *p = strstr(argv[1], ".gif");
if(p && p[4] == '\0'){ /* Argument ends with ".gif", try to read it as a gif file */
FILE *f = fopen(argv[1], "r");
if(!f){
fprintf(stderr, "Error opening gif file from argument (\"%s\"): %s\n", argv[1], strerror(errno));
return 1;
}
uint8_t *buf = NULL;
size_t size = 0;
size_t read = 0;
const size_t READ_INC = 1024;
do{
size_t newsize = size+READ_INC;
buf = realloc(buf, newsize);
if(!buf){
fprintf(stderr, "Error opening gif file from argument (\"%s\"): Cannot allocate %lu bytes.\n", argv[1], newsize);
return 1;
}
console_render_buffer(fb);
printf("\n");
read += fread(buf+size, 1, newsize, f);
size = newsize;
}while(read == size);
fb = framebuffer_render_gif(buf, read, &gifstate, &delay);
free(buf);
}else{
fb = framebuffer_render_text(argv[1], glyph_table, BLP_SIZE);
}
for(;;){ /* Never gonna give you up, never gonna let you down! */
printf("\033[2J");
if(!fb){
fprintf(stderr, "Error rendering text.\n");
return 1;
}
console_render_buffer(fb);
printf("\n");
usleep(delay*1000);
if(gifstate){
fb = framebuffer_render_gif(NULL, 0, &gifstate, &delay);
if(delay == -1)
delay = 20;
}else{
fb = framebuffer_render_text(argv[1], glyph_table, BLP_SIZE);
}
usleep(20000);
}
return 0;
}

View file

@ -1,6 +1,10 @@
#ifndef __MAIN_H__
#define __MAIN_H__
#include "color.h"
#include "font.h"
framebuffer_t *framebuffer_render_text(char *s, glyph_t **glyph_table, unsigned int glyph_table_size);
void console_render_buffer(framebuffer_t *fb);
#endif//__MAIN_H__