home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
BURKS 2
/
BURKS_AUG97.ISO
/
BURKS
/
SOFTWARE
/
LIBS
/
GIFSAVE.ZIP
/
gifsave.c
(
.txt
)
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-26
|
32KB
|
1,204 lines
/**************************************************************************
*
* FILE: GIFSAVE.C
*
* MODULE OF: GIFSAVE
*
* DESCRIPTION: Routines to create a GIF-file. See GIFSAVE.DOC for
* a description . . .
*
* The functions were originally written using Borland's
* C-compiler on an IBM PC -compatible computer, but they
* are compiled and tested on SunOS (Unix) as well.
*
* WRITTEN BY: Sverre H. Huseby
* Bjoelsengt. 17
* N-0468 Oslo
* Norway
*
* sverrehu@ifi.uio.no
*
* LAST MODIFIED: 26/9-1992, v1.0, Sverre H. Huseby
* * Version 1.0, no modifications
*
**************************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include "gifsave.h"
/**************************************************************************
* *
* P R I V A T E D A T A *
* *
**************************************************************************/
typedef unsigned Word; /* At least two bytes (16 bits) */
typedef unsigned char Byte; /* Exactly one byte (8 bits) */
/*========================================================================*
= =
= I/O Routines =
= =
*========================================================================*/
static FILE *OutFile; /* File to write to */
/*========================================================================*
= =
= Routines to write a bit-file =
= =
*========================================================================*/
static Byte Buffer[256]; /* There must be one to much !!! */
static int Index, /* Current byte in buffer */
BitsLeft; /* Bits left to fill in current byte. These
/* are right-justified */
/*========================================================================*
= =
= Routines to maintain an LZW-string table =
= =
*========================================================================*/
#define RES_CODES 2
#define HASH_FREE 0xFFFF
#define NEXT_FIRST 0xFFFF
#define MAXBITS 12
#define MAXSTR (1 << MAXBITS)
#define HASHSIZE 9973
#define HASHSTEP 2039
#define HASH(index, lastbyte) (((lastbyte << 8) ^ index) % HASHSIZE)
static Byte *StrChr = NULL;
static Word *StrNxt = NULL,
*StrHsh = NULL,
NumStrings;
/*========================================================================*
= =
= Main routines =
= =
*========================================================================*/
typedef struct {
Word LocalScreenWidth,
LocalScreenHeight;
Byte GlobalColorTableSize : 3,
SortFlag : 1,
ColorResolution : 3,
GlobalColorTableFlag : 1;
Byte BackgroundColorIndex;
Byte PixelAspectRatio;
} ScreenDescriptor;
typedef struct {
Byte Separator;
Word LeftPosition,
TopPosition;
Word Width,
Height;
Byte LocalColorTableSize : 3,
Reserved : 2,
SortFlag : 1,
InterlaceFlag : 1,
LocalColorTableFlag : 1;
} ImageDescriptor;
static int BitsPrPrimColor, /* Bits pr primary color */
NumColors; /* Number of colors in color table */
static Byte *ColorTable = NULL;
static Word ScreenHeight,
ScreenWidth,
ImageHeight,
ImageWidth,
ImageLeft,
ImageTop,
RelPixX, RelPixY; /* Used by InputByte() -function */
static int (*GetPixel)(int x, int y);
/**************************************************************************
* *
* P R I V A T E F U N C T I O N S *
* *
**************************************************************************/
/*========================================================================*
= =
= Routines to do file IO =
= =
*========================================================================*/
/*-------------------------------------------------------------------------
*
* NAME: Create()
*
* DESCRIPTION: Creates a new file, and enables referencing using the
* global variable OutFile. This variable is only used
* by these IO-functions, making it relatively simple to
* rewrite file IO.
*
* PARAMETERS: filename - Name of file to create
*
* RETURNS: GIF_OK - OK
* GIF_ERRWRITE - Error opening the file
*
*/
static int Create(char *filename)
{
if ((OutFile = fopen(filename, "wb")) == NULL)
return GIF_ERRCREATE;
return GIF_OK;
}
/*-------------------------------------------------------------------------
*
* NAME: Write()
*
* DESCRIPTION: Output bytes to the current OutFile.
*
* PARAMETERS: buf - Pointer to buffer to write
* len - Number of bytes to write
*
* RETURNS: GIF_OK - OK
* GIF_ERRWRITE - Error writing to the file
*
*/
static int Write(void *buf, unsigned len)
{
if (fwrite(buf, sizeof(Byte), len, OutFile) < len)
return GIF_ERRWRITE;
return GIF_OK;
}
/*-------------------------------------------------------------------------
*
* NAME: WriteByte()
*
* DESCRIPTION: Output one byte to the current OutFile.
*
* PARAMETERS: b - Byte to write
*
* RETURNS: GIF_OK - OK
* GIF_ERRWRITE - Error writing to the file
*
*/
static int WriteByte(Byte b)
{
if (putc(b, OutFile) == EOF)
return GIF_ERRWRITE;
return GIF_OK;
}
/*-------------------------------------------------------------------------
*
* NAME: WriteWord()
*
* DESCRIPTION: Output one word (2 bytes with byte-swapping, like on
* the IBM PC) to the current OutFile.
*
* PARAMETERS: w - Word to write
*
* RETURNS: GIF_OK - OK
* GIF_ERRWRITE - Error writing to the file
*
*/
static int WriteWord(Word w)
{
if (putc(w & 0xFF, OutFile) == EOF)
return GIF_ERRWRITE;
if (putc((w >> 8), OutFile) == EOF)
return GIF_ERRWRITE;
return GIF_OK;
}
/*-------------------------------------------------------------------------
*
* NAME: Close()
*
* DESCRIPTION: Close current OutFile.
*
* PARAMETERS: None
*
* RETURNS: Nothing
*
*/
static void Close(void)