libnsgif
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Data Structures | Macros | Functions | Variables
libnsgif.c File Reference

GIF image decoder. More...

#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include "libnsgif.h"
#include "utils/log.h"

Data Structures

struct  lzw_s
 

Macros

#define GIF_MAX_COLOURS   256
 
#define GIF_PROCESS_COLOURS   0xaa000000
 
#define GIF_INVALID_FRAME   -1
 
#define GIF_MAX_LZW   12
 
#define GIF_TRANSPARENT_COLOUR   0x00
 
#define GIF_FRAME_COMBINE   1
 
#define GIF_FRAME_CLEAR   2
 
#define GIF_FRAME_RESTORE   3
 
#define GIF_FRAME_QUIRKS_RESTORE   4
 
#define GIF_IMAGE_SEPARATOR   0x2c
 
#define GIF_INTERLACE_MASK   0x40
 
#define GIF_COLOUR_TABLE_MASK   0x80
 
#define GIF_COLOUR_TABLE_SIZE_MASK   0x07
 
#define GIF_EXTENSION_INTRODUCER   0x21
 
#define GIF_EXTENSION_GRAPHIC_CONTROL   0xf9
 
#define GIF_DISPOSAL_MASK   0x1c
 
#define GIF_TRANSPARENCY_MASK   0x01
 
#define GIF_EXTENSION_COMMENT   0xfe
 
#define GIF_EXTENSION_PLAIN_TEXT   0x01
 
#define GIF_EXTENSION_APPLICATION   0xff
 
#define GIF_BLOCK_TERMINATOR   0x00
 
#define GIF_TRAILER   0x3b
 
#define GIF_STANDARD_HEADER_SIZE   13
 

Functions

static int gif_next_code (gif_animation *gif, int code_size)
 
static gif_result gif_clear_codes_LZW (gif_animation *gif)
 
static gif_result gif_initialise_LZW (gif_animation *gif, int initial_code_size)
 
static bool gif_next_LZW (gif_animation *gif)
 
static gif_result gif_initialise_sprite (gif_animation *gif, unsigned int width, unsigned int height)
 
static gif_result gif_initialise_frame_extensions (gif_animation *gif, const int frame)
 
static gif_result gif_initialise_frame (gif_animation *gif)
 
static gif_result gif_skip_frame_extensions (gif_animation *gif)
 
static unsigned int gif_interlaced_line (int height, int y)
 
static gif_result gif_internal_decode_frame (gif_animation *gif, unsigned int frame, bool clear_image)
 
void gif_create (gif_animation *gif, gif_bitmap_callback_vt *bitmap_callbacks)
 
gif_result gif_initialise (gif_animation *gif, size_t size, unsigned char *data)
 
gif_result gif_decode_frame (gif_animation *gif, unsigned int frame)
 
void gif_finalise (gif_animation *gif)
 

Variables

static struct lzw_s lzw_params
 
static struct lzw_slzw = &lzw_params
 

Detailed Description

GIF image decoder.

The GIF format is thoroughly documented; a full description can be found at http://www.w3.org/Graphics/GIF/spec-gif89a.txt

Todo:
Plain text and comment extensions should be implemented.

Macro Definition Documentation

#define GIF_BLOCK_TERMINATOR   0x00
#define GIF_COLOUR_TABLE_MASK   0x80
#define GIF_COLOUR_TABLE_SIZE_MASK   0x07
#define GIF_DISPOSAL_MASK   0x1c
#define GIF_EXTENSION_APPLICATION   0xff
#define GIF_EXTENSION_COMMENT   0xfe
#define GIF_EXTENSION_GRAPHIC_CONTROL   0xf9
#define GIF_EXTENSION_INTRODUCER   0x21
#define GIF_EXTENSION_PLAIN_TEXT   0x01
#define GIF_FRAME_CLEAR   2
#define GIF_FRAME_COMBINE   1
#define GIF_FRAME_QUIRKS_RESTORE   4
#define GIF_FRAME_RESTORE   3
#define GIF_IMAGE_SEPARATOR   0x2c
#define GIF_INTERLACE_MASK   0x40
#define GIF_INVALID_FRAME   -1

Internal flag that a frame is invalid/unprocessed

#define GIF_MAX_COLOURS   256

Maximum colour table size

#define GIF_MAX_LZW   12

Maximum LZW bits available

#define GIF_PROCESS_COLOURS   0xaa000000

Internal flag that the colour table needs to be processed

#define GIF_STANDARD_HEADER_SIZE   13

standard GIF header size

#define GIF_TRAILER   0x3b
#define GIF_TRANSPARENCY_MASK   0x01
#define GIF_TRANSPARENT_COLOUR   0x00

Transparent colour

Function Documentation

static gif_result gif_clear_codes_LZW ( gif_animation gif)
static

Clear LZW code dictionary

Parameters
gifThe GIF context.
Returns
GIF_OK or error code.
void gif_create ( gif_animation gif,
gif_bitmap_callback_vt bitmap_callbacks 
)

Initialises necessary gif_animation members.

gif_result gif_decode_frame ( gif_animation gif,
unsigned int  frame 
)

Decodes a GIF frame.

Returns
Error return value. If a frame does not contain any image data, GIF_OK is returned and gif->current_error is set to GIF_FRAME_NO_DISPLAY
  • GIF_FRAME_DATA_ERROR for GIF frame data error
  • GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
  • GIF_DATA_ERROR for GIF error (invalid frame header)
  • GIF_INSUFFICIENT_DATA for insufficient data to do anything
  • GIF_INSUFFICIENT_MEMORY for insufficient memory to process
  • GIF_OK for successful decoding
void gif_finalise ( gif_animation gif)

Releases any workspace held by a gif

gif_result gif_initialise ( gif_animation gif,
size_t  size,
unsigned char *  data 
)

Initialises any workspace held by the animation and attempts to decode any information that hasn't already been decoded. If an error occurs, all previously decoded frames are retained.

Returns
Error return value.
  • GIF_FRAME_DATA_ERROR for GIF frame data error
  • GIF_INSUFFICIENT_FRAME_DATA for insufficient data to process any more frames
  • GIF_INSUFFICIENT_MEMORY for memory error
  • GIF_DATA_ERROR for GIF error
  • GIF_INSUFFICIENT_DATA for insufficient data to do anything
  • GIF_OK for successful decoding
  • GIF_WORKING for successful decoding if more frames are expected
static gif_result gif_initialise_frame ( gif_animation gif)
static

Attempts to initialise the next frame

Parameters
gifThe animation context
Returns
error code
  • GIF_INSUFFICIENT_DATA for insufficient data to do anything
  • GIF_FRAME_DATA_ERROR for GIF frame data error
  • GIF_INSUFFICIENT_MEMORY for insufficient memory to process
  • GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame
  • GIF_DATA_ERROR for GIF error (invalid frame header)
  • GIF_OK for successful decoding
  • GIF_WORKING for successful decoding if more frames are expected
static gif_result gif_initialise_frame_extensions ( gif_animation gif,
const int  frame 
)
static

Attempts to initialise the frame's extensions

Parameters
gifThe animation context
frameThe frame number
Returns
GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame GIF_OK for successful initialisation.
static gif_result gif_initialise_LZW ( gif_animation gif,
int  initial_code_size 
)
static

Initialise LZW

This initialises a LZW context ready to commence decompression.

Parameters
initial_code_sizeThe size of codes used on clearing of code dictionary
static gif_result gif_initialise_sprite ( gif_animation gif,
unsigned int  width,
unsigned int  height 
)
static

Updates the sprite memory size

Parameters
gifThe animation context
widthThe width of the sprite
heightThe height of the sprite
Returns
GIF_INSUFFICIENT_MEMORY for a memory error GIF_OK for success
static unsigned int gif_interlaced_line ( int  height,
int  y 
)
static
static gif_result gif_internal_decode_frame ( gif_animation gif,
unsigned int  frame,
bool  clear_image 
)
static

decode a gif frame

Parameters
gifgif animation context.
frameThe frame number to decode.
clear_imageflag for image data being cleared instead of plotted.
static int gif_next_code ( gif_animation gif,
int  code_size 
)
static

get the next LZW code from the GIF

reads codes from the input data stream coping with GIF data sub blocking

Parameters
gifThe GIF context
code_sizeThe number of bitsin the current LZW code
Returns
The next code to process or error return code
static bool gif_next_LZW ( gif_animation gif)
static

fill the LZW stack with decompressed data

Parameters
gifThe GIF context.
Returns
true on sucessful decode of the next LZW code else false.
static gif_result gif_skip_frame_extensions ( gif_animation gif)
static

Skips the frame's extensions (which have been previously initialised)

Parameters
gifThe animation context
Returns
GIF_INSUFFICIENT_FRAME_DATA for insufficient data to complete the frame GIF_OK for successful decoding

Variable Documentation

struct lzw_s* lzw = &lzw_params
static
struct lzw_s lzw_params
static
Initial value:
= {
.zero_data_block = false,
}