aboutsummaryrefslogtreecommitdiff
path: root/host
diff options
context:
space:
mode:
authorjaseg <jaseg@jaseg.net>2014-01-10 12:34:25 +0100
committerjaseg <jaseg@jaseg.net>2014-01-10 12:34:25 +0100
commit1e72c8221210bbc24fd34d5cab75818193f2d962 (patch)
tree1e79b979223e053d48e04ef3bfa2b5e0d24e6d50 /host
parent4c84439c103d4d119f253a1dab8c3b22060c2ab5 (diff)
downloadmatelight-1e72c8221210bbc24fd34d5cab75818193f2d962.tar.gz
matelight-1e72c8221210bbc24fd34d5cab75818193f2d962.tar.bz2
matelight-1e72c8221210bbc24fd34d5cab75818193f2d962.zip
Working on gif handling. Not working yet.
libgif does not yet read all parts of the file
Diffstat (limited to 'host')
-rw-r--r--host/gif.c11
-rw-r--r--host/gif.h4
-rw-r--r--host/main.c69
-rw-r--r--host/main.h4
4 files changed, 68 insertions, 20 deletions
diff --git a/host/gif.c b/host/gif.c
index 6474269..ac77bd8 100644
--- a/host/gif.c
+++ b/host/gif.c
@@ -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 */
diff --git a/host/gif.h b/host/gif.h
index 8ff4981..fde10b4 100644
--- a/host/gif.h
+++ b/host/gif.h
@@ -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__
diff --git a/host/main.c b/host/main.c
index 29e56f9..07a99dd 100644
--- a/host/main.c
+++ b/host/main.c
@@ -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;
}
diff --git a/host/main.h b/host/main.h
index bbfcf74..98c4ded 100644
--- a/host/main.h
+++ b/host/main.h
@@ -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__