icon2gif.c
来自「giflib-4.1.6.tar.gz,最新的GIF 解码库」· C语言 代码 · 共 771 行 · 第 1/2 页
C
771 行
/****************************************************************************** "Gif-Lib" - Yet another gif library. ** ** Written by: Gershon Elber Ver 0.1, Jun. 1989 ******************************************************************************** Module to convert an editable text representation into a GIF file. ******************************************************************************** History: ** 15 Sep 92 - Version 1.0 by Eric Raymond. ******************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#ifdef __MSDOS__#include <dos.h>#include <alloc.h>#include <stdlib.h>#include <graphics.h>#include <io.h>#endif /* __MSDOS__ */#ifndef __MSDOS__#include <stdlib.h>#endif#include <stdio.h>#include <string.h>#include <ctype.h>#include "gif_lib.h"#include "getarg.h"#define PROGRAM_NAME "icon2gif"#ifdef __MSDOS__extern unsigned int _stklen = 16384; /* Increase default stack size. */#endif /* __MSDOS__ */#ifdef SYSVstatic char *VersionStr = "Gif compiler,\t\tEric S. Raymond\n\ (C) Copyright 1992 Eric S. Raymond, all rights reserved.\n";static char *CtrlStr = "GifAsm q%- d%- t%-Characters!s h%- GifFile(s)!*s";#elsestatic char *VersionStr = PROGRAM_NAME GIF_LIB_VERSION " Eric Raymond, " __DATE__ ", " __TIME__ "\n" "(C) Copyright 1992 Eric Raymond.\n";static char *CtrlStr = PROGRAM_NAME " q%- d%- t%-Characters!s h%- GifFile(s)!*s";#endif /* SYSV */static char KeyLetters[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:<=>?@[\\]^_`{|}~";#define PRINTABLES (sizeof(KeyLetters) - 1)static int HandleGifError(GifFileType *GifFile);static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);#if FALSE/* Apparently this is an unimplemented function of the program */static int MergeImage(GifFileType *BaseFile, GifFileType *Inclusion, SavedImage *NewImage);#endifstatic void Icon2Gif(char *FileName, FILE *txtin, int fdout);static void Gif2Icon(char *FileName, int fdin, int fdout, char NameTable[]);static void VisibleDumpBuffer(char *buf, int Len);static int EscapeString(char *cp, char *tp);/******************************************************************************* Main Sequence *******************************************************************************/int main(int argc, char **argv){ int i, Error, NumFiles, DisasmFlag = FALSE, HelpFlag = FALSE, TextLineFlag = FALSE; char **FileNames = NULL; char *TextLines[1]; if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuietPrint, &DisasmFlag, &TextLineFlag, &TextLines[0], &HelpFlag, &NumFiles, &FileNames)) != FALSE) { GAPrintErrMsg(Error); GAPrintHowTo(CtrlStr); exit(EXIT_FAILURE); } if (HelpFlag) { fprintf(stderr, VersionStr); GAPrintHowTo(CtrlStr); exit(EXIT_SUCCESS); } if (!DisasmFlag && NumFiles > 1) { GIF_MESSAGE("Error in command line parsing - one text input please."); GAPrintHowTo(CtrlStr); exit(EXIT_FAILURE); } if (!DisasmFlag && TextLineFlag) { GIF_MESSAGE("Error in command line parsing - -t invalid without -d."); GAPrintHowTo(CtrlStr); exit(EXIT_FAILURE); } if (NumFiles == 0) { if (DisasmFlag) Gif2Icon("Stdin", 0, 1, TextLineFlag ? TextLines[0] : KeyLetters); else Icon2Gif("Stdin", stdin, 1); } else for (i = 0; i < NumFiles; i++) { FILE *fp; if ((fp = fopen(FileNames[i], "r")) == (FILE *)NULL) { (void) fprintf(stderr, "Can't open %s\n", FileNames[i]); exit(EXIT_FAILURE); } if (DisasmFlag) { printf("#\n# GIF information from %s\n", FileNames[i]); Gif2Icon(FileNames[i], -1, 1, TextLineFlag ? TextLines[0] : KeyLetters); } else { Icon2Gif(FileNames[i], fp, 1); } (void) fclose(fp); } return 0;}/******************************************************************************* Parse image directives *******************************************************************************/#define PARSE_ERROR(str) (void) fprintf(stderr,"%s:%d: %s\n",FileName,LineNum,str);static void Icon2Gif(char *FileName, FILE *txtin, int fdout){ unsigned int ExtCode, ColorMapSize = 0; GifColorType GlobalColorMap[256], LocalColorMap[256], *ColorMap = GlobalColorMap; char GlobalColorKeys[PRINTABLES], LocalColorKeys[PRINTABLES], *KeyTable = GlobalColorKeys; int red, green, blue; char buf[BUFSIZ * 2], InclusionFile[64]; GifFileType *GifFileOut; SavedImage *NewImage = NULL; int n, LineNum = 0; if ((GifFileOut = EGifOpenFileHandle(fdout)) == NULL) { (void) HandleGifError(GifFileOut); } /* OK, interpret directives */ while (fgets(buf, sizeof(buf), txtin) != (char *)NULL) { char *cp; ++LineNum; /* * Skip lines consisting only of whitespace and comments */ for (cp = buf; isspace((int)(*cp)); cp++) continue; if (*cp == '#' || *cp == '\0') continue; /* * If there's a trailing comment, nuke it and all preceding whitespace. * But preserve the EOL. */ if ((cp = strchr(buf, '#')) && (cp == strrchr(cp, '#'))) { while (isspace((int)(*--cp))) continue; *++cp = '\n'; *++cp = '\0'; } /* * Explicit header declarations */ if (sscanf(buf, "screen width %d\n", &GifFileOut->SWidth) == 1) continue; else if (sscanf(buf, "screen height %d\n", &GifFileOut->SHeight) == 1) continue; else if (sscanf(buf, "screen colors %d\n", &n) == 1) { int ResBits = BitSize(n); if (n > 256 || n < 0 || n != (1 << ResBits)) { PARSE_ERROR("Invalid color resolution value."); exit(EXIT_FAILURE); } GifFileOut->SColorResolution = ResBits; continue; } else if (sscanf(buf, "screen background %d\n", &GifFileOut->SBackGroundColor) == 1) continue; /* * Color table parsing */ else if (strcmp(buf, "screen map\n") == 0) { if (GifFileOut->SColorMap != NULL) { PARSE_ERROR("You've already declared a global color map."); exit(EXIT_FAILURE); } ColorMapSize = 0; ColorMap = GlobalColorMap; KeyTable = GlobalColorKeys; memset(GlobalColorKeys, '\0', sizeof(GlobalColorKeys)); } else if (strcmp(buf, "image map\n") == 0) { if (NewImage == NULL) { PARSE_ERROR("No previous image declaration."); exit(EXIT_FAILURE); } ColorMapSize = 0; ColorMap = LocalColorMap; KeyTable = LocalColorKeys; memset(LocalColorKeys, '\0', sizeof(LocalColorKeys)); } else if (sscanf(buf, " rgb %d %d %d is %c", &red, &green, &blue, &KeyTable[ColorMapSize]) == 4) { ColorMap[ColorMapSize].Red = red; ColorMap[ColorMapSize].Green = green; ColorMap[ColorMapSize].Blue = blue; ColorMapSize++; } else if (strcmp(buf, "end\n") == 0) { ColorMapObject *NewMap; NewMap = MakeMapObject(1 << BitSize(ColorMapSize), ColorMap); if (NewMap == (ColorMapObject *)NULL) { PARSE_ERROR("Out of memory while allocating new color map."); exit(EXIT_FAILURE); } if (NewImage) NewImage->ImageDesc.ColorMap = NewMap; else GifFileOut->SColorMap = NewMap; } /* GIF inclusion */ else if (sscanf(buf, "include %s", InclusionFile) == 1) { GifBooleanType DoTranslation; GifPixelType Translation[256]; GifFileType *Inclusion; SavedImage *NewImage, *CopyFrom; if ((Inclusion = DGifOpenFileName(InclusionFile)) == NULL || DGifSlurp(Inclusion) == GIF_ERROR) { PARSE_ERROR("Inclusion read failed."); QuitGifError(Inclusion, GifFileOut); } if ((DoTranslation = (GifFileOut->SColorMap!=(ColorMapObject*)NULL))) { ColorMapObject *UnionMap; UnionMap = UnionColorMap(GifFileOut->SColorMap, Inclusion->SColorMap, Translation); if (UnionMap == NULL) { PARSE_ERROR("Inclusion failed --- global map conflict."); QuitGifError(Inclusion, GifFileOut); } FreeMapObject(GifFileOut->SColorMap); GifFileOut->SColorMap = UnionMap; } for (CopyFrom = Inclusion->SavedImages; CopyFrom < Inclusion->SavedImages + Inclusion->ImageCount; CopyFrom++) { if ((NewImage = MakeSavedImage(GifFileOut, CopyFrom)) == NULL) { PARSE_ERROR("Inclusion failed --- out of memory."); QuitGifError(Inclusion, GifFileOut); } else if (DoTranslation) ApplyTranslation(NewImage, Translation); GifQprintf( "%s: Image %d at (%d, %d) [%dx%d]: from %s\n", PROGRAM_NAME, GifFileOut->ImageCount, NewImage->ImageDesc.Left, NewImage->ImageDesc.Top, NewImage->ImageDesc.Width, NewImage->ImageDesc.Height, InclusionFile); } (void) DGifCloseFile(Inclusion); } /* * Explicit image declarations */ else if (strcmp(buf, "image\n") == 0) { if ((NewImage = MakeSavedImage(GifFileOut, NULL)) == (SavedImage *)NULL) { PARSE_ERROR("Out of memory while allocating image block."); exit(EXIT_FAILURE); } /* use global table unless user specifies a local one */ ColorMap = GlobalColorMap; KeyTable = GlobalColorKeys; } /* * Nothing past this point is valid unless we've seen a previous * image declaration. */ else if (NewImage == (SavedImage *)NULL) { (void) fputs(buf, stderr); PARSE_ERROR("Syntax error in header block."); exit(EXIT_FAILURE); } /* * Accept image attributes */ else if (sscanf(buf, "image top %d\n", &NewImage->ImageDesc.Top) == 1) continue; else if (sscanf(buf, "image left %d\n", &NewImage->ImageDesc.Left)== 1) continue; else if (strcmp(buf, "image interlaced\n") == 0) { NewImage->ImageDesc.Interlace = TRUE; continue; } else if (sscanf(buf,
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?