gif2epsn.c
来自「giflib-4.1.6.tar.gz,最新的GIF 解码库」· C语言 代码 · 共 615 行 · 第 1/2 页
C
615 行
/****************************************************************************** "Gif-Lib" - Yet another gif library. ** ** Written by: Gershon Elber Ver 0.1, Jul. 1989 ******************************************************************************** Program to dump GIF file into EPSON type printers ** Options: ** -q : quiet printing mode. ** -d factor : use dithering of matrix of size factor by factor. ** -t level : set the threshold level of white in the result (0..100). ** -m mapping : methods for mapping the 24bits colors into 1 BW bit. ** -p printer : specify printer to print to (lpt1: by default). ** -n : nice mode : uses double density to achieve better quality. ** -i : invert the image. ** -h : on-line help. ******************************************************************************** History: ** 15 Jul 89 - Version 1.0 by Gershon Elber. ** 22 Dec 89 - Fix problems with const strings been modified (Version 1.1). ******************************************************************************/#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__ */#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 "Gif2Epsn"#define C2BW_BACK_GROUND 0 /*Methods to map 24bits Colors to 1 BW bit.*/#define C2BW_GREY_LEVELS 1#define C2BW_DITHER 2#define C2BW_NUM_METHODS 3 /* Always hold # of methods. */#define DEFAULT_THRESHOLD 5000 /* Color->BW threshold level. */#define DITHER_MIN_MATRIX 2#define DITHER_MAX_MATRIX 4/* The epson specific are defined here: */#define EPSON_WIDTH 80 /* 80 char per line. */#define EPSON_PIXEL_2_CHAR 8 /* 8 pixels per char, in REG_DENSITY. */#define EPSON_ESC "\033" /* Actually regular escape char. */#define EPSON_RESET "\033@" /* Reset the printer. */#define EPSON_VERTICAL_SPACE "\033A\010" /* 8/72 inch vertically. */#define EPSON_REG_DENSITY "\033K" /* 640 pixels per 7.5" (line). */#define EPSON_DUAL_DENSITY "\033L" /* 1280 pixels per 7.5" (line). */#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 = "Gif2Epsn q%- d%-DitherSize!d t%-BWThreshold!d m%-Mapping!d i%- n%- p%-PrinterName!s 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%- d%-DitherSize!d t%-BWThreshold!d m%-Mapping!d i%- n%- p%-PrinterName!s h%- GifFile!*s";#endif /* SYSV */static char *PrinterName = NULL;/* Make some variables global, so we could access them faster: */static int ImageNum = 0, BackGround = 0, DitherSize = 2, DitherFlag = FALSE, BWThresholdFlag = FALSE, Threshold, BWThreshold = DEFAULT_THRESHOLD, /* Color->BW mapping threshold. */ Mapping, MappingFlag = FALSE, InvertFlag = FALSE, NiceFlag = FALSE, PrinterFlag = FALSE, HelpFlag = FALSE, ColorToBWMapping = C2BW_BACK_GROUND, InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should */ InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */static GifColorType *ColorMap;static void EvalDitheredScanline(GifRowType *ScreenBuffer, int Row, int RowSize, GifRowType *DitherBuffer);static void DumpScreen2Epsn(GifRowType *ScreenBuffer, int ScreenWidth, int ScreenHeight);static void PutString(FILE *Prt, int DirectPrint, char *Str, int Len);static void PutString2(FILE *Prt, int DirectPrint, char *Str, int Len);/******************************************************************************* Interpret the command line and scan the given GIF file. *******************************************************************************/int main(int argc, char **argv){ int i, j, Error, NumFiles, 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, &DitherFlag, &DitherSize, &BWThresholdFlag, &Threshold, &MappingFlag, &Mapping, &InvertFlag, &NiceFlag, &PrinterFlag, &PrinterName, &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 (!PrinterFlag) PrinterName = ""; if (DitherFlag) { /* Make sure we are o.k.: */ if (DitherSize > DITHER_MAX_MATRIX) DitherSize = DITHER_MAX_MATRIX; if (DitherSize < DITHER_MIN_MATRIX) DitherSize = DITHER_MAX_MATRIX; } /* As Threshold is in [0..100] range and BWThreshold is [0..25500]: */ if (BWThresholdFlag) { if (Threshold > 100 || Threshold < 0) GIF_EXIT("Threshold not in 0..100 percent."); BWThreshold = Threshold * 255; if (BWThreshold == 0) BWThreshold = 1; /* Overcome divide by zero! */ } /* No message is emitted, but mapping method is clipped to exists method.*/ if (MappingFlag) ColorToBWMapping = Mapping % C2BW_NUM_METHODS; 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); } } /* 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, andset their color to background too: */ if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL) GIF_EXIT("Failed to allocate memory required, aborted.\n"); 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->Colors : (GifFile->SColorMap ? GifFile->SColorMap->Colors : NULL)); if (ColorMap == NULL) { fprintf(stderr, "Gif Image does not have a colormap\n"); exit(EXIT_FAILURE); } DumpScreen2Epsn(ScreenBuffer, GifFile->SWidth, GifFile->SHeight); if (DGifCloseFile(GifFile) == GIF_ERROR) { PrintGifError(); exit(EXIT_FAILURE); } return 0;}/****************************************************************************** Routine to evaluate dithered scanlines out of given ones, using Size ** dithering matrix, starting from Row. The given scanlines are NOT modified. ******************************************************************************/static void EvalDitheredScanline(GifRowType *ScreenBuffer, int Row, int RowSize, GifRowType *DitherBuffer){ static char Dither2[2][2] = { /* See Foley & Van Dam pp. 597-601. */
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?