0N/A/*
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/******************************************************************************
0N/A * "Gif-Lib" - Yet another gif library.
0N/A *
0N/A * Written by: Gershon Elber IBM PC Ver 1.1, Aug. 1990
0N/A ******************************************************************************
0N/A * The kernel of the GIF Decoding process can be found here.
0N/A ******************************************************************************
0N/A * History:
0N/A * 16 Jun 89 - Version 1.0 by Gershon Elber.
0N/A * 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names).
0N/A *****************************************************************************/
0N/A
0N/A/* !!!! */
0N/A
0N/A#ifdef HAVE_CONFIG_H
0N/A#include <config.h>
0N/A#endif
0N/A
0N/A#include <stdlib.h>
0N/A#if defined (__MSDOS__) && !defined(__DJGPP__) && !defined(__GNUC__)
0N/A#include <io.h>
0N/A#include <alloc.h>
0N/A#include <sys\stat.h>
0N/A#else
0N/A#include <sys/types.h>
0N/A#include <sys/stat.h>
0N/A#endif /* __MSDOS__ */
0N/A
0N/A#ifdef _WIN32
0N/A#include <io.h>
0N/A#define _OPEN_BINARY
0N/A#else
0N/A#include <unistd.h>
0N/A#endif
0N/A
0N/A#include <fcntl.h>
0N/A
0N/A#include <stdio.h>
0N/A#include <string.h>
0N/A#include "gif_lib.h"
0N/A#include "gif_lib_private.h"
0N/A
0N/A#define COMMENT_EXT_FUNC_CODE 0xfe /* Extension function code for
0N/A comment. */
0N/A
0N/A/* avoid extra function call in case we use fread (TVT) */
0N/A#define READ(_gif,_buf,_len) \
0N/A (((GifFilePrivateType*)_gif->Private)->Read ? \
4063N/A (size_t)((GifFilePrivateType*)_gif->Private)->Read(_gif,_buf,_len) : \
0N/A fread(_buf,1,_len,((GifFilePrivateType*)_gif->Private)->File))
0N/A
0N/Astatic int DGifGetWord(GifFileType *GifFile, int *Word);
0N/Astatic int DGifSetupDecompress(GifFileType *GifFile);
0N/Astatic int DGifDecompressLine(GifFileType *GifFile, GifPixelType *Line,
0N/A int LineLen);
0N/Astatic int DGifGetPrefixChar(unsigned int *Prefix, int Code, int ClearCode);
0N/Astatic int DGifDecompressInput(GifFileType *GifFile, int *Code);
0N/Astatic int DGifBufferedInput(GifFileType *GifFile, GifByteType *Buf,
0N/A GifByteType *NextByte);
0N/A
0N/A/******************************************************************************
0N/A * Open a new gif file for read, given by its name.
0N/A * Returns GifFileType pointer dynamically allocated which serves as the gif
0N/A * info record. _GifError is cleared if succesfull.
0N/A *****************************************************************************/
0N/AGifFileType *
0N/ADGifOpenFileName(const char *FileName) {
0N/A int FileHandle;
0N/A GifFileType *GifFile;
0N/A
0N/A if ((FileHandle = open(FileName, O_RDONLY
0N/A#if defined(__MSDOS__) || defined(_OPEN_BINARY)
0N/A | O_BINARY
0N/A#endif /* __MSDOS__ || _OPEN_BINARY */
0N/A )) == -1) {
0N/A _GifError = D_GIF_ERR_OPEN_FAILED;
0N/A return NULL;
0N/A }
0N/A
0N/A GifFile = DGifOpenFileHandle(FileHandle);
0N/A if (GifFile == (GifFileType *)NULL)
0N/A close(FileHandle);
0N/A return GifFile;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Update a new gif file, given its file handle.
0N/A * Returns GifFileType pointer dynamically allocated which serves as the gif
0N/A * info record. _GifError is cleared if succesfull.
0N/A *****************************************************************************/
0N/AGifFileType *
0N/ADGifOpenFileHandle(int FileHandle) {
0N/A
0N/A unsigned char Buf[GIF_STAMP_LEN + 1];
0N/A GifFileType *GifFile;
0N/A GifFilePrivateType *Private;
0N/A FILE *f;
0N/A
0N/A GifFile = (GifFileType *)malloc(sizeof(GifFileType));
0N/A if (GifFile == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return NULL;
0N/A }
0N/A
0N/A memset(GifFile, '\0', sizeof(GifFileType));
0N/A
0N/A Private = (GifFilePrivateType *)malloc(sizeof(GifFilePrivateType));
0N/A if (Private == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A#ifdef __MSDOS__
0N/A setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
0N/A#endif /* __MSDOS__ */
0N/A
0N/A f = fdopen(FileHandle, "rb"); /* Make it into a stream: */
0N/A
0N/A#ifdef __MSDOS__
0N/A setvbuf(f, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE); /* And inc. stream
0N/A buffer. */
0N/A#endif /* __MSDOS__ */
0N/A
0N/A GifFile->Private = (VoidPtr)Private;
0N/A Private->FileHandle = FileHandle;
0N/A Private->File = f;
0N/A Private->FileState = FILE_STATE_READ;
0N/A Private->Read = 0; /* don't use alternate input method (TVT) */
0N/A GifFile->UserData = 0; /* TVT */
0N/A
0N/A /* Lets see if this is a GIF file: */
0N/A if (READ(GifFile, Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A fclose(f);
0N/A free((char *)Private);
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A /* The GIF Version number is ignored at this time. Maybe we should do
0N/A * something more useful with it. */
0N/A Buf[GIF_STAMP_LEN] = 0;
0N/A if (strncmp(GIF_STAMP, (const char*)Buf, GIF_VERSION_POS) != 0) {
0N/A _GifError = D_GIF_ERR_NOT_GIF_FILE;
0N/A fclose(f);
0N/A free((char *)Private);
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A if (DGifGetScreenDesc(GifFile) == GIF_ERROR) {
0N/A fclose(f);
0N/A free((char *)Private);
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A _GifError = 0;
0N/A
0N/A return GifFile;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * GifFileType constructor with user supplied input function (TVT)
0N/A *****************************************************************************/
0N/AGifFileType *
0N/ADGifOpen(void *userData,
0N/A InputFunc readFunc) {
0N/A
0N/A unsigned char Buf[GIF_STAMP_LEN + 1];
0N/A GifFileType *GifFile;
0N/A GifFilePrivateType *Private;
0N/A
0N/A GifFile = (GifFileType *)malloc(sizeof(GifFileType));
0N/A if (GifFile == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return NULL;
0N/A }
0N/A
0N/A memset(GifFile, '\0', sizeof(GifFileType));
0N/A
0N/A Private = (GifFilePrivateType *)malloc(sizeof(GifFilePrivateType));
0N/A if (!Private) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A GifFile->Private = (VoidPtr)Private;
0N/A Private->FileHandle = 0;
0N/A Private->File = 0;
0N/A Private->FileState = FILE_STATE_READ;
0N/A
0N/A Private->Read = readFunc; /* TVT */
0N/A GifFile->UserData = userData; /* TVT */
0N/A
0N/A /* Lets see if this is a GIF file: */
0N/A if (READ(GifFile, Buf, GIF_STAMP_LEN) != GIF_STAMP_LEN) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A free((char *)Private);
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A /* The GIF Version number is ignored at this time. Maybe we should do
0N/A * something more useful with it. */
0N/A Buf[GIF_STAMP_LEN] = 0;
0N/A if (strncmp(GIF_STAMP, (const char*)Buf, GIF_VERSION_POS) != 0) {
0N/A _GifError = D_GIF_ERR_NOT_GIF_FILE;
0N/A free((char *)Private);
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A if (DGifGetScreenDesc(GifFile) == GIF_ERROR) {
0N/A free((char *)Private);
0N/A free((char *)GifFile);
0N/A return NULL;
0N/A }
0N/A
0N/A _GifError = 0;
0N/A
0N/A return GifFile;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * This routine should be called before any other DGif calls. Note that
0N/A * this routine is called automatically from DGif file open routines.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetScreenDesc(GifFileType * GifFile) {
0N/A
0N/A int i, BitsPerPixel;
0N/A GifByteType Buf[3];
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A /* Put the screen descriptor into the file: */
0N/A if (DGifGetWord(GifFile, &GifFile->SWidth) == GIF_ERROR ||
0N/A DGifGetWord(GifFile, &GifFile->SHeight) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A
0N/A if (READ(GifFile, Buf, 3) != 3) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A GifFile->SColorResolution = (((Buf[0] & 0x70) + 1) >> 4) + 1;
0N/A BitsPerPixel = (Buf[0] & 0x07) + 1;
0N/A GifFile->SBackGroundColor = Buf[1];
0N/A if (Buf[0] & 0x80) { /* Do we have global color map? */
0N/A
0N/A GifFile->SColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
0N/A if (GifFile->SColorMap == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A /* Get the global color map: */
0N/A for (i = 0; i < GifFile->SColorMap->ColorCount; i++) {
0N/A if (READ(GifFile, Buf, 3) != 3) {
0N/A FreeMapObject(GifFile->SColorMap);
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A GifFile->SColorMap->Colors[i].Red = Buf[0];
0N/A GifFile->SColorMap->Colors[i].Green = Buf[1];
0N/A GifFile->SColorMap->Colors[i].Blue = Buf[2];
0N/A }
0N/A } else {
0N/A GifFile->SColorMap = NULL;
0N/A }
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * This routine should be called before any attempt to read an image.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetRecordType(GifFileType * GifFile,
0N/A GifRecordType * Type) {
0N/A
0N/A GifByteType Buf;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (READ(GifFile, &Buf, 1) != 1) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A switch (Buf) {
0N/A case ',':
0N/A *Type = IMAGE_DESC_RECORD_TYPE;
0N/A break;
0N/A case '!':
0N/A *Type = EXTENSION_RECORD_TYPE;
0N/A break;
0N/A case ';':
0N/A *Type = TERMINATE_RECORD_TYPE;
0N/A break;
0N/A default:
0N/A *Type = UNDEFINED_RECORD_TYPE;
0N/A _GifError = D_GIF_ERR_WRONG_RECORD;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * This routine should be called before any attempt to read an image.
0N/A * Note it is assumed the Image desc. header (',') has been read.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetImageDesc(GifFileType * GifFile) {
0N/A
0N/A int i, BitsPerPixel;
0N/A GifByteType Buf[3];
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A SavedImage *sp;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (DGifGetWord(GifFile, &GifFile->Image.Left) == GIF_ERROR ||
0N/A DGifGetWord(GifFile, &GifFile->Image.Top) == GIF_ERROR ||
0N/A DGifGetWord(GifFile, &GifFile->Image.Width) == GIF_ERROR ||
0N/A DGifGetWord(GifFile, &GifFile->Image.Height) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A if (READ(GifFile, Buf, 1) != 1) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A BitsPerPixel = (Buf[0] & 0x07) + 1;
0N/A GifFile->Image.Interlace = (Buf[0] & 0x40);
0N/A if (Buf[0] & 0x80) { /* Does this image have local color map? */
0N/A
0N/A /*** FIXME: Why do we check both of these in order to do this?
0N/A * Why do we have both Image and SavedImages? */
0N/A if (GifFile->Image.ColorMap && GifFile->SavedImages == NULL)
0N/A FreeMapObject(GifFile->Image.ColorMap);
0N/A
0N/A GifFile->Image.ColorMap = MakeMapObject(1 << BitsPerPixel, NULL);
0N/A if (GifFile->Image.ColorMap == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A /* Get the image local color map: */
0N/A for (i = 0; i < GifFile->Image.ColorMap->ColorCount; i++) {
0N/A if (READ(GifFile, Buf, 3) != 3) {
0N/A FreeMapObject(GifFile->Image.ColorMap);
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A GifFile->Image.ColorMap->Colors[i].Red = Buf[0];
0N/A GifFile->Image.ColorMap->Colors[i].Green = Buf[1];
0N/A GifFile->Image.ColorMap->Colors[i].Blue = Buf[2];
0N/A }
0N/A } else if (GifFile->Image.ColorMap) {
0N/A FreeMapObject(GifFile->Image.ColorMap);
0N/A GifFile->Image.ColorMap = NULL;
0N/A }
0N/A
0N/A if (GifFile->SavedImages) {
0N/A if ((GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
0N/A sizeof(SavedImage) *
0N/A (GifFile->ImageCount + 1))) == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return GIF_ERROR;
0N/A }
0N/A } else {
0N/A if ((GifFile->SavedImages =
0N/A (SavedImage *) malloc(sizeof(SavedImage))) == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return GIF_ERROR;
0N/A }
0N/A }
0N/A
0N/A sp = &GifFile->SavedImages[GifFile->ImageCount];
0N/A memcpy(&sp->ImageDesc, &GifFile->Image, sizeof(GifImageDesc));
0N/A if (GifFile->Image.ColorMap != NULL) {
0N/A sp->ImageDesc.ColorMap = MakeMapObject(
0N/A GifFile->Image.ColorMap->ColorCount,
0N/A GifFile->Image.ColorMap->Colors);
0N/A if (sp->ImageDesc.ColorMap == NULL) {
0N/A _GifError = D_GIF_ERR_NOT_ENOUGH_MEM;
0N/A return GIF_ERROR;
0N/A }
0N/A }
0N/A sp->RasterBits = (unsigned char *)NULL;
0N/A sp->ExtensionBlockCount = 0;
0N/A sp->ExtensionBlocks = (ExtensionBlock *) NULL;
0N/A
0N/A GifFile->ImageCount++;
0N/A
0N/A Private->PixelCount = (long)GifFile->Image.Width *
0N/A (long)GifFile->Image.Height;
0N/A
0N/A DGifSetupDecompress(GifFile); /* Reset decompress algorithm parameters. */
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Get one full scanned line (Line) of length LineLen from GIF file.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetLine(GifFileType * GifFile,
0N/A GifPixelType * Line,
0N/A int LineLen) {
0N/A
0N/A GifByteType *Dummy;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (!LineLen)
0N/A LineLen = GifFile->Image.Width;
0N/A
0N/A#if defined(__MSDOS__) || defined(__GNUC__)
0N/A if ((Private->PixelCount -= LineLen) > 0xffff0000UL) {
0N/A#else
0N/A if ((Private->PixelCount -= LineLen) > 0xffff0000) {
0N/A#endif /* __MSDOS__ */
0N/A _GifError = D_GIF_ERR_DATA_TOO_BIG;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (DGifDecompressLine(GifFile, Line, LineLen) == GIF_OK) {
0N/A if (Private->PixelCount == 0) {
0N/A /* We probably would not be called any more, so lets clean
0N/A * everything before we return: need to flush out all rest of
0N/A * image until empty block (size 0) detected. We use GetCodeNext. */
0N/A do
0N/A if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A while (Dummy != NULL) ;
0N/A }
0N/A return GIF_OK;
0N/A } else
0N/A return GIF_ERROR;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Put one pixel (Pixel) into GIF file.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetPixel(GifFileType * GifFile,
0N/A GifPixelType Pixel) {
0N/A
0N/A GifByteType *Dummy;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A#if defined(__MSDOS__) || defined(__GNUC__)
0N/A if (--Private->PixelCount > 0xffff0000UL)
0N/A#else
0N/A if (--Private->PixelCount > 0xffff0000)
0N/A#endif /* __MSDOS__ */
0N/A {
0N/A _GifError = D_GIF_ERR_DATA_TOO_BIG;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (DGifDecompressLine(GifFile, &Pixel, 1) == GIF_OK) {
0N/A if (Private->PixelCount == 0) {
0N/A /* We probably would not be called any more, so lets clean
0N/A * everything before we return: need to flush out all rest of
0N/A * image until empty block (size 0) detected. We use GetCodeNext. */
0N/A do
0N/A if (DGifGetCodeNext(GifFile, &Dummy) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A while (Dummy != NULL) ;
0N/A }
0N/A return GIF_OK;
0N/A } else
0N/A return GIF_ERROR;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Get an extension block (see GIF manual) from gif file. This routine only
0N/A * returns the first data block, and DGifGetExtensionNext should be called
0N/A * after this one until NULL extension is returned.
0N/A * The Extension should NOT be freed by the user (not dynamically allocated).
0N/A * Note it is assumed the Extension desc. header ('!') has been read.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetExtension(GifFileType * GifFile,
0N/A int *ExtCode,
0N/A GifByteType ** Extension) {
0N/A
0N/A GifByteType Buf;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (READ(GifFile, &Buf, 1) != 1) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A *ExtCode = Buf;
0N/A
0N/A return DGifGetExtensionNext(GifFile, Extension);
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Get a following extension block (see GIF manual) from gif file. This
0N/A * routine should be called until NULL Extension is returned.
0N/A * The Extension should NOT be freed by the user (not dynamically allocated).
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetExtensionNext(GifFileType * GifFile,
0N/A GifByteType ** Extension) {
0N/A
0N/A GifByteType Buf;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (READ(GifFile, &Buf, 1) != 1) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A if (Buf > 0) {
0N/A *Extension = Private->Buf; /* Use private unused buffer. */
0N/A (*Extension)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
0N/A if (READ(GifFile, &((*Extension)[1]), Buf) != Buf) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A } else
0N/A *Extension = NULL;
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * This routine should be called last, to close the GIF file.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifCloseFile(GifFileType * GifFile) {
0N/A
0N/A GifFilePrivateType *Private;
0N/A FILE *File;
0N/A
0N/A if (GifFile == NULL)
0N/A return GIF_ERROR;
0N/A
0N/A Private = (GifFilePrivateType *) GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A File = Private->File;
0N/A
0N/A if (GifFile->Image.ColorMap) {
0N/A FreeMapObject(GifFile->Image.ColorMap);
0N/A GifFile->Image.ColorMap = NULL;
0N/A }
0N/A
0N/A if (GifFile->SColorMap) {
0N/A FreeMapObject(GifFile->SColorMap);
0N/A GifFile->SColorMap = NULL;
0N/A }
0N/A
0N/A if (Private) {
0N/A free((char *)Private);
0N/A Private = NULL;
0N/A }
0N/A
0N/A if (GifFile->SavedImages) {
0N/A FreeSavedImages(GifFile);
0N/A GifFile->SavedImages = NULL;
0N/A }
0N/A
0N/A free(GifFile);
0N/A
0N/A if (File && (fclose(File) != 0)) {
0N/A _GifError = D_GIF_ERR_CLOSE_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Get 2 bytes (word) from the given file:
0N/A *****************************************************************************/
0N/Astatic int
0N/ADGifGetWord(GifFileType * GifFile,
0N/A int *Word) {
0N/A
0N/A unsigned char c[2];
0N/A
0N/A if (READ(GifFile, c, 2) != 2) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A *Word = (((unsigned int)c[1]) << 8) + c[0];
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Get the image code in compressed form. This routine can be called if the
0N/A * information needed to be piped out as is. Obviously this is much faster
0N/A * than decoding and encoding again. This routine should be followed by calls
0N/A * to DGifGetCodeNext, until NULL block is returned.
0N/A * The block should NOT be freed by the user (not dynamically allocated).
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetCode(GifFileType * GifFile,
0N/A int *CodeSize,
0N/A GifByteType ** CodeBlock) {
0N/A
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A *CodeSize = Private->BitsPerPixel;
0N/A
0N/A return DGifGetCodeNext(GifFile, CodeBlock);
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Continue to get the image code in compressed form. This routine should be
0N/A * called until NULL block is returned.
0N/A * The block should NOT be freed by the user (not dynamically allocated).
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetCodeNext(GifFileType * GifFile,
0N/A GifByteType ** CodeBlock) {
0N/A
0N/A GifByteType Buf;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (READ(GifFile, &Buf, 1) != 1) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (Buf > 0) {
0N/A *CodeBlock = Private->Buf; /* Use private unused buffer. */
0N/A (*CodeBlock)[0] = Buf; /* Pascal strings notation (pos. 0 is len.). */
0N/A if (READ(GifFile, &((*CodeBlock)[1]), Buf) != Buf) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A } else {
0N/A *CodeBlock = NULL;
0N/A Private->Buf[0] = 0; /* Make sure the buffer is empty! */
0N/A Private->PixelCount = 0; /* And local info. indicate image read. */
0N/A }
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Setup the LZ decompression for this image:
0N/A *****************************************************************************/
0N/Astatic int
0N/ADGifSetupDecompress(GifFileType * GifFile) {
0N/A
0N/A int i, BitsPerPixel;
0N/A GifByteType CodeSize;
0N/A unsigned int *Prefix;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A READ(GifFile, &CodeSize, 1); /* Read Code size from file. */
1126N/A if (CodeSize >= 12) {
1126N/A /* Invalid initial code size: report failure */
1126N/A return GIF_ERROR;
1126N/A }
0N/A BitsPerPixel = CodeSize;
0N/A
0N/A Private->Buf[0] = 0; /* Input Buffer empty. */
0N/A Private->BitsPerPixel = BitsPerPixel;
0N/A Private->ClearCode = (1 << BitsPerPixel);
0N/A Private->EOFCode = Private->ClearCode + 1;
0N/A Private->RunningCode = Private->EOFCode + 1;
0N/A Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
0N/A Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
0N/A Private->StackPtr = 0; /* No pixels on the pixel stack. */
0N/A Private->LastCode = NO_SUCH_CODE;
0N/A Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
0N/A Private->CrntShiftDWord = 0;
0N/A
0N/A Prefix = Private->Prefix;
0N/A for (i = 0; i <= LZ_MAX_CODE; i++)
0N/A Prefix[i] = NO_SUCH_CODE;
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * The LZ decompression routine:
0N/A * This version decompress the given gif file into Line of length LineLen.
0N/A * This routine can be called few times (one per scan line, for example), in
0N/A * order the complete the whole image.
0N/A *****************************************************************************/
0N/Astatic int
0N/ADGifDecompressLine(GifFileType * GifFile,
0N/A GifPixelType * Line,
0N/A int LineLen) {
0N/A
0N/A int i = 0;
0N/A int j, CrntCode, EOFCode, ClearCode, CrntPrefix, LastCode, StackPtr;
0N/A GifByteType *Stack, *Suffix;
0N/A unsigned int *Prefix;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
0N/A
0N/A StackPtr = Private->StackPtr;
0N/A Prefix = Private->Prefix;
0N/A Suffix = Private->Suffix;
0N/A Stack = Private->Stack;
0N/A EOFCode = Private->EOFCode;
0N/A ClearCode = Private->ClearCode;
0N/A LastCode = Private->LastCode;
0N/A
0N/A if (StackPtr != 0) {
0N/A /* Let pop the stack off before continueing to read the gif file: */
0N/A while (StackPtr != 0 && i < LineLen)
0N/A Line[i++] = Stack[--StackPtr];
0N/A }
0N/A
0N/A while (i < LineLen) { /* Decode LineLen items. */
0N/A if (DGifDecompressInput(GifFile, &CrntCode) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A
0N/A if (CrntCode == EOFCode) {
0N/A /* Note however that usually we will not be here as we will stop
0N/A * decoding as soon as we got all the pixel, or EOF code will
0N/A * not be read at all, and DGifGetLine/Pixel clean everything. */
0N/A if (i != LineLen - 1 || Private->PixelCount != 0) {
0N/A _GifError = D_GIF_ERR_EOF_TOO_SOON;
0N/A return GIF_ERROR;
0N/A }
0N/A i++;
0N/A } else if (CrntCode == ClearCode) {
0N/A /* We need to start over again: */
0N/A for (j = 0; j <= LZ_MAX_CODE; j++)
0N/A Prefix[j] = NO_SUCH_CODE;
0N/A Private->RunningCode = Private->EOFCode + 1;
0N/A Private->RunningBits = Private->BitsPerPixel + 1;
0N/A Private->MaxCode1 = 1 << Private->RunningBits;
0N/A LastCode = Private->LastCode = NO_SUCH_CODE;
0N/A } else {
0N/A /* Its regular code - if in pixel range simply add it to output
0N/A * stream, otherwise trace to codes linked list until the prefix
0N/A * is in pixel range: */
0N/A if (CrntCode < ClearCode) {
0N/A /* This is simple - its pixel scalar, so add it to output: */
0N/A Line[i++] = CrntCode;
0N/A } else {
0N/A /* Its a code to needed to be traced: trace the linked list
0N/A * until the prefix is a pixel, while pushing the suffix
0N/A * pixels on our stack. If we done, pop the stack in reverse
0N/A * (thats what stack is good for!) order to output. */
0N/A if (Prefix[CrntCode] == NO_SUCH_CODE) {
0N/A /* Only allowed if CrntCode is exactly the running code:
0N/A * In that case CrntCode = XXXCode, CrntCode or the
0N/A * prefix code is last code and the suffix char is
0N/A * exactly the prefix of last code! */
0N/A if (CrntCode == Private->RunningCode - 2) {
0N/A CrntPrefix = LastCode;
0N/A Suffix[Private->RunningCode - 2] =
0N/A Stack[StackPtr++] = DGifGetPrefixChar(Prefix,
0N/A LastCode,
0N/A ClearCode);
0N/A } else {
0N/A _GifError = D_GIF_ERR_IMAGE_DEFECT;
0N/A return GIF_ERROR;
0N/A }
0N/A } else
0N/A CrntPrefix = CrntCode;
0N/A
0N/A /* Now (if image is O.K.) we should not get an NO_SUCH_CODE
0N/A * During the trace. As we might loop forever, in case of
0N/A * defective image, we count the number of loops we trace
0N/A * and stop if we got LZ_MAX_CODE. obviously we can not
0N/A * loop more than that. */
0N/A j = 0;
0N/A while (j++ <= LZ_MAX_CODE &&
0N/A CrntPrefix > ClearCode && CrntPrefix <= LZ_MAX_CODE) {
0N/A Stack[StackPtr++] = Suffix[CrntPrefix];
0N/A CrntPrefix = Prefix[CrntPrefix];
0N/A }
0N/A if (j >= LZ_MAX_CODE || CrntPrefix > LZ_MAX_CODE) {
0N/A _GifError = D_GIF_ERR_IMAGE_DEFECT;
0N/A return GIF_ERROR;
0N/A }
0N/A /* Push the last character on stack: */
0N/A Stack[StackPtr++] = CrntPrefix;
0N/A
0N/A /* Now lets pop all the stack into output: */
0N/A while (StackPtr != 0 && i < LineLen)
0N/A Line[i++] = Stack[--StackPtr];
0N/A }
0N/A if (LastCode != NO_SUCH_CODE) {
0N/A Prefix[Private->RunningCode - 2] = LastCode;
0N/A
0N/A if (CrntCode == Private->RunningCode - 2) {
0N/A /* Only allowed if CrntCode is exactly the running code:
0N/A * In that case CrntCode = XXXCode, CrntCode or the
0N/A * prefix code is last code and the suffix char is
0N/A * exactly the prefix of last code! */
0N/A Suffix[Private->RunningCode - 2] =
0N/A DGifGetPrefixChar(Prefix, LastCode, ClearCode);
0N/A } else {
0N/A Suffix[Private->RunningCode - 2] =
0N/A DGifGetPrefixChar(Prefix, CrntCode, ClearCode);
0N/A }
0N/A }
0N/A LastCode = CrntCode;
0N/A }
0N/A }
0N/A
0N/A Private->LastCode = LastCode;
0N/A Private->StackPtr = StackPtr;
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Routine to trace the Prefixes linked list until we get a prefix which is
0N/A * not code, but a pixel value (less than ClearCode). Returns that pixel value.
0N/A * If image is defective, we might loop here forever, so we limit the loops to
0N/A * the maximum possible if image O.k. - LZ_MAX_CODE times.
0N/A *****************************************************************************/
0N/Astatic int
0N/ADGifGetPrefixChar(unsigned int *Prefix,
0N/A int Code,
0N/A int ClearCode) {
0N/A
0N/A int i = 0;
0N/A
0N/A while (Code > ClearCode && i++ <= LZ_MAX_CODE)
0N/A Code = Prefix[Code];
0N/A return Code;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * Interface for accessing the LZ codes directly. Set Code to the real code
0N/A * (12bits), or to -1 if EOF code is returned.
0N/A *****************************************************************************/
0N/Aint
0N/ADGifGetLZCodes(GifFileType * GifFile,
0N/A int *Code) {
0N/A
0N/A GifByteType *CodeBlock;
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A if (!IS_READABLE(Private)) {
0N/A /* This file was NOT open for reading: */
0N/A _GifError = D_GIF_ERR_NOT_READABLE;
0N/A return GIF_ERROR;
0N/A }
0N/A
0N/A if (DGifDecompressInput(GifFile, Code) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A
0N/A if (*Code == Private->EOFCode) {
0N/A /* Skip rest of codes (hopefully only NULL terminating block): */
0N/A do {
0N/A if (DGifGetCodeNext(GifFile, &CodeBlock) == GIF_ERROR)
0N/A return GIF_ERROR;
0N/A } while (CodeBlock != NULL) ;
0N/A
0N/A *Code = -1;
0N/A } else if (*Code == Private->ClearCode) {
0N/A /* We need to start over again: */
0N/A Private->RunningCode = Private->EOFCode + 1;
0N/A Private->RunningBits = Private->BitsPerPixel + 1;
0N/A Private->MaxCode1 = 1 << Private->RunningBits;
0N/A }
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * The LZ decompression input routine:
0N/A * This routine is responsable for the decompression of the bit stream from
0N/A * 8 bits (bytes) packets, into the real codes.
0N/A * Returns GIF_OK if read succesfully.
0N/A *****************************************************************************/
0N/Astatic int
0N/ADGifDecompressInput(GifFileType * GifFile,
0N/A int *Code) {
0N/A
0N/A GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
0N/A
0N/A GifByteType NextByte;
0N/A static unsigned int CodeMasks[] = {
0N/A 0x0000, 0x0001, 0x0003, 0x0007,
0N/A 0x000f, 0x001f, 0x003f, 0x007f,
0N/A 0x00ff, 0x01ff, 0x03ff, 0x07ff,
0N/A 0x0fff
0N/A };
0N/A
0N/A while (Private->CrntShiftState < Private->RunningBits) {
0N/A /* Needs to get more bytes from input stream for next code: */
0N/A if (DGifBufferedInput(GifFile, Private->Buf, &NextByte) == GIF_ERROR) {
0N/A return GIF_ERROR;
0N/A }
0N/A Private->CrntShiftDWord |=
0N/A ((unsigned long)NextByte) << Private->CrntShiftState;
0N/A Private->CrntShiftState += 8;
0N/A }
0N/A *Code = Private->CrntShiftDWord & CodeMasks[Private->RunningBits];
0N/A
0N/A Private->CrntShiftDWord >>= Private->RunningBits;
0N/A Private->CrntShiftState -= Private->RunningBits;
0N/A
0N/A /* If code cannot fit into RunningBits bits, must raise its size. Note
0N/A * however that codes above 4095 are used for special signaling. */
1126N/A if (++Private->RunningCode > Private->MaxCode1) {
1126N/A if (Private->RunningBits < LZ_BITS) {
1126N/A Private->MaxCode1 <<= 1;
1126N/A Private->RunningBits++;
1126N/A } else {
1126N/A Private->RunningCode = Private->MaxCode1;
1126N/A }
0N/A }
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * This routines read one gif data block at a time and buffers it internally
0N/A * so that the decompression routine could access it.
0N/A * The routine returns the next byte from its internal buffer (or read next
0N/A * block in if buffer empty) and returns GIF_OK if succesful.
0N/A *****************************************************************************/
0N/Astatic int
0N/ADGifBufferedInput(GifFileType * GifFile,
0N/A GifByteType * Buf,
0N/A GifByteType * NextByte) {
0N/A
0N/A if (Buf[0] == 0) {
0N/A /* Needs to read the next buffer - this one is empty: */
0N/A if (READ(GifFile, Buf, 1) != 1) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A if (READ(GifFile, &Buf[1], Buf[0]) != Buf[0]) {
0N/A _GifError = D_GIF_ERR_READ_FAILED;
0N/A return GIF_ERROR;
0N/A }
0N/A *NextByte = Buf[1];
0N/A Buf[1] = 2; /* We use now the second place as last char read! */
0N/A Buf[0]--;
0N/A } else {
0N/A *NextByte = Buf[Buf[1]++];
0N/A Buf[0]--;
0N/A }
0N/A
0N/A return GIF_OK;
0N/A}
0N/A
0N/A/******************************************************************************
0N/A * This routine reads an entire GIF into core, hanging all its state info off
0N/A * the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
0N/A * first to initialize I/O. Its inverse is EGifSpew().
0N/A ******************************************************************************/
0N/Aint
0N/ADGifSlurp(GifFileType * GifFile) {
0N/A
0N/A int ImageSize;
0N/A GifRecordType RecordType;
0N/A SavedImage *sp;
0N/A GifByteType *ExtData;
0N/A SavedImage temp_save;
0N/A
0N/A temp_save.ExtensionBlocks = NULL;
0N/A temp_save.ExtensionBlockCount = 0;
0N/A
0N/A do {
0N/A if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR)
0N/A return (GIF_ERROR);
0N/A
0N/A switch (RecordType) {
0N/A case IMAGE_DESC_RECORD_TYPE:
0N/A if (DGifGetImageDesc(GifFile) == GIF_ERROR)
0N/A return (GIF_ERROR);
0N/A
0N/A sp = &GifFile->SavedImages[GifFile->ImageCount - 1];
0N/A ImageSize = sp->ImageDesc.Width * sp->ImageDesc.Height;
0N/A
0N/A sp->RasterBits = (unsigned char *)malloc(ImageSize *
0N/A sizeof(GifPixelType));
0N/A if (sp->RasterBits == NULL) {
0N/A return GIF_ERROR;
0N/A }
0N/A if (DGifGetLine(GifFile, sp->RasterBits, ImageSize) ==
0N/A GIF_ERROR)
0N/A return (GIF_ERROR);
0N/A if (temp_save.ExtensionBlocks) {
0N/A sp->ExtensionBlocks = temp_save.ExtensionBlocks;
0N/A sp->ExtensionBlockCount = temp_save.ExtensionBlockCount;
0N/A
0N/A temp_save.ExtensionBlocks = NULL;
0N/A temp_save.ExtensionBlockCount = 0;
0N/A
0N/A /* FIXME: The following is wrong. It is left in only for
0N/A * backwards compatibility. Someday it should go away. Use
0N/A * the sp->ExtensionBlocks->Function variable instead. */
0N/A sp->Function = sp->ExtensionBlocks[0].Function;
0N/A }
0N/A break;
0N/A
0N/A case EXTENSION_RECORD_TYPE:
0N/A if (DGifGetExtension(GifFile, &temp_save.Function, &ExtData) ==
0N/A GIF_ERROR)
0N/A return (GIF_ERROR);
0N/A while (ExtData != NULL) {
0N/A
0N/A /* Create an extension block with our data */
0N/A if (AddExtensionBlock(&temp_save, ExtData[0], &ExtData[1])
0N/A == GIF_ERROR)
0N/A return (GIF_ERROR);
0N/A
0N/A if (DGifGetExtensionNext(GifFile, &ExtData) == GIF_ERROR)
0N/A return (GIF_ERROR);
0N/A temp_save.Function = 0;
0N/A }
0N/A break;
0N/A
0N/A case TERMINATE_RECORD_TYPE:
0N/A break;
0N/A
0N/A default: /* Should be trapped by DGifGetRecordType */
0N/A break;
0N/A }
0N/A } while (RecordType != TERMINATE_RECORD_TYPE);
0N/A
0N/A /* Just in case the Gif has an extension block without an associated
0N/A * image... (Should we save this into a savefile structure with no image
0N/A * instead? Have to check if the present writing code can handle that as
0N/A * well.... */
0N/A if (temp_save.ExtensionBlocks)
0N/A FreeExtension(&temp_save);
0N/A
0N/A return (GIF_OK);
0N/A}