Bump to 5.2.1
[platform/upstream/giflib.git] / gifecho.c
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 __attribute__((__section__(".tizen.build-id")))
31 static char
32     VersionStr[] =
33         PROGRAM_NAME
34         VERSION_COOKIE
35         "       Gershon Elber,  "
36         __DATE__ ",   " __TIME__ "\n"
37         "(C) Copyright 1989 Gershon Elber.\n";
38 static char
39     *CtrlStr =
40         PROGRAM_NAME
41         " v%- s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
42
43 static unsigned int
44     RedColor = DEFAULT_COLOR_RED,
45     GreenColor = DEFAULT_COLOR_GREEN,
46     BlueColor = DEFAULT_COLOR_BLUE;
47
48 static void QuitGifError(GifFileType *GifFile);
49 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
50                                         int BufferWidth, int ForeGroundIndex);
51
52 /******************************************************************************
53  Interpret the command line and generate the given GIF file.
54 ******************************************************************************/
55 int main(int argc, char **argv)
56 {
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;
65     GifFileType *GifFile;
66
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) {
73         GAPrintErrMsg(Error);
74         GAPrintHowTo(CtrlStr);
75         exit(EXIT_FAILURE);
76     }
77
78     if (HelpFlag) {
79         (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
80         GAPrintHowTo(CtrlStr);
81         exit(EXIT_SUCCESS);
82     }
83
84     if (ForeGroundIndex > 255 || ForeGroundIndex < 1)
85         GIF_EXIT("Foregound (-f) should be in the range 1..255, aborted.");
86
87     if (ColorMapSize > 8 || ColorMapSize < 1)
88         GIF_EXIT("ColorMapSize (-s) should be in the range 1..8, aborted.");
89
90     if (TextLineFlag) {
91         NumOfLines = 1;
92         ImageHeight = GIF_FONT_HEIGHT;
93         ImageWidth = GIF_FONT_WIDTH * strlen(TextLines[0]);
94     }
95     else {
96         char Line[LINE_LEN];
97         NumOfLines = l = 0;
98         while (fgets(Line, LINE_LEN - 1, stdin)) {
99             for (i = strlen(Line); i > 0 && Line[i-1] <= ' '; i--);
100             Line[i] = 0;
101             if (l < i) l = i;
102             TextLines[NumOfLines++] = strdup(Line);
103             if (NumOfLines == MAX_NUM_TEXT_LINES)
104                 GIF_EXIT("Input file has too many lines, aborted.");
105         }
106         if (NumOfLines == 0)
107             GIF_EXIT("No input text, aborted.");
108         ImageHeight = GIF_FONT_HEIGHT * NumOfLines;
109         ImageWidth = GIF_FONT_WIDTH * l;
110     }
111
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.");
117
118     /* Open stdout for the output file: */
119     if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
120         PrintGifError(ErrorCode);
121         exit(EXIT_FAILURE);
122     }
123
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;
131     }
132
133     if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL)
134         GIF_EXIT("Failed to allocate memory required, aborted.");
135
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;
141
142     if (EGifPutScreenDesc(GifFile,
143         ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap)
144         == GIF_ERROR)
145         QuitGifError(GifFile);
146
147     /* Dump out the image descriptor: */
148     if (EGifPutImageDesc(GifFile,
149         0, 0, ImageWidth, ImageHeight, false, NULL) == GIF_ERROR)
150         QuitGifError(GifFile);
151
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);
155
156     for (i = l = 0; i < NumOfLines; i++) {
157         GenRasterTextLine(RasterBuffer, TextLines[i], ImageWidth,
158                                                         ForeGroundIndex);
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++);
163         }
164     }
165
166     if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
167     {
168         PrintGifError(ErrorCode);
169         exit(EXIT_FAILURE);
170     }
171
172     return 0;
173 }
174
175 /******************************************************************************
176  Generate raster bits corresponding to given text
177 ******************************************************************************/
178 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
179                                         int BufferWidth, int ForeGroundIndex)
180 {
181     unsigned char Byte, Mask;
182     int i, j, k, CharPosX, Len = strlen(TextLine);
183
184     for (i = 0; i < BufferWidth; i++)
185         for (j = 0; j < GIF_FONT_HEIGHT; j++) RasterBuffer[j][i] = 0;
186
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)
192                 if (Byte & Mask)
193                     RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
194         }
195     }
196 }
197
198 /******************************************************************************
199 * Close output file (if open), and exit.
200 ******************************************************************************/
201 static void QuitGifError(GifFileType *GifFile)
202 {
203     if (GifFile != NULL) {
204         PrintGifError(GifFile->Error);
205         EGifCloseFile(GifFile, NULL);
206     }
207     exit(EXIT_FAILURE);
208 }
209
210 /* end */