📄 egif_lib.c
字号:
/****************************************************************************** * "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/* Find a thirty-two bit int type */#ifdef HAVE_SYS_TYPES_H#include <sys/types.h>#endif#ifdef HAVE_STDINT_H#include <stdint.h>#endif#ifdef HAVE_UNISTD_H#include <unistd.h>#endif#ifdef HAVE_INTTYPES_H#include <inttypes.h>#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__ */#ifdef HAVE_IO_H#include <io.h>#endif#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif /* HAVE_FCNTL_H */#ifdef HAVE_UNISTD_H#include <unistd.h>#endif /* HAVE_UNISTD_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 ? \ ((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#if defined(__MSDOS__) || defined(WINDOWS32) || defined(_OPEN_BINARY) | O_BINARY#endif /* __MSDOS__ */ , S_IREAD | S_IWRITE); else FileHandle = open(FileName, O_WRONLY | O_CREAT | O_TRUNC#if defined(__MSDOS__) || defined(WINDOWS32) || defined(_OPEN_BINARY) | 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; GifFile = (GifFileType *) malloc(sizeof(GifFileType)); if (GifFile == NULL) { _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return NULL; } memset(GifFile, '\0', sizeof(GifFileType)); Private = (GifFilePrivateType *)malloc(sizeof(GifFilePrivateType)); if (Private == NULL) { free(GifFile); _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return NULL; } if ((Private->HashTable = _InitHashTable()) == NULL) { free(GifFile); free(Private); _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return NULL; }#if defined(__MSDOS__) || defined(WINDOWS32) || defined(_OPEN_BINARY) setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */#endif /* __MSDOS__ */ f = fdopen(FileHandle, "wb"); /* Make it into a stream: */#if defined (__MSDOS__) || defined(WINDOWS32) 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; GifFile = (GifFileType *)malloc(sizeof(GifFileType)); if (GifFile == NULL) { _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return NULL; } memset(GifFile, '\0', sizeof(GifFileType)); Private = (GifFilePrivateType *)malloc(sizeof(GifFilePrivateType)); if (Private == NULL) { free(GifFile); _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return NULL; } Private->HashTable = _InitHashTable(); if (Private->HashTable == NULL) { free (GifFile); free (Private); _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return NULL; } GifFile->Private = (VoidPtr) Private; Private->FileHandle = 0; Private->File = (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. *****************************************************************************/voidEGifSetGifVersion(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. *****************************************************************************/intEGifPutScreenDesc(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, (unsigned char *)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); if (GifFile->SColorMap == NULL) { _GifError = E_GIF_ERR_NOT_ENOUGH_MEM; return GIF_ERROR; } } else GifFile->SColorMap = NULL; /* * Put the logical screen descriptor into the file: */ /* Logical Screen Descriptor: Dimensions */ EGifPutWord(Width, GifFile); EGifPutWord(Height, GifFile); /* Logical Screen Descriptor: Packed Fields */ /* Note: We have actual size of the color table default to the largest * possible size (7+1 == 8 bits) because the decoder can use it to decide * how to display the files. */ Buf[0] = (ColorMap ? 0x80 : 0x00) | /* Yes/no global colormap */ ((ColorRes - 1) << 4) | /* Bits allocated to each primary color */ (ColorMap ? ColorMap->BitsPerPixel - 1 : 0x07 ); /* Actual size of the color table. */ Buf[1] = BackGround; /* Index into the ColorTable for background color */ Buf[2] = 0; /* Pixel Aspect Ratio */#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 attempt to dump an image - any * call to any of the pixel dump routines. *****************************************************************************/intEGifPutImageDesc(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(WINDOWS32) || 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); if (GifFile->Image.ColorMap == NULL) { _GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -