This source file includes following definitions.
- main
- QuitGifError
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdbool.h>
#include <stdlib.h>
#include "gif_lib.h"
#include "getarg.h"
#define PROGRAM_NAME "gifbg"
#define DEFAULT_WIDTH 640
#define DEFAULT_HEIGHT 350
#define DEFAULT_COLOR_RED 0
#define DEFAULT_COLOR_GREEN 0
#define DEFAULT_COLOR_BLUE 255
#define DEFAULT_MIN_INTENSITY 10
#define DEFAULT_MAX_INTENSITY 100
#define DEFAULT_NUM_LEVELS 16
#define DIR_NONE 0
#define DIR_TOP 1
#define DIR_TOP_RIGHT 2
#define DIR_RIGHT 3
#define DIR_BOT_RIGHT 4
#define DIR_BOT 5
#define DIR_BOT_LEFT 6
#define DIR_LEFT 7
#define DIR_TOP_LEFT 8
#define DEFAULT_DIR "T"
static char
*VersionStr =
PROGRAM_NAME
VERSION_COOKIE
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr =
PROGRAM_NAME
" v%- d%-Dir!s l%-#Lvls!d c%-R|G|B!d!d!d m%-MinI!d M%-MaxI!d s%-W|H!d!d h%-";
static int
MaximumIntensity = DEFAULT_MAX_INTENSITY,
MinimumIntensity = DEFAULT_MIN_INTENSITY,
NumLevels = DEFAULT_NUM_LEVELS,
ImageWidth = DEFAULT_WIDTH,
ImageHeight = DEFAULT_HEIGHT,
Direction;
static unsigned int
RedColor = DEFAULT_COLOR_RED,
GreenColor = DEFAULT_COLOR_GREEN,
BlueColor = DEFAULT_COLOR_BLUE;
static void QuitGifError(GifFileType *GifFile);
int main(int argc, char **argv)
{
unsigned int Ratio;
int i, l, LevelWidth, LogNumLevels, ErrorCode, Count = 0;
bool Error, FlipDir, DoAllMaximum = false,
DirectionFlag = false, LevelsFlag = false, ColorFlag = false,
MinFlag = false, MaxFlag = false, SizeFlag = false, HelpFlag = false;
GifPixelType Color;
char *DirectionStr = DEFAULT_DIR;
GifRowType Line;
ColorMapObject *ColorMap;
GifFileType *GifFile;
if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint,
&DirectionFlag, &DirectionStr, &LevelsFlag, &NumLevels,
&ColorFlag, &RedColor, &GreenColor, &BlueColor,
&MinFlag, &MinimumIntensity, &MaxFlag, &MaximumIntensity,
&SizeFlag, &ImageWidth, &ImageHeight,
&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 (MinimumIntensity < 0 || MinimumIntensity > 100 ||
MaximumIntensity < 0 || MaximumIntensity > 100)
GIF_EXIT("Intensities (-m or -M options) are not in [0..100] range (percent).");
Direction = DIR_NONE;
FlipDir = false;
for (i = 0; i < (int)strlen(DirectionStr); i++)
if (islower(DirectionStr[i]))
DirectionStr[i] = toupper(DirectionStr[i]);
switch(DirectionStr[0]) {
case 'T':
case 'N':
if (strlen(DirectionStr) < 2)
Direction = DIR_TOP;
else
switch(DirectionStr[1]) {
case 'R':
case 'E':
Direction = DIR_TOP_RIGHT;
break;
case 'L':
case 'W':
Direction = DIR_TOP_LEFT;
FlipDir = true;
break;
}
break;
case 'R':
case 'E':
Direction = DIR_RIGHT;
break;
case 'B':
case 'S':
if (strlen(DirectionStr) < 2) {
Direction = DIR_BOT;
FlipDir = true;
}
else
switch(DirectionStr[1]) {
case 'R':
case 'E':
Direction = DIR_BOT_RIGHT;
break;
case 'L':
case 'W':
Direction = DIR_BOT_LEFT;
FlipDir = true;
break;
}
break;
case 'L':
case 'W':
Direction = DIR_LEFT;
FlipDir = true;
break;
}
if (Direction == DIR_NONE)
GIF_EXIT("Direction requested (-d option) is wierd!");
if (FlipDir) {
switch (Direction) {
case DIR_BOT:
Direction = DIR_TOP;
break;
case DIR_BOT_LEFT:
Direction = DIR_TOP_RIGHT;
break;
case DIR_LEFT:
Direction = DIR_RIGHT;
break;
case DIR_TOP_LEFT:
Direction = DIR_BOT_RIGHT;
break;
}
}
if (MinimumIntensity == 100 && MaximumIntensity == 100 && NumLevels == 2) {
MinimumIntensity = 0;
DoAllMaximum = true;
Direction = DIR_RIGHT;
}
if (RedColor > 255 || GreenColor > 255 || BlueColor > 255)
GIF_EXIT("Colors are not in the ragne [0..255].");
for (i = 1; i < 8; i++) if (NumLevels == (1 << i)) break;
if (i == 8) GIF_EXIT("#Lvls (-l option) is not power of 2.");
LogNumLevels = i;
if ((GifFile = EGifOpenFileHandle(1, &ErrorCode)) == NULL) {
PrintGifError(ErrorCode);
exit(EXIT_FAILURE);
}
if ((ColorMap = GifMakeMapObject(NumLevels, NULL)) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
for (i = 1; i <= NumLevels; i++) {
Ratio = (MaximumIntensity * (i * (256 / NumLevels)) +
MinimumIntensity * ((NumLevels - i) * (256 / NumLevels))) /
256;
ColorMap->Colors[i-1].Red = (RedColor * Ratio) / 100;
ColorMap->Colors[i-1].Green = (GreenColor * Ratio) / 100;
ColorMap->Colors[i-1].Blue = (BlueColor * Ratio) / 100;
}
if (EGifPutScreenDesc(GifFile,
ImageWidth, ImageHeight, LogNumLevels, 0, ColorMap)
== GIF_ERROR)
QuitGifError(GifFile);
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);
if ((Line = (GifRowType) malloc(sizeof(GifPixelType) * ImageWidth * 2)) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
if (Direction == DIR_TOP) {
int LevelHeight;
LevelHeight = ImageHeight / NumLevels;
for (Color = NumLevels, i = l = 0; i < ImageHeight; i++) {
if (i == l) {
int j;
if (Color != 0) Color--;
for (j = 0; j < ImageWidth; j++)
Line[j] = (FlipDir ? NumLevels - Color - 1 : Color);
l += LevelHeight;
}
if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
QuitGifError(GifFile);
GifQprintf("\b\b\b\b%-4d", Count++);
}
}
else if (Direction == DIR_RIGHT) {
LevelWidth = ImageWidth / NumLevels;
if (DoAllMaximum) {
for (i = 0; i < ImageWidth; i++) Line[i] = 1;
}
else {
for (Color = i = 0, l = LevelWidth; i < ImageWidth; i++, l--) {
if (l == 0) {
l = LevelWidth;
if (Color < NumLevels - 1) Color++;
}
Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
}
}
for (i = 0; i < ImageHeight; i++) {
if (EGifPutLine(GifFile, Line, ImageWidth) == GIF_ERROR)
QuitGifError(GifFile);
GifQprintf("\b\b\b\b%-4d", Count++);
}
}
else {
int Accumulator, StartX, StepX;
LevelWidth = ImageWidth * 2 / NumLevels;
for (Color = i = 0, l = LevelWidth; i < ImageWidth * 2; i++, l--) {
if (l == 0) {
l = LevelWidth;
if (Color < NumLevels - 1) Color++;
}
Line[i] = (FlipDir ? NumLevels - Color - 1 : Color);
}
Accumulator = 0;
switch(Direction) {
case DIR_TOP_RIGHT:
StartX = ImageWidth;
StepX = -1;
break;
case DIR_BOT_RIGHT:
default:
StartX = 0;
StepX = 1;
break;
}
for (i = 0; i < ImageHeight; i++) {
if (EGifPutLine(GifFile, &Line[StartX], ImageWidth) == GIF_ERROR)
QuitGifError(GifFile);
GifQprintf("\b\b\b\b%-4d", Count++);
if ((Accumulator += ImageWidth) > ImageHeight) {
while (Accumulator > ImageHeight) {
Accumulator -= ImageHeight;
StartX += StepX;
}
if (Direction < 0) Direction = 0;
if (Direction > ImageWidth) Direction = ImageWidth;
}
}
}
if (EGifCloseFile(GifFile, &ErrorCode) == GIF_ERROR)
{
PrintGifError(ErrorCode);
if (GifFile != NULL) {
EGifCloseFile(GifFile, NULL);
}
exit(EXIT_FAILURE);
}
return 0;
}
static void QuitGifError(GifFileType *GifFile)
{
if (GifFile != NULL) {
PrintGifError(GifFile->Error);
EGifCloseFile(GifFile, NULL);
}
exit(EXIT_FAILURE);
}