1 /*****************************************************************************
2 
3 gifecho - generate a GIF from ASCII text
4 
5 SPDX-License-Identifier: MIT
6 
7 *****************************************************************************/
8 
9 #include <stdlib.h>
10 #include <stdio.h>
11 #include <ctype.h>
12 #include <string.h>
13 #include <stdbool.h>
14 
15 #include "gif_lib.h"
16 #include "getarg.h"
17 
18 #define PROGRAM_NAME	"gifecho"
19 
20 #define MAX_NUM_TEXT_LINES	100	 /* Maximum number of lines in file. */
21 
22 #define LINE_LEN		256	 /* Maximum length of one text line. */
23 
24 #define DEFAULT_FG_INDEX	1		   /* Text foreground index. */
25 
26 #define DEFAULT_COLOR_RED	255		   /* Text foreground color. */
27 #define DEFAULT_COLOR_GREEN	255
28 #define DEFAULT_COLOR_BLUE	255
29 
30 static char
31     *VersionStr =
32 	PROGRAM_NAME
33 	VERSION_COOKIE
34 	"	Gershon Elber,	"
35 	__DATE__ ",   " __TIME__ "\n"
36 	"(C) Copyright 1989 Gershon Elber.\n";
37 static char
38     *CtrlStr =
39 	PROGRAM_NAME
40 	" v%- s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
41 
42 static unsigned int
43     RedColor = DEFAULT_COLOR_RED,
44     GreenColor = DEFAULT_COLOR_GREEN,
45     BlueColor = DEFAULT_COLOR_BLUE;
46 
47 static void QuitGifError(GifFileType *GifFile);
48 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
49 					int BufferWidth, int ForeGroundIndex);
50 
51 /******************************************************************************
52  Interpret the command line and generate the given GIF file.
53 ******************************************************************************/
main(int argc,char ** argv)54 int main(int argc, char **argv)
55 {
56     int	i, j, l, ImageWidth, ImageHeight, NumOfLines, LogNumLevels,
57 	ErrorCode, NumLevels, ColorMapSize = 1,
58 	ForeGroundIndex = DEFAULT_FG_INDEX;
59     bool Error, ClrMapSizeFlag = false, ForeGroundFlag = false,
60 	TextLineFlag = false, HelpFlag = false, ColorFlag = false;
61     char *TextLines[MAX_NUM_TEXT_LINES];
62     GifRowType RasterBuffer[GIF_FONT_HEIGHT];
63     ColorMapObject *ColorMap;
64     GifFileType *GifFile;
65 
66     if ((Error = GAGetArgs(argc, argv, CtrlStr,
67 		&GifNoisyPrint, &ClrMapSizeFlag, &ColorMapSize,
68 		&ForeGroundFlag, &ForeGroundIndex,
69 		&ColorFlag, &RedColor, &GreenColor, &BlueColor,
70 		&TextLineFlag, &TextLines[0],
71 		&HelpFlag)) != false) {
72 	GAPrintErrMsg(Error);
73 	GAPrintHowTo(CtrlStr);
74 	exit(EXIT_FAILURE);
75     }
76 
77     if (HelpFlag) {
78 	(void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
79 	GAPrintHowTo(CtrlStr);
80 	exit(EXIT_SUCCESS);
81     }
82 
83     if (ForeGroundIndex > 255 || ForeGroundIndex < 1)
84 	GIF_EXIT("Foregound (-f) should be in the range 1..255, aborted.");
85 
86     if (ColorMapSize > 8 || ColorMapSize < 1)
87 	GIF_EXIT("ColorMapSize (-s) should be in the range 1..8, aborted.");
88 
89     if (TextLineFlag) {
90 	NumOfLines = 1;
91 	ImageHeight = GIF_FONT_HEIGHT;
92 	ImageWidth = GIF_FONT_WIDTH * strlen(TextLines[0]);
93     }
94     else {
95 	char Line[LINE_LEN];
96 	NumOfLines = l = 0;
97 	while (fgets(Line, LINE_LEN - 1, stdin)) {
98 	    for (i = strlen(Line); i > 0 && Line[i-1] <= ' '; i--);
99 	    Line[i] = 0;
100 	    if (l < i) l = i;
101 	    TextLines[NumOfLines++] = strdup(Line);
102 	    if (NumOfLines == MAX_NUM_TEXT_LINES)
103 		GIF_EXIT("Input file has too many lines, aborted.");
104 	}
105 	if (NumOfLines == 0)
106 	    GIF_EXIT("No input text, aborted.");
107 	ImageHeight = GIF_FONT_HEIGHT * NumOfLines;
108 	ImageWidth = GIF_FONT_WIDTH * l;
109     }
110 
111     /* Allocate the raster buffer for GIF_FONT_HEIGHT scan lines (one text line). */
112     for (i = 0; i < GIF_FONT_HEIGHT; i++)
113 	if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) *
114 							ImageWidth)) == NULL)
115 	    GIF_EXIT("Failed to allocate memory required, aborted.");
116 
117     /* Open stdout for the output file: */
118     if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
119 	PrintGifError(ErrorCode);
120 	exit(EXIT_FAILURE);
121     }
122 
123     /* Dump out screen description with given size and generated color map: */
124     for (LogNumLevels = 1, NumLevels = 2;
125 	 NumLevels < ForeGroundIndex;
126 	 LogNumLevels++, NumLevels <<= 1);
127     if (NumLevels < (1 << ColorMapSize)) {
128     	NumLevels = (1 << ColorMapSize);
129 	LogNumLevels = ColorMapSize;
130     }
131 
132     if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL)
133 	GIF_EXIT("Failed to allocate memory required, aborted.");
134 
135     for (i = 0; i < NumLevels; i++)
136 	ColorMap->Colors[i].Red = ColorMap->Colors[i].Green = ColorMap->Colors[i].Blue = 0;
137     ColorMap->Colors[ForeGroundIndex].Red = RedColor;
138     ColorMap->Colors[ForeGroundIndex].Green = GreenColor;
139     ColorMap->Colors[ForeGroundIndex].Blue = BlueColor;
140 
141     if (EGifPutScreenDesc(GifFile,
142 	ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap)
143 	== GIF_ERROR)
144 	QuitGifError(GifFile);
145 
146     /* Dump out the image descriptor: */
147     if (EGifPutImageDesc(GifFile,
148 	0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR)
149 	QuitGifError(GifFile);
150 
151     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
152 		    PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
153 		    GifFile->Image.Width, GifFile->Image.Height);
154 
155     for (i = l = 0; i < NumOfLines; i++) {
156 	GenRasterTextLine(RasterBuffer, TextLines[i], ImageWidth,
157 							ForeGroundIndex);
158 	for (j = 0; j < GIF_FONT_HEIGHT; j++) {
159 	    if (EGifPutLine(GifFile, RasterBuffer[j], ImageWidth) == GIF_ERROR)
160 		QuitGifError(GifFile);
161 	    GifQprintf("\b\b\b\b%-4d", l++);
162 	}
163     }
164 
165     if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
166     {
167 	PrintGifError(ErrorCode);
168 	exit(EXIT_FAILURE);
169     }
170 
171     return 0;
172 }
173 
174 /******************************************************************************
175  Generate raster bits corresponding to given text
176 ******************************************************************************/
GenRasterTextLine(GifRowType * RasterBuffer,char * TextLine,int BufferWidth,int ForeGroundIndex)177 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
178 					int BufferWidth, int ForeGroundIndex)
179 {
180     unsigned char Byte, Mask;
181     int i, j, k, CharPosX, Len = strlen(TextLine);
182 
183     for (i = 0; i < BufferWidth; i++)
184         for (j = 0; j < GIF_FONT_HEIGHT; j++) RasterBuffer[j][i] = 0;
185 
186     for (i = CharPosX = 0; i < Len; i++, CharPosX += GIF_FONT_WIDTH) {
187 	unsigned char c = TextLine[i];
188 	for (j = 0; j < GIF_FONT_HEIGHT; j++) {
189 	    Byte = GifAsciiTable8x8[(unsigned short)c][j];
190 	    for (k = 0, Mask = 128; k < GIF_FONT_WIDTH; k++, Mask >>= 1)
191 		if (Byte & Mask)
192 		    RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
193 	}
194     }
195 }
196 
197 /******************************************************************************
198 * Close output file (if open), and exit.
199 ******************************************************************************/
QuitGifError(GifFileType * GifFile)200 static void QuitGifError(GifFileType *GifFile)
201 {
202     if (GifFile != NULL) {
203 	PrintGifError(GifFile->Error);
204 	EGifCloseFile(GifFile, NULL);
205     }
206     exit(EXIT_FAILURE);
207 }
208 
209 /* end */
210