/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/******************************************************************************
* "Gif-Lib" - Yet another gif library.
*
* Written by: Gershon Elber IBM PC Ver 1.1, Aug. 1990
******************************************************************************
* The kernel of the GIF Decoding process can be found here.
******************************************************************************
* History:
* 16 Jun 89 - Version 1.0 by Gershon Elber.
* 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names).
*****************************************************************************/
/* !!!! */
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <io.h>
#include <alloc.h>
#else
#endif /* __MSDOS__ */
#ifdef _WIN32
#include <io.h>
#define _OPEN_BINARY
#else
#include <unistd.h>
#endif
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "gif_lib.h"
#include "gif_lib_private.h"
comment. */
/* avoid extra function call in case we use fread (TVT) */
int LineLen);
/******************************************************************************
* Open a new gif file for read, given by its name.
* Returns GifFileType pointer dynamically allocated which serves as the gif
* info record. _GifError is cleared if succesfull.
*****************************************************************************/
int FileHandle;
#if defined(__MSDOS__) || defined(_OPEN_BINARY)
| O_BINARY
#endif /* __MSDOS__ || _OPEN_BINARY */
)) == -1) {
return NULL;
}
return GifFile;
}
/******************************************************************************
* Update a new gif file, given its file handle.
* Returns GifFileType pointer dynamically allocated which serves as the gif
* info record. _GifError is cleared if succesfull.
*****************************************************************************/
FILE *f;
return NULL;
}
return NULL;
}
#ifdef __MSDOS__
#endif /* __MSDOS__ */
#ifdef __MSDOS__
buffer. */
#endif /* __MSDOS__ */
/* Lets see if this is a GIF file: */
fclose(f);
return NULL;
}
/* The GIF Version number is ignored at this time. Maybe we should do
* something more useful with it. */
Buf[GIF_STAMP_LEN] = 0;
fclose(f);
return NULL;
}
fclose(f);
return NULL;
}
_GifError = 0;
return GifFile;
}
/******************************************************************************
* GifFileType constructor with user supplied input function (TVT)
*****************************************************************************/
return NULL;
}
if (!Private) {
return NULL;
}
Private->FileHandle = 0;
/* Lets see if this is a GIF file: */
return NULL;
}
/* The GIF Version number is ignored at this time. Maybe we should do
* something more useful with it. */
Buf[GIF_STAMP_LEN] = 0;
return NULL;
}
return NULL;
}
_GifError = 0;
return GifFile;
}
/******************************************************************************
* This routine should be called before any other DGif calls. Note that
* this routine is called automatically from DGif file open routines.
*****************************************************************************/
int
int i, BitsPerPixel;
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
/* Put the screen descriptor into the file: */
return GIF_ERROR;
return GIF_ERROR;
}
return GIF_ERROR;
}
/* Get the global color map: */
return GIF_ERROR;
}
}
} else {
}
return GIF_OK;
}
/******************************************************************************
* This routine should be called before any attempt to read an image.
*****************************************************************************/
int
GifRecordType * Type) {
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
return GIF_ERROR;
}
switch (Buf) {
case ',':
break;
case '!':
break;
case ';':
break;
default:
return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* This routine should be called before any attempt to read an image.
* Note it is assumed the Image desc. header (',') has been read.
*****************************************************************************/
int
int i, BitsPerPixel;
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
return GIF_ERROR;
return GIF_ERROR;
}
/*** FIXME: Why do we check both of these in order to do this?
* Why do we have both Image and SavedImages? */
return GIF_ERROR;
}
/* Get the image local color map: */
return GIF_ERROR;
}
}
}
if (GifFile->SavedImages) {
sizeof(SavedImage) *
return GIF_ERROR;
}
} else {
if ((GifFile->SavedImages =
return GIF_ERROR;
}
}
return GIF_ERROR;
}
}
sp->ExtensionBlockCount = 0;
GifFile->ImageCount++;
return GIF_OK;
}
/******************************************************************************
* Get one full scanned line (Line) of length LineLen from GIF file.
*****************************************************************************/
int
GifPixelType * Line,
int LineLen) {
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
if (!LineLen)
#else
#endif /* __MSDOS__ */
return GIF_ERROR;
}
if (Private->PixelCount == 0) {
/* We probably would not be called any more, so lets clean
* everything before we return: need to flush out all rest of
* image until empty block (size 0) detected. We use GetCodeNext. */
do
return GIF_ERROR;
}
return GIF_OK;
} else
return GIF_ERROR;
}
/******************************************************************************
* Put one pixel (Pixel) into GIF file.
*****************************************************************************/
int
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
#else
#endif /* __MSDOS__ */
{
return GIF_ERROR;
}
if (Private->PixelCount == 0) {
/* We probably would not be called any more, so lets clean
* everything before we return: need to flush out all rest of
* image until empty block (size 0) detected. We use GetCodeNext. */
do
return GIF_ERROR;
}
return GIF_OK;
} else
return GIF_ERROR;
}
/******************************************************************************
* Get an extension block (see GIF manual) from gif file. This routine only
* returns the first data block, and DGifGetExtensionNext should be called
* after this one until NULL extension is returned.
* The Extension should NOT be freed by the user (not dynamically allocated).
* Note it is assumed the Extension desc. header ('!') has been read.
*****************************************************************************/
int
int *ExtCode,
GifByteType ** Extension) {
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
return GIF_ERROR;
}
}
/******************************************************************************
* Get a following extension block (see GIF manual) from gif file. This
* routine should be called until NULL Extension is returned.
* The Extension should NOT be freed by the user (not dynamically allocated).
*****************************************************************************/
int
GifByteType ** Extension) {
return GIF_ERROR;
}
if (Buf > 0) {
return GIF_ERROR;
}
} else
return GIF_OK;
}
/******************************************************************************
* This routine should be called last, to close the GIF file.
*****************************************************************************/
int
return GIF_ERROR;
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
}
}
if (Private) {
}
if (GifFile->SavedImages) {
}
return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* Get 2 bytes (word) from the given file:
*****************************************************************************/
static int
int *Word) {
unsigned char c[2];
return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* Get the image code in compressed form. This routine can be called if the
* information needed to be piped out as is. Obviously this is much faster
* than decoding and encoding again. This routine should be followed by calls
* to DGifGetCodeNext, until NULL block is returned.
* The block should NOT be freed by the user (not dynamically allocated).
*****************************************************************************/
int
int *CodeSize,
GifByteType ** CodeBlock) {
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
}
/******************************************************************************
* Continue to get the image code in compressed form. This routine should be
* called until NULL block is returned.
* The block should NOT be freed by the user (not dynamically allocated).
*****************************************************************************/
int
GifByteType ** CodeBlock) {
return GIF_ERROR;
}
if (Buf > 0) {
return GIF_ERROR;
}
} else {
}
return GIF_OK;
}
/******************************************************************************
* Setup the LZ decompression for this image:
*****************************************************************************/
static int
int i, BitsPerPixel;
unsigned int *Prefix;
if (CodeSize >= 12) {
/* Invalid initial code size: report failure */
return GIF_ERROR;
}
Private->CrntShiftDWord = 0;
for (i = 0; i <= LZ_MAX_CODE; i++)
Prefix[i] = NO_SUCH_CODE;
return GIF_OK;
}
/******************************************************************************
* The LZ decompression routine:
* This version decompress the given gif file into Line of length LineLen.
* This routine can be called few times (one per scan line, for example), in
* order the complete the whole image.
*****************************************************************************/
static int
GifPixelType * Line,
int LineLen) {
int i = 0;
unsigned int *Prefix;
if (StackPtr != 0) {
/* Let pop the stack off before continueing to read the gif file: */
}
while (i < LineLen) { /* Decode LineLen items. */
return GIF_ERROR;
/* Note however that usually we will not be here as we will stop
* decoding as soon as we got all the pixel, or EOF code will
* not be read at all, and DGifGetLine/Pixel clean everything. */
return GIF_ERROR;
}
i++;
/* We need to start over again: */
for (j = 0; j <= LZ_MAX_CODE; j++)
Prefix[j] = NO_SUCH_CODE;
} else {
/* Its regular code - if in pixel range simply add it to output
* stream, otherwise trace to codes linked list until the prefix
* is in pixel range: */
/* This is simple - its pixel scalar, so add it to output: */
} else {
/* Its a code to needed to be traced: trace the linked list
* until the prefix is a pixel, while pushing the suffix
* pixels on our stack. If we done, pop the stack in reverse
* (thats what stack is good for!) order to output. */
/* Only allowed if CrntCode is exactly the running code:
* In that case CrntCode = XXXCode, CrntCode or the
* prefix code is last code and the suffix char is
* exactly the prefix of last code! */
} else {
return GIF_ERROR;
}
} else
/* Now (if image is O.K.) we should not get an NO_SUCH_CODE
* During the trace. As we might loop forever, in case of
* defective image, we count the number of loops we trace
* and stop if we got LZ_MAX_CODE. obviously we can not
* loop more than that. */
j = 0;
while (j++ <= LZ_MAX_CODE &&
}
return GIF_ERROR;
}
/* Push the last character on stack: */
/* Now lets pop all the stack into output: */
}
if (LastCode != NO_SUCH_CODE) {
/* Only allowed if CrntCode is exactly the running code:
* In that case CrntCode = XXXCode, CrntCode or the
* prefix code is last code and the suffix char is
* exactly the prefix of last code! */
} else {
}
}
}
}
return GIF_OK;
}
/******************************************************************************
* Routine to trace the Prefixes linked list until we get a prefix which is
* not code, but a pixel value (less than ClearCode). Returns that pixel value.
* If image is defective, we might loop here forever, so we limit the loops to
* the maximum possible if image O.k. - LZ_MAX_CODE times.
*****************************************************************************/
static int
int Code,
int ClearCode) {
int i = 0;
return Code;
}
/******************************************************************************
* Interface for accessing the LZ codes directly. Set Code to the real code
* (12bits), or to -1 if EOF code is returned.
*****************************************************************************/
int
int *Code) {
if (!IS_READABLE(Private)) {
/* This file was NOT open for reading: */
return GIF_ERROR;
}
return GIF_ERROR;
/* Skip rest of codes (hopefully only NULL terminating block): */
do {
return GIF_ERROR;
*Code = -1;
/* We need to start over again: */
}
return GIF_OK;
}
/******************************************************************************
* The LZ decompression input routine:
* This routine is responsable for the decompression of the bit stream from
* 8 bits (bytes) packets, into the real codes.
* Returns GIF_OK if read succesfully.
*****************************************************************************/
static int
int *Code) {
static unsigned int CodeMasks[] = {
0x0000, 0x0001, 0x0003, 0x0007,
0x000f, 0x001f, 0x003f, 0x007f,
0x00ff, 0x01ff, 0x03ff, 0x07ff,
0x0fff
};
/* Needs to get more bytes from input stream for next code: */
return GIF_ERROR;
}
}
/* If code cannot fit into RunningBits bits, must raise its size. Note
* however that codes above 4095 are used for special signaling. */
Private->RunningBits++;
} else {
}
}
return GIF_OK;
}
/******************************************************************************
* This routines read one gif data block at a time and buffers it internally
* so that the decompression routine could access it.
* The routine returns the next byte from its internal buffer (or read next
* block in if buffer empty) and returns GIF_OK if succesful.
*****************************************************************************/
static int
GifByteType * Buf,
GifByteType * NextByte) {
if (Buf[0] == 0) {
/* Needs to read the next buffer - this one is empty: */
return GIF_ERROR;
}
return GIF_ERROR;
}
Buf[0]--;
} else {
Buf[0]--;
}
return GIF_OK;
}
/******************************************************************************
* This routine reads an entire GIF into core, hanging all its state info off
* the GifFileType pointer. Call DGifOpenFileName() or DGifOpenFileHandle()
* first to initialize I/O. Its inverse is EGifSpew().
******************************************************************************/
int
int ImageSize;
do {
return (GIF_ERROR);
switch (RecordType) {
case IMAGE_DESC_RECORD_TYPE:
return (GIF_ERROR);
sizeof(GifPixelType));
return GIF_ERROR;
}
return (GIF_ERROR);
if (temp_save.ExtensionBlocks) {
/* FIXME: The following is wrong. It is left in only for
* backwards compatibility. Someday it should go away. Use
* the sp->ExtensionBlocks->Function variable instead. */
}
break;
case EXTENSION_RECORD_TYPE:
return (GIF_ERROR);
/* Create an extension block with our data */
== GIF_ERROR)
return (GIF_ERROR);
return (GIF_ERROR);
}
break;
case TERMINATE_RECORD_TYPE:
break;
default: /* Should be trapped by DGifGetRecordType */
break;
}
} while (RecordType != TERMINATE_RECORD_TYPE);
/* Just in case the Gif has an extension block without an associated
* image... (Should we save this into a savefile structure with no image
* instead? Have to check if the present writing code can handle that as
* well.... */
if (temp_save.ExtensionBlocks)
return (GIF_OK);
}