gifsicle  1.92
About: Gifsicle is a UNIX command-line tool for creating, editing, and getting information about GIF images and animations.
  Fossies Dox: gifsicle-1.92.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

gif.h
Go to the documentation of this file.
1 /* gif.h - Interface to the LCDF GIF library.
2  Copyright (C) 1997-2017 Eddie Kohler, ekohler@gmail.com
3  This file is part of the LCDF GIF library.
4 
5  The GIF library is free software. It is distributed under the GNU General
6  Public License, version 2; you can copy, distribute, or alter it at will,
7  as long as this notice is kept intact and this source code is made
8  available. There is no warranty, express or implied. */
9 
10 #ifndef LCDF_GIF_H /* -*- mode: c -*- */
11 #define LCDF_GIF_H
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <lcdf/inttypes.h>
15 #ifdef __cplusplus
16 extern "C" {
17 #endif
18 
19 /* NOTE: You should define the types uint8_t, uint16_t and uint32_t before
20  including this file, probably by #including <inttypes.h>. */
21 
22 #define GIF_MAJOR_VERSION 2
23 #define GIF_MINOR_VERSION 0
24 #define GIF_VERSION "2.0"
25 
26 typedef struct Gif_Stream Gif_Stream;
27 typedef struct Gif_Image Gif_Image;
28 typedef struct Gif_Colormap Gif_Colormap;
29 typedef struct Gif_Comment Gif_Comment;
31 typedef struct Gif_Record Gif_Record;
32 
33 typedef uint16_t Gif_Code;
34 #define GIF_MAX_CODE_BITS 12
35 #define GIF_MAX_CODE 0x1000
36 #define GIF_MAX_BLOCK 255
37 
38 
41 struct Gif_Stream {
43  int nimages;
44  int imagescap;
45 
47  uint16_t background; /* 256 means no background */
48 
49  uint16_t screen_width;
50  uint16_t screen_height;
51  long loopcount; /* -1 means no loop count */
52 
55 
56  unsigned errors;
57  uint32_t user_flags;
58 
59  const char* landmark;
60  int refcount;
61 };
62 
65 
68 
69 #define Gif_ScreenWidth(gfs) ((gfs)->screen_width)
70 #define Gif_ScreenHeight(gfs) ((gfs)->screen_height)
71 #define Gif_ImageCount(gfs) ((gfs)->nimages)
72 
73 #define GIF_UNOPTIMIZE_SIMPLEST_DISPOSAL 1
74 #define GIF_MAX_SCREEN_WIDTH 65535
75 #define GIF_MAX_SCREEN_HEIGHT 65535
76 
77 void Gif_CalculateScreenSize(Gif_Stream *, int force);
79 int Gif_FullUnoptimize(Gif_Stream *, int flags);
80 
81 
84 struct Gif_Image {
85  uint8_t **img; /* img[y][x] == image byte (x,y) */
86  uint8_t *image_data;
87 
88  uint16_t width;
89  uint16_t height;
90  uint16_t left;
91  uint16_t top;
92  uint16_t delay;
93  uint8_t disposal;
94  uint8_t interlace;
95 
96  short transparent; /* -1 means no transparent index */
98 
99  char* identifier;
102 
103  void (*free_image_data)(void *);
104 
105  uint32_t compressed_len;
107  uint8_t* compressed;
108  void (*free_compressed)(void *);
109 
110  uint32_t user_flags;
111  void* user_data;
112  void (*free_user_data)(void *);
113  int refcount;
114 
115 };
116 
117 #define GIF_DISPOSAL_NONE 0
118 #define GIF_DISPOSAL_ASIS 1
119 #define GIF_DISPOSAL_BACKGROUND 2
120 #define GIF_DISPOSAL_PREVIOUS 3
121 
122 Gif_Image * Gif_NewImage(void);
123 void Gif_DeleteImage(Gif_Image *gfi);
124 
125 int Gif_AddImage(Gif_Stream *gfs, Gif_Image *gfi);
126 void Gif_RemoveImage(Gif_Stream *gfs, int i);
128 void Gif_MakeImageEmpty(Gif_Image* gfi);
129 
130 Gif_Image * Gif_GetImage(Gif_Stream *gfs, int i);
131 Gif_Image * Gif_GetNamedImage(Gif_Stream *gfs, const char *name);
132 int Gif_ImageNumber(Gif_Stream *gfs, Gif_Image *gfi);
133 
134 #define Gif_ImageWidth(gfi) ((gfi)->width)
135 #define Gif_ImageHeight(gfi) ((gfi)->height)
136 #define Gif_ImageDelay(gfi) ((gfi)->delay)
137 #define Gif_ImageUserData(gfi) ((gfi)->userdata)
138 #define Gif_SetImageUserData(gfi, v) ((gfi)->userdata = v)
139 int Gif_ImageColorBound(const Gif_Image* gfi);
140 
141 typedef void (*Gif_ReadErrorHandler)(Gif_Stream* gfs,
142  Gif_Image* gfi,
143  int is_error,
144  const char* error_text);
145 
146 typedef struct {
147  int flags;
148  int loss;
149  void *padding[7];
151 
152 #define Gif_UncompressImage(gfs, gfi) Gif_FullUncompressImage((gfs),(gfi),0)
154  Gif_ReadErrorHandler handler);
155 int Gif_CompressImage(Gif_Stream *gfs, Gif_Image *gfi);
157  const Gif_CompressInfo *gcinfo);
160 int Gif_SetUncompressedImage(Gif_Image *gfi, uint8_t *data,
161  void (*free_data)(void *), int data_interlaced);
162 int Gif_CreateUncompressedImage(Gif_Image* gfi, int data_interlaced);
163 
164 int Gif_ClipImage(Gif_Image *gfi, int l, int t, int w, int h);
165 
167 
168 
171 typedef struct {
172  uint8_t haspixel; /* semantics assigned by user */
173  uint8_t gfc_red; /* red component (0-255) */
174  uint8_t gfc_green; /* green component (0-255) */
175  uint8_t gfc_blue; /* blue component (0-255) */
176  uint32_t pixel; /* semantics assigned by user */
177 } Gif_Color;
178 
179 
180 struct Gif_Colormap {
181  int ncol;
182  int capacity;
183  uint32_t user_flags;
184  int refcount;
186 };
187 
189 Gif_Colormap * Gif_NewFullColormap(int count, int capacity);
191 
193 
195 #define GIF_COLOREQ(c1, c2) \
196 ((c1)->gfc_red==(c2)->gfc_red && (c1)->gfc_green==(c2)->gfc_green && \
197  (c1)->gfc_blue==(c2)->gfc_blue)
198 #define GIF_SETCOLOR(c, r, g, b) \
199 ((c)->gfc_red = (r), (c)->gfc_green = (g), (c)->gfc_blue = (b))
200 
202 int Gif_AddColor(Gif_Colormap *, Gif_Color *, int look_from);
203 
204 
207 struct Gif_Comment {
208  char **str;
209  int *len;
210  int count;
211  int cap;
212 };
213 
216 int Gif_AddCommentTake(Gif_Comment *, char *, int);
217 int Gif_AddComment(Gif_Comment *, const char *, int);
218 
219 
223  int kind; /* negative kinds are reserved */
224  char* appname;
226  uint8_t* data;
227  uint32_t length;
229 
233  void (*free_data)(void *);
234 };
235 
236 
237 Gif_Extension* Gif_NewExtension(int kind, const char* appname, int applength);
240 int Gif_AddExtension(Gif_Stream* gfs, Gif_Image* gfi,
241  Gif_Extension* gfex);
242 
243 
246 struct Gif_Record {
247  const unsigned char *data;
248  uint32_t length;
249 };
250 
251 #define GIF_READ_COMPRESSED 1
252 #define GIF_READ_UNCOMPRESSED 2
253 #define GIF_READ_CONST_RECORD 4
254 #define GIF_READ_TRAILING_GARBAGE_OK 8
255 #define GIF_WRITE_CAREFUL_MIN_CODE_SIZE 1
256 #define GIF_WRITE_EAGER_CLEAR 2
257 #define GIF_WRITE_OPTIMIZE 4
258 #define GIF_WRITE_SHRINK 8
259 
261 Gif_Stream* Gif_ReadFile(FILE* f);
262 Gif_Stream* Gif_FullReadFile(FILE* f, int flags, const char* landmark,
263  Gif_ReadErrorHandler handler);
264 Gif_Stream* Gif_ReadRecord(const Gif_Record* record);
265 Gif_Stream* Gif_FullReadRecord(const Gif_Record* record, int flags,
266  const char* landmark,
267  Gif_ReadErrorHandler handler);
268 int Gif_WriteFile(Gif_Stream *gfs, FILE *f);
270  const Gif_CompressInfo *gcinfo, FILE *f);
271 
272 #define Gif_ReadFile(f) Gif_FullReadFile((f),GIF_READ_UNCOMPRESSED,0,0)
273 #define Gif_ReadRecord(r) Gif_FullReadRecord((r),GIF_READ_UNCOMPRESSED,0,0)
274 #define Gif_CompressImage(s, i) Gif_FullCompressImage((s),(i),0)
275 #define Gif_WriteFile(s, f) Gif_FullWriteFile((s),0,(f))
276 
277 typedef struct Gif_Writer Gif_Writer;
281 
282 
285 int Gif_InterlaceLine(int y, int height);
286 char * Gif_CopyString(const char *);
287 
288 #define GIF_T_STREAM (0)
289 #define GIF_T_IMAGE (1)
290 #define GIF_T_COLORMAP (2)
291 typedef void (*Gif_DeletionHookFunc)(int, void *, void *);
292 int Gif_AddDeletionHook(int, Gif_DeletionHookFunc, void *);
294 
295 #ifdef GIF_DEBUGGING
296 #define GIF_DEBUG(x) Gif_Debug x
297 void Gif_Debug(char *x, ...);
298 #else
299 #define GIF_DEBUG(x)
300 #endif
301 
302 void* Gif_Realloc(void* p, size_t s, size_t n,
303  const char* file, int line);
304 void Gif_Free(void* p);
305 #if !GIF_ALLOCATOR_DEFINED
306 # define Gif_Free free
307 #endif
308 
309 #ifndef Gif_New
310 # define Gif_New(t) ((t*) Gif_Realloc(0, sizeof(t), 1, __FILE__, __LINE__))
311 # define Gif_NewArray(t, n) ((t*) Gif_Realloc(0, sizeof(t), (n), __FILE__, __LINE__))
312 # define Gif_ReArray(p, t, n) ((p)=(t*) Gif_Realloc((void*) (p), sizeof(t), (n), __FILE__, __LINE__))
313 # define Gif_Delete(p) Gif_Free((void*) (p))
314 # define Gif_DeleteArray(p) Gif_Free((void*) (p))
315 #endif
316 
317 #ifdef __cplusplus
318 }
319 #endif
320 #endif
Gif_WriteFile
#define Gif_WriteFile(s, f)
Definition: gif.h:275
Gif_NewStream
Gif_Stream * Gif_NewStream(void)
Definition: giffunc.c:22
Gif_Extension
Definition: gif.h:222
Gif_FullUncompressImage
int Gif_FullUncompressImage(Gif_Stream *gfs, Gif_Image *gfi, Gif_ReadErrorHandler handler)
Definition: gifread.c:520
Gif_Extension::image
Gif_Image * image
Definition: gif.h:231
Gif_Stream::background
uint16_t background
Definition: gif.h:47
Gif_GetImage
Gif_Image * Gif_GetImage(Gif_Stream *gfs, int i)
Definition: giffunc.c:675
Gif_Code
uint16_t Gif_Code
Definition: gif.h:33
Gif_Extension::appname
char * appname
Definition: gif.h:224
Gif_NewFullColormap
Gif_Colormap * Gif_NewFullColormap(int count, int capacity)
Definition: giffunc.c:90
Gif_ReadErrorHandler
void(* Gif_ReadErrorHandler)(Gif_Stream *gfs, Gif_Image *gfi, int is_error, const char *error_text)
Definition: gif.h:141
Gif_InitCompressInfo
void Gif_InitCompressInfo(Gif_CompressInfo *gcinfo)
Definition: giffunc.c:821
Gif_Image::local
Gif_Colormap * local
Definition: gif.h:97
Gif_InterlaceLine
int Gif_InterlaceLine(int y, int height)
Definition: giffunc.c:766
Gif_Color::gfc_red
uint8_t gfc_red
Definition: gif.h:173
Gif_Image::free_compressed
void(* free_compressed)(void *)
Definition: gif.h:108
Gif_Colormap
Definition: gif.h:180
Gif_CompressInfo
Definition: gif.h:146
Gif_Unoptimize
int Gif_Unoptimize(Gif_Stream *)
Definition: gifunopt.c:231
Gif_Stream::loopcount
long loopcount
Definition: gif.h:51
Gif_ImageColorBound
int Gif_ImageColorBound(const Gif_Image *gfi)
Definition: giffunc.c:227
Gif_Image::top
uint16_t top
Definition: gif.h:91
Gif_Writer
Definition: gifwrite.c:73
Gif_Comment
Definition: gif.h:207
Gif_Color
Definition: gif.h:171
Gif_AddColor
int Gif_AddColor(Gif_Colormap *, Gif_Color *, int look_from)
Definition: giffunc.c:654
Gif_Image::compressed_errors
uint32_t compressed_errors
Definition: gif.h:106
Gif_Extension::length
uint32_t length
Definition: gif.h:227
Gif_Debug
void Gif_Debug(char *x,...)
Definition: giffunc.c:829
Gif_CopyImage
Gif_Image * Gif_CopyImage(Gif_Image *gfi)
Definition: giffunc.c:390
Gif_Stream::landmark
const char * landmark
Definition: gif.h:59
Gif_SetUncompressedImage
int Gif_SetUncompressedImage(Gif_Image *gfi, uint8_t *data, void(*free_data)(void *), int data_interlaced)
Definition: giffunc.c:781
Gif_Writer::gcinfo
Gif_CompressInfo gcinfo
Definition: gifwrite.c:78
Gif_AddDeletionHook
int Gif_AddDeletionHook(int, Gif_DeletionHookFunc, void *)
Definition: giffunc.c:601
Gif_CompressImage
#define Gif_CompressImage(s, i)
Definition: gif.h:274
Gif_ReleaseCompressedImage
void Gif_ReleaseCompressedImage(Gif_Image *gfi)
Definition: giffunc.c:702
Gif_Comment::str
char ** str
Definition: gif.h:208
Gif_Comment::len
int * len
Definition: gif.h:209
Gif_Stream::end_extension_list
Gif_Extension * end_extension_list
Definition: gif.h:54
Gif_DeleteImage
void Gif_DeleteImage(Gif_Image *gfi)
Definition: giffunc.c:519
Gif_Image::width
uint16_t width
Definition: gif.h:88
Gif_AddImage
int Gif_AddImage(Gif_Stream *gfs, Gif_Image *gfi)
Definition: giffunc.c:195
Gif_RemoveDeletionHook
void Gif_RemoveDeletionHook(int, Gif_DeletionHookFunc, void *)
Definition: giffunc.c:616
Gif_Image::compressed
uint8_t * compressed
Definition: gif.h:107
Gif_Image::free_user_data
void(* free_user_data)(void *)
Definition: gif.h:112
Gif_Extension::kind
int kind
Definition: gif.h:223
Gif_ReleaseUncompressedImage
void Gif_ReleaseUncompressedImage(Gif_Image *gfi)
Definition: giffunc.c:713
Gif_IncrementalWriteImage
int Gif_IncrementalWriteImage(Gif_Writer *grr, Gif_Stream *gfs, Gif_Image *gfi)
Definition: gifwrite.c:1098
Gif_Image::compressed_len
uint32_t compressed_len
Definition: gif.h:105
Gif_Color::gfc_green
uint8_t gfc_green
Definition: gif.h:174
Gif_DeleteColormap
void Gif_DeleteColormap(Gif_Colormap *)
Definition: giffunc.c:546
Gif_Image::transparent
short transparent
Definition: gif.h:96
Gif_ImageNumber
int Gif_ImageNumber(Gif_Stream *gfs, Gif_Image *gfi)
Definition: giffunc.c:294
Gif_Extension::applength
int applength
Definition: gif.h:225
Gif_Color::gfc_blue
uint8_t gfc_blue
Definition: gif.h:175
Gif_Image::left
uint16_t left
Definition: gif.h:90
Gif_CopyExtension
Gif_Extension * Gif_CopyExtension(Gif_Extension *gfex)
Definition: giffunc.c:155
Gif_Image::comment
Gif_Comment * comment
Definition: gif.h:100
Gif_DeletionHookFunc
void(* Gif_DeletionHookFunc)(int, void *, void *)
Definition: gif.h:291
Gif_CopyStreamSkeleton
Gif_Stream * Gif_CopyStreamSkeleton(Gif_Stream *)
Definition: giffunc.c:337
Gif_Stream::errors
unsigned errors
Definition: gif.h:56
Gif_Record
Definition: gif.h:246
Gif_Stream::nimages
int nimages
Definition: gif.h:43
Gif_FullWriteFile
int Gif_FullWriteFile(Gif_Stream *gfs, const Gif_CompressInfo *gcinfo, FILE *f)
Definition: gifwrite.c:1070
Gif_Stream::refcount
int refcount
Definition: gif.h:60
Gif_ReadRecord
#define Gif_ReadRecord(r)
Definition: gif.h:273
Gif_Record::data
const unsigned char * data
Definition: gif.h:247
Gif_FullReadRecord
Gif_Stream * Gif_FullReadRecord(const Gif_Record *record, int flags, const char *landmark, Gif_ReadErrorHandler handler)
Definition: gifread.c:935
Gif_CompressInfo::loss
int loss
Definition: gif.h:148
Gif_Extension::next
Gif_Extension * next
Definition: gif.h:232
Gif_ReadFile
#define Gif_ReadFile(f)
Definition: gif.h:272
Gif_Extension::stream
Gif_Stream * stream
Definition: gif.h:230
Gif_Image::user_flags
uint32_t user_flags
Definition: gif.h:110
Gif_Stream::screen_height
uint16_t screen_height
Definition: gif.h:50
Gif_SetErrorHandler
void Gif_SetErrorHandler(Gif_ReadErrorHandler handler)
Definition: gifread.c:963
Gif_Image::free_image_data
void(* free_image_data)(void *)
Definition: gif.h:103
Gif_FullReadFile
Gif_Stream * Gif_FullReadFile(FILE *f, int flags, const char *landmark, Gif_ReadErrorHandler handler)
Definition: gifread.c:920
Gif_Colormap::user_flags
uint32_t user_flags
Definition: gif.h:183
Gif_Image::refcount
int refcount
Definition: gif.h:113
Gif_Stream::imagescap
int imagescap
Definition: gif.h:44
Gif_Extension::packetized
int packetized
Definition: gif.h:228
Gif_Image::user_data
void * user_data
Definition: gif.h:111
Gif_CopyColormap
Gif_Colormap * Gif_CopyColormap(Gif_Colormap *)
Definition: giffunc.c:374
Gif_IncrementalWriteFileInit
Gif_Writer * Gif_IncrementalWriteFileInit(Gif_Stream *gfs, const Gif_CompressInfo *gcinfo, FILE *f)
Definition: gifwrite.c:1082
Gif_Stream::screen_width
uint16_t screen_width
Definition: gif.h:49
Gif_Image::disposal
uint8_t disposal
Definition: gif.h:93
Gif_Image
Definition: gif.h:84
Gif_Extension::free_data
void(* free_data)(void *)
Definition: gif.h:233
Gif_Stream::user_flags
uint32_t user_flags
Definition: gif.h:57
Gif_CopyStreamImages
Gif_Stream * Gif_CopyStreamImages(Gif_Stream *)
Definition: giffunc.c:356
Gif_IncrementalWriteComplete
int Gif_IncrementalWriteComplete(Gif_Writer *grr, Gif_Stream *gfs)
Definition: gifwrite.c:1113
Gif_Stream::images
Gif_Image ** images
Definition: gif.h:42
Gif_CreateUncompressedImage
int Gif_CreateUncompressedImage(Gif_Image *gfi, int data_interlaced)
Definition: giffunc.c:813
Gif_Stream
Definition: gif.h:41
Gif_AddCommentTake
int Gif_AddCommentTake(Gif_Comment *, char *, int)
Definition: giffunc.c:237
Gif_NewComment
Gif_Comment * Gif_NewComment(void)
Definition: giffunc.c:113
Gif_Image::identifier
char * identifier
Definition: gif.h:99
Gif_Realloc
void * Gif_Realloc(void *p, size_t s, size_t n, const char *file, int line)
Definition: fmalloc.c:13
Gif_CalculateScreenSize
void Gif_CalculateScreenSize(Gif_Stream *, int force)
Definition: giffunc.c:306
Gif_CompressInfo::flags
int flags
Definition: gif.h:147
Gif_Image::image_data
uint8_t * image_data
Definition: gif.h:86
Gif_AddExtension
int Gif_AddExtension(Gif_Stream *gfs, Gif_Image *gfi, Gif_Extension *gfex)
Definition: giffunc.c:277
Gif_Color::pixel
uint32_t pixel
Definition: gif.h:176
Gif_DeleteComment
void Gif_DeleteComment(Gif_Comment *)
Definition: giffunc.c:562
Gif_RemoveImage
void Gif_RemoveImage(Gif_Stream *gfs, int i)
Definition: giffunc.c:214
Gif_NewImage
Gif_Image * Gif_NewImage(void)
Definition: giffunc.c:44
Gif_NewColormap
Gif_Colormap * Gif_NewColormap(void)
Definition: giffunc.c:75
Gif_ColorEq
int Gif_ColorEq(Gif_Color *, Gif_Color *)
Definition: giffunc.c:636
Gif_DeleteStream
void Gif_DeleteStream(Gif_Stream *)
Definition: giffunc.c:494
Gif_CopyString
char * Gif_CopyString(const char *)
Definition: giffunc.c:179
Gif_Color::haspixel
uint8_t haspixel
Definition: gif.h:172
Gif_Image::delay
uint16_t delay
Definition: gif.h:92
Gif_FindColor
int Gif_FindColor(Gif_Colormap *, Gif_Color *)
Definition: giffunc.c:643
Gif_Image::extension_list
Gif_Extension * extension_list
Definition: gif.h:101
Gif_Image::img
uint8_t ** img
Definition: gif.h:85
Gif_MakeImageEmpty
void Gif_MakeImageEmpty(Gif_Image *gfi)
Definition: giffunc.c:472
Gif_Comment::count
int count
Definition: gif.h:210
Gif_NewExtension
Gif_Extension * Gif_NewExtension(int kind, const char *appname, int applength)
Definition: giffunc.c:126
Gif_DeleteExtension
void Gif_DeleteExtension(Gif_Extension *gfex)
Definition: giffunc.c:576
Gif_Image::interlace
uint8_t interlace
Definition: gif.h:94
Gif_Free
#define Gif_Free
Definition: gif.h:306
Gif_Colormap::refcount
int refcount
Definition: gif.h:184
Gif_Colormap::col
Gif_Color * col
Definition: gif.h:185
Gif_Record::length
uint32_t length
Definition: gif.h:248
Gif_FullUnoptimize
int Gif_FullUnoptimize(Gif_Stream *, int flags)
Definition: gifunopt.c:176
Gif_Writer::f
FILE * f
Definition: gifwrite.c:74
Gif_AddComment
int Gif_AddComment(Gif_Comment *, const char *, int)
Definition: giffunc.c:259
line
static uint16_t * line
Definition: gifdiff.c:52
Gif_Extension::data
uint8_t * data
Definition: gif.h:226
Gif_Stream::end_comment
Gif_Comment * end_comment
Definition: gif.h:53
inttypes.h
Gif_Stream::global
Gif_Colormap * global
Definition: gif.h:46
Gif_Colormap::ncol
int ncol
Definition: gif.h:181
Gif_GetNamedImage
Gif_Image * Gif_GetNamedImage(Gif_Stream *gfs, const char *name)
Definition: giffunc.c:685
Gif_Comment::cap
int cap
Definition: gif.h:211
Gif_ClipImage
int Gif_ClipImage(Gif_Image *gfi, int l, int t, int w, int h)
Definition: giffunc.c:725
Gif_Colormap::capacity
int capacity
Definition: gif.h:182
Gif_Image::height
uint16_t height
Definition: gif.h:89
Gif_FullCompressImage
int Gif_FullCompressImage(Gif_Stream *gfs, Gif_Image *gfi, const Gif_CompressInfo *gcinfo)
Definition: gifwrite.c:752