1 /*
   2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.  Oracle designates this
   7  * particular file as subject to the "Classpath" exception as provided
   8  * by Oracle in the LICENSE file that accompanied this code.
   9  *
  10  * This code is distributed in the hope that it will be useful, but WITHOUT
  11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  13  * version 2 for more details (a copy is included in the LICENSE file that
  14  * accompanied this code).
  15  *
  16  * You should have received a copy of the GNU General Public License version
  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /******************************************************************************
  26 
  27 gif_lib.h - service library for decoding and encoding GIF images
  28 
  29 *****************************************************************************/
  30 
  31 #ifndef _GIF_LIB_H_
  32 #define _GIF_LIB_H_ 1
  33 
  34 #ifdef __cplusplus
  35 extern "C" {
  36 #endif /* __cplusplus */
  37 
  38 #define GIFLIB_MAJOR 5
  39 #define GIFLIB_MINOR 1
  40 #define GIFLIB_RELEASE 4
  41 
  42 #define GIF_ERROR   0
  43 #define GIF_OK      1
  44 
  45 #include <stddef.h>
  46 
  47 #ifdef bool
  48 #undef bool
  49 #endif
  50 typedef int bool;
  51 #define false 0
  52 #define true 1
  53 
  54 #define GIF_STAMP "GIFVER"          /* First chars in file - GIF stamp.  */
  55 #define GIF_STAMP_LEN sizeof(GIF_STAMP) - 1
  56 #define GIF_VERSION_POS 3           /* Version first character in stamp. */
  57 #define GIF87_STAMP "GIF87a"        /* First chars in file - GIF stamp.  */
  58 #define GIF89_STAMP "GIF89a"        /* First chars in file - GIF stamp.  */
  59 
  60 typedef unsigned char GifPixelType;
  61 typedef unsigned char *GifRowType;
  62 typedef unsigned char GifByteType;
  63 typedef unsigned int GifPrefixType;
  64 typedef int GifWord;
  65 
  66 typedef struct GifColorType {
  67     GifByteType Red, Green, Blue;
  68 } GifColorType;
  69 
  70 typedef struct ColorMapObject {
  71     int ColorCount;
  72     int BitsPerPixel;
  73     bool SortFlag;
  74     GifColorType *Colors;    /* on malloc(3) heap */
  75 } ColorMapObject;
  76 
  77 typedef struct GifImageDesc {
  78     GifWord Left, Top, Width, Height;   /* Current image dimensions. */
  79     bool Interlace;                     /* Sequential/Interlaced lines. */
  80     ColorMapObject *ColorMap;           /* The local color map */
  81 } GifImageDesc;
  82 
  83 typedef struct ExtensionBlock {
  84     int ByteCount;
  85     GifByteType *Bytes; /* on malloc(3) heap */
  86     int Function;       /* The block function code */
  87 #define CONTINUE_EXT_FUNC_CODE    0x00    /* continuation subblock */
  88 #define COMMENT_EXT_FUNC_CODE     0xfe    /* comment */
  89 #define GRAPHICS_EXT_FUNC_CODE    0xf9    /* graphics control (GIF89) */
  90 #define PLAINTEXT_EXT_FUNC_CODE   0x01    /* plaintext */
  91 #define APPLICATION_EXT_FUNC_CODE 0xff    /* application block */
  92 } ExtensionBlock;
  93 
  94 typedef struct SavedImage {
  95     GifImageDesc ImageDesc;
  96     GifByteType *RasterBits;         /* on malloc(3) heap */
  97     int ExtensionBlockCount;         /* Count of extensions before image */
  98     ExtensionBlock *ExtensionBlocks; /* Extensions before image */
  99 } SavedImage;
 100 
 101 typedef struct GifFileType {
 102     GifWord SWidth, SHeight;         /* Size of virtual canvas */
 103     GifWord SColorResolution;        /* How many colors can we generate? */
 104     GifWord SBackGroundColor;        /* Background color for virtual canvas */
 105     GifByteType AspectByte;          /* Used to compute pixel aspect ratio */
 106     ColorMapObject *SColorMap;       /* Global colormap, NULL if nonexistent. */
 107     int ImageCount;                  /* Number of current image (both APIs) */
 108     GifImageDesc Image;              /* Current image (low-level API) */
 109     SavedImage *SavedImages;         /* Image sequence (high-level API) */
 110     int ExtensionBlockCount;         /* Count extensions past last image */
 111     ExtensionBlock *ExtensionBlocks; /* Extensions past last image */
 112     int Error;                       /* Last error condition reported */
 113     void *UserData;                  /* hook to attach user data (TVT) */
 114     void *Private;                   /* Don't mess with this! */
 115 } GifFileType;
 116 
 117 #define GIF_ASPECT_RATIO(n)    ((n)+15.0/64.0)
 118 
 119 typedef enum {
 120     UNDEFINED_RECORD_TYPE,
 121     SCREEN_DESC_RECORD_TYPE,
 122     IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */
 123     EXTENSION_RECORD_TYPE,  /* Begin with '!' */
 124     TERMINATE_RECORD_TYPE   /* Begin with ';' */
 125 } GifRecordType;
 126 
 127 /* func type to read gif data from arbitrary sources (TVT) */
 128 typedef int (*InputFunc) (GifFileType *, GifByteType *, int);
 129 
 130 /* func type to write gif data to arbitrary targets.
 131  * Returns count of bytes written. (MRB)
 132  */
 133 typedef int (*OutputFunc) (GifFileType *, const GifByteType *, int);
 134 
 135 /******************************************************************************
 136  GIF89 structures
 137 ******************************************************************************/
 138 
 139 typedef struct GraphicsControlBlock {
 140     int DisposalMode;
 141 #define DISPOSAL_UNSPECIFIED      0       /* No disposal specified. */
 142 #define DISPOSE_DO_NOT            1       /* Leave image in place */
 143 #define DISPOSE_BACKGROUND        2       /* Set area too background color */
 144 #define DISPOSE_PREVIOUS          3       /* Restore to previous content */
 145     bool UserInputFlag;      /* User confirmation required before disposal */
 146     int DelayTime;           /* pre-display delay in 0.01sec units */
 147     int TransparentColor;    /* Palette index for transparency, -1 if none */
 148 #define NO_TRANSPARENT_COLOR    -1
 149 } GraphicsControlBlock;
 150 
 151 /******************************************************************************
 152  GIF encoding routines
 153 ******************************************************************************/
 154 
 155 /* Main entry points */
 156 GifFileType *EGifOpenFileName(const char *GifFileName,
 157                               const bool GifTestExistence, int *Error);
 158 GifFileType *EGifOpenFileHandle(const int GifFileHandle, int *Error);
 159 GifFileType *EGifOpen(void *userPtr, OutputFunc writeFunc, int *Error);
 160 int EGifSpew(GifFileType * GifFile);
 161 const char *EGifGetGifVersion(GifFileType *GifFile); /* new in 5.x */
 162 int EGifCloseFile(GifFileType *GifFile, int *ErrorCode);
 163 
 164 #define E_GIF_SUCCEEDED          0
 165 #define E_GIF_ERR_OPEN_FAILED    1    /* And EGif possible errors. */
 166 #define E_GIF_ERR_WRITE_FAILED   2
 167 #define E_GIF_ERR_HAS_SCRN_DSCR  3
 168 #define E_GIF_ERR_HAS_IMAG_DSCR  4
 169 #define E_GIF_ERR_NO_COLOR_MAP   5
 170 #define E_GIF_ERR_DATA_TOO_BIG   6
 171 #define E_GIF_ERR_NOT_ENOUGH_MEM 7
 172 #define E_GIF_ERR_DISK_IS_FULL   8
 173 #define E_GIF_ERR_CLOSE_FAILED   9
 174 #define E_GIF_ERR_NOT_WRITEABLE  10
 175 
 176 /* These are legacy.  You probably do not want to call them directly */
 177 int EGifPutScreenDesc(GifFileType *GifFile,
 178                       const int GifWidth, const int GifHeight,
 179                       const int GifColorRes,
 180                       const int GifBackGround,
 181                       const ColorMapObject *GifColorMap);
 182 int EGifPutImageDesc(GifFileType *GifFile,
 183                      const int GifLeft, const int GifTop,
 184                      const int GifWidth, const int GifHeight,
 185                      const bool GifInterlace,
 186                      const ColorMapObject *GifColorMap);
 187 void EGifSetGifVersion(GifFileType *GifFile, const bool gif89);
 188 int EGifPutLine(GifFileType *GifFile, GifPixelType *GifLine,
 189                 int GifLineLen);
 190 int EGifPutPixel(GifFileType *GifFile, const GifPixelType GifPixel);
 191 int EGifPutComment(GifFileType *GifFile, const char *GifComment);
 192 int EGifPutExtensionLeader(GifFileType *GifFile, const int GifExtCode);
 193 int EGifPutExtensionBlock(GifFileType *GifFile,
 194                          const int GifExtLen, const void *GifExtension);
 195 int EGifPutExtensionTrailer(GifFileType *GifFile);
 196 int EGifPutExtension(GifFileType *GifFile, const int GifExtCode,
 197                      const int GifExtLen,
 198                      const void *GifExtension);
 199 int EGifPutCode(GifFileType *GifFile, int GifCodeSize,
 200                 const GifByteType *GifCodeBlock);
 201 int EGifPutCodeNext(GifFileType *GifFile,
 202                     const GifByteType *GifCodeBlock);
 203 
 204 /******************************************************************************
 205  GIF decoding routines
 206 ******************************************************************************/
 207 
 208 /* Main entry points */
 209 GifFileType *DGifOpenFileName(const char *GifFileName, int *Error);
 210 GifFileType *DGifOpenFileHandle(int GifFileHandle, int *Error);
 211 int DGifSlurp(GifFileType * GifFile);
 212 GifFileType *DGifOpen(void *userPtr, InputFunc readFunc, int *Error);    /* new one (TVT) */
 213     int DGifCloseFile(GifFileType * GifFile, int *ErrorCode);
 214 
 215 #define D_GIF_SUCCEEDED          0
 216 #define D_GIF_ERR_OPEN_FAILED    101    /* And DGif possible errors. */
 217 #define D_GIF_ERR_READ_FAILED    102
 218 #define D_GIF_ERR_NOT_GIF_FILE   103
 219 #define D_GIF_ERR_NO_SCRN_DSCR   104
 220 #define D_GIF_ERR_NO_IMAG_DSCR   105
 221 #define D_GIF_ERR_NO_COLOR_MAP   106
 222 #define D_GIF_ERR_WRONG_RECORD   107
 223 #define D_GIF_ERR_DATA_TOO_BIG   108
 224 #define D_GIF_ERR_NOT_ENOUGH_MEM 109
 225 #define D_GIF_ERR_CLOSE_FAILED   110
 226 #define D_GIF_ERR_NOT_READABLE   111
 227 #define D_GIF_ERR_IMAGE_DEFECT   112
 228 #define D_GIF_ERR_EOF_TOO_SOON   113
 229 
 230 /* These are legacy.  You probably do not want to call them directly */
 231 int DGifGetScreenDesc(GifFileType *GifFile);
 232 int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
 233 int DGifGetImageDesc(GifFileType *GifFile);
 234 int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
 235 int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel);
 236 int DGifGetComment(GifFileType *GifFile, char *GifComment);
 237 int DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
 238                      GifByteType **GifExtension);
 239 int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension);
 240 int DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
 241                 GifByteType **GifCodeBlock);
 242 int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock);
 243 int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
 244 
 245 
 246 /******************************************************************************
 247  Color table quantization (deprecated)
 248 ******************************************************************************/
 249 int GifQuantizeBuffer(unsigned int Width, unsigned int Height,
 250                    int *ColorMapSize, GifByteType * RedInput,
 251                    GifByteType * GreenInput, GifByteType * BlueInput,
 252                    GifByteType * OutputBuffer,
 253                    GifColorType * OutputColorMap);
 254 
 255 /******************************************************************************
 256  Error handling and reporting.
 257 ******************************************************************************/
 258 extern const char *GifErrorString(int ErrorCode);     /* new in 2012 - ESR */
 259 
 260 /*****************************************************************************
 261  Everything below this point is new after version 1.2, supporting `slurp
 262  mode' for doing I/O in two big belts with all the image-bashing in core.
 263 ******************************************************************************/
 264 
 265 /******************************************************************************
 266  Color map handling from gif_alloc.c
 267 ******************************************************************************/
 268 
 269 extern ColorMapObject *GifMakeMapObject(int ColorCount,
 270                                      const GifColorType *ColorMap);
 271 extern void GifFreeMapObject(ColorMapObject *Object);
 272 extern ColorMapObject *GifUnionColorMap(const ColorMapObject *ColorIn1,
 273                                      const ColorMapObject *ColorIn2,
 274                                      GifPixelType ColorTransIn2[]);
 275 extern int GifBitSize(int n);
 276 
 277 extern void * reallocarray(void *optr, size_t nmemb, size_t size);
 278 
 279 
 280 /******************************************************************************
 281  Support for the in-core structures allocation (slurp mode).
 282 ******************************************************************************/
 283 
 284 extern void GifApplyTranslation(SavedImage *Image, GifPixelType Translation[]);
 285 extern int GifAddExtensionBlock(int *ExtensionBlock_Count,
 286                                 ExtensionBlock **ExtensionBlocks,
 287                                 int Function,
 288                                 unsigned int Len, unsigned char ExtData[]);
 289 extern void GifFreeExtensions(int *ExtensionBlock_Count,
 290                               ExtensionBlock **ExtensionBlocks);
 291 extern SavedImage *GifMakeSavedImage(GifFileType *GifFile,
 292                                   const SavedImage *CopyFrom);
 293 extern void GifFreeSavedImages(GifFileType *GifFile);
 294 
 295 /******************************************************************************
 296  5.x functions for GIF89 graphics control blocks
 297 ******************************************************************************/
 298 
 299 int DGifExtensionToGCB(const size_t GifExtensionLength,
 300                        const GifByteType *GifExtension,
 301                        GraphicsControlBlock *GCB);
 302 size_t EGifGCBToExtension(const GraphicsControlBlock *GCB,
 303                           GifByteType *GifExtension);
 304 
 305 int DGifSavedExtensionToGCB(GifFileType *GifFile,
 306                             int ImageIndex,
 307                             GraphicsControlBlock *GCB);
 308 int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB,
 309                             GifFileType *GifFile,
 310                             int ImageIndex);
 311 
 312 /******************************************************************************
 313  The library's internal utility font
 314 ******************************************************************************/
 315 
 316 #define GIF_FONT_WIDTH  8
 317 #define GIF_FONT_HEIGHT 8
 318 extern const unsigned char GifAsciiTable8x8[][GIF_FONT_WIDTH];
 319 
 320 extern void GifDrawText8x8(SavedImage *Image,
 321                      const int x, const int y,
 322                      const char *legend, const int color);
 323 
 324 extern void GifDrawBox(SavedImage *Image,
 325                     const int x, const int y,
 326                     const int w, const int d, const int color);
 327 
 328 extern void GifDrawRectangle(SavedImage *Image,
 329                    const int x, const int y,
 330                    const int w, const int d, const int color);
 331 
 332 extern void GifDrawBoxedText8x8(SavedImage *Image,
 333                           const int x, const int y,
 334                           const char *legend,
 335                           const int border, const int bg, const int fg);
 336 
 337 #ifdef __cplusplus
 338 }
 339 #endif /* __cplusplus */
 340 #endif /* _GIF_LIB_H */
 341 
 342 /* end */