#pragma once

#include <stdint.h>  // for uint32_t
#include <string.h>  // for memcpy

#define EXTENSION_INTRODUCER   0x21
#define IMAGE_DESCRIPTOR       0x2C
#define TRAILER                0x3B
#define GRAPHIC_CONTROL        0xF9
#define APPLICATION_EXTENSION  0xFF
#define COMMENT_EXTENSION      0xFE
#define PLAINTEXT_EXTENSION    0x01

#define READ(dst, size) memcpy(dst, buffer, size); buffer += size

typedef struct {
    unsigned short width;
    unsigned short height;
    unsigned char fields;
    unsigned char background_color_index;
    unsigned char pixel_aspect_ratio;
} screen_descriptor_t;

typedef struct {
    unsigned char r, g, b;
} rgb;

typedef struct {
    unsigned short image_left_position;
    unsigned short image_top_position;
    unsigned short image_width;
    unsigned short image_height;
    unsigned char fields;
} image_descriptor_t;

typedef struct {
    unsigned char byte;
    int prev;
    int len;
} dictionary_entry_t;

typedef struct {
    unsigned char extension_code;
    unsigned char block_size;
} extension_t;

typedef struct {
    unsigned char fields;
    unsigned short delay_time;
    unsigned char transparent_color_index;
} graphic_control_extension_t;

typedef struct {
    unsigned char application_id[8];
    unsigned char version[3];
} application_extension_t;

typedef struct {
    unsigned short left, top, width, height;
    unsigned char cell_width, cell_height;
    unsigned char foreground_color, background_color;
} plaintext_extension_t;

void uncompress(int code_length, const unsigned char *input, int input_length, unsigned char *out);
uint32_t* LoadPalette(unsigned char *buffer);
unsigned char* LoadGif(unsigned char *buffer, unsigned short* w, unsigned short* h);