/****************************************************************************** Interpret the command line and generate the given GIF file. ******************************************************************************/ int main(int argc, char **argv) { int i, j, l, GifNoisyPrint, ColorMapSize, ErrorCode; bool Error, BackGroundFlag = false, HelpFlag = false; char Line[LINE_LEN]; GifRowType RasterBuffer[GIF_FONT_HEIGHT]; ColorMapObject *ColorMap; GifFileType *GifFile; GifColorType ScratchMap[256]; int red, green, blue; if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint, &BackGroundFlag, &BackGround, &HelpFlag)) != false) { GAPrintErrMsg(Error); GAPrintHowTo(CtrlStr); exit(EXIT_FAILURE); } if (HelpFlag) { (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR); GAPrintHowTo(CtrlStr); exit(EXIT_SUCCESS); } /* Allocate the raster buffer for GIF_FONT_HEIGHT scan lines. */ for (i = 0; i < GIF_FONT_HEIGHT; i++) { if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) * IMAGEWIDTH)) == NULL) GIF_EXIT("Failed to allocate memory required, aborted."); } /* Open stdout for the output file: */ if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) { PrintGifError(ErrorCode); exit(EXIT_FAILURE); } /* Read the color map in ColorFile into this color map: */ ColorMapSize = 0; while (fscanf(stdin, "%*3d %3d %3d %3d\n", &red, &green, &blue) == 3) { ScratchMap[ColorMapSize].Red = red; ScratchMap[ColorMapSize].Green = green; ScratchMap[ColorMapSize].Blue = blue; ColorMapSize++; } if ((ColorMap = GifMakeMapObject(1 << GifBitSize(ColorMapSize), ScratchMap)) == NULL) GIF_EXIT("Failed to allocate memory required, aborted."); if (EGifPutScreenDesc(GifFile, IMAGEWIDTH, ColorMapSize * GIF_FONT_HEIGHT, GifBitSize(ColorMapSize), BackGround, ColorMap) == GIF_ERROR) QuitGifError(GifFile); /* Dump out the image descriptor: */ if (EGifPutImageDesc(GifFile, 0, 0, IMAGEWIDTH, ColorMapSize * GIF_FONT_HEIGHT, false, NULL) == GIF_ERROR) QuitGifError(GifFile); GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ", PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top, GifFile->Image.Width, GifFile->Image.Height); for (i = l = 0; i < ColorMap->ColorCount; i++) { (void)snprintf(Line, sizeof(Line), "Color %-3d: [%-3d, %-3d, %-3d] ", i, ColorMap->Colors[i].Red, ColorMap->Colors[i].Green, ColorMap->Colors[i].Blue); GenRasterTextLine(RasterBuffer, Line, IMAGEWIDTH, i); for (j = 0; j < GIF_FONT_HEIGHT; j++) { if (EGifPutLine(GifFile, RasterBuffer[j], IMAGEWIDTH) == GIF_ERROR) QuitGifError(GifFile); GifQprintf("\b\b\b\b%-4d", l++); } } if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR) { PrintGifError(ErrorCode); if (GifFile != NULL) { EGifCloseFile(GifFile, NULL); } exit(EXIT_FAILURE); } return 0; }
/****************************************************************************** Interpret the command line and generate the given GIF file. ******************************************************************************/ int main(int argc, char **argv) { int i, j, l, ImageWidth, ImageHeight, NumOfLines, LogNumLevels, ErrorCode, NumLevels, ColorMapSize = 1, ForeGroundIndex = DEFAULT_FG_INDEX; bool Error, ClrMapSizeFlag = false, ForeGroundFlag = false, TextLineFlag = false, HelpFlag = false, ColorFlag = false; char *TextLines[MAX_NUM_TEXT_LINES], Line[LINE_LEN]; GifRowType RasterBuffer[GIF_FONT_HEIGHT]; ColorMapObject *ColorMap; GifFileType *GifFile; if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint, &ClrMapSizeFlag, &ColorMapSize, &ForeGroundFlag, &ForeGroundIndex, &ColorFlag, &RedColor, &GreenColor, &BlueColor, &TextLineFlag, &TextLines[0], &HelpFlag)) != false) { GAPrintErrMsg(Error); GAPrintHowTo(CtrlStr); exit(EXIT_FAILURE); } if (HelpFlag) { (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR); GAPrintHowTo(CtrlStr); exit(EXIT_SUCCESS); } if (ForeGroundIndex > 255 || ForeGroundIndex < 1) GIF_EXIT("Foregound (-f) should be in the range 1..255, aborted."); if (ColorMapSize > 8 || ColorMapSize < 1) GIF_EXIT("ColorMapSize (-s) should be in the range 1..8, aborted."); if (TextLineFlag) { NumOfLines = 1; ImageHeight = GIF_FONT_HEIGHT; ImageWidth = GIF_FONT_WIDTH * strlen(TextLines[0]); } else { NumOfLines = l = 0; while (fgets(Line, LINE_LEN - 1, stdin)) { for (i = strlen(Line); i > 0 && Line[i-1] <= ' '; i--); Line[i] = 0; if (l < i) l = i; TextLines[NumOfLines++] = strdup(Line); if (NumOfLines == MAX_NUM_TEXT_LINES) GIF_EXIT("Input file has too many lines, aborted."); } if (NumOfLines == 0) GIF_EXIT("No input text, aborted."); ImageHeight = GIF_FONT_HEIGHT * NumOfLines; ImageWidth = GIF_FONT_WIDTH * l; } /* Allocate the raster buffer for GIF_FONT_HEIGHT scan lines (one text line). */ for (i = 0; i < GIF_FONT_HEIGHT; i++) if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth)) == NULL) GIF_EXIT("Failed to allocate memory required, aborted."); /* Open stdout for the output file: */ if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) { PrintGifError(ErrorCode); exit(EXIT_FAILURE); } /* Dump out screen description with given size and generated color map: */ for (LogNumLevels = 1, NumLevels = 2; NumLevels < ForeGroundIndex; LogNumLevels++, NumLevels <<= 1); if (NumLevels < (1 << ColorMapSize)) { NumLevels = (1 << ColorMapSize); LogNumLevels = ColorMapSize; } if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL) GIF_EXIT("Failed to allocate memory required, aborted."); for (i = 0; i < NumLevels; i++) ColorMap->Colors[i].Red = ColorMap->Colors[i].Green = ColorMap->Colors[i].Blue = 0; ColorMap->Colors[ForeGroundIndex].Red = RedColor; ColorMap->Colors[ForeGroundIndex].Green = GreenColor; ColorMap->Colors[ForeGroundIndex].Blue = BlueColor; if (EGifPutScreenDesc(GifFile, ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap) == GIF_ERROR) QuitGifError(GifFile); /* Dump out the image descriptor: */ if (EGifPutImageDesc(GifFile, 0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR) QuitGifError(GifFile); GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ", PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top, GifFile->Image.Width, GifFile->Image.Height); for (i = l = 0; i < NumOfLines; i++) { GenRasterTextLine(RasterBuffer, TextLines[i], ImageWidth, ForeGroundIndex); for (j = 0; j < GIF_FONT_HEIGHT; j++) { if (EGifPutLine(GifFile, RasterBuffer[j], ImageWidth) == GIF_ERROR) QuitGifError(GifFile); GifQprintf("\b\b\b\b%-4d", l++); } } if (EGifCloseFile(GifFile) == GIF_ERROR) QuitGifError(GifFile); return 0; }