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 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
|
/******************************************************************************
* "Gif-Lib" - Yet another gif library. *
* *
* Written by: Gershon Elber Ver 1.1, Aug. 1990 *
*******************************************************************************
* The kernel of the GIF Encoding process can be found here. *
*******************************************************************************
* History: *
* 14 Jun 89 - Version 1.0 by Gershon Elber. *
* 3 Sep 90 - Version 1.1 by Gershon Elber (Support for Gif89, Unique names). *
* 26 Jun 96 - Version 3.0 by Eric S. Raymond (Full GIF89 support)
******************************************************************************/
#ifdef __MSDOS__
#include <io.h>
#include <alloc.h>
#include <sys\stat.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#ifdef R6000
#include <sys/mode.h>
#endif
#endif /* __MSDOS__ */
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include "gif_lib.h"
#include "gif_hash.h"
#define PROGRAM_NAME "GIFLIB"
#define GIF87_STAMP "GIF87a" /* First chars in file - GIF stamp. */
#define GIF89_STAMP "GIF89a" /* First chars in file - GIF stamp. */
#define ZL_MAX_CODE 4095 /* Biggest code possible in 12 bits. */
#define FILE_STATE_WRITE 0x01/* 1 write, 0 read - DGIF_LIB compatible.*/
#define FILE_STATE_SCREEN 0x02
#define FILE_STATE_IMAGE 0x04
#define FLUSH_OUTPUT 4096 /* Impossible code, to signal flush. */
#define FIRST_CODE 4097 /* Impossible code, to signal first. */
#define IS_WRITEABLE(Private) (Private->FileState & FILE_STATE_WRITE)
/* #define DEBUG_NO_PREFIX Dump only compressed data. */
typedef struct GifFilePrivateType {
int FileState,
FileHandle, /* Where old this data goes to! */
BitsPerPixel, /* Bits per pixel (Codes uses at least this + 1). */
ClearCode, /* The CLEAR LZ code. */
EOFCode, /* The EOF LZ code. */
RunningCode, /* The next code algorithm can generate. */
RunningBits,/* The number of bits required to represent RunningCode. */
MaxCode1, /* 1 bigger than max. possible code, in RunningBits bits. */
CrntCode, /* Current algorithm code. */
CrntShiftState; /* Number of bits in CrntShiftDWord. */
unsigned long CrntShiftDWord; /* For bytes decomposition into codes. */
unsigned long PixelCount;
FILE *File; /* File as stream. */
GifByteType Buf[256]; /* Compressed output is buffered here. */
GifHashTableType *HashTable;
} GifFilePrivateType;
extern int _GifError;
/* Masks given codes to BitsPerPixel, to make sure all codes are in range: */
static GifPixelType CodeMask[] = {
0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff
};
#ifdef SYSV
static char *VersionStr =
"Gif library module,\t\tGershon Elber\n\
(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
#else
static char *VersionStr =
PROGRAM_NAME
" IBMPC "
GIF_LIB_VERSION
" Gershon Elber, "
__DATE__ ", " __TIME__ "\n"
"(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
#endif /* SYSV */
static char *GifVersionPrefix = GIF87_STAMP;
static int EGifPutWord(int Word, FILE *File);
static int EGifSetupCompress(GifFileType *GifFile);
static int EGifCompressLine(GifFileType *GifFile, GifPixelType *Line,
int LineLen);
static int EGifCompressOutput(GifFilePrivateType *Private, int Code);
static int EGifBufferedOutput(FILE *File, GifByteType *Buf, int c);
/******************************************************************************
* Open a new gif file for write, given by its name. If TestExistance then *
* if the file exists this routines fails (returns NULL). *
* Returns GifFileType pointer dynamically allocated which serves as the gif *
* info record. _GifError is cleared if succesfull. *
******************************************************************************/
GifFileType *EGifOpenFileName(char *FileName, int TestExistance)
{
int FileHandle;
if (TestExistance)
FileHandle = open(FileName,
O_WRONLY | O_CREAT | O_EXCL
#ifdef __MSDOS__
| O_BINARY
#endif /* __MSDOS__ */
,
S_IREAD | S_IWRITE);
else
FileHandle = open(FileName,
O_WRONLY | O_CREAT | O_TRUNC
#ifdef __MSDOS__
| O_BINARY
#endif /* __MSDOS__ */
,
S_IREAD | S_IWRITE);
if (FileHandle == -1) {
_GifError = E_GIF_ERR_OPEN_FAILED;
return NULL;
}
return EGifOpenFileHandle(FileHandle);
}
/******************************************************************************
* Update a new gif file, given its file handle, which must be opened for *
* write in binary mode. *
* Returns GifFileType pointer dynamically allocated which serves as the gif *
* info record. _GifError is cleared if succesfull. *
******************************************************************************/
GifFileType *EGifOpenFileHandle(int FileHandle)
{
GifFileType *GifFile;
GifFilePrivateType *Private;
FILE *f;
#ifdef __MSDOS__
setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */
f = fdopen(FileHandle, "wb"); /* Make it into a stream: */
setvbuf(f, NULL, _IOFBF, GIF_FILE_BUFFER_SIZE); /* And inc. stream buffer. */
#else
f = fdopen(FileHandle, "w"); /* Make it into a stream: */
#endif /* __MSDOS__ */
if ((GifFile = (GifFileType *) malloc(sizeof(GifFileType))) == NULL) {
_GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
return NULL;
}
memset(GifFile, '\0', sizeof(GifFileType));
#ifndef DEBUG_NO_PREFIX
if (fwrite(GifVersionPrefix, 1, strlen(GifVersionPrefix), f) !=
strlen(GifVersionPrefix)) {
_GifError = E_GIF_ERR_WRITE_FAILED;
free((char *) GifFile);
return NULL;
}
#endif /* DEBUG_NO_PREFIX */
if ((Private = (GifFilePrivateType *) malloc(sizeof(GifFilePrivateType)))
== NULL) {
_GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
return NULL;
}
GifFile->Private = (VoidPtr) Private;
Private->FileHandle = FileHandle;
Private->File = f;
Private->FileState = FILE_STATE_WRITE;
if ((Private->HashTable = _InitHashTable()) == NULL) {
_GifError = E_GIF_ERR_NOT_ENOUGH_MEM;
return NULL;
}
_GifError = 0;
return GifFile;
}
/******************************************************************************
* Routine to set current GIF version. All files open for write will be *
* using this version until next call to this routine. Version consists of *
* 3 characters as "87a" or "89a". No test is made to validate the version. *
******************************************************************************/
void EGifSetGifVersion(char *Version)
{
strncpy(&GifVersionPrefix[3], Version, 3);
}
/******************************************************************************
* This routine should be called before any other EGif calls, immediately *
* follows the GIF file openning. *
******************************************************************************/
int EGifPutScreenDesc(GifFileType *GifFile,
int Width, int Height, int ColorRes, int BackGround,
ColorMapObject *ColorMap)
{
int i, Size;
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (Private->FileState & FILE_STATE_SCREEN) {
/* If already has screen descriptor - something is wrong! */
_GifError = E_GIF_ERR_HAS_SCRN_DSCR;
return GIF_ERROR;
}
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
GifFile->SWidth = Width;
GifFile->SHeight = Height;
GifFile->SColorResolution = ColorRes;
GifFile->SBackGroundColor = BackGround;
if(ColorMap)
GifFile->SColorMap=MakeMapObject(ColorMap->ColorCount,ColorMap->Colors);
else
GifFile->SColorMap=NULL;
/* Put the screen descriptor into the file: */
EGifPutWord(Width, Private->File);
EGifPutWord(Height, Private->File);
Buf[0] = (ColorMap ? 0x80 : 0x00) |
((ColorRes - 1) << 4) |
(ColorMap->BitsPerPixel - 1);
Buf[1] = BackGround;
Buf[2] = 0;
#ifndef DEBUG_NO_PREFIX
fwrite(Buf, 1, 3, Private->File);
#endif /* DEBUG_NO_PREFIX */
/* If we have Global color map - dump it also: */
#ifndef DEBUG_NO_PREFIX
if (ColorMap != NULL)
for (i = 0; i < ColorMap->ColorCount; i++) {
/* Put the ColorMap out also: */
Buf[0] = ColorMap->Colors[i].Red;
Buf[1] = ColorMap->Colors[i].Green;
Buf[2] = ColorMap->Colors[i].Blue;
if (fwrite(Buf, 1, 3, Private->File) != 3) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
#endif /* DEBUG_NO_PREFIX */
/* Mark this file as has screen descriptor, and no pixel written yet: */
Private->FileState |= FILE_STATE_SCREEN;
return GIF_OK;
}
/******************************************************************************
* This routine should be called before any attemp to dump an image - any *
* call to any of the pixel dump routines. *
******************************************************************************/
int EGifPutImageDesc(GifFileType *GifFile,
int Left, int Top, int Width, int Height, int Interlace,
ColorMapObject *ColorMap)
{
int i, Size;
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (Private->FileState & FILE_STATE_IMAGE &&
#if defined(__MSDOS__) || defined(__GNUC__)
Private->PixelCount > 0xffff0000UL) {
#else
Private->PixelCount > 0xffff0000) {
#endif /* __MSDOS__ */
/* If already has active image descriptor - something is wrong! */
_GifError = E_GIF_ERR_HAS_IMAG_DSCR;
return GIF_ERROR;
}
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
GifFile->Image.Left = Left;
GifFile->Image.Top = Top;
GifFile->Image.Width = Width;
GifFile->Image.Height = Height;
GifFile->Image.Interlace = Interlace;
if(ColorMap)
GifFile->Image.ColorMap =MakeMapObject(ColorMap->ColorCount,ColorMap->Colors);
else
GifFile->Image.ColorMap = NULL;
/* Put the image descriptor into the file: */
Buf[0] = ','; /* Image seperator character. */
#ifndef DEBUG_NO_PREFIX
fwrite(Buf, 1, 1, Private->File);
#endif /* DEBUG_NO_PREFIX */
EGifPutWord(Left, Private->File);
EGifPutWord(Top, Private->File);
EGifPutWord(Width, Private->File);
EGifPutWord(Height, Private->File);
Buf[0] = (ColorMap ? 0x80 : 0x00) |
(Interlace ? 0x40 : 0x00) |
(ColorMap ? ColorMap->BitsPerPixel - 1 : 0);
#ifndef DEBUG_NO_PREFIX
fwrite(Buf, 1, 1, Private->File);
#endif /* DEBUG_NO_PREFIX */
/* If we have Global color map - dump it also: */
#ifndef DEBUG_NO_PREFIX
if (ColorMap != NULL)
for (i = 0; i < ColorMap->ColorCount; i++) {
/* Put the ColorMap out also: */
Buf[0] = ColorMap->Colors[i].Red;
Buf[1] = ColorMap->Colors[i].Green;
Buf[2] = ColorMap->Colors[i].Blue;
if (fwrite(Buf, 1, 3, Private->File) != 3) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
#endif /* DEBUG_NO_PREFIX */
if (GifFile->SColorMap == NULL && GifFile->Image.ColorMap == NULL)
{
_GifError = E_GIF_ERR_NO_COLOR_MAP;
return GIF_ERROR;
}
/* Mark this file as has screen descriptor: */
Private->FileState |= FILE_STATE_IMAGE;
Private->PixelCount = (long) Width * (long) Height;
EGifSetupCompress(GifFile); /* Reset compress algorithm parameters. */
return GIF_OK;
}
/******************************************************************************
* Put one full scanned line (Line) of length LineLen into GIF file. *
******************************************************************************/
int EGifPutLine(GifFileType *GifFile, GifPixelType *Line, int LineLen)
{
int i;
GifPixelType Mask;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (!LineLen)
LineLen = GifFile->Image.Width;
if (Private->PixelCount < (unsigned)LineLen) {
_GifError = E_GIF_ERR_DATA_TOO_BIG;
return GIF_ERROR;
}
Private->PixelCount -= LineLen;
/* Make sure the codes are not out of bit range, as we might generate */
/* wrong code (because of overflow when we combine them) in this case: */
Mask = CodeMask[Private->BitsPerPixel];
for (i = 0; i < LineLen; i++) Line[i] &= Mask;
return EGifCompressLine(GifFile, Line, LineLen);
}
/******************************************************************************
* Put one pixel (Pixel) into GIF file. *
******************************************************************************/
int EGifPutPixel(GifFileType *GifFile, GifPixelType Pixel)
{
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (Private->PixelCount == 0)
{
_GifError = E_GIF_ERR_DATA_TOO_BIG;
return GIF_ERROR;
}
--Private->PixelCount;
/* Make sure the code is not out of bit range, as we might generate */
/* wrong code (because of overflow when we combine them) in this case: */
Pixel &= CodeMask[Private->BitsPerPixel];
return EGifCompressLine(GifFile, &Pixel, 1);
}
/******************************************************************************
* Put a comment into GIF file using the GIF89 comment extension block. *
* *
* The original version of this function did no bounds checking to break *
* comments longer than 255 characters into multiple blocks. The Gif89 *
* spec does not limit comments to 255 characters. *
******************************************************************************/
int EGifPutComment(GifFileType *GifFile, char *Comment)
{
int length;
int cnt;
int partlen;
length = strlen(Comment);
if (length < 255) {
return EGifPutExtension(GifFile, COMMENT_EXT_FUNC_CODE, length,
Comment);
} else {
if (EGifPutExtensionHeader(GifFile, COMMENT_EXT_FUNC_CODE) == GIF_ERROR)
return GIF_ERROR;
for (cnt = 0; cnt < length; cnt += 255) {
partlen = length - cnt;
if (partlen > 255)
partlen = 255;
if (EGifPutExtensionBlock(GifFile, partlen, Comment + cnt)
==GIF_ERROR)
return GIF_ERROR;
}
return EGifPutExtensionBlock(GifFile, 0, NULL);
}
}
/******************************************************************************
* Put an extension block (see GIF manual) into gif file. *
******************************************************************************/
int EGifPutExtension(GifFileType *GifFile, int ExtCode, int ExtLen,
VoidPtr Extension)
{
GifByteType Buf[3];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (ExtCode == 0)
fwrite(&ExtLen, 1, 1, Private->File);
else
{
Buf[0] = '!';
Buf[1] = ExtCode;
Buf[2] = ExtLen;
fwrite(Buf, 1, 3, Private->File);
}
fwrite(Extension, 1, ExtLen, Private->File);
Buf[0] = 0;
fwrite(Buf, 1, 1, Private->File);
return GIF_OK;
}
/******************************************************************************
* Put an extension header (see GIF manual) into gif file. *
* This function, along with EGifPutExtensionBlock, allows *
* the construction of multiblock extensions. *
******************************************************************************/
int EGifPutExtensionHeader(GifFileType *GifFile, int ExtCode)
{
GifByteType Buf[2];
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
if (ExtCode == 0)
return GIF_ERROR;
else
{
Buf[0] = '!';
Buf[1] = ExtCode;
fwrite(Buf, 1, 2, Private->File);
}
return GIF_OK;
}
/******************************************************************************
* Put an extension sub-block (see GIF manual) into gif file. *
******************************************************************************/
int EGifPutExtensionBlock(GifFileType *GifFile, int ExtLen, VoidPtr Extension)
{
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
Buf = ExtLen;
fwrite(&Buf, 1, 1, Private->File);
if (ExtLen != 0) {
fwrite(Extension, 1, ExtLen, Private->File);
}
return GIF_OK;
}
/******************************************************************************
* Put the image code in compressed form. This routine can be called if the *
* information needed to be piped out as is. Obviously this is much faster *
* than decoding and encoding again. This routine should be followed by calls *
* to EGifPutCodeNext, until NULL block is given. *
* The block should NOT be freed by the user (not dynamically allocated). *
******************************************************************************/
int EGifPutCode(GifFileType *GifFile, int CodeSize, GifByteType *CodeBlock)
{
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
/* No need to dump code size as Compression set up does any for us: */
/*
Buf = CodeSize;
if (fwrite(&Buf, 1, 1, Private->File) != 1) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
*/
return EGifPutCodeNext(GifFile, CodeBlock);
}
/******************************************************************************
* Continue to put the image code in compressed form. This routine should be *
* called with blocks of code as read via DGifGetCode/DGifGetCodeNext. If *
* given buffer pointer is NULL, empty block is written to mark end of code. *
******************************************************************************/
int EGifPutCodeNext(GifFileType *GifFile, GifByteType *CodeBlock)
{
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
if (CodeBlock != NULL) {
if (fwrite(CodeBlock, 1, CodeBlock[0] + 1, Private->File)
!= (unsigned)(CodeBlock[0] + 1)) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
else {
Buf = 0;
if (fwrite(&Buf, 1, 1, Private->File) != 1) {
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
Private->PixelCount = 0; /* And local info. indicate image read. */
}
return GIF_OK;
}
/******************************************************************************
* This routine should be called last, to close GIF file. *
******************************************************************************/
int EGifCloseFile(GifFileType *GifFile)
{
GifByteType Buf;
GifFilePrivateType *Private;
FILE *File;
if (GifFile == NULL) return GIF_ERROR;
Private = (GifFilePrivateType *) GifFile->Private;
if (!IS_WRITEABLE(Private)) {
/* This file was NOT open for writing: */
_GifError = E_GIF_ERR_NOT_WRITEABLE;
return GIF_ERROR;
}
File = Private->File;
Buf = ';';
fwrite(&Buf, 1, 1, Private->File);
if (GifFile->Image.ColorMap)
FreeMapObject(GifFile->Image.ColorMap);
if (GifFile->SColorMap)
FreeMapObject(GifFile->SColorMap);
if (Private) {
if (Private->HashTable) free((char *) Private->HashTable);
free((char *) Private);
}
free(GifFile);
if (fclose(File) != 0) {
_GifError = E_GIF_ERR_CLOSE_FAILED;
return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* Put 2 bytes (word) into the given file: *
******************************************************************************/
static int EGifPutWord(int Word, FILE *File)
{
char c[2];
c[0] = Word & 0xff;
c[1] = (Word >> 8) & 0xff;
#ifndef DEBUG_NO_PREFIX
if (fwrite(c, 1, 2, File) == 2)
return GIF_OK;
else
return GIF_ERROR;
#else
return GIF_OK;
#endif /* DEBUG_NO_PREFIX */
}
/******************************************************************************
* Setup the LZ compression for this image: *
******************************************************************************/
static int EGifSetupCompress(GifFileType *GifFile)
{
int BitsPerPixel;
GifByteType Buf;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
/* Test and see what color map to use, and from it # bits per pixel: */
if (GifFile->Image.ColorMap)
BitsPerPixel = GifFile->Image.ColorMap->BitsPerPixel;
else if (GifFile->SColorMap)
BitsPerPixel = GifFile->SColorMap->BitsPerPixel;
else {
_GifError = E_GIF_ERR_NO_COLOR_MAP;
return GIF_ERROR;
}
Buf = BitsPerPixel = (BitsPerPixel < 2 ? 2 : BitsPerPixel);
fwrite(&Buf, 1, 1, Private->File); /* Write the Code size to file. */
Private->Buf[0] = 0; /* Nothing was output yet. */
Private->BitsPerPixel = BitsPerPixel;
Private->ClearCode = (1 << BitsPerPixel);
Private->EOFCode = Private->ClearCode + 1;
Private->RunningCode = Private->EOFCode + 1;
Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */
Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */
Private->CrntCode = FIRST_CODE; /* Signal that this is first one! */
Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */
Private->CrntShiftDWord = 0;
/* Clear hash table and send Clear to make sure the decoder do the same. */
_ClearHashTable(Private->HashTable);
if (EGifCompressOutput(Private, Private->ClearCode) == GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
return GIF_OK;
}
/******************************************************************************
* The LZ compression routine: *
* This version compress the given buffer Line of length LineLen. *
* This routine can be called few times (one per scan line, for example), in *
* order the complete the whole image. *
******************************************************************************/
static int EGifCompressLine(GifFileType *GifFile, GifPixelType *Line,
int LineLen)
{
int i = 0, CrntCode, NewCode;
unsigned long NewKey;
GifPixelType Pixel;
GifHashTableType *HashTable;
GifFilePrivateType *Private = (GifFilePrivateType *) GifFile->Private;
HashTable = Private->HashTable;
if (Private->CrntCode == FIRST_CODE) /* Its first time! */
CrntCode = Line[i++];
else
CrntCode = Private->CrntCode; /* Get last code in compression. */
while (i < LineLen) { /* Decode LineLen items. */
Pixel = Line[i++]; /* Get next pixel from stream. */
/* Form a new unique key to search hash table for the code combines */
/* CrntCode as Prefix string with Pixel as postfix char. */
NewKey = (((unsigned long) CrntCode) << 8) + Pixel;
if ((NewCode = _ExistsHashTable(HashTable, NewKey)) >= 0) {
/* This Key is already there, or the string is old one, so */
/* simple take new code as our CrntCode: */
CrntCode = NewCode;
}
else {
/* Put it in hash table, output the prefix code, and make our */
/* CrntCode equal to Pixel. */
if (EGifCompressOutput(Private, CrntCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
CrntCode = Pixel;
/* If however the HashTable if full, we send a clear first and */
/* Clear the hash table. */
if (Private->RunningCode >= ZL_MAX_CODE) {
/* Time to do some clearance: */
if (EGifCompressOutput(Private, Private->ClearCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
Private->RunningCode = Private->EOFCode + 1;
Private->RunningBits = Private->BitsPerPixel + 1;
Private->MaxCode1 = 1 << Private->RunningBits;
_ClearHashTable(HashTable);
}
else {
/* Put this unique key with its relative Code in hash table: */
_InsertHashTable(HashTable, NewKey, Private->RunningCode++);
}
}
}
/* Preserve the current state of the compression algorithm: */
Private->CrntCode = CrntCode;
if (Private->PixelCount == 0)
{
/* We are done - output last Code and flush output buffers: */
if (EGifCompressOutput(Private, CrntCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
if (EGifCompressOutput(Private, Private->EOFCode)
== GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
if (EGifCompressOutput(Private, FLUSH_OUTPUT) == GIF_ERROR) {
_GifError = E_GIF_ERR_DISK_IS_FULL;
return GIF_ERROR;
}
}
return GIF_OK;
}
/******************************************************************************
* The LZ compression output routine: *
* This routine is responsable for the compression of the bit stream into *
* 8 bits (bytes) packets. *
* Returns GIF_OK if written succesfully. *
******************************************************************************/
static int EGifCompressOutput(GifFilePrivateType *Private, int Code)
{
int retval = GIF_OK;
if (Code == FLUSH_OUTPUT) {
while (Private->CrntShiftState > 0) {
/* Get Rid of what is left in DWord, and flush it. */
if (EGifBufferedOutput(Private->File, Private->Buf,
Private->CrntShiftDWord & 0xff) == GIF_ERROR)
retval = GIF_ERROR;
Private->CrntShiftDWord >>= 8;
Private->CrntShiftState -= 8;
}
Private->CrntShiftState = 0; /* For next time. */
if (EGifBufferedOutput(Private->File, Private->Buf,
FLUSH_OUTPUT) == GIF_ERROR)
retval = GIF_ERROR;
}
else {
Private->CrntShiftDWord |= ((long) Code) << Private->CrntShiftState;
Private->CrntShiftState += Private->RunningBits;
while (Private->CrntShiftState >= 8) {
/* Dump out full bytes: */
if (EGifBufferedOutput(Private->File, Private->Buf,
Private->CrntShiftDWord & 0xff) == GIF_ERROR)
retval = GIF_ERROR;
Private->CrntShiftDWord >>= 8;
Private->CrntShiftState -= 8;
}
}
/* If code cannt fit into RunningBits bits, must raise its size. Note */
/* however that codes above 4095 are used for special signaling. */
if (Private->RunningCode >= Private->MaxCode1 && Code <= 4095) {
Private->MaxCode1 = 1 << ++Private->RunningBits;
}
return retval;
}
/******************************************************************************
* This routines buffers the given characters until 255 characters are ready *
* to be output. If Code is equal to -1 the buffer is flushed (EOF). *
* The buffer is Dumped with first byte as its size, as GIF format requires. *
* Returns GIF_OK if written succesfully. *
******************************************************************************/
static int EGifBufferedOutput(FILE *File, GifByteType *Buf, int c)
{
if (c == FLUSH_OUTPUT) {
/* Flush everything out. */
if (Buf[0] != 0 && fwrite(Buf, 1, Buf[0]+1, File) != (unsigned)(Buf[0] + 1))
{
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
/* Mark end of compressed data, by an empty block (see GIF doc): */
Buf[0] = 0;
if (fwrite(Buf, 1, 1, File) != 1)
{
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
}
else {
if (Buf[0] == 255) {
/* Dump out this buffer - it is full: */
if (fwrite(Buf, 1, Buf[0] + 1, File) != (unsigned)(Buf[0] + 1))
{
_GifError = E_GIF_ERR_WRITE_FAILED;
return GIF_ERROR;
}
Buf[0] = 0;
}
Buf[++Buf[0]] = c;
}
return GIF_OK;
}
/******************************************************************************
* This routine writes to disk an in-core representation of a GIF previously *
* created by DGifSlurp(). *
******************************************************************************/
int EGifSpew(GifFileType *GifFileOut)
{
int i, j, gif89 = FALSE;
char *SavedStamp;
for (i = 0; i < GifFileOut->ImageCount; i++)
{
int function = GifFileOut->SavedImages[i].Function;
if (function == COMMENT_EXT_FUNC_CODE
|| function == GRAPHICS_EXT_FUNC_CODE
|| function == PLAINTEXT_EXT_FUNC_CODE
|| function == APPLICATION_EXT_FUNC_CODE)
gif89 = TRUE;
}
SavedStamp = GifVersionPrefix;
GifVersionPrefix = gif89 ? GIF89_STAMP : GIF87_STAMP;
if (EGifPutScreenDesc(GifFileOut,
GifFileOut->SWidth,
GifFileOut->SHeight,
GifFileOut->SColorResolution,
GifFileOut->SBackGroundColor,
GifFileOut->SColorMap) == GIF_ERROR)
{
GifVersionPrefix = SavedStamp;
return(GIF_ERROR);
}
GifVersionPrefix = SavedStamp;
for (i = 0; i < GifFileOut->ImageCount; i++)
{
SavedImage *sp = &GifFileOut->SavedImages[i];
int SavedHeight = sp->ImageDesc.Height;
int SavedWidth = sp->ImageDesc.Width;
ExtensionBlock *ep;
/* this allows us to delete images by nuking their rasters */
if (sp->RasterBits == NULL)
continue;
if (EGifPutImageDesc(GifFileOut,
sp->ImageDesc.Left,
sp->ImageDesc.Top,
SavedWidth,
SavedHeight,
sp->ImageDesc.Interlace,
sp->ImageDesc.ColorMap
) == GIF_ERROR)
return(GIF_ERROR);
for (j = 0; j < SavedHeight; j++)
{
if (EGifPutLine(GifFileOut,
sp->RasterBits + j * SavedWidth,
SavedWidth) == GIF_ERROR)
return(GIF_ERROR);
}
if (sp->ExtensionBlocks)
{
for (ep = sp->ExtensionBlocks;
ep < sp->ExtensionBlocks + sp->ExtensionBlockCount;
ep++)
if (EGifPutExtension(GifFileOut,
(ep == sp->ExtensionBlocks) ? sp->Function : '\0',
ep->ByteCount, ep->Bytes) == GIF_ERROR)
return(GIF_ERROR);
}
}
if (EGifCloseFile(GifFileOut) == GIF_ERROR)
return(GIF_ERROR);
return(GIF_OK);
}
|