Imported Upstream version 5.1.9 into tizen
[platform/upstream/giflib.git] / gifcolor.c
1 /*****************************************************************************
2
3 gifcolor - generate color test-pattern GIFs
4
5 *****************************************************************************/
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <string.h>
11 #include <stdbool.h>
12
13 #include "gif_lib.h"
14 #include "getarg.h"
15
16 #define PROGRAM_NAME    "gifcolor"
17
18 #define LINE_LEN                40
19 #define IMAGEWIDTH              LINE_LEN*GIF_FONT_WIDTH
20
21 __attribute__((__section__(".tizen.build-id")))
22 static char
23     VersionStr[] =
24         PROGRAM_NAME
25         VERSION_COOKIE
26         "       Gershon Elber,  "
27         __DATE__ ",   " __TIME__ "\n"
28         "(C) Copyright 1989 Gershon Elber.\n";
29 static char
30     *CtrlStr = PROGRAM_NAME " v%- b%-Background!d h%-";
31
32 static int BackGround = 0;
33 static void QuitGifError(GifFileType *GifFile);
34 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
35                                         int BufferWidth, int ForeGroundIndex);
36
37 /******************************************************************************
38  Interpret the command line and generate the given GIF file.
39 ******************************************************************************/
40 int main(int argc, char **argv)
41 {
42     int i, j, l, GifNoisyPrint, ColorMapSize, ErrorCode;
43     bool Error, BackGroundFlag = false, HelpFlag = false;
44     char Line[LINE_LEN];
45     GifRowType RasterBuffer[GIF_FONT_HEIGHT];
46     ColorMapObject *ColorMap;
47     GifFileType *GifFile;
48     GifColorType        ScratchMap[256];
49     int red, green, blue;
50
51     if ((Error = GAGetArgs(argc, argv, CtrlStr,
52                            &GifNoisyPrint,
53                            &BackGroundFlag, &BackGround,
54                            &HelpFlag)) != false) {
55         GAPrintErrMsg(Error);
56         GAPrintHowTo(CtrlStr);
57         exit(EXIT_FAILURE);
58     }
59
60     if (HelpFlag) {
61         (void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
62         GAPrintHowTo(CtrlStr);
63         exit(EXIT_SUCCESS);
64     }
65
66     /* Allocate the raster buffer for GIF_FONT_HEIGHT scan lines. */
67     for (i = 0; i < GIF_FONT_HEIGHT; i++)
68     {
69         if ((RasterBuffer[i] = (GifRowType) malloc(sizeof(GifPixelType) *
70                                                         IMAGEWIDTH)) == NULL)
71             GIF_EXIT("Failed to allocate memory required, aborted.");
72     }
73
74     /* Open stdout for the output file: */
75     if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
76         PrintGifError(ErrorCode);
77         exit(EXIT_FAILURE);
78     }
79
80     /* Read the color map in ColorFile into this color map: */
81     ColorMapSize = 0;
82     while (fscanf(stdin,
83                   "%*3d %3d %3d %3d\n",
84                   &red, &green, &blue) == 3) {
85             ScratchMap[ColorMapSize].Red = red;
86             ScratchMap[ColorMapSize].Green = green;
87             ScratchMap[ColorMapSize].Blue = blue;
88             ColorMapSize++;
89         }
90
91     if ((ColorMap = GifMakeMapObject(1 << GifBitSize(ColorMapSize), ScratchMap)) == NULL)
92         GIF_EXIT("Failed to allocate memory required, aborted.");
93
94     if (EGifPutScreenDesc(GifFile,
95                           IMAGEWIDTH, ColorMapSize * GIF_FONT_HEIGHT,
96                           GifBitSize(ColorMapSize),
97                           BackGround, ColorMap) == GIF_ERROR)
98         QuitGifError(GifFile);
99
100     /* Dump out the image descriptor: */
101     if (EGifPutImageDesc(GifFile,
102         0, 0, IMAGEWIDTH, ColorMapSize * GIF_FONT_HEIGHT, false, NULL) == GIF_ERROR)
103         QuitGifError(GifFile);
104
105     GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]:     ",
106                     PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
107                     GifFile->Image.Width, GifFile->Image.Height);
108
109     for (i = l = 0; i < ColorMap->ColorCount; i++) {
110         (void)snprintf(Line, sizeof(Line),
111                        "Color %-3d: [%-3d, %-3d, %-3d] ", i,
112                        ColorMap->Colors[i].Red,
113                        ColorMap->Colors[i].Green,
114                        ColorMap->Colors[i].Blue);
115         GenRasterTextLine(RasterBuffer, Line, IMAGEWIDTH, i);
116         for (j = 0; j < GIF_FONT_HEIGHT; j++) {
117             if (EGifPutLine(GifFile, RasterBuffer[j], IMAGEWIDTH) == GIF_ERROR)
118                 QuitGifError(GifFile);
119             GifQprintf("\b\b\b\b%-4d", l++);
120         }
121     }
122
123     if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
124     {
125         PrintGifError(ErrorCode);
126         exit(EXIT_FAILURE);
127     }
128
129     return 0;
130 }
131
132 /******************************************************************************
133  Close output file (if open), and exit.
134 ******************************************************************************/
135 static void GenRasterTextLine(GifRowType *RasterBuffer, char *TextLine,
136                                         int BufferWidth, int ForeGroundIndex)
137 {
138     unsigned char Byte, Mask;
139     int i, j, k, CharPosX, Len = strlen(TextLine);
140
141     for (i = 0; i < BufferWidth; i++)
142         for (j = 0; j < GIF_FONT_HEIGHT; j++) RasterBuffer[j][i] = BackGround;
143
144     for (i = CharPosX = 0; i < Len; i++, CharPosX += GIF_FONT_WIDTH) {
145         unsigned char c = TextLine[i];
146         for (j = 0; j < GIF_FONT_HEIGHT; j++) {
147             Byte = GifAsciiTable8x8[(unsigned short)c][j];
148             for (k = 0, Mask = 128; k < GIF_FONT_WIDTH; k++, Mask >>= 1)
149                 if (Byte & Mask)
150                     RasterBuffer[j][CharPosX + k] = ForeGroundIndex;
151         }
152     }
153 }
154
155 /******************************************************************************
156  Close output file (if open), and exit.
157 ******************************************************************************/
158 static void QuitGifError(GifFileType *GifFile)
159 {
160     if (GifFile != NULL) {
161         PrintGifError(GifFile->Error);
162         EGifCloseFile(GifFile, NULL);
163     }
164     exit(EXIT_FAILURE);
165 }