commit f9bcb3eb69976b7eefa42da8438f659e502e2b37
parent 3a071e16f10fb7d922d361d14ac121659247f13e
Author: phoebos <ben@bvnf.space>
Date: Wed, 1 Sep 2021 21:34:10 +0100
implement color table as a struct
Diffstat:
M | gif.c | | | 32 | +++++++++++++++++++------------- |
1 file changed, 19 insertions(+), 13 deletions(-)
diff --git a/gif.c b/gif.c
@@ -18,6 +18,10 @@
#define FAIL_SYS 2
typedef unsigned char rgb[3];
+struct Color_table {
+ int len; /* 2- 256 */
+ rgb table[256];
+};
struct Gif {
char version[3]; /* 87a or 89a */
uint16_t width;
@@ -30,20 +34,20 @@ struct Gif {
*/
unsigned char background; /* color index in GCT of screen background */
unsigned char aspect_ratio; /* 89a+ only */
- rgb colortable[256];
- int colortable_len; /* 2 - 256 */
+ struct Color_table *gct;
unsigned char *pixels; /* array of indices to colors */
};
struct Image {
- int colortable_len; /* 2 - 256 */
- /* TODO: local color table */
+ struct Color_table *lct;
unsigned char *pixels;
};
void
-print_colortable(struct Gif *g){
- for (int i = 0; i < g->colortable_len; i++){
- printf("%d: \033[48;2;%d;%d;%dm(%02x;%02x;%02x)\033[0m\n", i, g->colortable[i][0], g->colortable[i][1], g->colortable[i][2], g->colortable[i][0], g->colortable[i][1], g->colortable[i][2]);
+print_colortable(struct Color_table *c){
+ for (int i = 0; i < c->len; i++){
+ printf("%d: \033[48;2;%d;%d;%dm(%02x;%02x;%02x)\033[0m\n",
+ i, c->table[i][0], c->table[i][1], c->table[i][2],
+ c->table[i][0], c->table[i][1], c->table[i][2]);
}
}
@@ -57,6 +61,7 @@ gif_decode_image(struct Image *img, unsigned char *buf){
int
gif_decode(unsigned char *buf, size_t len){
struct Gif g = {0 };
+ g.gct = malloc(sizeof(struct Color_table));
struct Image *img;
int cur_block;
enum { HEAD = 1, SPEC = 2, IMGE = 4 };
@@ -87,19 +92,19 @@ gif_decode(unsigned char *buf, size_t len){
g.background = *buf++;
g.aspect_ratio = *buf++;
- g.colortable_len = (int)pow(2.0, (double) (g.flags & 7U) + 1);
- fprintf(stderr, "%d colors in GCT\n", g.colortable_len);
+ g.gct->len = (int)pow(2.0, (double) (g.flags & 7U) + 1);
+ fprintf(stderr, "%d colors in GCT\n", g.gct->len);
/* if GCT... */
if (g.flags & 0x80) {
/* TODO: gif87a.txt L 336? */
- for (int i = 0; i < g.colortable_len; i+=1) {
+ for (int i = 0; i < g.gct->len; i+=1) {
rgb c = {*buf++, *buf++, *buf++};
- memcpy(g.colortable[i], c, sizeof(rgb));
+ memcpy(g.gct->table[i], c, sizeof(rgb));
}
}
- print_colortable(&g);
+ print_colortable(g.gct);
/* END OF HEADER */
@@ -115,7 +120,8 @@ gif_decode(unsigned char *buf, size_t len){
if (cur_block != IMGE) {
cur_block = IMGE;
img = malloc(sizeof(struct Image));
- img->colortable_len = g.colortable_len;
+ img->lct = malloc(sizeof(struct Color_table));
+ img->lct->len = g.gct->len;
gif_decode_image(img, buf);
}
break;