Git init
[external/giflib.git] / util / gifcolor.c
1 /*****************************************************************************
2 *   "Gif-Lib" - Yet another gif library.                                     *
3 *                                                                            *
4 * Written by:  Gershon Elber                            Ver 0.1, Jul. 1989   *
5 ******************************************************************************
6 * Program to generate a test pattern from a given color map                  *
7 ******************************************************************************
8 * Options:                                                                   *
9 * -q : quiet printing mode.                                                  *
10 * -b : set background color.
11 * -h : on-line help.                                                         *
12 ******************************************************************************
13 * History:                                                                   *
14 * 21 Sep 92 - Version 1.0 by Eric S. Raymond.                                *
15 *****************************************************************************/
16
17 #ifdef HAVE_CONFIG_H
18 #include <config.h>
19 #endif
20
21 #ifdef __MSDOS__
22 #include <stdlib.h>
23 #include <alloc.h>
24 #endif /* __MSDOS__ */
25
26 #ifndef __MSDOS__
27 #include <stdlib.h>
28 #endif
29 #include <stdio.h>
30 #include <ctype.h>
31 #include <string.h>
32 #include "gif_lib.h"
33 #include "getarg.h"
34
35 #define PROGRAM_NAME    "GifColor"
36
37 #define LINE_LEN                40
38 #define IMAGEWIDTH              LINE_LEN*GIF_FONT_WIDTH
39
40 #ifdef __MSDOS__
41 extern unsigned int
42     _stklen = 16384;                         /* Increase default stack size. */
43 #endif /* __MSDOS__ */
44
45 #ifdef SYSV
46 static char *VersionStr =
47         "Gif library module     \t\tGershon Elber\n\
48         (C) Copyright 1989 Gershon Elber.\n";
49 static char
50     *CtrlStr = "GifColor q%- b%-Background!d h%-";
51 #else
52 static char
53     *VersionStr =
54         PROGRAM_NAME
55         GIF_LIB_VERSION
56         "       Gershon Elber,  "
57         __DATE__ ",   " __TIME__ "\n"
58         "(C) Copyright 1989 Gershon Elber.\n";
59 static char
60     *CtrlStr = PROGRAM_NAME " q%- b%-Background!d h%-";
61 #endif /* SYSV */
62
63 static int BackGround = 0;
64 static void QuitGifError(GifFileType *GifFile);
65 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
66                                         int BufferWidth, int ForeGroundIndex);
67
68 /******************************************************************************
69 * Interpret the command line and generate the given GIF file.                 *
70 ******************************************************************************/
71 int main(int argc, char **argv)
72 {
73     int i, j, l, Error, GifQuietPrint, ColorMapSize,
74         BackGroundFlag = FALSE, HelpFlag = FALSE;
75     char Line[LINE_LEN];
76     GifRowType RasterBuffer[GIF_FONT_HEIGHT];
77     ColorMapObject *ColorMap;
78     GifFileType *GifFile;
79     GifColorType        ScratchMap[256];
80     int red, green, blue;
81
82     if ((Error = GAGetArgs(argc, argv, CtrlStr,
83                            &GifQuietPrint,
84                            &BackGroundFlag, &BackGround,
85                            &HelpFlag)) != FALSE) {
86         GAPrintErrMsg(Error);
87         GAPrintHowTo(CtrlStr);
88         exit(EXIT_FAILURE);
89     }
90
91     if (HelpFlag) {
92         fprintf(stderr, VersionStr);
93         GAPrintHowTo(CtrlStr);
94         exit(EXIT_SUCCESS);
95     }
96
97     /* Allocate the raster buffer for GIF_FONT_HEIGHT scan lines. */
98     for (i = 0; i < GIF_FONT_HEIGHT; i++)
99     {
100         if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) *
101                                                         IMAGEWIDTH)) == NULL)
102             GIF_EXIT("Failed to allocate memory required, aborted.");
103     }
104
105     /* Open stdout for the output file: */
106     if ((GifFile = EGifOpenFileHandle(1)) == NULL)
107         QuitGifError(GifFile);
108
109     /* Read the color map in ColorFile into this color map: */
110     ColorMapSize = 0;
111     while (fscanf(stdin,
112                   "%*3d %3d %3d %3d\n",
113                   &red, &green, &blue) == 3) {
114             ScratchMap[ColorMapSize].Red = red;
115             ScratchMap[ColorMapSize].Green = green;
116             ScratchMap[ColorMapSize].Blue = blue;
117             ColorMapSize++;
118         }
119
120     if ((ColorMap = MakeMapObject(1 << BitSize(ColorMapSize), ScratchMap)) == NULL)
121         GIF_EXIT("Failed to allocate memory required, aborted.");
122
123     if (EGifPutScreenDesc(GifFile,
124                           IMAGEWIDTH, ColorMapSize * GIF_FONT_HEIGHT,
125                           BitSize(ColorMapSize),
126                           BackGround, ColorMap) == GIF_ERROR)
127         QuitGifError(GifFile);
128
129     /* Dump out the image descriptor: */
130     if (EGifPutImageDesc(GifFile,
131         0, 0, IMAGEWIDTH, ColorMapSize * GIF_FONT_HEIGHT, FALSE, NULL) == GIF_ERROR)
132         QuitGifError(GifFile);
133
134     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
135                     PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
136                     GifFile->Image.Width, GifFile->Image.Height);
137
138     for (i = l = 0; i < ColorMap->ColorCount; i++) {
139         (void)sprintf(Line, "Color %-3d: [%-3d, %-3d, %-3d] ", i,
140                       ColorMap->Colors[i].Red,
141                       ColorMap->Colors[i].Green,
142                       ColorMap->Colors[i].Blue);
143         GenRasterTextLine(RasterBuffer, Line, IMAGEWIDTH, i);
144         for (j = 0; j < GIF_FONT_HEIGHT; j++) {
145             if (EGifPutLine(GifFile, RasterBuffer[j], IMAGEWIDTH) == GIF_ERROR)
146                 QuitGifError(GifFile);
147             GifQprintf("\b\b\b\b%-4d", l++);
148         }
149     }
150
151     if (EGifCloseFile(GifFile) == GIF_ERROR)
152         QuitGifError(GifFile);
153
154     return 0;
155 }
156
157 /******************************************************************************
158 * Close output file (if open), and exit.                                      *
159 ******************************************************************************/
160 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
161                                         int BufferWidth, int ForeGroundIndex)
162 {
163     unsigned char c;
164     unsigned char Byte, Mask;
165     int i, j, k, CharPosX, Len = strlen(TextLine);
166
167     for (i = 0; i < BufferWidth; i++)
168         for (j = 0; j < GIF_FONT_HEIGHT; j++) RasterBuffer[j][i] = BackGround;
169
170     for (i = CharPosX = 0; i < Len; i++, CharPosX += GIF_FONT_WIDTH) {
171         c = TextLine[i];
172         for (j = 0; j < GIF_FONT_HEIGHT; j++) {
173             Byte = AsciiTable[(unsigned short)c][j];
174             for (k = 0, Mask = 128; k < GIF_FONT_WIDTH; k++, Mask >>= 1)
175                 if (Byte & Mask)
176                     RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
177         }
178     }
179 }
180
181 /******************************************************************************
182 * Close output file (if open), and exit.                                      *
183 ******************************************************************************/
184 static void QuitGifError(GifFileType *GifFile)
185 {
186     PrintGifError();
187     if (GifFile != NULL) EGifCloseFile(GifFile);
188     exit(EXIT_FAILURE);
189 }