📄 gif2x11.c
字号:
/****************************************************************************** "Gif-Lib" - Yet another gif library. ** ** Written by: Gershon Elber Ver 0.1, Jul. 1989 ******************************************************************************** Program to display GIF file under X11 window system. ** Options: ** -q : quiet printing mode. ** -p PosX PosY : defines the position where to put the image. ** -d Display : what display should go to. ** -f : force attempt to allocate the exact colors. This usually look bad... ** -h : on-line help. ******************************************************************************** History: ** 28 Dec 89 - Version 1.0 by Gershon Elber, color allocation is based on the ** xgif program by John Bradley, bradley@cis.ipenn.edu. ******************************************************************************/#ifdef HAVE_CONFIG_H#include <config.h>#endif#ifdef __MSDOS__#include <graphics.h>#include <stdlib.h>#include <alloc.h>#include <io.h>#include <dos.h>#include <bios.h>#endif /* __MSDOS__ */#ifdef HAVE_LIBX11#include <X11/Xlib.h>#include <X11/Xutil.h>#include <X11/cursorfont.h>#endif /* HAVE_LIBX11 */#ifndef __MSDOS__#include <stdlib.h>#endif#include <stdio.h>#include <ctype.h>#include <string.h>#ifdef HAVE_FCNTL_H#include <fcntl.h>#endif /* HAVE_FCNTL_H */#include "gif_lib.h"#include "getarg.h"#define PROGRAM_NAME "Gif2X11"#define ICON_SIZE 60#define ABS(x) ((x) > 0 ? (x) : (-(x)))#ifdef __MSDOS__extern unsigned int _stklen = 16384; /* Increase default stack size. */#endif /* __MSDOS__ */#ifdef SYSVstatic char *VersionStr = "Gif toolkit module,\t\tGershon Elber\n\ (C) Copyright 1989 Gershon Elber.\n";static char *CtrlStr = "Gif2X11 q%- p%-PosX|PosY!d!d d%-Display!s f%- h%- GifFile!*s";#elsestatic char *VersionStr = PROGRAM_NAME GIF_LIB_VERSION " Gershon Elber, " __DATE__ ", " __TIME__ "\n" "(C) Copyright 1989 Gershon Elber.\n";static char *CtrlStr = PROGRAM_NAME " q%- p%-PosX|PosY!d!d d%-Display!s f%- h%- GifFile!*s";#endif /* SYSV *//* Make some variables global, so we could access them faster: */static int PosFlag = FALSE, HelpFlag = FALSE, DisplayFlag = FALSE, ForceFlag = FALSE, ColorMapSize = 0, BackGround = 0, XPosX = 0, XPosY = 0, InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */ InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */static char *DisplayName = NULL;static ColorMapObject *ColorMap;/* X specific staff goes here. XColorTable will hold the GIF image colors, *//* while XPixelTable will hold the pixel number so we can redirect through *//* it when forming the image bitmap in X format. *//* Note the table has 256 entry which is the maximum allowed in GIF format. */static XColor XColorTable[256];static unsigned long XPixelTable[256];static Display *XDisplay;static int XScreen;static Window Xroot, XImageWndw;static Colormap XColorMap;static GC XGraphContext;static Visual *XVisual;static XImage *XImageBuffer;static Pixmap XIcon;static Cursor XCursor;#define BYTESPERPIXEL 4static void Screen2X(int argc, char **argv, GifRowType *ScreenBuffer, int ScreenWidth, int ScreenHeight);static void AllocateColors1(void);static void AllocateColors2(void);/******************************************************************************* Interpret the command line and scan the given GIF file. *******************************************************************************/int main(int argc, char **argv){ int i, j, Error, NumFiles, ImageNum = 0, Size, Row, Col, Width, Height, ExtCode, Count; GifRecordType RecordType; GifByteType *Extension; char **FileName = NULL; GifRowType *ScreenBuffer; GifFileType *GifFile; if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuietPrint, &PosFlag, &XPosX, &XPosY, &DisplayFlag, &DisplayName, &ForceFlag, &HelpFlag, &NumFiles, &FileName)) != FALSE || (NumFiles > 1 && !HelpFlag)) { if (Error) GAPrintErrMsg(Error); else if (NumFiles > 1) GIF_MESSAGE("Error in command line parsing - one GIF file please."); GAPrintHowTo(CtrlStr); exit(EXIT_FAILURE); } if (HelpFlag) { fprintf(stderr, VersionStr); GAPrintHowTo(CtrlStr); exit(EXIT_SUCCESS); } if (NumFiles == 1) { if ((GifFile = DGifOpenFileName(*FileName)) == NULL) { PrintGifError(); exit(EXIT_FAILURE); } } else { /* Use the stdin instead: */#ifdef __MSDOS__ setmode(0, O_BINARY);#endif /* __MSDOS__ */ if ((GifFile = DGifOpenFileHandle(0)) == NULL) { PrintGifError(); exit(EXIT_FAILURE); } } /* Lets see if we can get access to the X server before we even start: */ if ((XDisplay = (Display *) XOpenDisplay(DisplayName)) == NULL) GIF_EXIT("Failed to access X server, abored."); XScreen = DefaultScreen(XDisplay); Xroot = RootWindow(XDisplay, XScreen); XColorMap = DefaultColormap(XDisplay, XScreen); XGraphContext = DefaultGC(XDisplay, XScreen); XVisual = DefaultVisual(XDisplay, XScreen); XSetBackground(XDisplay, XGraphContext, BlackPixel(XDisplay, XScreen)); XSetForeground(XDisplay, XGraphContext, WhitePixel(XDisplay, XScreen)); /* Allocate the screen as vector of column of rows. We cannt allocate */ /* the all screen at once, as this broken minded CPU can allocate up to */ /* 64k at a time and our image can be bigger than that: */ /* Note this screen is device independent - its the screen as defined by */ /* the GIF file parameters itself. */ if ((ScreenBuffer = (GifRowType *) malloc(GifFile->SHeight * sizeof(GifRowType *))) == NULL) GIF_EXIT("Failed to allocate memory required, aborted."); Size = GifFile->SWidth * sizeof(GifPixelType);/* Size in bytes one row.*/ if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) /* First row. */ GIF_EXIT("Failed to allocate memory required, aborted."); for (i = 0; i < GifFile->SWidth; i++) /* Set its color to BackGround. */ ScreenBuffer[0][i] = GifFile->SBackGroundColor; for (i = 1; i < GifFile->SHeight; i++) { /* Allocate the other rows, and set their color to background too: */ if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL) GIF_EXIT("Failed to allocate memory required, aborted."); memcpy(ScreenBuffer[i], ScreenBuffer[0], Size); } /* Scan the content of the GIF file and load the image(s) in: */ do { if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } switch (RecordType) { case IMAGE_DESC_RECORD_TYPE: if (DGifGetImageDesc(GifFile) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } Row = GifFile->Image.Top; /* Image Position relative to Screen. */ Col = GifFile->Image.Left; Width = GifFile->Image.Width; Height = GifFile->Image.Height; GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ", PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height); if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth || GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) { fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n",ImageNum); exit(EXIT_FAILURE); } if (GifFile->Image.Interlace) { /* Need to perform 4 passes on the images: */ for (Count = i = 0; i < 4; i++) for (j = Row + InterlacedOffset[i]; j<Row + Height; j += InterlacedJumps[i]) { GifQprintf("\b\b\b\b%-4d", Count++); if (DGifGetLine(GifFile, &ScreenBuffer[j][Col], Width) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } } } else { for (i = 0; i < Height; i++) { GifQprintf("\b\b\b\b%-4d", i); if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col], Width) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } } } break; case EXTENSION_RECORD_TYPE: /* Skip any extension blocks in file: */ if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } while (Extension != NULL) { if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } } break; case TERMINATE_RECORD_TYPE: break; default: /* Should be traps by DGifGetRecordType. */ break; } } while (RecordType != TERMINATE_RECORD_TYPE); /* Lets display it - set the global variables required and do it: */ BackGround = GifFile->SBackGroundColor; ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap); if (ColorMap == NULL) { fprintf(stderr, "Gif Image does not have a colormap\n"); exit(EXIT_FAILURE); } ColorMapSize = ColorMap->ColorCount; Screen2X(argc, argv, ScreenBuffer, GifFile->SWidth, GifFile->SHeight);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -