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 212 213 214 215 216 217 218 219 220 221 222
|
/*****************************************************************************
* "Gif-Lib" - Yet another gif library. *
* *
* Written by: Gershon Elber Ver 0.1, Jul. 1989 *
******************************************************************************
* Program to attempt and fix broken GIF images. Currently fix the following: *
* 1. EOF terminates before end of image size (adds black in the end). *
* Options: *
* -q : quiet printing mode. *
* -h : on-line help *
******************************************************************************
* History: *
* 5 May 91 - Version 1.0 by Gershon Elber. *
*****************************************************************************/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef __MSDOS__
#include <stdlib.h>
#include <alloc.h>
#endif /* __MSDOS__ */
#ifndef __MSDOS__
#include <stdlib.h>
#endif
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include "gif_lib.h"
#include "getarg.h"
#define PROGRAM_NAME "GifFix"
#ifdef __MSDOS__
extern unsigned int
_stklen = 16384; /* Increase default stack size. */
#endif /* __MSDOS__ */
#ifdef SYSV
static char *VersionStr =
"Gif toolkit module,\t\tGershon Elber\n\
(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr = "GifFix q%- h%- GifFile!*s";
#else
static char
*VersionStr =
PROGRAM_NAME
GIF_LIB_VERSION
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber.\n";
static char
*CtrlStr =
PROGRAM_NAME
" q%- h%- GifFile!*s";
#endif /* SYSV */
/* Make some variables global, so we could access them faster: */
static int
ImageNum = 0;
static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
/******************************************************************************
* Interpret the command line and scan the given GIF file. *
******************************************************************************/
int main(int argc, char **argv)
{
int i, j, Error, NumFiles, ExtCode, Row, Col, Width, Height,
DarkestColor = 0, ColorIntens = 10000, HelpFlag = FALSE;
GifRecordType RecordType;
GifByteType *Extension;
char **FileName = NULL;
GifRowType LineBuffer;
ColorMapObject *ColorMap;
GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuietPrint, &HelpFlag,
&NumFiles, &FileName)) != FALSE ||
(NumFiles > 1 && !HelpFlag)) {
if (Error)
GAPrintErrMsg(Error);
else if (NumFiles > 1)
GIF_MESSAGE("Error in command line parsing - one GIF file please.");
GAPrintHowTo(CtrlStr);
exit(EXIT_FAILURE);
}
if (HelpFlag) {
fprintf(stderr, "%s", VersionStr);
GAPrintHowTo(CtrlStr);
exit(EXIT_SUCCESS);
}
if (NumFiles == 1) {
if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
QuitGifError(GifFileIn, GifFileOut);
}
else {
/* Use the stdin instead: */
if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
QuitGifError(GifFileIn, GifFileOut);
}
/* Open stdout for the output file: */
if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
QuitGifError(GifFileIn, GifFileOut);
/* Dump out exactly same screen information: */
if (EGifPutScreenDesc(GifFileOut,
GifFileIn->SWidth, GifFileIn->SHeight,
GifFileIn->SColorResolution, GifFileIn->SBackGroundColor,
GifFileIn->SColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if ((LineBuffer = (GifRowType) malloc(GifFileIn->SWidth)) == NULL)
GIF_EXIT("Failed to allocate memory required, aborted.");
/* Scan the content of the GIF file and load the image(s) in: */
do {
if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
switch (RecordType) {
case IMAGE_DESC_RECORD_TYPE:
if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (GifFileIn->Image.Interlace)
GIF_EXIT("Cannt fix interlaced images.");
Row = GifFileIn->Image.Top; /* Image Position relative to Screen. */
Col = GifFileIn->Image.Left;
Width = GifFileIn->Image.Width;
Height = GifFileIn->Image.Height;
GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ",
PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
if (Width > GifFileIn->SWidth)
GIF_EXIT("Image is wider than total");
/* Put the image descriptor to out file: */
if (EGifPutImageDesc(GifFileOut, Col, Row, Width, Height,
FALSE, GifFileIn->Image.ColorMap) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
/* Find the darkest color in color map to use as a filler. */
ColorMap = (GifFileIn->Image.ColorMap ? GifFileIn->Image.ColorMap :
GifFileIn->SColorMap);
for (i = 0; i < ColorMap->ColorCount; i++) {
j = ((int) ColorMap->Colors[i].Red) * 30 +
((int) ColorMap->Colors[i].Green) * 59 +
((int) ColorMap->Colors[i].Blue) * 11;
if (j < ColorIntens) {
ColorIntens = j;
DarkestColor = i;
}
}
/* Load the image, and dump it. */
for (i = 0; i < Height; i++) {
GifQprintf("\b\b\b\b%-4d", i);
if (DGifGetLine(GifFileIn, LineBuffer, Width)
== GIF_ERROR) break;
if (EGifPutLine(GifFileOut, LineBuffer, Width)
== GIF_ERROR) QuitGifError(GifFileIn, GifFileOut);
}
if (i < Height) {
fprintf(stderr, "\nFollowing error occured (and ignored):");
PrintGifError();
/* Fill in with the darkest color in color map. */
for (j = 0; j < Width; j++)
LineBuffer[j] = DarkestColor;
for (; i < Height; i++)
if (EGifPutLine(GifFileOut, LineBuffer, Width)
== GIF_ERROR) QuitGifError(GifFileIn, GifFileOut);
}
break;
case EXTENSION_RECORD_TYPE:
/* Skip any extension blocks in file: */
if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
/* No support to more than one extension blocks, so discard: */
while (Extension != NULL) {
if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
}
break;
case TERMINATE_RECORD_TYPE:
break;
default: /* Should be traps by DGifGetRecordType. */
break;
}
}
while (RecordType != TERMINATE_RECORD_TYPE);
if (DGifCloseFile(GifFileIn) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
if (EGifCloseFile(GifFileOut) == GIF_ERROR)
QuitGifError(GifFileIn, GifFileOut);
return 0;
}
/******************************************************************************
* Close both input and output file (if open), and exit. *
******************************************************************************/
static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
{
fprintf(stderr, "\nFollowing unrecoverable error occured:");
PrintGifError();
if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
exit(EXIT_FAILURE);
}
|