[go: up one dir, main page]

Menu

[efba94]: / util / text2gif.c  Maximize  Restore  History

Download this file

212 lines (180 with data), 7.1 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*****************************************************************************
* "Gif-Lib" - Yet another gif library.
*
* Written by: Gershon Elber Ver 0.1, Jul. 1989
******************************************************************************
* Program to generate GIF image page from a given text by drawing the chars
* using 8 by 8 fixed font.
* Options:
* -v : verbose mode.
* -s ColorMapSize : in bits, i.e. 6 bits for 64 colors.
* -f ForeGroundIndex : by default foreground is 1. Must be in range 0..255.
* -c R G B : set the foregound color values. By default it is white.
* -t "Text" : Make a one-line GIF (8 pixels high) from the given Text.
* -h : on-line help.
******************************************************************************
* History:
* 3 May 90 - Version 1.0 by Gershon Elber.
*****************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include "gif_lib.h"
#include "getarg.h"
#define PROGRAM_NAME "text2gif"
#define MAX_NUM_TEXT_LINES 100 /* Maximum number of lines in file. */
#define LINE_LEN 256 /* Maximum length of one text line. */
#define DEFAULT_FG_INDEX 1 /* Text foreground index. */
#define DEFAULT_COLOR_RED 255 /* Text foreground color. */
#define DEFAULT_COLOR_GREEN 255
#define DEFAULT_COLOR_BLUE 255
static char
*VersionStr =
PROGRAM_NAME
VERSION_COOKIE
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr =
PROGRAM_NAME
" v%- s%-ClrMapSize!d f%-FGClr!d c%-R|G|B!d!d!d t%-\"Text\"!s h%-";
static unsigned int
RedColor = DEFAULT_COLOR_RED,
GreenColor = DEFAULT_COLOR_GREEN,
BlueColor = DEFAULT_COLOR_BLUE;
static void QuitGifError(GifFileType *GifFile);
static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
int BufferWidth, int ForeGroundIndex);
/******************************************************************************
* Interpret the command line and generate the given GIF file.
******************************************************************************/
int main(int argc, char **argv)
{
int i, j, l, ImageWidth, ImageHeight, NumOfLines, LogNumLevels,
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)) == NULL)
QuitGifError(GifFile);
/* 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 = MakeMapObject(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;
}
/******************************************************************************
* Close output file (if open), and exit.
******************************************************************************/
static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
int BufferWidth, int ForeGroundIndex)
{
unsigned char c;
unsigned char Byte, Mask;
int i, j, k, CharPosX, Len = strlen(TextLine);
for (i = 0; i < BufferWidth; i++)
for (j = 0; j < GIF_FONT_HEIGHT; j++) RasterBuffer[j][i] = 0;
for (i = CharPosX = 0; i < Len; i++, CharPosX += GIF_FONT_WIDTH) {
c = TextLine[i];
for (j = 0; j < GIF_FONT_HEIGHT; j++) {
Byte = AsciiTable[(unsigned short)c][j];
for (k = 0, Mask = 128; k < GIF_FONT_WIDTH; k++, Mask >>= 1)
if (Byte & Mask)
RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
}
}
}
/******************************************************************************
* Close output file (if open), and exit.
******************************************************************************/
static void QuitGifError(GifFileType *GifFile)
{
PrintGifError();
if (GifFile != NULL) EGifCloseFile(GifFile);
exit(EXIT_FAILURE);
}