Chan-Vese Segmentation
Data Structures | Macros | Functions
gifwrite.c File Reference

Write animated GIF files. More...

#include <stdio.h>
#include <stdlib.h>

Go to the source code of this file.

Data Structures

struct  tableentry
 Entry in the compression hash table representing a string. More...
 
struct  bitstream
 Management for writing codes of variable bitlength to a file. More...
 

Macros

#define MAXBITS   12
 Maximum number of bits allowed by GIF for encoded symbols.
 
#define MAXCODE   4095
 Maximum code, equals 2^MAXBITS - 1.
 
#define TABLESIZE   5003
 Size of the hash table.
 
#define HASHSHIFT   4
 Shift value used for hashing.
 
#define UNUSED   -1
 Hash value for an unused table entry.
 

Functions

static void WriteWordLE (FILE *File, unsigned short Value)
 Write a 16-bit word in big Endian format.
 
static void WriteImageData (FILE *File, tableentry *Table, unsigned char *Data, int FrameLeft, int FrameTop, int FrameWidth, int FrameHeight, int ImageWidth)
 Write compressed image data for one frame of a GIF animation.
 
static void CropFrame (int *FrameLeft, int *FrameTop, int *FrameWidth, int *FrameHeight, unsigned char *Data, int ImageWidth, int ImageHeight, int TransparentColor)
 Crop the extent of a frame according to its transparency.
 
int GifWrite (unsigned char **Image, int ImageWidth, int ImageHeight, int NumFrames, const unsigned char *Palette, int NumColors, int TransparentColor, const int *Delays, const char *OutputFile)
 Write an animated GIF image. More...
 
static void FlushBlock (bitstream *Stream)
 Flush the block buffer to GIF file.
 
static void FlushBits (bitstream *Stream, int MaxRemaining)
 Flush the bit accumulator to the block buffer.
 
static void WriteBits (bitstream *Stream, unsigned short Code)
 Write a code having Stream->BitsPerCode bits.
 
void FrameDifference (unsigned char **Image, int ImageWidth, int ImageHeight, int NumFrames, int TransparentColor)
 Optimize animation frames by setting unchanged pixels to transparent. More...
 

Detailed Description

Write animated GIF files.

Author
Pascal Getreuer getre.nosp@m.uer@.nosp@m.gmail.nosp@m..com

Copyright (c) 2011, Pascal Getreuer All rights reserved.

This program is free software: you can redistribute it and/or modify it under, at your option, the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version, or the terms of the simplified BSD license.

You should have received a copy of these licenses along with this program. If not, see http://www.gnu.org/licenses/ and http://www.opensource.org/licenses/bsd-license.html.

Definition in file gifwrite.c.

Function Documentation

void FrameDifference ( unsigned char **  Image,
int  ImageWidth,
int  ImageHeight,
int  NumFrames,
int  TransparentColor 
)

Optimize animation frames by setting unchanged pixels to transparent.

Parameters
Imagearray holding the image data for each frame
ImageWidth,ImageHeightdimensions of the image
NumFramesnumber of frames
Palette(global) color palette used by all the frames
NumColorsnumber of colors in Palette
TransparentColorindex of which color is transparent
Delaysthe delay for each frame in centiseconds
OutputFilefilename of the output GIF file

Definition at line 401 of file gifwrite.c.

int GifWrite ( unsigned char **  Image,
int  ImageWidth,
int  ImageHeight,
int  NumFrames,
const unsigned char *  Palette,
int  NumColors,
int  TransparentColor,
const int *  Delays,
const char *  OutputFile 
)

Write an animated GIF image.

Parameters
Imagearray holding the image data for each frame
ImageWidth,ImageHeightdimensions of the image
NumFramesnumber of frames
Palette(global) color palette used by all the frames
NumColorsnumber of colors in Palette
TransparentColorindex of which color is transparent
Delaysthe delay for each frame in centiseconds
OutputFilefilename of the output GIF file
Returns
1 on success, 0 on failure

This routine writes a sequence of image frames as an animated GIF file. Image should be an array of pointers where Image[k] points to image data in row-major order for the kth frame,

Image[k][x + ImageWidth*y] = pixel at (x,y) in the kth frame.

The value of Image[k][x + ImageWidth*y] is the palette index of the pixel's color. Palette should be ordered such that

Palette[3*i + 0] = red intensity of the ith color,
Palette[3*i + 1] = green intensity of the ith color,
Palette[3*i + 2] = blue intensity of the ith color,

where i = 0, 1, ... NumColors - 1. TransparentColor specifies an index to denote transparent pixels.

This routine always uses the overwrite "frame disposal" method, which means that the current frame is drawn on top of the previous ones. This approach allows animated frames to be transparent everywhere except for the pixels that differ from the preceding frame. Call the FrameDifference() function before GifWrite() to perform this optimization.

Delays is an array specifying how long each frame of the animation is shown in units of centiseconds (1/100th of a second). Specifying Delays as NULL sets a delay of 0.1 seconds per frame.

Only a limited portion of the GIF specification is implemented. Particularly,

  • transparency is always used (GIF does not require this)
  • local color tables are not supported
  • the frame disposal method is hardcoded to overwrite
  • the background color index is hardcoded to 0

See [1] for details on the GIF format.

For LZW compression, we follow the hash table algorithm used in GIFCSRC [2] and the UNIX "compress" program [3]. The hash table uses open addressing double hashing (no chaining) on the prefix code / next character combination and a variant of Knuth's algorithm D (vol. 3, sec. 6.4) with G. Knott's relatively-prime secondary probe.

References: [1] http://www.w3.org/Graphics/GIF/spec-gif89a.txt [2] http://www.programmersheaven.com/download/15257/download.aspx [3] http://www.freebsd.org/cgi/cvsweb.cgi/src/usr.bin/compress/

Definition at line 120 of file gifwrite.c.