1 /*****************************************************************************
3 gifecho - generate a GIF from ASCII text
5 SPDX-License-Identifier: MIT
7 *****************************************************************************/
18 #define PROGRAM_NAME "gifecho"
20 #define MAX_NUM_TEXT_LINES 100 /* Maximum number of lines in file. */
22 #define LINE_LEN 256 /* Maximum length of one text line. */
24 #define DEFAULT_FG_INDEX 1 /* Text foreground index. */
26 #define DEFAULT_COLOR_RED 255 /* Text foreground color. */
27 #define DEFAULT_COLOR_GREEN 255
28 #define DEFAULT_COLOR_BLUE 255
30 __attribute__((__section__(".tizen.build-id")))
36 __DATE__ ", " __TIME__ "\n"
37 "(C) Copyright 1989 Gershon Elber.\n";
41 " v%- s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
44 RedColor = DEFAULT_COLOR_RED,
45 GreenColor = DEFAULT_COLOR_GREEN,
46 BlueColor = DEFAULT_COLOR_BLUE;
48 static void QuitGifError(GifFileType *GifFile);
49 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
50 int BufferWidth, int ForeGroundIndex);
52 /******************************************************************************
53 Interpret the command line and generate the given GIF file.
54 ******************************************************************************/
55 int main(int argc, char **argv)
57 int i, j, l, ImageWidth, ImageHeight, NumOfLines, LogNumLevels,
58 ErrorCode, NumLevels, ColorMapSize = 1,
59 ForeGroundIndex = DEFAULT_FG_INDEX;
60 bool Error, ClrMapSizeFlag = false, ForeGroundFlag = false,
61 TextLineFlag = false, HelpFlag = false, ColorFlag = false;
62 char *TextLines[MAX_NUM_TEXT_LINES];
63 GifRowType RasterBuffer[GIF_FONT_HEIGHT];
64 ColorMapObject *ColorMap;
67 if ((Error = GAGetArgs(argc, argv, CtrlStr,
68 &GifNoisyPrint, &ClrMapSizeFlag, &ColorMapSize,
69 &ForeGroundFlag, &ForeGroundIndex,
70 &ColorFlag, &RedColor, &GreenColor, &BlueColor,
71 &TextLineFlag, &TextLines[0],
72 &HelpFlag)) != false) {
74 GAPrintHowTo(CtrlStr);
79 (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
80 GAPrintHowTo(CtrlStr);
84 if (ForeGroundIndex > 255 || ForeGroundIndex < 1)
85 GIF_EXIT("Foregound (-f) should be in the range 1..255, aborted.");
87 if (ColorMapSize > 8 || ColorMapSize < 1)
88 GIF_EXIT("ColorMapSize (-s) should be in the range 1..8, aborted.");
92 ImageHeight = GIF_FONT_HEIGHT;
93 ImageWidth = GIF_FONT_WIDTH * strlen(TextLines[0]);
98 while (fgets(Line, LINE_LEN - 1, stdin)) {
99 for (i = strlen(Line); i > 0 && Line[i-1] <= ' '; i--);
102 TextLines[NumOfLines++] = strdup(Line);
103 if (NumOfLines == MAX_NUM_TEXT_LINES)
104 GIF_EXIT("Input file has too many lines, aborted.");
107 GIF_EXIT("No input text, aborted.");
108 ImageHeight = GIF_FONT_HEIGHT * NumOfLines;
109 ImageWidth = GIF_FONT_WIDTH * l;
112 /* Allocate the raster buffer for GIF_FONT_HEIGHT scan lines (one text line). */
113 for (i = 0; i < GIF_FONT_HEIGHT; i++)
114 if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) *
115 ImageWidth)) == NULL)
116 GIF_EXIT("Failed to allocate memory required, aborted.");
118 /* Open stdout for the output file: */
119 if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
120 PrintGifError(ErrorCode);
124 /* Dump out screen description with given size and generated color map: */
125 for (LogNumLevels = 1, NumLevels = 2;
126 NumLevels < ForeGroundIndex;
127 LogNumLevels++, NumLevels <<= 1);
128 if (NumLevels < (1 << ColorMapSize)) {
129 NumLevels = (1 << ColorMapSize);
130 LogNumLevels = ColorMapSize;
133 if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL)
134 GIF_EXIT("Failed to allocate memory required, aborted.");
136 for (i = 0; i < NumLevels; i++)
137 ColorMap->Colors[i].Red = ColorMap->Colors[i].Green = ColorMap->Colors[i].Blue = 0;
138 ColorMap->Colors[ForeGroundIndex].Red = RedColor;
139 ColorMap->Colors[ForeGroundIndex].Green = GreenColor;
140 ColorMap->Colors[ForeGroundIndex].Blue = BlueColor;
142 if (EGifPutScreenDesc(GifFile,
143 ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap)
145 QuitGifError(GifFile);
147 /* Dump out the image descriptor: */
148 if (EGifPutImageDesc(GifFile,
149 0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR)
150 QuitGifError(GifFile);
152 GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ",
153 PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
154 GifFile->Image.Width, GifFile->Image.Height);
156 for (i = l = 0; i < NumOfLines; i++) {
157 GenRasterTextLine(RasterBuffer, TextLines[i], ImageWidth,
159 for (j = 0; j < GIF_FONT_HEIGHT; j++) {
160 if (EGifPutLine(GifFile, RasterBuffer[j], ImageWidth) == GIF_ERROR)
161 QuitGifError(GifFile);
162 GifQprintf("\b\b\b\b%-4d", l++);
166 if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
168 PrintGifError(ErrorCode);
175 /******************************************************************************
176 Generate raster bits corresponding to given text
177 ******************************************************************************/
178 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
179 int BufferWidth, int ForeGroundIndex)
181 unsigned char Byte, Mask;
182 int i, j, k, CharPosX, Len = strlen(TextLine);
184 for (i = 0; i < BufferWidth; i++)
185 for (j = 0; j < GIF_FONT_HEIGHT; j++) RasterBuffer[j][i] = 0;
187 for (i = CharPosX = 0; i < Len; i++, CharPosX += GIF_FONT_WIDTH) {
188 unsigned char c = TextLine[i];
189 for (j = 0; j < GIF_FONT_HEIGHT; j++) {
190 Byte = GifAsciiTable8x8[(unsigned short)c][j];
191 for (k = 0, Mask = 128; k < GIF_FONT_WIDTH; k++, Mask >>= 1)
193 RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
198 /******************************************************************************
199 * Close output file (if open), and exit.
200 ******************************************************************************/
201 static void QuitGifError(GifFileType *GifFile)
203 if (GifFile != NULL) {
204 PrintGifError(GifFile->Error);
205 EGifCloseFile(GifFile, NULL);