added everything

This commit is contained in:
Metario
2017-04-17 06:17:10 -06:00
commit 9c6ff74f19
6121 changed files with 1625704 additions and 0 deletions

1020
lib/lungif/dgif_lib.c Executable file

File diff suppressed because it is too large Load Diff

926
lib/lungif/egif_lib.c Executable file
View File

@ -0,0 +1,926 @@
/******************************************************************************
* "Gif-Lib" - Yet another gif library. *
* *
* Written by: Gershon Elber Ver 1.1, Aug. 1990 *
*******************************************************************************
* The kernel of the GIF Encoding process can be found here. *
*******************************************************************************
* History: *
* 14 Jun 89 - Version 1.0 by Gershon Elber. *
* 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
* 26 Jun 96 - Version 3.0 by Eric S. Raymond (Full GIF89 support)
******************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#if defined(__MWERKS__)
#pragma warn_unusedarg off
#pragma warn_unusedvar off
#endif
#ifdef __MSDOS__
#include <io.h>
#include <alloc.h>
#include <sys\stat.h>
#else
//#include <sys/types.h>
//#include <sys/stat.h>
#ifdef R6000
/* FIXME: What is sys/mode.h? Can we substitute a check for this file rather
* than a check based on machine type?
*/
#include <sys/mode.h>
#endif
#endif /* __MSDOS__ */
//#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gif_lib.h"
#include "gif_lib_private.h"
/* #define DEBUG_NO_PREFIX Dump only compressed data. */
/* Masks given codes to BitsPerPixel, to make sure all codes are in range: */
static GifPixelType CodeMask[] = {
0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
};
static char GifVersionPrefix[GIF_STAMP_LEN + 1] = GIF87_STAMP;
#define WRITE(_gif,_buf,_len) \
((GifFilePrivateType*)_gif->Private)->Write(_gif,(GifByteType*)_buf,_len)
// (((GifFilePrivateType*)_gif->Private)->Write ? \
// ((GifFilePrivateType*)_gif->Private)->Write(_gif,_buf,_len) : \
// fwrite(_buf, 1, _len, ((GifFilePrivateType*)_gif->Private)->File))
static int EGifPutWord(int Word, GifFileType *GifFile);
static int EGifSetupCompress(GifFileType *GifFile);
static int EGifCompressLine(GifFileType *GifFile, GifPixelType *Line,
int LineLen);
static int EGifCompressOutput(GifFileType *GifFile, int Code);
static int EGifBufferedOutput(GifFileType *GifFile, GifByteType *Buf, int c);
/******************************************************************************
* Open a new gif file for write, given by its name. If TestExistance then *
* if the file exists this routines fails (returns NULL). *
* Returns GifFileType pointer dynamically allocated which serves as the gif *
* info record. _GifError is cleared if succesfull. *
******************************************************************************/
//GifFileType *EGifOpenFileName(const char *FileName, int TestExistance)
//{
// int FileHandle;
// GifFileType *GifFile;
//
// if (TestExistance)
// FileHandle = open(FileName,
// O_WRONLY | O_CREAT | O_EXCL
//#ifdef __MSDOS__
// | O_BINARY
//#endif /* __MSDOS__ */
// ,
// S_IREAD | S_IWRITE);
// else
// FileHandle = open(FileName,
// O_WRONLY | O_CREAT | O_TRUNC
//#ifdef __MSDOS__
// | O_BINARY
//#endif /* __MSDOS__ */
// ,
// S_IREAD | S_IWRITE);
//
// if (FileHandle == -1) {
// _GifError = E_GIF_ERR_OPEN_FAILED;
// return NULL;
// }
// GifFile = EGifOpenFileHandle(FileHandle);
// if (GifFile == (GifFileType*)NULL)
// close(FileHandle);
// return GifFile;
//}
/******************************************************************************
* Update a new gif file, given its file handle, which must be opened for *
* write in binary mode. *
* Returns GifFileType pointer dynamically allocated which serves as the gif *
* info record. _GifError is cleared if succesfull. *
******************************************************************************/
//GifFileType *EGifOpenFileHandle(int FileHandle)
//{
// GifFileType *GifFile;
// GifFilePrivateType *Private;
// FILE *f;
//
// if ((GifFile = (GifFileType *) malloc(sizeof(GifFileType))) == NULL) {
// _GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
// return NULL;
// }
//
// memset(GifFile, '\0', sizeof(GifFileType));
//
// if ((Private = (GifFilePrivateType *)
// malloc(sizeof(GifFilePrivateType))) == NULL) {
// free(GifFile);
// _GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
// return NULL;
// }
//
//#ifdef __MSDOS__
// setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
//#endif /* __MSDOS__ */
//
// f = fdopen(FileHandle, "wb"); /* Make it into a stream: */
//
//#ifdef __MSDOS__
// setvbuf(f, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE); /* And inc. stream buffer. */
//#endif /* __MSDOS__ */
//
// GifFile->Private = (VoidPtr) Private;
// Private->FileHandle = FileHandle;
// Private->File = f;
// Private->FileState = FILE_STATE_WRITE;
//
// Private->Write = (OutputFunc)0; /* No user write routine (MRB) */
// GifFile->UserData = (VoidPtr)0; /* No user write handle (MRB) */
//
// _GifError = 0;
//
// return GifFile;
//}
/******************************************************************************
* Output constructor that takes user supplied output function. *
* Basically just a copy of EGifOpenFileHandle. (MRB) *
******************************************************************************/
GifFileType* EGifOpen(void* userData, OutputFunc writeFunc)
{
GifFileType* GifFile;
GifFilePrivateType* Private;
if ((GifFile = (GifFileType *) malloc(sizeof(GifFileType))) == NULL) {
_GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
return NULL;
}
memset(GifFile, '\0', sizeof(GifFileType));
if ((Private = (GifFilePrivateType *)
malloc(sizeof(GifFilePrivateType))) == NULL) {
free(GifFile);
_GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
return NULL;
}
GifFile->Private = (VoidPtr) Private;
Private->FileHandle = 0;
Private->File = 0;
Private->FileState = FILE_STATE_WRITE;
Private->Write = writeFunc; /* User write routine (MRB) */
GifFile->UserData = userData; /* User write handle (MRB) */
_GifError = 0;
return GifFile;
}
/******************************************************************************
* Routine to set current GIF version. All files open for write will be *
* using this version until next call to this routine. Version consists of *
* 3 characters as "87a" or "89a". No test is made to validate the version. *
******************************************************************************/
void EGifSetGifVersion(const char *Version)
{
strncpy(GifVersionPrefix + GIF_VERSION_POS, Version, 3);
}
/******************************************************************************
* This routine should be called before any other EGif calls, immediately *
* follows the GIF file openning. *
******************************************************************************/
int EGifPutScreenDesc(GifFileType *GifFile,
int Width, int Height, int ColorRes, int BackGround,
const ColorMapObject *ColorMap)
{
int i;
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (Private->FileState & FILE_STATE_SCREEN) {
/* If already has screen descriptor - something is wrong! */
_GifError = E_GIF_ERR_HAS_SCRN_DSCR;
return GIF_ERROR;
}
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
/* First write the version prefix into the file. */
#ifndef DEBUG_NO_PREFIX
if (WRITE(GifFile, GifVersionPrefix, strlen(GifVersionPrefix)) !=
strlen(GifVersionPrefix)) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
#endif /* DEBUG_NO_PREFIX */
GifFile->SWidth = Width;
GifFile->SHeight = Height;
GifFile->SColorResolution = ColorRes;
GifFile->SBackGroundColor = BackGround;
if(ColorMap)
GifFile->SColorMap=MakeMapObject(ColorMap->ColorCount,ColorMap->Colors);
else
GifFile->SColorMap=NULL;
/* Put the screen descriptor into the file: */
EGifPutWord(Width, GifFile);
EGifPutWord(Height, GifFile);
Buf[0] = (ColorMap ? 0x80 : 0x00) |
((ColorRes - 1) << 4) |
(ColorMap->BitsPerPixel - 1);
Buf[1] = BackGround;
Buf[2] = 0;
#ifndef DEBUG_NO_PREFIX
WRITE(GifFile, Buf, 3);
#endif /* DEBUG_NO_PREFIX */
/* If we have Global color map - dump it also: */
#ifndef DEBUG_NO_PREFIX
if (ColorMap != NULL)
for (i = 0; i < ColorMap->ColorCount; i++) {
/* Put the ColorMap out also: */
Buf[0] = ColorMap->Colors[i].Red;
Buf[1] = ColorMap->Colors[i].Green;
Buf[2] = ColorMap->Colors[i].Blue;
if (WRITE(GifFile, Buf, 3) != 3) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
#endif /* DEBUG_NO_PREFIX */
/* Mark this file as has screen descriptor, and no pixel written yet: */
Private->FileState |= FILE_STATE_SCREEN;
return GIF_OK;
}
/******************************************************************************
* This routine should be called before any attemp to dump an image - any *
* call to any of the pixel dump routines. *
******************************************************************************/
int EGifPutImageDesc(GifFileType *GifFile,
int Left, int Top, int Width, int Height, int Interlace,
const ColorMapObject *ColorMap)
{
int i;
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (Private->FileState & FILE_STATE_IMAGE &&
#if defined(__MSDOS__) || defined(__GNUC__)
Private->PixelCount > 0xffff0000UL) {
#else
Private->PixelCount > 0xffff0000) {
#endif /* __MSDOS__ */
/* If already has active image descriptor - something is wrong! */
_GifError = E_GIF_ERR_HAS_IMAG_DSCR;
return GIF_ERROR;
}
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
GifFile->Image.Left = Left;
GifFile->Image.Top = Top;
GifFile->Image.Width = Width;
GifFile->Image.Height = Height;
GifFile->Image.Interlace = Interlace;
if(ColorMap)
GifFile->Image.ColorMap =MakeMapObject(ColorMap->ColorCount,ColorMap->Colors);
else
GifFile->Image.ColorMap = NULL;
/* Put the image descriptor into the file: */
Buf[0] = ','; /* Image seperator character. */
#ifndef DEBUG_NO_PREFIX
WRITE(GifFile, Buf, 1);
#endif /* DEBUG_NO_PREFIX */
EGifPutWord(Left, GifFile);
EGifPutWord(Top, GifFile);
EGifPutWord(Width, GifFile);
EGifPutWord(Height, GifFile);
Buf[0] = (ColorMap ? 0x80 : 0x00) |
(Interlace ? 0x40 : 0x00) |
(ColorMap ? ColorMap->BitsPerPixel - 1 : 0);
#ifndef DEBUG_NO_PREFIX
WRITE(GifFile, Buf, 1);
#endif /* DEBUG_NO_PREFIX */
/* If we have Global color map - dump it also: */
#ifndef DEBUG_NO_PREFIX
if (ColorMap != NULL)
for (i = 0; i < ColorMap->ColorCount; i++) {
/* Put the ColorMap out also: */
Buf[0] = ColorMap->Colors[i].Red;
Buf[1] = ColorMap->Colors[i].Green;
Buf[2] = ColorMap->Colors[i].Blue;
if (WRITE(GifFile, Buf, 3) != 3) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
#endif /* DEBUG_NO_PREFIX */
if (GifFile->SColorMap == NULL && GifFile->Image.ColorMap == NULL)
{
_GifError = E_GIF_ERR_NO_COLOR_MAP;
return GIF_ERROR;
}
/* Mark this file as has screen descriptor: */
Private->FileState |= FILE_STATE_IMAGE;
Private->PixelCount = (long) Width * (long) Height;
EGifSetupCompress(GifFile); /* Reset compress algorithm parameters. */
return GIF_OK;
}
/******************************************************************************
* Put one full scanned line (Line) of length LineLen into GIF file. *
******************************************************************************/
int EGifPutLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
{
int i;
GifPixelType Mask;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (!LineLen)
LineLen = GifFile->Image.Width;
if (Private->PixelCount < (unsigned)LineLen) {
_GifError = E_GIF_ERR_DATA_TOO_BIG;
return GIF_ERROR;
}
Private->PixelCount -= LineLen;
/* Make sure the codes are not out of bit range, as we might generate */
/* wrong code (because of overflow when we combine them) in this case: */
Mask = CodeMask[Private->BitsPerPixel];
for (i = 0; i < LineLen; i++) Line[i] &= Mask;
return EGifCompressLine(GifFile, Line, LineLen);
}
/******************************************************************************
* Put one pixel (Pixel) into GIF file. *
******************************************************************************/
int EGifPutPixel(GifFileType *GifFile, GifPixelType Pixel)
{
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (Private->PixelCount == 0)
{
_GifError = E_GIF_ERR_DATA_TOO_BIG;
return GIF_ERROR;
}
--Private->PixelCount;
/* Make sure the code is not out of bit range, as we might generate */
/* wrong code (because of overflow when we combine them) in this case: */
Pixel &= CodeMask[Private->BitsPerPixel];
return EGifCompressLine(GifFile, &Pixel, 1);
}
/******************************************************************************
* Put a comment into GIF file using the GIF89 comment extension block. *
******************************************************************************/
int EGifPutComment(GifFileType *GifFile, const char *Comment)
{
return EGifPutExtension(GifFile, COMMENT_EXT_FUNC_CODE, strlen(Comment),
Comment);
}
/******************************************************************************
* Put a first extension block (see GIF manual) into gif file. Here more *
* extensions can be dumped using EGifPutExtensionMid until *
* EGifPutExtensionLast is invoked. *
******************************************************************************/
int EGifPutExtensionFirst(GifFileType *GifFile, int ExtCode, int ExtLen,
const VoidPtr Extension)
{
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (ExtCode == 0)
WRITE(GifFile, &ExtLen, 1);
//fwrite(&ExtLen, 1, 1, Private->File);
else
{
Buf[0] = '!';
Buf[1] = ExtCode;
Buf[2] = ExtLen;
WRITE(GifFile, Buf, 3);
//fwrite(Buf, 1, 3, Private->File);
}
WRITE(GifFile, Extension, ExtLen);
//fwrite(Extension, 1, ExtLen, Private->File);
return GIF_OK;
}
/******************************************************************************
* Put a middle extension block (see GIF manual) into gif file. *
******************************************************************************/
int EGifPutExtensionNext(GifFileType *GifFile, int ExtCode, int ExtLen,
const VoidPtr Extension)
{
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
Buf = ExtLen;
WRITE(GifFile, &Buf, 1);
WRITE(GifFile, Extension, ExtLen);
//fwrite(&Buf, 1, 1, Private->File);
//fwrite(Extension, 1, ExtLen, Private->File);
return GIF_OK;
}
/******************************************************************************
* Put a last extension block (see GIF manual) into gif file. *
******************************************************************************/
int EGifPutExtensionLast(GifFileType *GifFile, int ExtCode, int ExtLen,
const VoidPtr Extension)
{
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
Buf = ExtLen;
WRITE(GifFile, &Buf, 1);
WRITE(GifFile, Extension, ExtLen);
//fwrite(&Buf, 1, 1, Private->File);
//fwrite(Extension, 1, ExtLen, Private->File);
Buf = 0;
WRITE(GifFile, &Buf, 1);
//fwrite(&Buf, 1, 1, Private->File);
return GIF_OK;
}
/******************************************************************************
* Put an extension block (see GIF manual) into gif file. *
******************************************************************************/
int EGifPutExtension(GifFileType *GifFile, int ExtCode, int ExtLen,
const VoidPtr Extension)
{
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (ExtCode == 0)
WRITE(GifFile, (GifByteType*)&ExtLen, 1);
else {
Buf[0] = '!';
Buf[1] = ExtCode;
Buf[2] = ExtLen;
WRITE(GifFile, Buf, 3);
}
WRITE(GifFile, Extension, ExtLen);
Buf[0] = 0;
WRITE(GifFile, Buf, 1);
return GIF_OK;
}
/******************************************************************************
* Put 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 EGifPutCodeNext, until NULL block is given. *
* The block should NOT be freed by the user (not dynamically allocated). *
******************************************************************************/
int EGifPutCode(GifFileType *GifFile, int CodeSize, const GifByteType *CodeBlock)
{
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
/* No need to dump code size as Compression set up does any for us: */
/*
Buf = CodeSize;
if (WRITE(GifFile, &Buf, 1) != 1) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
*/
return EGifPutCodeNext(GifFile, CodeBlock);
}
/******************************************************************************
* Continue to put the image code in compressed form. This routine should be *
* called with blocks of code as read via DGifGetCode/DGifGetCodeNext. If *
* given buffer pointer is NULL, empty block is written to mark end of code. *
******************************************************************************/
int EGifPutCodeNext(GifFileType *GifFile, const GifByteType *CodeBlock)
{
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (CodeBlock != NULL) {
if (WRITE(GifFile, CodeBlock, CodeBlock[0] + 1)
!= (unsigned)(CodeBlock[0] + 1)) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
} else {
Buf = 0;
if (WRITE(GifFile, &Buf, 1) != 1) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
Private->PixelCount = 0; /* And local info. indicate image read. */
}
return GIF_OK;
}
/******************************************************************************
* This routine should be called last, to close GIF file. *
******************************************************************************/
int EGifCloseFile(GifFileType *GifFile)
{
GifByteType Buf;
GifFilePrivateType *Private;
int *File;
if (GifFile == NULL) return GIF_ERROR;
Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
File = Private->File;
Buf = ';';
WRITE(GifFile, &Buf, 1);
if (GifFile->Image.ColorMap)
FreeMapObject(GifFile->Image.ColorMap);
if (GifFile->SColorMap)
FreeMapObject(GifFile->SColorMap);
if (Private) {
free((char *) Private);
}
free(GifFile);
// if (File && fclose(File) != 0) {
// _GifError = E_GIF_ERR_CLOSE_FAILED;
// return GIF_ERROR;
// }
return GIF_OK;
}
/******************************************************************************
* Put 2 bytes (word) into the given file: *
******************************************************************************/
static int EGifPutWord(int Word, GifFileType *GifFile)
{
char c[2];
c[0] = Word & 0xff;
c[1] = (Word >> 8) & 0xff;
#ifndef DEBUG_NO_PREFIX
if (WRITE(GifFile, c, 2) == 2)
return GIF_OK;
else
return GIF_ERROR;
#else
return GIF_OK;
#endif /* DEBUG_NO_PREFIX */
}
/******************************************************************************
* Setup the LZ compression for this image: *
******************************************************************************/
static int EGifSetupCompress(GifFileType *GifFile)
{
int BitsPerPixel;
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
/* Test and see what color map to use, and from it # bits per pixel: */
if (GifFile->Image.ColorMap)
BitsPerPixel = GifFile->Image.ColorMap->BitsPerPixel;
else if (GifFile->SColorMap)
BitsPerPixel = GifFile->SColorMap->BitsPerPixel;
else {
_GifError = E_GIF_ERR_NO_COLOR_MAP;
return GIF_ERROR;
}
Buf = BitsPerPixel = (BitsPerPixel < 2 ? 2 : BitsPerPixel);
WRITE(GifFile, &Buf, 1); /* Write the Code size to file. */
Private->Buf[0] = 0; /* Nothing was output yet. */
Private->BitsPerPixel = BitsPerPixel;
Private->ClearCode = (1 << BitsPerPixel);
Private->EOFCode = Private->ClearCode + 1;
Private->RunningCode = 0;
Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
Private->CrntCode = FIRST_CODE; /* Signal that this is first one! */
Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
Private->CrntShiftDWord = 0;
/* Send Clear to make sure the encoder is initialized. */
if (EGifCompressOutput(GifFile, Private->ClearCode) == GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* The LZ compression routine: *
* This version compress the given buffer 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 EGifCompressLine(GifFileType *GifFile, GifPixelType *Line,
int LineLen)
{
int i = 0, CrntCode, NewCode;
unsigned long NewKey;
GifPixelType Pixel;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (Private->CrntCode == FIRST_CODE) /* Its first time! */
CrntCode = Line[i++];
else
CrntCode = Private->CrntCode; /* Get last code in compression. */
while (i < LineLen) { /* Decode LineLen items. */
Pixel = Line[i++]; /* Get next pixel from stream. */
if (EGifCompressOutput(GifFile, CrntCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
Private->RunningCode++;
CrntCode = Pixel;
if (Private->RunningCode >= (1 << (Private->BitsPerPixel)) - 2) {
if (EGifCompressOutput(GifFile, Private->ClearCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
Private->RunningCode=0;
}
}
/* Preserve the current state of the compression algorithm: */
Private->CrntCode = CrntCode;
if (Private->PixelCount == 0)
{
/* We are done - output last Code and flush output buffers: */
if (EGifCompressOutput(GifFile, CrntCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
if (EGifCompressOutput(GifFile, Private->EOFCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
if (EGifCompressOutput(GifFile, FLUSH_OUTPUT) == GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
}
return GIF_OK;
}
/******************************************************************************
* The LZ compression output routine: *
* This routine is responsible for the compression of the bit stream into *
* 8 bits (bytes) packets. *
* Returns GIF_OK if written succesfully. *
******************************************************************************/
static int EGifCompressOutput(GifFileType *GifFile, int Code)
{
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private;
int retval = GIF_OK;
if (Code == FLUSH_OUTPUT) {
while (Private->CrntShiftState > 0) {
/* Get Rid of what is left in DWord, and flush it. */
if (EGifBufferedOutput(GifFile, Private->Buf,
Private->CrntShiftDWord & 0xff) == GIF_ERROR)
retval = GIF_ERROR;
Private->CrntShiftDWord >>= 8;
Private->CrntShiftState -= 8;
}
Private->CrntShiftState = 0; /* For next time. */
if (EGifBufferedOutput(GifFile, Private->Buf,
FLUSH_OUTPUT) == GIF_ERROR)
retval = GIF_ERROR;
}
else {
Private->CrntShiftDWord |= ((long) Code) << Private->CrntShiftState;
Private->CrntShiftState += Private->RunningBits;
while (Private->CrntShiftState >= 8) {
/* Dump out full bytes: */
if (EGifBufferedOutput(GifFile, Private->Buf,
Private->CrntShiftDWord & 0xff) == GIF_ERROR)
retval = GIF_ERROR;
Private->CrntShiftDWord >>= 8;
Private->CrntShiftState -= 8;
}
}
return retval;
}
/******************************************************************************
* This routines buffers the given characters until 255 characters are ready *
* to be output. If Code is equal to -1 the buffer is flushed (EOF). *
* The buffer is Dumped with first byte as its size, as GIF format requires. *
* Returns GIF_OK if written succesfully. *
******************************************************************************/
static int EGifBufferedOutput(GifFileType *GifFile, GifByteType *Buf, int c)
{
if (c == FLUSH_OUTPUT) {
/* Flush everything out. */
if (Buf[0] != 0 && WRITE(GifFile, Buf, Buf[0]+1) != (unsigned)(Buf[0] + 1))
{
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
/* Mark end of compressed data, by an empty block (see GIF doc): */
Buf[0] = 0;
if (WRITE(GifFile, Buf, 1) != 1)
{
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
else {
if (Buf[0] == 255) {
/* Dump out this buffer - it is full: */
if (WRITE(GifFile, Buf, Buf[0] + 1) != (unsigned)(Buf[0] + 1))
{
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
Buf[0] = 0;
}
Buf[++Buf[0]] = c;
}
return GIF_OK;
}
/******************************************************************************
* This routine writes to disk an in-core representation of a GIF previously *
* created by DGifSlurp(). *
******************************************************************************/
int EGifSpew(GifFileType *GifFileOut)
{
int i, j, gif89 = FALSE;
char SavedStamp[GIF_STAMP_LEN + 1];
for (i = 0; i < GifFileOut->ImageCount; i++)
{
for (j = 0; j < GifFileOut->SavedImages[i].ExtensionBlockCount; j++) {
int function=GifFileOut->SavedImages[i].ExtensionBlocks[j].Function;
if (function == COMMENT_EXT_FUNC_CODE
|| function == GRAPHICS_EXT_FUNC_CODE
|| function == PLAINTEXT_EXT_FUNC_CODE
|| function == APPLICATION_EXT_FUNC_CODE)
gif89 = TRUE;
}
}
strncpy(SavedStamp, GifVersionPrefix, GIF_STAMP_LEN);
if (gif89)
{
strncpy(GifVersionPrefix, GIF89_STAMP, GIF_STAMP_LEN);
}
else
{
strncpy(GifVersionPrefix, GIF87_STAMP, GIF_STAMP_LEN);
}
if (EGifPutScreenDesc(GifFileOut,
GifFileOut->SWidth,
GifFileOut->SHeight,
GifFileOut->SColorResolution,
GifFileOut->SBackGroundColor,
GifFileOut->SColorMap) == GIF_ERROR)
{
strncpy(GifVersionPrefix, SavedStamp, GIF_STAMP_LEN);
return(GIF_ERROR);
}
strncpy(GifVersionPrefix, SavedStamp, GIF_STAMP_LEN);
for (i = 0; i < GifFileOut->ImageCount; i++)
{
SavedImage *sp = &GifFileOut->SavedImages[i];
int SavedHeight = sp->ImageDesc.Height;
int SavedWidth = sp->ImageDesc.Width;
ExtensionBlock *ep;
/* this allows us to delete images by nuking their rasters */
if (sp->RasterBits == NULL)
continue;
if (sp->ExtensionBlocks)
{
for ( j = 0; j < sp->ExtensionBlockCount; j++) {
ep = &sp->ExtensionBlocks[j];
if (EGifPutExtension(GifFileOut,
(ep->Function != 0) ? ep->Function : '\0',
ep->ByteCount, ep->Bytes) == GIF_ERROR)
return (GIF_ERROR);
}
}
if (EGifPutImageDesc(GifFileOut,
sp->ImageDesc.Left,
sp->ImageDesc.Top,
SavedWidth,
SavedHeight,
sp->ImageDesc.Interlace,
sp->ImageDesc.ColorMap
) == GIF_ERROR)
return(GIF_ERROR);
for (j = 0; j < SavedHeight; j++)
{
if (EGifPutLine(GifFileOut,
(GifByteType*)(sp->RasterBits + j * SavedWidth),
SavedWidth) == GIF_ERROR)
return(GIF_ERROR);
}
}
if (EGifCloseFile(GifFileOut) == GIF_ERROR)
return(GIF_ERROR);
return(GIF_OK);
}

134
lib/lungif/gif_err.c Executable file
View File

@ -0,0 +1,134 @@
/*****************************************************************************
* "Gif-Lib" - Yet another gif library. *
* *
* Written by: Gershon Elber IBM PC Ver 0.1, Jun. 1989 *
******************************************************************************
* Handle error reporting for the GIF library. *
******************************************************************************
* History: *
* 17 Jun 89 - Version 1.0 by Gershon Elber. *
*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include "gif_lib.h"
#define PROGRAM_NAME "GIF_LIBRARY"
int _GifError = 0;
#ifdef SYSV
static char *VersionStr =
"Gif library module,\t\tEric S. Raymond\n\
(C) Copyright 1997 Eric S. Raymond\n";
#else
static char *VersionStr =
PROGRAM_NAME
" IBMPC "
GIF_LIB_VERSION
" Eric S. Raymond, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1997 Eric S. Raymond\n";
#endif /* SYSV */
/*****************************************************************************
* Return the last GIF error (0 if none) and reset the error. *
*****************************************************************************/
int GifLastError(void)
{
int i = _GifError;
_GifError = 0;
return i;
}
/*****************************************************************************
* Print the last GIF error to stderr. *
*****************************************************************************/
void PrintGifError(void)
{
char *Err;
switch(_GifError) {
case E_GIF_ERR_OPEN_FAILED:
Err = "Failed to open given file";
break;
case E_GIF_ERR_WRITE_FAILED:
Err = "Failed to Write to given file";
break;
case E_GIF_ERR_HAS_SCRN_DSCR:
Err = "Screen Descriptor already been set";
break;
case E_GIF_ERR_HAS_IMAG_DSCR:
Err = "Image Descriptor is still active";
break;
case E_GIF_ERR_NO_COLOR_MAP:
Err = "Neither Global Nor Local color map";
break;
case E_GIF_ERR_DATA_TOO_BIG:
Err = "#Pixels bigger than Width * Height";
break;
case E_GIF_ERR_NOT_ENOUGH_MEM:
Err = "Fail to allocate required memory";
break;
case E_GIF_ERR_DISK_IS_FULL:
Err = "Write failed (disk full?)";
break;
case E_GIF_ERR_CLOSE_FAILED:
Err = "Failed to close given file";
break;
case E_GIF_ERR_NOT_WRITEABLE:
Err = "Given file was not opened for write";
break;
case D_GIF_ERR_OPEN_FAILED:
Err = "Failed to open given file";
break;
case D_GIF_ERR_READ_FAILED:
Err = "Failed to Read from given file";
break;
case D_GIF_ERR_NOT_GIF_FILE:
Err = "Given file is NOT GIF file";
break;
case D_GIF_ERR_NO_SCRN_DSCR:
Err = "No Screen Descriptor detected";
break;
case D_GIF_ERR_NO_IMAG_DSCR:
Err = "No Image Descriptor detected";
break;
case D_GIF_ERR_NO_COLOR_MAP:
Err = "Neither Global Nor Local color map";
break;
case D_GIF_ERR_WRONG_RECORD:
Err = "Wrong record type detected";
break;
case D_GIF_ERR_DATA_TOO_BIG:
Err = "#Pixels bigger than Width * Height";
break;
case D_GIF_ERR_NOT_ENOUGH_MEM:
Err = "Fail to allocate required memory";
break;
case D_GIF_ERR_CLOSE_FAILED:
Err = "Failed to close given file";
break;
case D_GIF_ERR_NOT_READABLE:
Err = "Given file was not opened for read";
break;
case D_GIF_ERR_IMAGE_DEFECT:
Err = "Image is defective, decoding aborted";
break;
case D_GIF_ERR_EOF_TOO_SOON:
Err = "Image EOF detected, before image complete";
break;
default:
Err = NULL;
break;
}
if (Err != NULL)
fprintf(stderr, "\nGIF-LIB error: %s.\n", Err);
else
fprintf(stderr, "\nGIF-LIB undefined error %d.\n", _GifError);
}

323
lib/lungif/gif_lib.h Executable file
View File

@ -0,0 +1,323 @@
/******************************************************************************
* In order to make life a little bit easier when using the GIF file format, *
* this library was written, and which does all the dirty work... *
* *
* Written by Gershon Elber, Jun. 1989 *
* Hacks by Eric S. Raymond, Sep. 1992 *
*******************************************************************************
* History: *
* 14 Jun 89 - Version 1.0 by Gershon Elber. *
* 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names) *
* 15 Sep 90 - Version 2.0 by Eric S. Raymond (Changes to suoport GIF slurp) *
* 26 Jun 96 - Version 3.0 by Eric S. Raymond (Full GIF89 support) *
* 17 Dec 98 - Version 4.0 by Toshio Kuratomi (Fix extension writing code) *
******************************************************************************/
#ifndef _GIF_LIB_H
#define _GIF_LIB_H
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define GIF_LIB_VERSION " Version 4.0, "
#define GIF_ERROR 0
#define GIF_OK 1
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
#ifndef NULL
#define NULL 0
#endif /* NULL */
#define GIF_STAMP "GIFVER" /* First chars in file - GIF stamp. */
#define GIF_STAMP_LEN sizeof(GIF_STAMP) - 1
#define GIF_VERSION_POS 3 /* Version first character in stamp. */
#define GIF87_STAMP "GIF87a" /* First chars in file - GIF stamp. */
#define GIF89_STAMP "GIF89a" /* First chars in file - GIF stamp. */
#define GIF_FILE_BUFFER_SIZE 16384 /* Files uses bigger buffers than usual. */
typedef int GifBooleanType;
typedef unsigned char GifPixelType;
typedef unsigned char * GifRowType;
typedef unsigned char GifByteType;
#define GIF_MESSAGE(Msg) fprintf(stderr, "\n%s: %s\n", PROGRAM_NAME, Msg)
#define GIF_EXIT(Msg) { GIF_MESSAGE(Msg); exit(-3); }
#ifdef SYSV
#define VoidPtr char *
#else
#define VoidPtr void *
#endif /* SYSV */
typedef struct GifColorType {
GifByteType Red, Green, Blue;
} GifColorType;
typedef struct ColorMapObject
{
int ColorCount;
int BitsPerPixel;
GifColorType *Colors; /* on malloc(3) heap */
}
ColorMapObject;
typedef struct GifImageDesc {
int Left, Top, Width, Height, /* Current image dimensions. */
Interlace; /* Sequential/Interlaced lines. */
ColorMapObject *ColorMap; /* The local color map */
} GifImageDesc;
typedef struct GifFileType {
int SWidth, SHeight, /* Screen dimensions. */
SColorResolution, /* How many colors can we generate? */
SBackGroundColor; /* I hope you understand this one... */
ColorMapObject *SColorMap; /* NULL if not exists. */
int ImageCount; /* Number of current image */
GifImageDesc Image; /* Block describing current image */
struct SavedImage *SavedImages; /* Use this to accumulate file state */
VoidPtr UserData; /* hook to attach user data (TVT) */
VoidPtr Private; /* Don't mess with this! */
} GifFileType;
typedef enum {
UNDEFINED_RECORD_TYPE,
SCREEN_DESC_RECORD_TYPE,
IMAGE_DESC_RECORD_TYPE, /* Begin with ',' */
EXTENSION_RECORD_TYPE, /* Begin with '!' */
TERMINATE_RECORD_TYPE /* Begin with ';' */
} GifRecordType;
/* DumpScreen2Gif routine constants identify type of window/screen to dump. */
/* Note all values below 1000 are reserved for the IBMPC different display */
/* devices (it has many!) and are compatible with the numbering TC2.0 */
/* (Turbo C 2.0 compiler for IBM PC) gives to these devices. */
typedef enum {
GIF_DUMP_SGI_WINDOW = 1000,
GIF_DUMP_X_WINDOW = 1001
} GifScreenDumpType;
/* func type to read gif data from arbitrary sources (TVT) */
typedef int (*InputFunc)(GifFileType*,GifByteType*,int);
/* func type to write gif data ro arbitrary targets.
* Returns count of bytes written. (MRB)
*/
typedef int (*OutputFunc)(GifFileType *, const GifByteType *, int);
/******************************************************************************
* GIF89 extension function codes *
******************************************************************************/
#define COMMENT_EXT_FUNC_CODE 0xfe /* comment */
#define GRAPHICS_EXT_FUNC_CODE 0xf9 /* graphics control */
#define PLAINTEXT_EXT_FUNC_CODE 0x01 /* plaintext */
#define APPLICATION_EXT_FUNC_CODE 0xff /* application block */
/******************************************************************************
* O.K., here are the routines one can access in order to encode GIF file: *
* (GIF_LIB file EGIF_LIB.C). *
******************************************************************************/
GifFileType *EGifOpenFileName(const char *GifFileName, int GifTestExistance);
GifFileType *EGifOpenFileHandle(int GifFileHandle);
GifFileType *EgifOpen(void *userPtr, OutputFunc writeFunc);
int EGifSpew(GifFileType *GifFile);
void EGifSetGifVersion(const char *Version);
int EGifPutScreenDesc(GifFileType *GifFile,
int GifWidth, int GifHeight, int GifColorRes, int GifBackGround,
const ColorMapObject *GifColorMap);
int EGifPutImageDesc(GifFileType *GifFile,
int GifLeft, int GifTop, int Width, int GifHeight, int GifInterlace,
const ColorMapObject *GifColorMap);
int EGifPutLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
int EGifPutPixel(GifFileType *GifFile, GifPixelType GifPixel);
int EGifPutComment(GifFileType *GifFile, const char *GifComment);
int EGifPutExtensionFirst(GifFileType *GifFile, int GifExtCode, int GifExtLen,
const VoidPtr GifExtension);
int EGifPutExtensionNext(GifFileType *GifFile, int GifExtCode, int GifExtLen,
const VoidPtr GifExtension);
int EGifPutExtensionLast(GifFileType *GifFile, int GifExtCode, int GifExtLen,
const VoidPtr GifExtension);
int EGifPutExtension(GifFileType *GifFile, int GifExtCode, int GifExtLen,
const VoidPtr GifExtension);
int EGifPutCode(GifFileType *GifFile, int GifCodeSize,
const GifByteType *GifCodeBlock);
int EGifPutCodeNext(GifFileType *GifFile, const GifByteType *GifCodeBlock);
int EGifCloseFile(GifFileType *GifFile);
#define E_GIF_ERR_OPEN_FAILED 1 /* And EGif possible errors. */
#define E_GIF_ERR_WRITE_FAILED 2
#define E_GIF_ERR_HAS_SCRN_DSCR 3
#define E_GIF_ERR_HAS_IMAG_DSCR 4
#define E_GIF_ERR_NO_COLOR_MAP 5
#define E_GIF_ERR_DATA_TOO_BIG 6
#define E_GIF_ERR_NOT_ENOUGH_MEM 7
#define E_GIF_ERR_DISK_IS_FULL 8
#define E_GIF_ERR_CLOSE_FAILED 9
#define E_GIF_ERR_NOT_WRITEABLE 10
/******************************************************************************
* O.K., here are the routines one can access in order to decode GIF file: *
* (GIF_LIB file DGIF_LIB.C). *
******************************************************************************/
GifFileType *DGifOpenFileName(const char *GifFileName);
GifFileType *DGifOpenFileHandle(int GifFileHandle);
GifFileType *DGifOpen( void* userPtr, InputFunc readFunc ); /* new one (TVT) */
int DGifSlurp(GifFileType *GifFile);
int DGifGetScreenDesc(GifFileType *GifFile);
int DGifGetRecordType(GifFileType *GifFile, GifRecordType *GifType);
int DGifGetImageDesc(GifFileType *GifFile);
int DGifGetLine(GifFileType *GifFile, GifPixelType *GifLine, int GifLineLen);
int DGifGetPixel(GifFileType *GifFile, GifPixelType GifPixel);
int DGifGetComment(GifFileType *GifFile, char *GifComment);
int DGifGetExtension(GifFileType *GifFile, int *GifExtCode,
GifByteType **GifExtension);
int DGifGetExtensionNext(GifFileType *GifFile, GifByteType **GifExtension);
int DGifGetCode(GifFileType *GifFile, int *GifCodeSize,
GifByteType **GifCodeBlock);
int DGifGetCodeNext(GifFileType *GifFile, GifByteType **GifCodeBlock);
int DGifGetLZCodes(GifFileType *GifFile, int *GifCode);
int DGifCloseFile(GifFileType *GifFile);
#define D_GIF_ERR_OPEN_FAILED 101 /* And DGif possible errors. */
#define D_GIF_ERR_READ_FAILED 102
#define D_GIF_ERR_NOT_GIF_FILE 103
#define D_GIF_ERR_NO_SCRN_DSCR 104
#define D_GIF_ERR_NO_IMAG_DSCR 105
#define D_GIF_ERR_NO_COLOR_MAP 106
#define D_GIF_ERR_WRONG_RECORD 107
#define D_GIF_ERR_DATA_TOO_BIG 108
#define D_GIF_ERR_NOT_ENOUGH_MEM 109
#define D_GIF_ERR_CLOSE_FAILED 110
#define D_GIF_ERR_NOT_READABLE 111
#define D_GIF_ERR_IMAGE_DEFECT 112
#define D_GIF_ERR_EOF_TOO_SOON 113
/******************************************************************************
* O.K., here are the routines from GIF_LIB file QUANTIZE.C. *
******************************************************************************/
int QuantizeBuffer(unsigned int Width, unsigned int Height, int *ColorMapSize,
GifByteType *RedInput, GifByteType *GreenInput, GifByteType *BlueInput,
GifByteType *OutputBuffer, GifColorType *OutputColorMap);
/******************************************************************************
* O.K., here are the routines from GIF_LIB file QPRINTF.C. *
******************************************************************************/
extern int GifQuietPrint;
#ifdef HAVE_VARARGS_H
extern void GifQprintf();
#else
extern void GifQprintf(char *Format, ...);
#endif /* HAVE_VARARGS_H */
/******************************************************************************
* O.K., here are the routines from GIF_LIB file GIF_ERR.C. *
******************************************************************************/
extern void PrintGifError(void);
extern int GifLastError(void);
/******************************************************************************
* O.K., here are the routines from GIF_LIB file DEV2GIF.C. *
******************************************************************************/
extern int DumpScreen2Gif(const char *FileName,
int ReqGraphDriver,
int ReqGraphMode1,
int ReqGraphMode2,
int ReqGraphMode3);
/*****************************************************************************
*
* Everything below this point is new after version 1.2, supporting `slurp
* mode' for doing I/O in two big belts with all the image-bashing in core.
*
*****************************************************************************/
/******************************************************************************
* Color Map handling from ALLOCGIF.C *
******************************************************************************/
extern ColorMapObject *MakeMapObject(int ColorCount, const GifColorType *ColorMap);
extern void FreeMapObject(ColorMapObject *Object);
extern ColorMapObject *UnionColorMap(
const ColorMapObject *ColorIn1,
const ColorMapObject *ColorIn2,
GifPixelType ColorTransIn2[]);
extern int BitSize(int n);
/******************************************************************************
* Support for the in-core structures allocation (slurp mode). *
******************************************************************************/
/* This is the in-core version of an extension record */
typedef struct {
int ByteCount;
char *Bytes; /* on malloc(3) heap */
int Function; /* Holds the type of the Extension block. */
} ExtensionBlock;
/* This holds an image header, its unpacked raster bits, and extensions */
typedef struct SavedImage {
GifImageDesc ImageDesc;
char *RasterBits; /* on malloc(3) heap */
int Function; /* DEPRECATED: Use ExtensionBlocks[x].Function
* instead */
int ExtensionBlockCount;
ExtensionBlock *ExtensionBlocks; /* on malloc(3) heap */
} SavedImage;
extern void ApplyTranslation(SavedImage *Image, GifPixelType Translation[]);
extern void MakeExtension(SavedImage *New, int Function);
extern int AddExtensionBlock(SavedImage *New, int Len, char ExtData[]);
extern void FreeExtension(SavedImage *Image);
extern SavedImage *MakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom);
extern void FreeSavedImages(GifFileType *GifFile);
/******************************************************************************
* The library's internal utility font *
******************************************************************************/
#define GIF_FONT_WIDTH 8
#define GIF_FONT_HEIGHT 8
extern unsigned char AsciiTable[][GIF_FONT_WIDTH];
extern void DrawText(SavedImage *Image,
const int x, const int y,
const char *legend,
const int color);
extern void DrawBox(SavedImage *Image,
const int x, const int y,
const int w, const int d,
const int color);
void DrawRectangle(SavedImage *Image,
const int x, const int y,
const int w, const int d,
const int color);
extern void DrawBoxedText(SavedImage *Image,
const int x, const int y,
const char *legend,
const int border,
const int bg,
const int fg);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _GIF_LIB_H */

64
lib/lungif/gif_lib_private.h Executable file
View File

@ -0,0 +1,64 @@
#ifndef _GIF_LIB_PRIVATE_H
#define _GIF_LIB_PRIVATE_H
#include "gif_lib.h"
#define PROGRAM_NAME "GIFLIB"
#define LZ_MAX_CODE 4095 /* Biggest code possible in 12 bits. */
#define LZ_BITS 12
#define FLUSH_OUTPUT 4096 /* Impossible code, to signal flush. */
#define FIRST_CODE 4097 /* Impossible code, to signal first. */
#define NO_SUCH_CODE 4098 /* Impossible code, to signal empty. */
#define FILE_STATE_WRITE 0x01
#define FILE_STATE_SCREEN 0x02
#define FILE_STATE_IMAGE 0x04
#define FILE_STATE_READ 0x08
#define IS_READABLE(Private) (Private->FileState & FILE_STATE_READ)
#define IS_WRITEABLE(Private) (Private->FileState & FILE_STATE_WRITE)
typedef struct GifFilePrivateType {
int FileState,
FileHandle, /* Where all this data goes to! */
BitsPerPixel, /* Bits per pixel (Codes uses at least this + 1). */
ClearCode, /* The CLEAR LZ code. */
EOFCode, /* The EOF LZ code. */
RunningCode, /* The next code algorithm can generate. */
RunningBits,/* The number of bits required to represent RunningCode. */
MaxCode1, /* 1 bigger than max. possible code, in RunningBits bits. */
LastCode, /* The code before the current code. */
CrntCode, /* Current algorithm code. */
StackPtr, /* For character stack (see below). */
CrntShiftState; /* Number of bits in CrntShiftDWord. */
unsigned long CrntShiftDWord; /* For bytes decomposition into codes. */
unsigned long PixelCount; /* Number of pixels in image. */
int *File; /* File as stream. */
InputFunc Read; /* function to read gif input (TVT) */
OutputFunc Write; /* function to write gif output (MRB) */
GifByteType Buf[256]; /* Compressed input is buffered here. */
GifByteType Stack[LZ_MAX_CODE]; /* Decoded pixels are stacked here. */
GifByteType Suffix[LZ_MAX_CODE+1]; /* So we can trace the codes. */
unsigned int Prefix[LZ_MAX_CODE+1];
} GifFilePrivateType;
extern int _GifError;
#ifdef SYSV
static char *VersionStr =
"Gif library module,\t\tEric S. Raymond\n\
(C) Copyright 1997 Eric S. Raymond\n";
#else
static char *VersionStr =
PROGRAM_NAME
" IBMPC "
GIF_LIB_VERSION
" Eric S. Raymond, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1997 Eric S. Raymond\n";
#endif /* SYSV */
#endif /* _GIF_LIB_PRIVATE_H */

350
lib/lungif/gifalloc.c Executable file
View File

@ -0,0 +1,350 @@
/*****************************************************************************
* "Gif-Lib" - Yet another gif library. *
* *
* Written by: Gershon Elber Ver 0.1, Jun. 1989 *
* Extensively hacked by: Eric S. Raymond Ver 1.?, Sep 1992 *
******************************************************************************
* GIF construction tools *
******************************************************************************
* History: *
* 15 Sep 92 - Version 1.0 by Eric Raymond. *
*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "gif_lib.h"
#define MAX(x, y) (((x) > (y)) ? (x) : (y))
/******************************************************************************
* Miscellaneous utility functions *
******************************************************************************/
int BitSize(int n)
/* return smallest bitfield size n will fit in */
{
register i;
for (i = 1; i <= 8; i++)
if ((1 << i) >= n)
break;
return(i);
}
/******************************************************************************
* Color map object functions *
******************************************************************************/
ColorMapObject *MakeMapObject(int ColorCount, const GifColorType *ColorMap)
/*
* Allocate a color map of given size; initialize with contents of
* ColorMap if that pointer is non-NULL.
*/
{
ColorMapObject *Object;
if (ColorCount != (1 << BitSize(ColorCount)))
return((ColorMapObject *)NULL);
Object = (ColorMapObject *)malloc(sizeof(ColorMapObject));
if (Object == (ColorMapObject *)NULL)
return((ColorMapObject *)NULL);
Object->Colors = (GifColorType *)calloc(ColorCount, sizeof(GifColorType));
if (Object->Colors == (GifColorType *)NULL)
return((ColorMapObject *)NULL);
Object->ColorCount = ColorCount;
Object->BitsPerPixel = BitSize(ColorCount);
if (ColorMap)
memcpy((char *)Object->Colors,
(char *)ColorMap, ColorCount * sizeof(GifColorType));
return(Object);
}
void FreeMapObject(ColorMapObject *Object)
/*
* Free a color map object
*/
{
free(Object->Colors);
free(Object);
}
#ifdef DEBUG
void DumpColorMap(ColorMapObject *Object, FILE *fp)
{
if (Object)
{
int i, j, Len = Object->ColorCount;
for (i = 0; i < Len; i+=4) {
for (j = 0; j < 4 && j < Len; j++) {
fprintf(fp,
"%3d: %02x %02x %02x ", i + j,
Object->Colors[i + j].Red,
Object->Colors[i + j].Green,
Object->Colors[i + j].Blue);
}
fprintf(fp, "\n");
}
}
}
#endif /* DEBUG */
ColorMapObject *UnionColorMap(
const ColorMapObject *ColorIn1,
const ColorMapObject *ColorIn2,
GifPixelType ColorTransIn2[])
/*
* Compute the union of two given color maps and return it. If result can't
* fit into 256 colors, NULL is returned, the allocated union otherwise.
* ColorIn1 is copied as is to ColorUnion, while colors from ColorIn2 are
* copied iff they didn't exist before. ColorTransIn2 maps the old
* ColorIn2 into ColorUnion color map table.
*/
{
int i, j, CrntSlot, RoundUpTo, NewBitSize;
ColorMapObject *ColorUnion;
/*
* Allocate table which will hold the result for sure.
*/
ColorUnion
= MakeMapObject(MAX(ColorIn1->ColorCount,ColorIn2->ColorCount)*2,NULL);
if (ColorUnion == NULL)
return(NULL);
/* Copy ColorIn1 to ColorUnionSize; */
for (i = 0; i < ColorIn1->ColorCount; i++)
ColorUnion->Colors[i] = ColorIn1->Colors[i];
CrntSlot = ColorIn1->ColorCount;
/*
* Potentially obnoxious hack:
*
* Back CrntSlot down past all contiguous {0, 0, 0} slots at the end
* of table 1. This is very useful if your display is limited to
* 16 colors.
*/
while (ColorIn1->Colors[CrntSlot-1].Red == 0
&& ColorIn1->Colors[CrntSlot-1].Green == 0
&& ColorIn1->Colors[CrntSlot-1].Red == 0)
CrntSlot--;
/* Copy ColorIn2 to ColorUnionSize (use old colors if they exist): */
for (i = 0; i < ColorIn2->ColorCount && CrntSlot<=256; i++)
{
/* Let's see if this color already exists: */
for (j = 0; j < ColorIn1->ColorCount; j++)
if (memcmp(&ColorIn1->Colors[j], &ColorIn2->Colors[i], sizeof(GifColorType)) == 0)
break;
if (j < ColorIn1->ColorCount)
ColorTransIn2[i] = j; /* color exists in Color1 */
else
{
/* Color is new - copy it to a new slot: */
ColorUnion->Colors[CrntSlot] = ColorIn2->Colors[i];
ColorTransIn2[i] = CrntSlot++;
}
}
if (CrntSlot > 256)
{
FreeMapObject(ColorUnion);
return((ColorMapObject *)NULL);
}
NewBitSize = BitSize(CrntSlot);
RoundUpTo = (1 << NewBitSize);
if (RoundUpTo != ColorUnion->ColorCount)
{
register GifColorType *Map = ColorUnion->Colors;
/*
* Zero out slots up to next power of 2.
* We know these slots exist because of the way ColorUnion's
* start dimension was computed.
*/
for (j = CrntSlot; j < RoundUpTo; j++)
Map[j].Red = Map[j].Green = Map[j].Blue = 0;
/* perhaps we can shrink the map? */
if (RoundUpTo < ColorUnion->ColorCount)
ColorUnion->Colors
= (GifColorType *)realloc(Map, sizeof(GifColorType)*RoundUpTo);
}
ColorUnion->ColorCount = RoundUpTo;
ColorUnion->BitsPerPixel = NewBitSize;
return(ColorUnion);
}
void ApplyTranslation(SavedImage *Image, GifPixelType Translation[])
/*
* Apply a given color translation to the raster bits of an image
*/
{
register int i;
register int RasterSize = Image->ImageDesc.Height * Image->ImageDesc.Width;
for (i = 0; i < RasterSize; i++)
Image->RasterBits[i] = Translation[Image->RasterBits[i]];
}
/******************************************************************************
* Extension record functions *
******************************************************************************/
void MakeExtension(SavedImage *New, int Function)
{
New->Function = Function;
/*
* Someday we might have to deal with multiple extensions.
*/
}
int AddExtensionBlock(SavedImage *New, int Len, char ExtData[])
{
ExtensionBlock *ep;
if (New->ExtensionBlocks == NULL)
New->ExtensionBlocks = (ExtensionBlock *)malloc(sizeof(ExtensionBlock));
else
New->ExtensionBlocks =
(ExtensionBlock *)realloc(New->ExtensionBlocks,
sizeof(ExtensionBlock) * (New->ExtensionBlockCount + 1));
if (New->ExtensionBlocks == NULL)
return(GIF_ERROR);
ep = &New->ExtensionBlocks[New->ExtensionBlockCount++];
if ((ep->Bytes = (char *)malloc(ep->ByteCount = Len)) == NULL)
return(GIF_ERROR);
if (ExtData) {
memcpy(ep->Bytes, ExtData, Len);
ep->Function = New->Function;
}
return(GIF_OK);
}
void FreeExtension(SavedImage *Image)
{
ExtensionBlock *ep;
for (ep = Image->ExtensionBlocks;
ep < Image->ExtensionBlocks + Image->ExtensionBlockCount;
ep++)
(void) free((char *)ep->Bytes);
free((char *)Image->ExtensionBlocks);
Image->ExtensionBlocks = NULL;
}
/******************************************************************************
* Image block allocation functions *
******************************************************************************/
SavedImage *MakeSavedImage(GifFileType *GifFile, const SavedImage *CopyFrom)
/*
* Append an image block to the SavedImages array
*/
{
SavedImage *sp;
if (GifFile->SavedImages == NULL)
GifFile->SavedImages = (SavedImage *)malloc(sizeof(SavedImage));
else
GifFile->SavedImages = (SavedImage *)realloc(GifFile->SavedImages,
sizeof(SavedImage) * (GifFile->ImageCount+1));
if (GifFile->SavedImages == NULL)
return((SavedImage *)NULL);
else
{
sp = &GifFile->SavedImages[GifFile->ImageCount++];
memset((char *)sp, '\0', sizeof(SavedImage));
if (CopyFrom)
{
memcpy((char *)sp, CopyFrom, sizeof(SavedImage));
/*
* Make our own allocated copies of the heap fields in the
* copied record. This guards against potential aliasing
* problems.
*/
/* first, the local color map */
if (sp->ImageDesc.ColorMap)
sp->ImageDesc.ColorMap =
MakeMapObject(CopyFrom->ImageDesc.ColorMap->ColorCount,
CopyFrom->ImageDesc.ColorMap->Colors);
/* next, the raster */
sp->RasterBits = (char *)malloc(sizeof(GifPixelType)
* CopyFrom->ImageDesc.Height
* CopyFrom->ImageDesc.Width);
memcpy(sp->RasterBits,
CopyFrom->RasterBits,
sizeof(GifPixelType)
* CopyFrom->ImageDesc.Height
* CopyFrom->ImageDesc.Width);
/* finally, the extension blocks */
if (sp->ExtensionBlocks)
{
sp->ExtensionBlocks
= (ExtensionBlock*)malloc(sizeof(ExtensionBlock)
* CopyFrom->ExtensionBlockCount);
memcpy(sp->ExtensionBlocks,
CopyFrom->ExtensionBlocks,
sizeof(ExtensionBlock)
* CopyFrom->ExtensionBlockCount);
/*
* For the moment, the actual blocks can take their
* chances with free(). We'll fix this later.
*/
}
}
return(sp);
}
}
void FreeSavedImages(GifFileType *GifFile)
{
SavedImage *sp;
for (sp = GifFile->SavedImages;
sp < GifFile->SavedImages + GifFile->ImageCount;
sp++)
{
if (sp->ImageDesc.ColorMap)
FreeMapObject(sp->ImageDesc.ColorMap);
if (sp->RasterBits)
free((char *)sp->RasterBits);
if (sp->ExtensionBlocks)
FreeExtension(sp);
}
free((char *) GifFile->SavedImages);
}