AnimaEngine  1.0.0
Nintendo DS Pokemon Black, White, Black 2, and White 2 sprite extraction and preview toolchain
Data Structures | Macros | Typedefs | Functions
gif_writer.c File Reference

Implementations of custom LZW animated GIF writing utilities. More...

#include "gif_writer.h"

Data Structures

struct  GifBitWriter
 

Macros

#define GIF_COMPAT_MIN_FAST_DELAY_CS   5
 

Typedefs

typedef struct GifBitWriter GifBitWriter
 

Functions

static void WriteU16 (FILE *f, int value)
 Writes a 16-bit unsigned integer in little-endian format to a file stream. More...
 
static void ResolveCompatibleTiming (int requested_delay_cs, int *out_frame_step, int *out_delay_cs)
 Resolves compatible delay timing constraints to prevent browser lag. More...
 
static int ColorTableSize (int color_count)
 Computes power-of-two color table size matching number of palette colors. More...
 
static int BitsForColorTable (int table_size)
 Computes bits of precision needed to index a color table size. More...
 
static int GifBitWriter_FlushBlock (GifBitWriter *writer)
 Flushes accumulated byte block buffer of the LZW stream to disk. More...
 
static int GifBitWriter_WriteByte (GifBitWriter *writer, u8 value)
 Adds a single byte to the output stream block. More...
 
static int GifBitWriter_WriteCode (GifBitWriter *writer, int code, int code_size)
 Writes an LZW code of variable bit length. More...
 
static int GifBitWriter_Finish (GifBitWriter *writer)
 Flushes remaining bits and terminates image block. More...
 
static int WriteLzwLiteralImageData (FILE *f, const u8 *frame, int width, int height, int scale, int min_code_size)
 Emits LZW literal code stream from pixel index frame data. More...
 
int Gif_WriteIndexed (const char *path, const u8 *frames, int frame_count, int width, int height, const RgbaColor *palette, int palette_count, int transparent_index, int delay_cs, int loop_count, int scale)
 Encodes and writes a sequence of indexed frames to a GIF file. More...
 

Detailed Description

Implementations of custom LZW animated GIF writing utilities.

Macro Definition Documentation

◆ GIF_COMPAT_MIN_FAST_DELAY_CS

#define GIF_COMPAT_MIN_FAST_DELAY_CS   5

Typedef Documentation

◆ GifBitWriter

typedef struct GifBitWriter GifBitWriter

Function Documentation

◆ BitsForColorTable()

static int BitsForColorTable ( int  table_size)
static

Computes bits of precision needed to index a color table size.

Parameters
table_sizeColor table size.
Returns
Number of bits.

◆ ColorTableSize()

static int ColorTableSize ( int  color_count)
static

Computes power-of-two color table size matching number of palette colors.

Parameters
color_countQuantity of colors.
Returns
Resolved size of color table.

◆ Gif_WriteIndexed()

int Gif_WriteIndexed ( const char *  path,
const u8 frames,
int  frame_count,
int  width,
int  height,
const RgbaColor palette,
int  palette_count,
int  transparent_index,
int  delay_cs,
int  loop_count,
int  scale 
)

Encodes and writes a sequence of indexed frames to a GIF file.

Parameters
pathPath to write the final GIF file.
framesPointer to 8-bit indexed frame pixel buffer. Size: (width * height * frame_count).
frame_countTotal number of frames in sequence.
widthCanvas width in pixels.
heightCanvas height in pixels.
palettePointer to an array of colors defining the color map (palette).
palette_countTotal number of entries in color palette (up to 256).
transparent_indexIndex in the palette mapping transparency, or -1.
delay_csBase duration between frames in centiseconds (1/100ths of a second).
loop_countNumber of loops (0 = infinite).
scaleBilinear scale multiplier applied to output image sizes.
Returns
0 on success; negative value on file or encoding failure.

◆ GifBitWriter_Finish()

static int GifBitWriter_Finish ( GifBitWriter writer)
static

Flushes remaining bits and terminates image block.

Parameters
writerBit writer context.
Returns
0 on success; negative on failure.

◆ GifBitWriter_FlushBlock()

static int GifBitWriter_FlushBlock ( GifBitWriter writer)
static

Flushes accumulated byte block buffer of the LZW stream to disk.

Parameters
writerBit writer context.
Returns
0 on success; negative on failure.

◆ GifBitWriter_WriteByte()

static int GifBitWriter_WriteByte ( GifBitWriter writer,
u8  value 
)
static

Adds a single byte to the output stream block.

Parameters
writerBit writer context.
valueByte value.
Returns
0 on success; negative on write failure.

◆ GifBitWriter_WriteCode()

static int GifBitWriter_WriteCode ( GifBitWriter writer,
int  code,
int  code_size 
)
static

Writes an LZW code of variable bit length.

Parameters
writerBit writer context.
codeLZW code value.
code_sizeBit size of the code.
Returns
0 on success; negative on failure.

◆ ResolveCompatibleTiming()

static void ResolveCompatibleTiming ( int  requested_delay_cs,
int *  out_frame_step,
int *  out_delay_cs 
)
static

Resolves compatible delay timing constraints to prevent browser lag.

Parameters
requested_delay_csTarget requested delay duration.
out_frame_stepPointer to resolved frame step multiplier.
out_delay_csPointer to output resolved delay duration.

◆ WriteLzwLiteralImageData()

static int WriteLzwLiteralImageData ( FILE *  f,
const u8 frame,
int  width,
int  height,
int  scale,
int  min_code_size 
)
static

Emits LZW literal code stream from pixel index frame data.

Parameters
fOutput file stream.
frameSource indexed pixel frame.
widthSource frame width.
heightSource frame height.
scaleScaling factor.
min_code_sizeMinimum LZW code size.
Returns
0 on success; negative on failure.

◆ WriteU16()

static void WriteU16 ( FILE *  f,
int  value 
)
static

Writes a 16-bit unsigned integer in little-endian format to a file stream.

Parameters
fTarget file stream.
valueInteger value to write.