| 1 |
/****************************************************************************** |
| 2 |
|
| 3 |
egif_lib.c - GIF encoding |
| 4 |
|
| 5 |
The functions here and in dgif_lib.c are partitioned carefully so that |
| 6 |
if you only require one of read and write capability, only one of these |
| 7 |
two modules will be linked. Preserve this property! |
| 8 |
|
| 9 |
*****************************************************************************/ |
| 10 |
// SPDX-License-Identifier: MIT |
| 11 |
// SPDX-FileCopyrightText: Copyright (C) Eric S. Raymond <[email protected]> |
| 12 |
|
| 13 |
#include <fcntl.h> |
| 14 |
#include <stdint.h> |
| 15 |
#include <stdio.h> |
| 16 |
#include <stdlib.h> |
| 17 |
#include <string.h> |
| 18 |
|
| 19 |
#ifdef _WIN32 |
| 20 |
#include <io.h> |
| 21 |
#else |
| 22 |
#include <sys/types.h> |
| 23 |
#include <unistd.h> |
| 24 |
#endif /* _WIN32 */ |
| 25 |
#include <sys/stat.h> |
| 26 |
|
| 27 |
#include "gif_lib.h" |
| 28 |
#include "gif_lib_private.h" |
| 29 |
|
| 30 |
/* Masks given codes to BitsPerPixel, to make sure all codes are in range: */ |
| 31 |
/*@+charint@*/ |
| 32 |
static const GifPixelType CodeMask[] = {0x00, 0x01, 0x03, 0x07, 0x0f, |
| 33 |
0x1f, 0x3f, 0x7f, 0xff}; |
| 34 |
/*@-charint@*/ |
| 35 |
|
| 36 |
static int EGifPutWord(int Word, GifFileType *GifFile); |
| 37 |
static int EGifSetupCompress(GifFileType *GifFile); |
| 38 |
static int EGifCompressLine(GifFileType *GifFile, const GifPixelType *Line, |
| 39 |
const int LineLen); |
| 40 |
static int EGifCompressOutput(GifFileType *GifFile, int Code); |
| 41 |
static int EGifBufferedOutput(GifFileType *GifFile, GifByteType *Buf, int c); |
| 42 |
|
| 43 |
/* extract bytes from an unsigned word */ |
| 44 |
#define LOBYTE(x) ((x)&0xff) |
| 45 |
#define HIBYTE(x) (((x) >> 8) & 0xff) |
| 46 |
|
| 47 |
#ifndef S_IREAD |
| 48 |
#define S_IREAD S_IRUSR |
| 49 |
#endif |
| 50 |
|
| 51 |
#ifndef S_IWRITE |
| 52 |
#define S_IWRITE S_IWUSR |
| 53 |
#endif |
| 54 |
/****************************************************************************** |
| 55 |
Open a new GIF file for write, specified by name. If TestExistance then |
| 56 |
if the file exists this routines fails (returns NULL). |
| 57 |
Returns a dynamically allocated GifFileType pointer which serves as the GIF |
| 58 |
info record. The Error member is cleared if successful. |
| 59 |
******************************************************************************/ |
| 60 |
GifFileType *EGifOpenFileName(const char *FileName, const bool TestExistence, |
| 61 |
int *Error) { |
| 62 |
|
| 63 |
int FileHandle; |
| 64 |
GifFileType *GifFile; |
| 65 |
|
| 66 |
if (TestExistence) { |
| 67 |
FileHandle = open(FileName, O_WRONLY | O_CREAT | O_EXCL, |
| 68 |
S_IREAD | S_IWRITE); |
| 69 |
} else { |
| 70 |
FileHandle = open(FileName, O_WRONLY | O_CREAT | O_TRUNC, |
| 71 |
S_IREAD | S_IWRITE); |
| 72 |
} |
| 73 |
|
| 74 |
if (FileHandle == -1) { |
| 75 |
if (Error != NULL) { |
| 76 |
*Error = E_GIF_ERR_OPEN_FAILED; |
| 77 |
} |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
GifFile = EGifOpenFileHandle(FileHandle, Error); |
| 81 |
if (GifFile == (GifFileType *)NULL) { |
| 82 |
(void)close(FileHandle); |
| 83 |
} |
| 84 |
return GifFile; |
| 85 |
} |
| 86 |
|
| 87 |
/****************************************************************************** |
| 88 |
Update a new GIF file, given its file handle, which must be opened for |
| 89 |
write in binary mode. |
| 90 |
Returns dynamically allocated a GifFileType pointer which serves as the GIF |
| 91 |
info record. |
| 92 |
Only fails on a memory allocation error. |
| 93 |
******************************************************************************/ |
| 94 |
GifFileType *EGifOpenFileHandle(const int FileHandle, int *Error) { |
| 95 |
GifFileType *GifFile; |
| 96 |
GifFilePrivateType *Private; |
| 97 |
FILE *f; |
| 98 |
|
| 99 |
GifFile = (GifFileType *)malloc(sizeof(GifFileType)); |
| 100 |
if (GifFile == NULL) { |
| 101 |
return NULL; |
| 102 |
} |
| 103 |
|
| 104 |
memset(GifFile, '\0', sizeof(GifFileType)); |
| 105 |
|
| 106 |
Private = (GifFilePrivateType *)malloc(sizeof(GifFilePrivateType)); |
| 107 |
if (Private == NULL) { |
| 108 |
free(GifFile); |
| 109 |
if (Error != NULL) { |
| 110 |
*Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 111 |
} |
| 112 |
return NULL; |
| 113 |
} |
| 114 |
/*@i1@*/ memset(Private, '\0', sizeof(GifFilePrivateType)); |
| 115 |
if ((Private->HashTable = _InitHashTable()) == NULL) { |
| 116 |
free(GifFile); |
| 117 |
free(Private); |
| 118 |
if (Error != NULL) { |
| 119 |
*Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 120 |
} |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
|
| 124 |
#ifdef _WIN32 |
| 125 |
_setmode(FileHandle, O_BINARY); /* Make sure it is in binary mode. */ |
| 126 |
#endif /* _WIN32 */ |
| 127 |
|
| 128 |
f = fdopen(FileHandle, "wb"); /* Make it into a stream: */ |
| 129 |
|
| 130 |
GifFile->Private = (void *)Private; |
| 131 |
Private->FileHandle = FileHandle; |
| 132 |
Private->File = f; |
| 133 |
Private->FileState = FILE_STATE_WRITE; |
| 134 |
Private->gif89 = false; |
| 135 |
|
| 136 |
Private->Write = (OutputFunc)0; /* No user write routine (MRB) */ |
| 137 |
GifFile->UserData = (void *)NULL; /* No user write handle (MRB) */ |
| 138 |
|
| 139 |
GifFile->Error = 0; |
| 140 |
|
| 141 |
return GifFile; |
| 142 |
} |
| 143 |
|
| 144 |
/****************************************************************************** |
| 145 |
Output constructor that takes user supplied output function. |
| 146 |
Basically just a copy of EGifOpenFileHandle. (MRB) |
| 147 |
******************************************************************************/ |
| 148 |
GifFileType *EGifOpen(void *userData, OutputFunc writeFunc, int *Error) { |
| 149 |
GifFileType *GifFile; |
| 150 |
GifFilePrivateType *Private; |
| 151 |
|
| 152 |
GifFile = (GifFileType *)malloc(sizeof(GifFileType)); |
| 153 |
if (GifFile == NULL) { |
| 154 |
if (Error != NULL) { |
| 155 |
*Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 156 |
} |
| 157 |
return NULL; |
| 158 |
} |
| 159 |
|
| 160 |
memset(GifFile, '\0', sizeof(GifFileType)); |
| 161 |
|
| 162 |
Private = (GifFilePrivateType *)malloc(sizeof(GifFilePrivateType)); |
| 163 |
if (Private == NULL) { |
| 164 |
free(GifFile); |
| 165 |
if (Error != NULL) { |
| 166 |
*Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 167 |
} |
| 168 |
return NULL; |
| 169 |
} |
| 170 |
|
| 171 |
memset(Private, '\0', sizeof(GifFilePrivateType)); |
| 172 |
|
| 173 |
Private->HashTable = _InitHashTable(); |
| 174 |
if (Private->HashTable == NULL) { |
| 175 |
free(GifFile); |
| 176 |
free(Private); |
| 177 |
if (Error != NULL) { |
| 178 |
*Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 179 |
} |
| 180 |
return NULL; |
| 181 |
} |
| 182 |
|
| 183 |
GifFile->Private = (void *)Private; |
| 184 |
Private->FileHandle = 0; |
| 185 |
Private->File = (FILE *)0; |
| 186 |
Private->FileState = FILE_STATE_WRITE; |
| 187 |
|
| 188 |
Private->Write = writeFunc; /* User write routine (MRB) */ |
| 189 |
GifFile->UserData = userData; /* User write handle (MRB) */ |
| 190 |
|
| 191 |
Private->gif89 = false; /* initially, write GIF87 */ |
| 192 |
|
| 193 |
GifFile->Error = 0; |
| 194 |
|
| 195 |
return GifFile; |
| 196 |
} |
| 197 |
|
| 198 |
/****************************************************************************** |
| 199 |
Routine to compute the GIF version that will be written on output. |
| 200 |
******************************************************************************/ |
| 201 |
const char *EGifGetGifVersion(GifFileType *GifFile) { |
| 202 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 203 |
int i, j; |
| 204 |
|
| 205 |
/* |
| 206 |
* Bulletproofing - always write GIF89 if we need to. |
| 207 |
* Note, we don't clear the gif89 flag here because |
| 208 |
* users of the sequential API might have called EGifSetGifVersion() |
| 209 |
* in order to set that flag. |
| 210 |
*/ |
| 211 |
for (i = 0; i < GifFile->ImageCount; i++) { |
| 212 |
for (j = 0; j < GifFile->SavedImages[i].ExtensionBlockCount; |
| 213 |
j++) { |
| 214 |
int function = |
| 215 |
GifFile->SavedImages[i].ExtensionBlocks[j].Function; |
| 216 |
|
| 217 |
if (function == COMMENT_EXT_FUNC_CODE || |
| 218 |
function == GRAPHICS_EXT_FUNC_CODE || |
| 219 |
function == PLAINTEXT_EXT_FUNC_CODE || |
| 220 |
function == APPLICATION_EXT_FUNC_CODE) { |
| 221 |
Private->gif89 = true; |
| 222 |
} |
| 223 |
} |
| 224 |
} |
| 225 |
for (i = 0; i < GifFile->ExtensionBlockCount; i++) { |
| 226 |
int function = GifFile->ExtensionBlocks[i].Function; |
| 227 |
|
| 228 |
if (function == COMMENT_EXT_FUNC_CODE || |
| 229 |
function == GRAPHICS_EXT_FUNC_CODE || |
| 230 |
function == PLAINTEXT_EXT_FUNC_CODE || |
| 231 |
function == APPLICATION_EXT_FUNC_CODE) { |
| 232 |
Private->gif89 = true; |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
if (Private->gif89) { |
| 237 |
return GIF89_STAMP; |
| 238 |
} else { |
| 239 |
return GIF87_STAMP; |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
/****************************************************************************** |
| 244 |
Set the GIF version. In the extremely unlikely event that there is ever |
| 245 |
another version, replace the bool argument with an enum in which the |
| 246 |
GIF87 value is 0 (numerically the same as bool false) and the GIF89 value |
| 247 |
is 1 (numerically the same as bool true). That way we'll even preserve |
| 248 |
object-file compatibility! |
| 249 |
******************************************************************************/ |
| 250 |
void EGifSetGifVersion(GifFileType *GifFile, const bool gif89) { |
| 251 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 252 |
|
| 253 |
Private->gif89 = gif89; |
| 254 |
} |
| 255 |
|
| 256 |
/****************************************************************************** |
| 257 |
All writes to the GIF should go through this. |
| 258 |
******************************************************************************/ |
| 259 |
static int InternalWrite(GifFileType *GifFileOut, const unsigned char *buf, |
| 260 |
size_t len) { |
| 261 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFileOut->Private; |
| 262 |
if (Private->Write) { |
| 263 |
return Private->Write(GifFileOut, buf, len); |
| 264 |
} else { |
| 265 |
return fwrite(buf, 1, len, Private->File); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
/****************************************************************************** |
| 270 |
This routine should be called before any other EGif calls, immediately |
| 271 |
following the GIF file opening. |
| 272 |
******************************************************************************/ |
| 273 |
int EGifPutScreenDesc(GifFileType *GifFile, const int Width, const int Height, |
| 274 |
const int ColorRes, const int BackGround, |
| 275 |
const ColorMapObject *ColorMap) { |
| 276 |
GifByteType Buf[3]; |
| 277 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 278 |
const char *write_version; |
| 279 |
GifFile->SColorMap = NULL; |
| 280 |
|
| 281 |
if (Private->FileState & FILE_STATE_SCREEN) { |
| 282 |
/* If already has screen descriptor - something is wrong! */ |
| 283 |
GifFile->Error = E_GIF_ERR_HAS_SCRN_DSCR; |
| 284 |
return GIF_ERROR; |
| 285 |
} |
| 286 |
if (!IS_WRITEABLE(Private)) { |
| 287 |
/* This file was NOT open for writing: */ |
| 288 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 289 |
return GIF_ERROR; |
| 290 |
} |
| 291 |
|
| 292 |
write_version = EGifGetGifVersion(GifFile); |
| 293 |
|
| 294 |
/* First write the version prefix into the file. */ |
| 295 |
if (InternalWrite(GifFile, (unsigned char *)write_version, |
| 296 |
strlen(write_version)) != strlen(write_version)) { |
| 297 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 298 |
return GIF_ERROR; |
| 299 |
} |
| 300 |
|
| 301 |
GifFile->SWidth = Width; |
| 302 |
GifFile->SHeight = Height; |
| 303 |
GifFile->SColorResolution = ColorRes; |
| 304 |
GifFile->SBackGroundColor = BackGround; |
| 305 |
if (ColorMap) { |
| 306 |
GifFile->SColorMap = |
| 307 |
GifMakeMapObject(ColorMap->ColorCount, ColorMap->Colors); |
| 308 |
if (GifFile->SColorMap == NULL) { |
| 309 |
GifFile->Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 310 |
return GIF_ERROR; |
| 311 |
} |
| 312 |
} else { |
| 313 |
GifFile->SColorMap = NULL; |
| 314 |
} |
| 315 |
|
| 316 |
/* |
| 317 |
* Put the logical screen descriptor into the file: |
| 318 |
*/ |
| 319 |
/* Logical Screen Descriptor: Dimensions */ |
| 320 |
(void)EGifPutWord(Width, GifFile); |
| 321 |
(void)EGifPutWord(Height, GifFile); |
| 322 |
|
| 323 |
/* Logical Screen Descriptor: Packed Fields */ |
| 324 |
/* Note: We have actual size of the color table default to the largest |
| 325 |
* possible size (7+1 == 8 bits) because the decoder can use it to |
| 326 |
* decide how to display the files. |
| 327 |
*/ |
| 328 |
Buf[0] = |
| 329 |
(ColorMap ? 0x80 : 0x00) | /* Yes/no global colormap */ |
| 330 |
((ColorRes - 1) << 4) | /* Bits allocated to each primary color */ |
| 331 |
(ColorMap ? ColorMap->BitsPerPixel - 1 |
| 332 |
: 0x07); /* Actual size of the |
| 333 |
color table. */ |
| 334 |
if (ColorMap != NULL && ColorMap->SortFlag) { |
| 335 |
Buf[0] |= 0x08; |
| 336 |
} |
| 337 |
Buf[1] = |
| 338 |
BackGround; /* Index into the ColorTable for background color */ |
| 339 |
Buf[2] = GifFile->AspectByte; /* Pixel Aspect Ratio */ |
| 340 |
InternalWrite(GifFile, Buf, 3); |
| 341 |
|
| 342 |
/* If we have Global color map - dump it also: */ |
| 343 |
if (ColorMap != NULL) { |
| 344 |
int i; |
| 345 |
for (i = 0; i < ColorMap->ColorCount; i++) { |
| 346 |
/* Put the ColorMap out also: */ |
| 347 |
Buf[0] = ColorMap->Colors[i].Red; |
| 348 |
Buf[1] = ColorMap->Colors[i].Green; |
| 349 |
Buf[2] = ColorMap->Colors[i].Blue; |
| 350 |
if (InternalWrite(GifFile, Buf, 3) != 3) { |
| 351 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 352 |
return GIF_ERROR; |
| 353 |
} |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
/* Mark this file as has screen descriptor, and no pixel written yet: */ |
| 358 |
Private->FileState |= FILE_STATE_SCREEN; |
| 359 |
|
| 360 |
return GIF_OK; |
| 361 |
} |
| 362 |
|
| 363 |
/****************************************************************************** |
| 364 |
This routine should be called before any attempt to dump an image - any |
| 365 |
call to any of the pixel dump routines. |
| 366 |
******************************************************************************/ |
| 367 |
int EGifPutImageDesc(GifFileType *GifFile, const int Left, const int Top, |
| 368 |
const int Width, const int Height, const bool Interlace, |
| 369 |
const ColorMapObject *ColorMap) { |
| 370 |
GifByteType Buf[3]; |
| 371 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 372 |
|
| 373 |
if (Private->FileState & FILE_STATE_IMAGE && |
| 374 |
Private->PixelCount > 0xffff0000UL) { |
| 375 |
/* If already has active image descriptor - something is wrong! |
| 376 |
*/ |
| 377 |
GifFile->Error = E_GIF_ERR_HAS_IMAG_DSCR; |
| 378 |
return GIF_ERROR; |
| 379 |
} |
| 380 |
if (!IS_WRITEABLE(Private)) { |
| 381 |
/* This file was NOT open for writing: */ |
| 382 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 383 |
return GIF_ERROR; |
| 384 |
} |
| 385 |
GifFile->Image.Left = Left; |
| 386 |
GifFile->Image.Top = Top; |
| 387 |
GifFile->Image.Width = Width; |
| 388 |
GifFile->Image.Height = Height; |
| 389 |
GifFile->Image.Interlace = Interlace; |
| 390 |
if (ColorMap != GifFile->Image.ColorMap) { |
| 391 |
if (ColorMap) { |
| 392 |
if (GifFile->Image.ColorMap != NULL) { |
| 393 |
GifFreeMapObject(GifFile->Image.ColorMap); |
| 394 |
GifFile->Image.ColorMap = NULL; |
| 395 |
} |
| 396 |
GifFile->Image.ColorMap = GifMakeMapObject( |
| 397 |
ColorMap->ColorCount, ColorMap->Colors); |
| 398 |
if (GifFile->Image.ColorMap == NULL) { |
| 399 |
GifFile->Error = E_GIF_ERR_NOT_ENOUGH_MEM; |
| 400 |
return GIF_ERROR; |
| 401 |
} |
| 402 |
} else { |
| 403 |
GifFile->Image.ColorMap = NULL; |
| 404 |
} |
| 405 |
} |
| 406 |
|
| 407 |
/* Put the image descriptor into the file: */ |
| 408 |
Buf[0] = DESCRIPTOR_INTRODUCER; /* Image separator character. */ |
| 409 |
InternalWrite(GifFile, Buf, 1); |
| 410 |
(void)EGifPutWord(Left, GifFile); |
| 411 |
(void)EGifPutWord(Top, GifFile); |
| 412 |
(void)EGifPutWord(Width, GifFile); |
| 413 |
(void)EGifPutWord(Height, GifFile); |
| 414 |
Buf[0] = (ColorMap ? 0x80 : 0x00) | (Interlace ? 0x40 : 0x00) | |
| 415 |
(ColorMap ? ColorMap->BitsPerPixel - 1 : 0); |
| 416 |
InternalWrite(GifFile, Buf, 1); |
| 417 |
|
| 418 |
/* If we have Global color map - dump it also: */ |
| 419 |
if (ColorMap != NULL) { |
| 420 |
int i; |
| 421 |
for (i = 0; i < ColorMap->ColorCount; i++) { |
| 422 |
/* Put the ColorMap out also: */ |
| 423 |
Buf[0] = ColorMap->Colors[i].Red; |
| 424 |
Buf[1] = ColorMap->Colors[i].Green; |
| 425 |
Buf[2] = ColorMap->Colors[i].Blue; |
| 426 |
if (InternalWrite(GifFile, Buf, 3) != 3) { |
| 427 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 428 |
return GIF_ERROR; |
| 429 |
} |
| 430 |
} |
| 431 |
} |
| 432 |
if (GifFile->SColorMap == NULL && GifFile->Image.ColorMap == NULL) { |
| 433 |
GifFile->Error = E_GIF_ERR_NO_COLOR_MAP; |
| 434 |
return GIF_ERROR; |
| 435 |
} |
| 436 |
|
| 437 |
/* Mark this file as has screen descriptor: */ |
| 438 |
Private->FileState |= FILE_STATE_IMAGE; |
| 439 |
Private->PixelCount = (long)Width * (long)Height; |
| 440 |
|
| 441 |
/* Reset compress algorithm parameters. */ |
| 442 |
(void)EGifSetupCompress(GifFile); |
| 443 |
|
| 444 |
return GIF_OK; |
| 445 |
} |
| 446 |
|
| 447 |
/****************************************************************************** |
| 448 |
Put one full scanned line (Line) of length LineLen into GIF file. |
| 449 |
******************************************************************************/ |
| 450 |
int EGifPutLine(GifFileType *GifFile, GifPixelType *Line, int LineLen) { |
| 451 |
int i; |
| 452 |
GifPixelType Mask; |
| 453 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 454 |
|
| 455 |
if (!IS_WRITEABLE(Private)) { |
| 456 |
/* This file was NOT open for writing: */ |
| 457 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 458 |
return GIF_ERROR; |
| 459 |
} |
| 460 |
|
| 461 |
if (!LineLen) { |
| 462 |
LineLen = GifFile->Image.Width; |
| 463 |
} |
| 464 |
if (Private->PixelCount < (unsigned)LineLen) { |
| 465 |
GifFile->Error = E_GIF_ERR_DATA_TOO_BIG; |
| 466 |
return GIF_ERROR; |
| 467 |
} |
| 468 |
Private->PixelCount -= LineLen; |
| 469 |
|
| 470 |
/* Make sure the codes are not out of bit range, as we might generate |
| 471 |
* wrong code (because of overflow when we combine them) in this case: |
| 472 |
*/ |
| 473 |
Mask = CodeMask[Private->BitsPerPixel]; |
| 474 |
for (i = 0; i < LineLen; i++) { |
| 475 |
Line[i] &= Mask; |
| 476 |
} |
| 477 |
|
| 478 |
return EGifCompressLine(GifFile, Line, LineLen); |
| 479 |
} |
| 480 |
|
| 481 |
/****************************************************************************** |
| 482 |
Put one pixel (Pixel) into GIF file. |
| 483 |
******************************************************************************/ |
| 484 |
int EGifPutPixel(GifFileType *GifFile, GifPixelType Pixel) { |
| 485 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 486 |
|
| 487 |
if (!IS_WRITEABLE(Private)) { |
| 488 |
/* This file was NOT open for writing: */ |
| 489 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 490 |
return GIF_ERROR; |
| 491 |
} |
| 492 |
|
| 493 |
if (Private->PixelCount == 0) { |
| 494 |
GifFile->Error = E_GIF_ERR_DATA_TOO_BIG; |
| 495 |
return GIF_ERROR; |
| 496 |
} |
| 497 |
--Private->PixelCount; |
| 498 |
|
| 499 |
/* Make sure the code is not out of bit range, as we might generate |
| 500 |
* wrong code (because of overflow when we combine them) in this case: |
| 501 |
*/ |
| 502 |
Pixel &= CodeMask[Private->BitsPerPixel]; |
| 503 |
|
| 504 |
return EGifCompressLine(GifFile, &Pixel, 1); |
| 505 |
} |
| 506 |
|
| 507 |
/****************************************************************************** |
| 508 |
Put a comment into GIF file using the GIF89 comment extension block. |
| 509 |
******************************************************************************/ |
| 510 |
int EGifPutComment(GifFileType *GifFile, const char *Comment) { |
| 511 |
unsigned int length; |
| 512 |
char *buf; |
| 513 |
|
| 514 |
length = strlen(Comment); |
| 515 |
if (length <= 255) { |
| 516 |
return EGifPutExtension(GifFile, COMMENT_EXT_FUNC_CODE, length, |
| 517 |
Comment); |
| 518 |
} else { |
| 519 |
buf = (char *)Comment; |
| 520 |
if (EGifPutExtensionLeader(GifFile, COMMENT_EXT_FUNC_CODE) == |
| 521 |
GIF_ERROR) { |
| 522 |
return GIF_ERROR; |
| 523 |
} |
| 524 |
|
| 525 |
/* Break the comment into 255 byte sub blocks */ |
| 526 |
while (length > 255) { |
| 527 |
if (EGifPutExtensionBlock(GifFile, 255, buf) == |
| 528 |
GIF_ERROR) { |
| 529 |
return GIF_ERROR; |
| 530 |
} |
| 531 |
buf = buf + 255; |
| 532 |
length -= 255; |
| 533 |
} |
| 534 |
/* Output any partial block and the clear code. */ |
| 535 |
if (length > 0) { |
| 536 |
if (EGifPutExtensionBlock(GifFile, length, buf) == |
| 537 |
GIF_ERROR) { |
| 538 |
return GIF_ERROR; |
| 539 |
} |
| 540 |
} |
| 541 |
if (EGifPutExtensionTrailer(GifFile) == GIF_ERROR) { |
| 542 |
return GIF_ERROR; |
| 543 |
} |
| 544 |
} |
| 545 |
return GIF_OK; |
| 546 |
} |
| 547 |
|
| 548 |
/****************************************************************************** |
| 549 |
Begin an extension block (see GIF manual). More |
| 550 |
extensions can be dumped using EGifPutExtensionBlock until |
| 551 |
EGifPutExtensionTrailer is invoked. |
| 552 |
******************************************************************************/ |
| 553 |
int EGifPutExtensionLeader(GifFileType *GifFile, const int ExtCode) { |
| 554 |
GifByteType Buf[3]; |
| 555 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 556 |
|
| 557 |
if (!IS_WRITEABLE(Private)) { |
| 558 |
/* This file was NOT open for writing: */ |
| 559 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 560 |
return GIF_ERROR; |
| 561 |
} |
| 562 |
|
| 563 |
Buf[0] = EXTENSION_INTRODUCER; |
| 564 |
Buf[1] = ExtCode; |
| 565 |
InternalWrite(GifFile, Buf, 2); |
| 566 |
|
| 567 |
return GIF_OK; |
| 568 |
} |
| 569 |
|
| 570 |
/****************************************************************************** |
| 571 |
Put extension block data (see GIF manual) into a GIF file. |
| 572 |
******************************************************************************/ |
| 573 |
int EGifPutExtensionBlock(GifFileType *GifFile, const int ExtLen, |
| 574 |
const void *Extension) { |
| 575 |
GifByteType Buf; |
| 576 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 577 |
|
| 578 |
if (!IS_WRITEABLE(Private)) { |
| 579 |
/* This file was NOT open for writing: */ |
| 580 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 581 |
return GIF_ERROR; |
| 582 |
} |
| 583 |
|
| 584 |
Buf = ExtLen; |
| 585 |
InternalWrite(GifFile, &Buf, 1); |
| 586 |
InternalWrite(GifFile, Extension, ExtLen); |
| 587 |
|
| 588 |
return GIF_OK; |
| 589 |
} |
| 590 |
|
| 591 |
/****************************************************************************** |
| 592 |
Put a terminating block (see GIF manual) into a GIF file. |
| 593 |
******************************************************************************/ |
| 594 |
int EGifPutExtensionTrailer(GifFileType *GifFile) { |
| 595 |
|
| 596 |
GifByteType Buf; |
| 597 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 598 |
|
| 599 |
if (!IS_WRITEABLE(Private)) { |
| 600 |
/* This file was NOT open for writing: */ |
| 601 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 602 |
return GIF_ERROR; |
| 603 |
} |
| 604 |
|
| 605 |
/* Write the block terminator */ |
| 606 |
Buf = 0; |
| 607 |
InternalWrite(GifFile, &Buf, 1); |
| 608 |
|
| 609 |
return GIF_OK; |
| 610 |
} |
| 611 |
|
| 612 |
/****************************************************************************** |
| 613 |
Put an extension block (see GIF manual) into a GIF file. |
| 614 |
Warning: This function is only useful for Extension blocks that have at |
| 615 |
most one subblock. Extensions with more than one subblock need to use the |
| 616 |
EGifPutExtension{Leader,Block,Trailer} functions instead. |
| 617 |
******************************************************************************/ |
| 618 |
int EGifPutExtension(GifFileType *GifFile, const int ExtCode, const int ExtLen, |
| 619 |
const void *Extension) { |
| 620 |
|
| 621 |
GifByteType Buf[3]; |
| 622 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 623 |
|
| 624 |
if (!IS_WRITEABLE(Private)) { |
| 625 |
/* This file was NOT open for writing: */ |
| 626 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 627 |
return GIF_ERROR; |
| 628 |
} |
| 629 |
|
| 630 |
if (ExtCode == 0) { |
| 631 |
InternalWrite(GifFile, (GifByteType *)&ExtLen, 1); |
| 632 |
} else { |
| 633 |
Buf[0] = EXTENSION_INTRODUCER; |
| 634 |
Buf[1] = ExtCode; /* Extension Label */ |
| 635 |
Buf[2] = ExtLen; /* Extension length */ |
| 636 |
InternalWrite(GifFile, Buf, 3); |
| 637 |
} |
| 638 |
InternalWrite(GifFile, Extension, ExtLen); |
| 639 |
Buf[0] = 0; |
| 640 |
InternalWrite(GifFile, Buf, 1); |
| 641 |
|
| 642 |
return GIF_OK; |
| 643 |
} |
| 644 |
|
| 645 |
/****************************************************************************** |
| 646 |
Render a Graphics Control Block as raw extension data |
| 647 |
******************************************************************************/ |
| 648 |
|
| 649 |
size_t EGifGCBToExtension(const GraphicsControlBlock *GCB, |
| 650 |
GifByteType *GifExtension) { |
| 651 |
GifExtension[0] = 0; |
| 652 |
GifExtension[0] |= |
| 653 |
(GCB->TransparentColor == NO_TRANSPARENT_COLOR) ? 0x00 : 0x01; |
| 654 |
GifExtension[0] |= GCB->UserInputFlag ? 0x02 : 0x00; |
| 655 |
GifExtension[0] |= ((GCB->DisposalMode & 0x07) << 2); |
| 656 |
GifExtension[1] = LOBYTE(GCB->DelayTime); |
| 657 |
GifExtension[2] = HIBYTE(GCB->DelayTime); |
| 658 |
GifExtension[3] = (char)GCB->TransparentColor; |
| 659 |
return 4; |
| 660 |
} |
| 661 |
|
| 662 |
/****************************************************************************** |
| 663 |
Replace the Graphics Control Block for a saved image, if it exists. |
| 664 |
******************************************************************************/ |
| 665 |
|
| 666 |
int EGifGCBToSavedExtension(const GraphicsControlBlock *GCB, |
| 667 |
GifFileType *GifFile, int ImageIndex) { |
| 668 |
int i; |
| 669 |
size_t Len; |
| 670 |
GifByteType buf[sizeof(GraphicsControlBlock)]; /* a bit dodgy... */ |
| 671 |
|
| 672 |
if (ImageIndex < 0 || ImageIndex > GifFile->ImageCount - 1) { |
| 673 |
return GIF_ERROR; |
| 674 |
} |
| 675 |
|
| 676 |
for (i = 0; i < GifFile->SavedImages[ImageIndex].ExtensionBlockCount; |
| 677 |
i++) { |
| 678 |
ExtensionBlock *ep = |
| 679 |
&GifFile->SavedImages[ImageIndex].ExtensionBlocks[i]; |
| 680 |
if (ep->Function == GRAPHICS_EXT_FUNC_CODE) { |
| 681 |
/* A Graphics Control Block is always 4 bytes. If |
| 682 |
* the existing block came from a malformed GIF and |
| 683 |
* is shorter, resize it so EGifGCBToExtension does |
| 684 |
* not write past the end of ep->Bytes. */ |
| 685 |
if (ep->ByteCount != 4) { |
| 686 |
GifByteType *new_bytes = |
| 687 |
(GifByteType *)realloc(ep->Bytes, 4); |
| 688 |
if (new_bytes == NULL) { |
| 689 |
return GIF_ERROR; |
| 690 |
} |
| 691 |
ep->Bytes = new_bytes; |
| 692 |
ep->ByteCount = 4; |
| 693 |
} |
| 694 |
|
| 695 |
EGifGCBToExtension(GCB, ep->Bytes); |
| 696 |
return GIF_OK; |
| 697 |
} |
| 698 |
} |
| 699 |
|
| 700 |
Len = EGifGCBToExtension(GCB, (GifByteType *)buf); |
| 701 |
if (GifAddExtensionBlock( |
| 702 |
&GifFile->SavedImages[ImageIndex].ExtensionBlockCount, |
| 703 |
&GifFile->SavedImages[ImageIndex].ExtensionBlocks, |
| 704 |
GRAPHICS_EXT_FUNC_CODE, Len, |
| 705 |
(unsigned char *)buf) == GIF_ERROR) { |
| 706 |
return (GIF_ERROR); |
| 707 |
} |
| 708 |
|
| 709 |
return (GIF_OK); |
| 710 |
} |
| 711 |
|
| 712 |
/****************************************************************************** |
| 713 |
Put the image code in compressed form. This routine can be called if the |
| 714 |
information needed to be piped out as is. Obviously this is much faster |
| 715 |
than decoding and encoding again. This routine should be followed by calls |
| 716 |
to EGifPutCodeNext, until NULL block is given. |
| 717 |
The block should NOT be freed by the user (not dynamically allocated). |
| 718 |
******************************************************************************/ |
| 719 |
int EGifPutCode(GifFileType *GifFile, int CodeSize, |
| 720 |
const GifByteType *CodeBlock) { |
| 721 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 722 |
|
| 723 |
if (!IS_WRITEABLE(Private)) { |
| 724 |
/* This file was NOT open for writing: */ |
| 725 |
GifFile->Error = E_GIF_ERR_NOT_WRITEABLE; |
| 726 |
return GIF_ERROR; |
| 727 |
} |
| 728 |
|
| 729 |
/* No need to dump code size as Compression set up does any for us: */ |
| 730 |
/* |
| 731 |
* Buf = CodeSize; |
| 732 |
* if (InternalWrite(GifFile, &Buf, 1) != 1) { |
| 733 |
* GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 734 |
* return GIF_ERROR; |
| 735 |
* } |
| 736 |
*/ |
| 737 |
|
| 738 |
return EGifPutCodeNext(GifFile, CodeBlock); |
| 739 |
} |
| 740 |
|
| 741 |
/****************************************************************************** |
| 742 |
Continue to put the image code in compressed form. This routine should be |
| 743 |
called with blocks of code as read via DGifGetCode/DGifGetCodeNext. If |
| 744 |
given buffer pointer is NULL, empty block is written to mark end of code. |
| 745 |
******************************************************************************/ |
| 746 |
int EGifPutCodeNext(GifFileType *GifFile, const GifByteType *CodeBlock) { |
| 747 |
GifByteType Buf; |
| 748 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 749 |
|
| 750 |
if (CodeBlock != NULL) { |
| 751 |
if (InternalWrite(GifFile, CodeBlock, CodeBlock[0] + 1) != |
| 752 |
(unsigned)(CodeBlock[0] + 1)) { |
| 753 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 754 |
return GIF_ERROR; |
| 755 |
} |
| 756 |
} else { |
| 757 |
Buf = 0; |
| 758 |
if (InternalWrite(GifFile, &Buf, 1) != 1) { |
| 759 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 760 |
return GIF_ERROR; |
| 761 |
} |
| 762 |
Private->PixelCount = |
| 763 |
0; /* And local info. indicate image read. */ |
| 764 |
} |
| 765 |
|
| 766 |
return GIF_OK; |
| 767 |
} |
| 768 |
|
| 769 |
/****************************************************************************** |
| 770 |
This routine should be called last, to close the GIF file. |
| 771 |
******************************************************************************/ |
| 772 |
int EGifCloseFile(GifFileType *GifFile, int *ErrorCode) { |
| 773 |
GifByteType Buf; |
| 774 |
GifFilePrivateType *Private; |
| 775 |
FILE *File; |
| 776 |
|
| 777 |
if (GifFile == NULL) { |
| 778 |
return GIF_ERROR; |
| 779 |
} |
| 780 |
|
| 781 |
Private = (GifFilePrivateType *)GifFile->Private; |
| 782 |
if (Private == NULL) { |
| 783 |
return GIF_ERROR; |
| 784 |
} else if (!IS_WRITEABLE(Private)) { |
| 785 |
/* This file was NOT open for writing: */ |
| 786 |
if (ErrorCode != NULL) { |
| 787 |
*ErrorCode = E_GIF_ERR_NOT_WRITEABLE; |
| 788 |
} |
| 789 |
free(GifFile); |
| 790 |
return GIF_ERROR; |
| 791 |
} else { |
| 792 |
File = Private->File; |
| 793 |
|
| 794 |
Buf = TERMINATOR_INTRODUCER; |
| 795 |
InternalWrite(GifFile, &Buf, 1); |
| 796 |
|
| 797 |
if (GifFile->Image.ColorMap) { |
| 798 |
GifFreeMapObject(GifFile->Image.ColorMap); |
| 799 |
GifFile->Image.ColorMap = NULL; |
| 800 |
} |
| 801 |
if (GifFile->SColorMap) { |
| 802 |
GifFreeMapObject(GifFile->SColorMap); |
| 803 |
GifFile->SColorMap = NULL; |
| 804 |
} |
| 805 |
if (Private->HashTable) { |
| 806 |
free((char *)Private->HashTable); |
| 807 |
} |
| 808 |
free((char *)Private); |
| 809 |
|
| 810 |
if (File && fclose(File) != 0) { |
| 811 |
if (ErrorCode != NULL) { |
| 812 |
*ErrorCode = E_GIF_ERR_CLOSE_FAILED; |
| 813 |
} |
| 814 |
free(GifFile); |
| 815 |
return GIF_ERROR; |
| 816 |
} |
| 817 |
|
| 818 |
free(GifFile); |
| 819 |
if (ErrorCode != NULL) { |
| 820 |
*ErrorCode = E_GIF_SUCCEEDED; |
| 821 |
} |
| 822 |
} |
| 823 |
return GIF_OK; |
| 824 |
} |
| 825 |
|
| 826 |
/****************************************************************************** |
| 827 |
Put 2 bytes (a word) into the given file in little-endian order: |
| 828 |
******************************************************************************/ |
| 829 |
static int EGifPutWord(int Word, GifFileType *GifFile) { |
| 830 |
unsigned char c[2]; |
| 831 |
|
| 832 |
c[0] = LOBYTE(Word); |
| 833 |
c[1] = HIBYTE(Word); |
| 834 |
if (InternalWrite(GifFile, c, 2) == 2) { |
| 835 |
return GIF_OK; |
| 836 |
} else { |
| 837 |
return GIF_ERROR; |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
/****************************************************************************** |
| 842 |
Setup the LZ compression for this image: |
| 843 |
******************************************************************************/ |
| 844 |
static int EGifSetupCompress(GifFileType *GifFile) { |
| 845 |
int BitsPerPixel; |
| 846 |
GifByteType Buf; |
| 847 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 848 |
|
| 849 |
/* Test and see what color map to use, and from it # bits per pixel: */ |
| 850 |
if (GifFile->Image.ColorMap) { |
| 851 |
BitsPerPixel = GifFile->Image.ColorMap->BitsPerPixel; |
| 852 |
} else if (GifFile->SColorMap) { |
| 853 |
BitsPerPixel = GifFile->SColorMap->BitsPerPixel; |
| 854 |
} else { |
| 855 |
GifFile->Error = E_GIF_ERR_NO_COLOR_MAP; |
| 856 |
return GIF_ERROR; |
| 857 |
} |
| 858 |
|
| 859 |
Buf = BitsPerPixel = (BitsPerPixel < 2 ? 2 : BitsPerPixel); |
| 860 |
InternalWrite(GifFile, &Buf, 1); /* Write the Code size to file. */ |
| 861 |
|
| 862 |
Private->Buf[0] = 0; /* Nothing was output yet. */ |
| 863 |
Private->BitsPerPixel = BitsPerPixel; |
| 864 |
Private->ClearCode = (1 << BitsPerPixel); |
| 865 |
Private->EOFCode = Private->ClearCode + 1; |
| 866 |
Private->RunningCode = Private->EOFCode + 1; |
| 867 |
Private->RunningBits = BitsPerPixel + 1; /* Number of bits per code. */ |
| 868 |
Private->MaxCode1 = 1 << Private->RunningBits; /* Max. code + 1. */ |
| 869 |
Private->CrntCode = FIRST_CODE; /* Signal that this is first one! */ |
| 870 |
Private->CrntShiftState = 0; /* No information in CrntShiftDWord. */ |
| 871 |
Private->CrntShiftDWord = 0; |
| 872 |
|
| 873 |
/* Clear hash table and send Clear to make sure the decoder do the same. |
| 874 |
*/ |
| 875 |
_ClearHashTable(Private->HashTable); |
| 876 |
|
| 877 |
if (EGifCompressOutput(GifFile, Private->ClearCode) == GIF_ERROR) { |
| 878 |
GifFile->Error = E_GIF_ERR_DISK_IS_FULL; |
| 879 |
return GIF_ERROR; |
| 880 |
} |
| 881 |
return GIF_OK; |
| 882 |
} |
| 883 |
|
| 884 |
/****************************************************************************** |
| 885 |
The LZ compression routine: |
| 886 |
This version compresses the given buffer Line of length LineLen. |
| 887 |
This routine can be called a few times (one per scan line, for example), in |
| 888 |
order to complete the whole image. |
| 889 |
******************************************************************************/ |
| 890 |
static int EGifCompressLine(GifFileType *GifFile, const GifPixelType *Line, |
| 891 |
const int LineLen) { |
| 892 |
int i = 0, CrntCode; |
| 893 |
GifHashTableType *HashTable; |
| 894 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 895 |
|
| 896 |
HashTable = Private->HashTable; |
| 897 |
|
| 898 |
if (Private->CrntCode == FIRST_CODE) { /* Its first time! */ |
| 899 |
CrntCode = Line[i++]; |
| 900 |
} else { |
| 901 |
CrntCode = |
| 902 |
Private->CrntCode; /* Get last code in compression. */ |
| 903 |
} |
| 904 |
while (i < LineLen) { /* Decode LineLen items. */ |
| 905 |
GifPixelType Pixel = |
| 906 |
Line[i++]; /* Get next pixel from stream. */ |
| 907 |
/* Form a new unique key to search hash table for the code |
| 908 |
* combines CrntCode as Prefix string with Pixel as postfix |
| 909 |
* char. |
| 910 |
*/ |
| 911 |
int NewCode; |
| 912 |
unsigned long NewKey = (((uint32_t)CrntCode) << 8) + Pixel; |
| 913 |
if ((NewCode = _ExistsHashTable(HashTable, NewKey)) >= 0) { |
| 914 |
/* This Key is already there, or the string is old one, |
| 915 |
* so simple take new code as our CrntCode: |
| 916 |
*/ |
| 917 |
CrntCode = NewCode; |
| 918 |
} else { |
| 919 |
/* Put it in hash table, output the prefix code, and |
| 920 |
* make our CrntCode equal to Pixel. |
| 921 |
*/ |
| 922 |
if (EGifCompressOutput(GifFile, CrntCode) == |
| 923 |
GIF_ERROR) { |
| 924 |
GifFile->Error = E_GIF_ERR_DISK_IS_FULL; |
| 925 |
return GIF_ERROR; |
| 926 |
} |
| 927 |
CrntCode = Pixel; |
| 928 |
|
| 929 |
/* If however the HashTable if full, we send a clear |
| 930 |
* first and Clear the hash table. |
| 931 |
*/ |
| 932 |
if (Private->RunningCode >= LZ_MAX_CODE) { |
| 933 |
/* Time to do some clearance: */ |
| 934 |
if (EGifCompressOutput(GifFile, |
| 935 |
Private->ClearCode) == |
| 936 |
GIF_ERROR) { |
| 937 |
GifFile->Error = E_GIF_ERR_DISK_IS_FULL; |
| 938 |
return GIF_ERROR; |
| 939 |
} |
| 940 |
Private->RunningCode = Private->EOFCode + 1; |
| 941 |
Private->RunningBits = |
| 942 |
Private->BitsPerPixel + 1; |
| 943 |
Private->MaxCode1 = 1 << Private->RunningBits; |
| 944 |
_ClearHashTable(HashTable); |
| 945 |
} else { |
| 946 |
/* Put this unique key with its relative Code in |
| 947 |
* hash table: */ |
| 948 |
_InsertHashTable(HashTable, NewKey, |
| 949 |
Private->RunningCode++); |
| 950 |
} |
| 951 |
} |
| 952 |
} |
| 953 |
|
| 954 |
/* Preserve the current state of the compression algorithm: */ |
| 955 |
Private->CrntCode = CrntCode; |
| 956 |
|
| 957 |
if (Private->PixelCount == 0) { |
| 958 |
/* We are done - output last Code and flush output buffers: */ |
| 959 |
if (EGifCompressOutput(GifFile, CrntCode) == GIF_ERROR) { |
| 960 |
GifFile->Error = E_GIF_ERR_DISK_IS_FULL; |
| 961 |
return GIF_ERROR; |
| 962 |
} |
| 963 |
if (EGifCompressOutput(GifFile, Private->EOFCode) == |
| 964 |
GIF_ERROR) { |
| 965 |
GifFile->Error = E_GIF_ERR_DISK_IS_FULL; |
| 966 |
return GIF_ERROR; |
| 967 |
} |
| 968 |
if (EGifCompressOutput(GifFile, FLUSH_OUTPUT) == GIF_ERROR) { |
| 969 |
GifFile->Error = E_GIF_ERR_DISK_IS_FULL; |
| 970 |
return GIF_ERROR; |
| 971 |
} |
| 972 |
} |
| 973 |
|
| 974 |
return GIF_OK; |
| 975 |
} |
| 976 |
|
| 977 |
/****************************************************************************** |
| 978 |
The LZ compression output routine: |
| 979 |
This routine is responsible for the compression of the bit stream into |
| 980 |
8 bits (bytes) packets. |
| 981 |
Returns GIF_OK if written successfully. |
| 982 |
******************************************************************************/ |
| 983 |
static int EGifCompressOutput(GifFileType *GifFile, const int Code) { |
| 984 |
GifFilePrivateType *Private = (GifFilePrivateType *)GifFile->Private; |
| 985 |
int retval = GIF_OK; |
| 986 |
|
| 987 |
if (Code == FLUSH_OUTPUT) { |
| 988 |
while (Private->CrntShiftState > 0) { |
| 989 |
/* Get Rid of what is left in DWord, and flush it. */ |
| 990 |
if (EGifBufferedOutput(GifFile, Private->Buf, |
| 991 |
Private->CrntShiftDWord & |
| 992 |
0xff) == GIF_ERROR) { |
| 993 |
retval = GIF_ERROR; |
| 994 |
} |
| 995 |
Private->CrntShiftDWord >>= 8; |
| 996 |
Private->CrntShiftState -= 8; |
| 997 |
} |
| 998 |
Private->CrntShiftState = 0; /* For next time. */ |
| 999 |
if (EGifBufferedOutput(GifFile, Private->Buf, FLUSH_OUTPUT) == |
| 1000 |
GIF_ERROR) { |
| 1001 |
retval = GIF_ERROR; |
| 1002 |
} |
| 1003 |
} else { |
| 1004 |
Private->CrntShiftDWord |= ((long)Code) |
| 1005 |
<< Private->CrntShiftState; |
| 1006 |
Private->CrntShiftState += Private->RunningBits; |
| 1007 |
while (Private->CrntShiftState >= 8) { |
| 1008 |
/* Dump out full bytes: */ |
| 1009 |
if (EGifBufferedOutput(GifFile, Private->Buf, |
| 1010 |
Private->CrntShiftDWord & |
| 1011 |
0xff) == GIF_ERROR) { |
| 1012 |
retval = GIF_ERROR; |
| 1013 |
} |
| 1014 |
Private->CrntShiftDWord >>= 8; |
| 1015 |
Private->CrntShiftState -= 8; |
| 1016 |
} |
| 1017 |
} |
| 1018 |
|
| 1019 |
/* If code cannot fit into RunningBits bits, must raise its size. Note */ |
| 1020 |
/* however that codes above 4095 are used for special signaling. */ |
| 1021 |
if (Private->RunningCode >= Private->MaxCode1 && Code <= 4095) { |
| 1022 |
Private->MaxCode1 = 1 << ++Private->RunningBits; |
| 1023 |
} |
| 1024 |
|
| 1025 |
return retval; |
| 1026 |
} |
| 1027 |
|
| 1028 |
/****************************************************************************** |
| 1029 |
This routines buffers the given characters until 255 characters are ready |
| 1030 |
to be output. If Code is equal to -1 the buffer is flushed (EOF). |
| 1031 |
The buffer is Dumped with first byte as its size, as GIF format requires. |
| 1032 |
Returns GIF_OK if written successfully. |
| 1033 |
******************************************************************************/ |
| 1034 |
static int EGifBufferedOutput(GifFileType *GifFile, GifByteType *Buf, int c) { |
| 1035 |
if (c == FLUSH_OUTPUT) { |
| 1036 |
/* Flush everything out. */ |
| 1037 |
if (Buf[0] != 0 && InternalWrite(GifFile, Buf, Buf[0] + 1) != |
| 1038 |
(unsigned)(Buf[0] + 1)) { |
| 1039 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 1040 |
return GIF_ERROR; |
| 1041 |
} |
| 1042 |
/* Mark end of compressed data, by an empty block (see GIF doc): |
| 1043 |
*/ |
| 1044 |
Buf[0] = 0; |
| 1045 |
if (InternalWrite(GifFile, Buf, 1) != 1) { |
| 1046 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 1047 |
return GIF_ERROR; |
| 1048 |
} |
| 1049 |
} else { |
| 1050 |
if (Buf[0] == 255) { |
| 1051 |
/* Dump out this buffer - it is full: */ |
| 1052 |
if (InternalWrite(GifFile, Buf, Buf[0] + 1) != |
| 1053 |
(unsigned)(Buf[0] + 1)) { |
| 1054 |
GifFile->Error = E_GIF_ERR_WRITE_FAILED; |
| 1055 |
return GIF_ERROR; |
| 1056 |
} |
| 1057 |
Buf[0] = 0; |
| 1058 |
} |
| 1059 |
Buf[++Buf[0]] = c; |
| 1060 |
} |
| 1061 |
|
| 1062 |
return GIF_OK; |
| 1063 |
} |
| 1064 |
|
| 1065 |
/****************************************************************************** |
| 1066 |
This routine writes to disk an in-core representation of a GIF previously |
| 1067 |
created by DGifSlurp(). |
| 1068 |
******************************************************************************/ |
| 1069 |
|
| 1070 |
static int EGifWriteExtensions(GifFileType *GifFileOut, |
| 1071 |
ExtensionBlock *ExtensionBlocks, |
| 1072 |
int ExtensionBlockCount) { |
| 1073 |
if (ExtensionBlocks) { |
| 1074 |
int j; |
| 1075 |
|
| 1076 |
for (j = 0; j < ExtensionBlockCount; j++) { |
| 1077 |
ExtensionBlock *ep = &ExtensionBlocks[j]; |
| 1078 |
if (ep->Function != CONTINUE_EXT_FUNC_CODE) { |
| 1079 |
if (EGifPutExtensionLeader(GifFileOut, |
| 1080 |
ep->Function) == |
| 1081 |
GIF_ERROR) { |
| 1082 |
return (GIF_ERROR); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
if (EGifPutExtensionBlock(GifFileOut, ep->ByteCount, |
| 1086 |
ep->Bytes) == GIF_ERROR) { |
| 1087 |
return (GIF_ERROR); |
| 1088 |
} |
| 1089 |
if (j == ExtensionBlockCount - 1 || |
| 1090 |
(ep + 1)->Function != CONTINUE_EXT_FUNC_CODE) { |
| 1091 |
if (EGifPutExtensionTrailer(GifFileOut) == |
| 1092 |
GIF_ERROR) { |
| 1093 |
return (GIF_ERROR); |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
} |
| 1098 |
|
| 1099 |
return (GIF_OK); |
| 1100 |
} |
| 1101 |
|
| 1102 |
int EGifSpew(GifFileType *GifFileOut) { |
| 1103 |
int i, j; |
| 1104 |
|
| 1105 |
if (EGifPutScreenDesc(GifFileOut, GifFileOut->SWidth, |
| 1106 |
GifFileOut->SHeight, GifFileOut->SColorResolution, |
| 1107 |
GifFileOut->SBackGroundColor, |
| 1108 |
GifFileOut->SColorMap) == GIF_ERROR) { |
| 1109 |
return (GIF_ERROR); |
| 1110 |
} |
| 1111 |
|
| 1112 |
for (i = 0; i < GifFileOut->ImageCount; i++) { |
| 1113 |
SavedImage *sp = &GifFileOut->SavedImages[i]; |
| 1114 |
int SavedHeight = sp->ImageDesc.Height; |
| 1115 |
int SavedWidth = sp->ImageDesc.Width; |
| 1116 |
|
| 1117 |
/* this allows us to delete images by nuking their rasters */ |
| 1118 |
if (sp->RasterBits == NULL) { |
| 1119 |
continue; |
| 1120 |
} |
| 1121 |
|
| 1122 |
if (EGifWriteExtensions(GifFileOut, sp->ExtensionBlocks, |
| 1123 |
sp->ExtensionBlockCount) == GIF_ERROR) { |
| 1124 |
return (GIF_ERROR); |
| 1125 |
} |
| 1126 |
|
| 1127 |
if (EGifPutImageDesc(GifFileOut, sp->ImageDesc.Left, |
| 1128 |
sp->ImageDesc.Top, SavedWidth, SavedHeight, |
| 1129 |
sp->ImageDesc.Interlace, |
| 1130 |
sp->ImageDesc.ColorMap) == GIF_ERROR) { |
| 1131 |
return (GIF_ERROR); |
| 1132 |
} |
| 1133 |
|
| 1134 |
if (sp->ImageDesc.Interlace) { |
| 1135 |
/* |
| 1136 |
* The way an interlaced image should be written - |
| 1137 |
* offsets and jumps... |
| 1138 |
*/ |
| 1139 |
static const int InterlacedOffset[] = {0, 4, 2, 1}; |
| 1140 |
static const int InterlacedJumps[] = {8, 8, 4, 2}; |
| 1141 |
int k; |
| 1142 |
/* Need to perform 4 passes on the images: */ |
| 1143 |
for (k = 0; k < 4; k++) { |
| 1144 |
for (j = InterlacedOffset[k]; j < SavedHeight; |
| 1145 |
j += InterlacedJumps[k]) { |
| 1146 |
if (EGifPutLine( |
| 1147 |
GifFileOut, |
| 1148 |
sp->RasterBits + j * SavedWidth, |
| 1149 |
SavedWidth) == GIF_ERROR) { |
| 1150 |
return (GIF_ERROR); |
| 1151 |
} |
| 1152 |
} |
| 1153 |
} |
| 1154 |
} else { |
| 1155 |
for (j = 0; j < SavedHeight; j++) { |
| 1156 |
if (EGifPutLine(GifFileOut, |
| 1157 |
sp->RasterBits + j * SavedWidth, |
| 1158 |
SavedWidth) == GIF_ERROR) { |
| 1159 |
return (GIF_ERROR); |
| 1160 |
} |
| 1161 |
} |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
if (EGifWriteExtensions(GifFileOut, GifFileOut->ExtensionBlocks, |
| 1166 |
GifFileOut->ExtensionBlockCount) == GIF_ERROR) { |
| 1167 |
return (GIF_ERROR); |
| 1168 |
} |
| 1169 |
|
| 1170 |
if (EGifCloseFile(GifFileOut, NULL) == GIF_ERROR) { |
| 1171 |
return (GIF_ERROR); |
| 1172 |
} |
| 1173 |
|
| 1174 |
return (GIF_OK); |
| 1175 |
} |
| 1176 |
|
| 1177 |
/* end */ |