|
Packit |
f1137c |
/*****************************************************************************
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
gif2rgb - convert GIF to 24-bit RGB pixel triples or vice-versa
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
*****************************************************************************/
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/***************************************************************************
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
Toshio Kuratomi had written this in a comment about the rgb2gif code:
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
Besides fixing bugs, what's really needed is for someone to work out how to
|
|
Packit |
f1137c |
calculate a colormap for writing GIFs from rgb sources. Right now, an rgb
|
|
Packit |
f1137c |
source that has only two colors (b/w) is being converted into an 8 bit GIF....
|
|
Packit |
f1137c |
Which is horrendously wasteful without compression.
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
I (ESR) took this off the main to-do list in 2012 because I don't think
|
|
Packit |
f1137c |
the GIFLIB project actually needs to be in the converters-and-tools business.
|
|
Packit |
f1137c |
Plenty of hackers do that; our jub is to supply stable library capability
|
|
Packit |
f1137c |
with our utilities mainly interesting as test tools.
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
***************************************************************************/
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
#include <stdlib.h>
|
|
Packit |
f1137c |
#include <stdio.h>
|
|
Packit |
f1137c |
#include <ctype.h>
|
|
Packit |
f1137c |
#include <string.h>
|
|
Packit |
f1137c |
#include <stdbool.h>
|
|
Packit |
f1137c |
#include <fcntl.h>
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
#ifdef _WIN32
|
|
Packit |
f1137c |
#include <io.h>
|
|
Packit |
f1137c |
#endif /* _WIN32 */
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
#include "gif_lib.h"
|
|
Packit |
f1137c |
#include "getarg.h"
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
#define PROGRAM_NAME "gif2rgb"
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
static char
|
|
Packit |
f1137c |
*VersionStr =
|
|
Packit |
f1137c |
PROGRAM_NAME
|
|
Packit |
f1137c |
VERSION_COOKIE
|
|
Packit |
f1137c |
" Gershon Elber, "
|
|
Packit |
f1137c |
__DATE__ ", " __TIME__ "\n"
|
|
Packit |
f1137c |
"(C) Copyright 1989 Gershon Elber.\n";
|
|
Packit |
f1137c |
static char
|
|
Packit |
f1137c |
*CtrlStr =
|
|
Packit |
f1137c |
PROGRAM_NAME
|
|
Packit |
f1137c |
" v%- c%-#Colors!d s%-Width|Height!d!d 1%- o%-OutFileName!s h%- GifFile!*s";
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
static void LoadRGB(char *FileName,
|
|
Packit |
f1137c |
int OneFileFlag,
|
|
Packit |
f1137c |
GifByteType **RedBuffer,
|
|
Packit |
f1137c |
GifByteType **GreenBuffer,
|
|
Packit |
f1137c |
GifByteType **BlueBuffer,
|
|
Packit |
f1137c |
int Width, int Height);
|
|
Packit |
f1137c |
static void SaveGif(GifByteType *OutputBuffer,
|
|
Packit |
f1137c |
int Width, int Height,
|
|
Packit |
f1137c |
int ExpColorMapSize, ColorMapObject *OutputColorMap);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/******************************************************************************
|
|
Packit |
f1137c |
Load RGB file into internal frame buffer.
|
|
Packit |
f1137c |
******************************************************************************/
|
|
Packit |
f1137c |
static void LoadRGB(char *FileName,
|
|
Packit |
f1137c |
int OneFileFlag,
|
|
Packit |
f1137c |
GifByteType **RedBuffer,
|
|
Packit |
f1137c |
GifByteType **GreenBuffer,
|
|
Packit |
f1137c |
GifByteType **BlueBuffer,
|
|
Packit |
f1137c |
int Width, int Height)
|
|
Packit |
f1137c |
{
|
|
Packit |
f1137c |
int i;
|
|
Packit |
f1137c |
unsigned long Size;
|
|
Packit |
f1137c |
GifByteType *RedP, *GreenP, *BlueP;
|
|
Packit |
f1137c |
FILE *rgbfp[3];
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
Size = ((long) Width) * Height * sizeof(GifByteType);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((*RedBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL ||
|
|
Packit |
f1137c |
(*GreenBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL ||
|
|
Packit |
f1137c |
(*BlueBuffer = (GifByteType *) malloc((unsigned int) Size)) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
RedP = *RedBuffer;
|
|
Packit |
f1137c |
GreenP = *GreenBuffer;
|
|
Packit |
f1137c |
BlueP = *BlueBuffer;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (FileName != NULL) {
|
|
Packit |
f1137c |
if (OneFileFlag) {
|
|
Packit |
f1137c |
if ((rgbfp[0] = fopen(FileName, "rb")) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Can't open input file name.");
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
else {
|
|
Packit |
f1137c |
static char *Postfixes[] = { ".R", ".G", ".B" };
|
|
Packit |
f1137c |
char OneFileName[80];
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
for (i = 0; i < 3; i++) {
|
|
Packit |
f1137c |
strncpy(OneFileName, FileName, sizeof(OneFileName)-1);
|
|
Packit |
f1137c |
/* cppcheck-suppress uninitstring */
|
|
Packit |
f1137c |
strncat(OneFileName, Postfixes[i],
|
|
Packit |
f1137c |
sizeof(OneFileName) - 1 - strlen(OneFileName));
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((rgbfp[i] = fopen(OneFileName, "rb")) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Can't open input file name.");
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
else {
|
|
Packit |
f1137c |
OneFileFlag = true;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
#ifdef _WIN32
|
|
Packit |
f1137c |
_setmode(0, O_BINARY);
|
|
Packit |
f1137c |
#endif /* _WIN32 */
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
rgbfp[0] = stdin;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
GifQprintf("\n%s: RGB image: ", PROGRAM_NAME);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (OneFileFlag) {
|
|
Packit |
f1137c |
GifByteType *Buffer, *BufferP;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((Buffer = (GifByteType *) malloc(Width * 3)) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
for (i = 0; i < Height; i++) {
|
|
Packit |
f1137c |
int j;
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", i);
|
|
Packit |
f1137c |
if (fread(Buffer, Width * 3, 1, rgbfp[0]) != 1)
|
|
Packit |
f1137c |
GIF_EXIT("Input file(s) terminated prematurly.");
|
|
Packit |
f1137c |
for (j = 0, BufferP = Buffer; j < Width; j++) {
|
|
Packit |
f1137c |
*RedP++ = *BufferP++;
|
|
Packit |
f1137c |
*GreenP++ = *BufferP++;
|
|
Packit |
f1137c |
*BlueP++ = *BufferP++;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
free((char *) Buffer);
|
|
Packit |
f1137c |
fclose(rgbfp[0]);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
else {
|
|
Packit |
f1137c |
for (i = 0; i < Height; i++) {
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", i);
|
|
Packit |
f1137c |
if (fread(RedP, Width, 1, rgbfp[0]) != 1 ||
|
|
Packit |
f1137c |
fread(GreenP, Width, 1, rgbfp[1]) != 1 ||
|
|
Packit |
f1137c |
fread(BlueP, Width, 1, rgbfp[2]) != 1)
|
|
Packit |
f1137c |
GIF_EXIT("Input file(s) terminated prematurly.");
|
|
Packit |
f1137c |
RedP += Width;
|
|
Packit |
f1137c |
GreenP += Width;
|
|
Packit |
f1137c |
BlueP += Width;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
fclose(rgbfp[0]);
|
|
Packit |
f1137c |
// cppcheck-suppress useClosedFile
|
|
Packit |
f1137c |
fclose(rgbfp[1]);
|
|
Packit |
f1137c |
// cppcheck-suppress useClosedFile
|
|
Packit |
f1137c |
fclose(rgbfp[2]);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/******************************************************************************
|
|
Packit |
f1137c |
Save the GIF resulting image.
|
|
Packit |
f1137c |
******************************************************************************/
|
|
Packit |
f1137c |
static void SaveGif(GifByteType *OutputBuffer,
|
|
Packit |
f1137c |
int Width, int Height,
|
|
Packit |
f1137c |
int ExpColorMapSize, ColorMapObject *OutputColorMap)
|
|
Packit |
f1137c |
{
|
|
Packit |
f1137c |
int i, Error;
|
|
Packit |
f1137c |
GifFileType *GifFile;
|
|
Packit |
f1137c |
GifByteType *Ptr = OutputBuffer;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/* Open stdout for the output file: */
|
|
Packit |
f1137c |
if ((GifFile = EGifOpenFileHandle(1, &Error)) == NULL) {
|
|
Packit |
f1137c |
PrintGifError(Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (EGifPutScreenDesc(GifFile,
|
|
Packit |
f1137c |
Width, Height, ExpColorMapSize, 0,
|
|
Packit |
f1137c |
OutputColorMap) == GIF_ERROR ||
|
|
Packit |
f1137c |
EGifPutImageDesc(GifFile,
|
|
Packit |
f1137c |
0, 0, Width, Height, false, NULL) ==
|
|
Packit |
f1137c |
GIF_ERROR)
|
|
Packit |
f1137c |
PrintGifError(Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
GifQprintf("\n%s: Image 1 at (%d, %d) [%dx%d]: ",
|
|
Packit |
f1137c |
PROGRAM_NAME, GifFile->Image.Left, GifFile->Image.Top,
|
|
Packit |
f1137c |
GifFile->Image.Width, GifFile->Image.Height);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
for (i = 0; i < Height; i++) {
|
|
Packit |
f1137c |
if (EGifPutLine(GifFile, Ptr, Width) == GIF_ERROR)
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", Height - i - 1);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
Ptr += Width;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (EGifCloseFile(GifFile, &Error) == GIF_ERROR)
|
|
Packit |
f1137c |
PrintGifError(Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/******************************************************************************
|
|
Packit |
f1137c |
Close output file (if open), and exit.
|
|
Packit |
f1137c |
******************************************************************************/
|
|
Packit |
f1137c |
static void RGB2GIF(bool OneFileFlag, int NumFiles, char *FileName,
|
|
Packit |
f1137c |
int ExpNumOfColors, int Width, int Height)
|
|
Packit |
f1137c |
{
|
|
Packit |
f1137c |
int ColorMapSize;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
GifByteType *RedBuffer = NULL, *GreenBuffer = NULL, *BlueBuffer = NULL,
|
|
Packit |
f1137c |
*OutputBuffer = NULL;
|
|
Packit |
f1137c |
ColorMapObject *OutputColorMap = NULL;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
ColorMapSize = 1 << ExpNumOfColors;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (NumFiles == 1) {
|
|
Packit |
f1137c |
LoadRGB(FileName, OneFileFlag,
|
|
Packit |
f1137c |
&RedBuffer, &GreenBuffer, &BlueBuffer, Width, Height);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
else {
|
|
Packit |
f1137c |
LoadRGB(NULL, OneFileFlag,
|
|
Packit |
f1137c |
&RedBuffer, &GreenBuffer, &BlueBuffer, Width, Height);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((OutputColorMap = GifMakeMapObject(ColorMapSize, NULL)) == NULL ||
|
|
Packit |
f1137c |
(OutputBuffer = (GifByteType *) malloc(Width * Height *
|
|
Packit |
f1137c |
sizeof(GifByteType))) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (GifQuantizeBuffer(Width, Height, &ColorMapSize,
|
|
Packit |
f1137c |
RedBuffer, GreenBuffer, BlueBuffer,
|
|
Packit |
f1137c |
OutputBuffer, OutputColorMap->Colors) == GIF_ERROR)
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
free((char *) RedBuffer);
|
|
Packit |
f1137c |
free((char *) GreenBuffer);
|
|
Packit |
f1137c |
free((char *) BlueBuffer);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
SaveGif(OutputBuffer, Width, Height, ExpNumOfColors, OutputColorMap);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/******************************************************************************
|
|
Packit |
f1137c |
The real screen dumping routine.
|
|
Packit |
f1137c |
******************************************************************************/
|
|
Packit |
f1137c |
static void DumpScreen2RGB(char *FileName, int OneFileFlag,
|
|
Packit |
f1137c |
ColorMapObject *ColorMap,
|
|
Packit |
f1137c |
GifRowType *ScreenBuffer,
|
|
Packit |
f1137c |
int ScreenWidth, int ScreenHeight)
|
|
Packit |
f1137c |
{
|
|
Packit |
f1137c |
int i, j;
|
|
Packit |
f1137c |
GifRowType GifRow;
|
|
Packit |
f1137c |
GifColorType *ColorMapEntry;
|
|
Packit |
f1137c |
FILE *rgbfp[3];
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (FileName != NULL) {
|
|
Packit |
f1137c |
if (OneFileFlag) {
|
|
Packit |
f1137c |
if ((rgbfp[0] = fopen(FileName, "wb")) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Can't open input file name.");
|
|
Packit |
f1137c |
} else {
|
|
Packit |
f1137c |
static char *Postfixes[] = { ".R", ".G", ".B" };
|
|
Packit |
f1137c |
char OneFileName[80];
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
for (i = 0; i < 3; i++) {
|
|
Packit |
f1137c |
strncpy(OneFileName, FileName, sizeof(OneFileName)-1);
|
|
Packit |
f1137c |
/* cppcheck-suppress uninitstring */
|
|
Packit |
f1137c |
strncat(OneFileName, Postfixes[i],
|
|
Packit |
f1137c |
sizeof(OneFileName) - 1 - strlen(OneFileName));
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((rgbfp[i] = fopen(OneFileName, "wb")) == NULL) {
|
|
Packit |
f1137c |
GIF_EXIT("Can't open input file name.");
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
} else {
|
|
Packit |
f1137c |
OneFileFlag = true;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
#ifdef _WIN32
|
|
Packit |
f1137c |
_setmode(1, O_BINARY);
|
|
Packit |
f1137c |
#endif /* _WIN32 */
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
rgbfp[0] = stdout;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (OneFileFlag) {
|
|
Packit |
f1137c |
unsigned char *Buffer, *BufferP;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((Buffer = (unsigned char *) malloc(ScreenWidth * 3)) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
for (i = 0; i < ScreenHeight; i++) {
|
|
Packit |
f1137c |
GifRow = ScreenBuffer[i];
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", ScreenHeight - i);
|
|
Packit |
f1137c |
for (j = 0, BufferP = Buffer; j < ScreenWidth; j++) {
|
|
Packit |
f1137c |
ColorMapEntry = &ColorMap->Colors[GifRow[j]];
|
|
Packit |
f1137c |
*BufferP++ = ColorMapEntry->Red;
|
|
Packit |
f1137c |
*BufferP++ = ColorMapEntry->Green;
|
|
Packit |
f1137c |
*BufferP++ = ColorMapEntry->Blue;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
if (fwrite(Buffer, ScreenWidth * 3, 1, rgbfp[0]) != 1)
|
|
Packit |
f1137c |
GIF_EXIT("Write to file(s) failed.");
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
free((char *) Buffer);
|
|
Packit |
f1137c |
fclose(rgbfp[0]);
|
|
Packit |
f1137c |
} else {
|
|
Packit |
f1137c |
unsigned char *Buffers[3];
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((Buffers[0] = (unsigned char *) malloc(ScreenWidth)) == NULL ||
|
|
Packit |
f1137c |
(Buffers[1] = (unsigned char *) malloc(ScreenWidth)) == NULL ||
|
|
Packit |
f1137c |
(Buffers[2] = (unsigned char *) malloc(ScreenWidth)) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
for (i = 0; i < ScreenHeight; i++) {
|
|
Packit |
f1137c |
GifRow = ScreenBuffer[i];
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", ScreenHeight - i);
|
|
Packit |
f1137c |
for (j = 0; j < ScreenWidth; j++) {
|
|
Packit |
f1137c |
ColorMapEntry = &ColorMap->Colors[GifRow[j]];
|
|
Packit |
f1137c |
Buffers[0][j] = ColorMapEntry->Red;
|
|
Packit |
f1137c |
Buffers[1][j] = ColorMapEntry->Green;
|
|
Packit |
f1137c |
Buffers[2][j] = ColorMapEntry->Blue;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
if (fwrite(Buffers[0], ScreenWidth, 1, rgbfp[0]) != 1 ||
|
|
Packit |
f1137c |
fwrite(Buffers[1], ScreenWidth, 1, rgbfp[1]) != 1 ||
|
|
Packit |
f1137c |
fwrite(Buffers[2], ScreenWidth, 1, rgbfp[2]) != 1)
|
|
Packit |
f1137c |
GIF_EXIT("Write to file(s) failed.");
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
free((char *) Buffers[0]);
|
|
Packit |
f1137c |
free((char *) Buffers[1]);
|
|
Packit |
f1137c |
free((char *) Buffers[2]);
|
|
Packit |
f1137c |
fclose(rgbfp[0]);
|
|
Packit |
f1137c |
// cppcheck-suppress useClosedFile
|
|
Packit |
f1137c |
fclose(rgbfp[1]);
|
|
Packit |
f1137c |
// cppcheck-suppress useClosedFile
|
|
Packit |
f1137c |
fclose(rgbfp[2]);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
static void GIF2RGB(int NumFiles, char *FileName,
|
|
Packit |
f1137c |
bool OneFileFlag,
|
|
Packit |
f1137c |
char *OutFileName)
|
|
Packit |
f1137c |
{
|
|
Packit |
f1137c |
int i, j, Size, Row, Col, Width, Height, ExtCode, Count;
|
|
Packit |
f1137c |
GifRecordType RecordType;
|
|
Packit |
f1137c |
GifByteType *Extension;
|
|
Packit |
f1137c |
GifRowType *ScreenBuffer;
|
|
Packit |
f1137c |
GifFileType *GifFile;
|
|
Packit |
f1137c |
int
|
|
Packit |
f1137c |
InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */
|
|
Packit |
f1137c |
InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */
|
|
Packit |
f1137c |
int ImageNum = 0;
|
|
Packit |
f1137c |
ColorMapObject *ColorMap;
|
|
Packit |
f1137c |
int Error;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (NumFiles == 1) {
|
|
Packit |
f1137c |
int Error;
|
|
Packit |
f1137c |
if ((GifFile = DGifOpenFileName(FileName, &Error)) == NULL) {
|
|
Packit |
f1137c |
PrintGifError(Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
else {
|
|
Packit |
f1137c |
int Error;
|
|
Packit |
f1137c |
/* Use stdin instead: */
|
|
Packit |
f1137c |
if ((GifFile = DGifOpenFileHandle(0, &Error)) == NULL) {
|
|
Packit |
f1137c |
PrintGifError(Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (GifFile->SHeight == 0 || GifFile->SWidth == 0) {
|
|
Packit |
f1137c |
fprintf(stderr, "Image of width or height 0\n");
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/*
|
|
Packit |
f1137c |
* Allocate the screen as vector of column of rows. Note this
|
|
Packit |
f1137c |
* screen is device independent - it's the screen defined by the
|
|
Packit |
f1137c |
* GIF file parameters.
|
|
Packit |
f1137c |
*/
|
|
Packit |
f1137c |
if ((ScreenBuffer = (GifRowType *)
|
|
Packit |
f1137c |
malloc(GifFile->SHeight * sizeof(GifRowType))) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
Size = GifFile->SWidth * sizeof(GifPixelType);/* Size in bytes one row.*/
|
|
Packit |
f1137c |
if ((ScreenBuffer[0] = (GifRowType) malloc(Size)) == NULL) /* First row. */
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
for (i = 0; i < GifFile->SWidth; i++) /* Set its color to BackGround. */
|
|
Packit |
f1137c |
ScreenBuffer[0][i] = GifFile->SBackGroundColor;
|
|
Packit |
f1137c |
for (i = 1; i < GifFile->SHeight; i++) {
|
|
Packit |
f1137c |
/* Allocate the other rows, and set their color to background too: */
|
|
Packit |
f1137c |
if ((ScreenBuffer[i] = (GifRowType) malloc(Size)) == NULL)
|
|
Packit |
f1137c |
GIF_EXIT("Failed to allocate memory required, aborted.");
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
memcpy(ScreenBuffer[i], ScreenBuffer[0], Size);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/* Scan the content of the GIF file and load the image(s) in: */
|
|
Packit |
f1137c |
do {
|
|
Packit |
f1137c |
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(GifFile->Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
switch (RecordType) {
|
|
Packit |
f1137c |
case IMAGE_DESC_RECORD_TYPE:
|
|
Packit |
f1137c |
if (DGifGetImageDesc(GifFile) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(GifFile->Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
Row = GifFile->Image.Top; /* Image Position relative to Screen. */
|
|
Packit |
f1137c |
Col = GifFile->Image.Left;
|
|
Packit |
f1137c |
Width = GifFile->Image.Width;
|
|
Packit |
f1137c |
Height = GifFile->Image.Height;
|
|
Packit |
f1137c |
GifQprintf("\n%s: Image %d at (%d, %d) [%dx%d]: ",
|
|
Packit |
f1137c |
PROGRAM_NAME, ++ImageNum, Col, Row, Width, Height);
|
|
Packit |
f1137c |
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth ||
|
|
Packit |
f1137c |
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) {
|
|
Packit |
f1137c |
fprintf(stderr, "Image %d is not confined to screen dimension, aborted.\n",ImageNum);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
if (GifFile->Image.Interlace) {
|
|
Packit |
f1137c |
/* Need to perform 4 passes on the images: */
|
|
Packit |
f1137c |
for (Count = i = 0; i < 4; i++)
|
|
Packit |
f1137c |
for (j = Row + InterlacedOffset[i]; j < Row + Height;
|
|
Packit |
f1137c |
j += InterlacedJumps[i]) {
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", Count++);
|
|
Packit |
f1137c |
if (DGifGetLine(GifFile, &ScreenBuffer[j][Col],
|
|
Packit |
f1137c |
Width) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(GifFile->Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
else {
|
|
Packit |
f1137c |
for (i = 0; i < Height; i++) {
|
|
Packit |
f1137c |
GifQprintf("\b\b\b\b%-4d", i);
|
|
Packit |
f1137c |
if (DGifGetLine(GifFile, &ScreenBuffer[Row++][Col],
|
|
Packit |
f1137c |
Width) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(GifFile->Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
break;
|
|
Packit |
f1137c |
case EXTENSION_RECORD_TYPE:
|
|
Packit |
f1137c |
/* Skip any extension blocks in file: */
|
|
Packit |
f1137c |
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(GifFile->Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
while (Extension != NULL) {
|
|
Packit |
f1137c |
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(GifFile->Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
break;
|
|
Packit |
f1137c |
case TERMINATE_RECORD_TYPE:
|
|
Packit |
f1137c |
break;
|
|
Packit |
f1137c |
default: /* Should be trapped by DGifGetRecordType. */
|
|
Packit |
f1137c |
break;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
} while (RecordType != TERMINATE_RECORD_TYPE);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/* Lets dump it - set the global variables required and do it: */
|
|
Packit |
f1137c |
ColorMap = (GifFile->Image.ColorMap
|
|
Packit |
f1137c |
? GifFile->Image.ColorMap
|
|
Packit |
f1137c |
: GifFile->SColorMap);
|
|
Packit |
f1137c |
if (ColorMap == NULL) {
|
|
Packit |
f1137c |
fprintf(stderr, "Gif Image does not have a colormap\n");
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
DumpScreen2RGB(OutFileName, OneFileFlag,
|
|
Packit |
f1137c |
ColorMap,
|
|
Packit |
f1137c |
ScreenBuffer,
|
|
Packit |
f1137c |
GifFile->SWidth, GifFile->SHeight);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
(void)free(ScreenBuffer);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (DGifCloseFile(GifFile, &Error) == GIF_ERROR) {
|
|
Packit |
f1137c |
PrintGifError(Error);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/******************************************************************************
|
|
Packit |
f1137c |
* Interpret the command line and scan the given GIF file.
|
|
Packit |
f1137c |
******************************************************************************/
|
|
Packit |
f1137c |
int main(int argc, char **argv)
|
|
Packit |
f1137c |
{
|
|
Packit |
f1137c |
bool Error, OutFileFlag = false, ColorFlag = false, SizeFlag = false;
|
|
Packit |
f1137c |
int NumFiles, Width = 0, Height = 0, ExpNumOfColors = 8;
|
|
Packit |
f1137c |
char *OutFileName,
|
|
Packit |
f1137c |
**FileName = NULL;
|
|
Packit |
f1137c |
static bool
|
|
Packit |
f1137c |
OneFileFlag = false,
|
|
Packit |
f1137c |
HelpFlag = false;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifNoisyPrint,
|
|
Packit |
f1137c |
&ColorFlag, &ExpNumOfColors, &SizeFlag, &Width, &Height,
|
|
Packit |
f1137c |
&OneFileFlag, &OutFileFlag, &OutFileName,
|
|
Packit |
f1137c |
&HelpFlag, &NumFiles, &FileName)) != false ||
|
|
Packit |
f1137c |
(NumFiles > 1 && !HelpFlag)) {
|
|
Packit |
f1137c |
if (Error)
|
|
Packit |
f1137c |
GAPrintErrMsg(Error);
|
|
Packit |
f1137c |
else if (NumFiles > 1)
|
|
Packit |
f1137c |
GIF_MESSAGE("Error in command line parsing - one input file please.");
|
|
Packit |
f1137c |
GAPrintHowTo(CtrlStr);
|
|
Packit |
f1137c |
exit(EXIT_FAILURE);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (HelpFlag) {
|
|
Packit |
f1137c |
(void)fprintf(stderr, VersionStr, GIFLIB_MAJOR, GIFLIB_MINOR);
|
|
Packit |
f1137c |
GAPrintHowTo(CtrlStr);
|
|
Packit |
f1137c |
exit(EXIT_SUCCESS);
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
if (!OutFileFlag) OutFileName = NULL;
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
if (SizeFlag && Width > 0 && Height > 0)
|
|
Packit |
f1137c |
RGB2GIF(OneFileFlag, NumFiles, *FileName,
|
|
Packit |
f1137c |
ExpNumOfColors, Width, Height);
|
|
Packit |
f1137c |
else
|
|
Packit |
f1137c |
GIF2RGB(NumFiles, *FileName, OneFileFlag, OutFileName);
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
return 0;
|
|
Packit |
f1137c |
}
|
|
Packit |
f1137c |
|
|
Packit |
f1137c |
/* end */
|