| 1 |
#include "imgif.h" |
| 2 |
#include <gif_lib.h> |
| 3 |
#ifdef _MSC_VER |
| 4 |
#include <io.h> |
| 5 |
#else |
| 6 |
#include <unistd.h> |
| 7 |
#endif |
| 8 |
#include <errno.h> |
| 9 |
#include <string.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <stdio.h> |
| 12 |
|
| 13 |
/* |
| 14 |
=head1 NAME |
| 15 |
|
| 16 |
imgif.c - read and write gif files for Imager |
| 17 |
|
| 18 |
=head1 SYNOPSIS |
| 19 |
|
| 20 |
i_img *img; |
| 21 |
i_img *imgs[count]; |
| 22 |
int fd; |
| 23 |
int *colour_table, |
| 24 |
int colours; |
| 25 |
int max_colours; // number of bits per colour |
| 26 |
int pixdev; // how much noise to add |
| 27 |
i_color fixed[N]; // fixed palette entries |
| 28 |
int fixedlen; // number of fixed colours |
| 29 |
int success; // non-zero on success |
| 30 |
char *data; // a GIF file in memory |
| 31 |
int length; // how big data is |
| 32 |
int reader(char *, char *, int, int); |
| 33 |
int writer(char *, char *, int); |
| 34 |
char *userdata; // user's data, whatever it is |
| 35 |
i_quantize quant; |
| 36 |
i_gif_opts opts; |
| 37 |
|
| 38 |
img = i_readgif(fd, &colour_table, &colours); |
| 39 |
success = i_writegif(img, fd, max_colours, pixdev, fixedlen, fixed); |
| 40 |
success = i_writegifmc(img, fd, max_colours); |
| 41 |
img = i_readgif_scalar(data, length, &colour_table, &colours); |
| 42 |
img = i_readgif_callback(cb, userdata, &colour_table, &colours); |
| 43 |
success = i_writegif_gen(&quant, fd, imgs, count, &opts); |
| 44 |
success = i_writegif_callback(&quant, writer, userdata, maxlength, |
| 45 |
imgs, count, &opts); |
| 46 |
|
| 47 |
=head1 DESCRIPTION |
| 48 |
|
| 49 |
This source file provides the C level interface to reading and writing |
| 50 |
GIF files for Imager. |
| 51 |
|
| 52 |
This has been tested with giflib 3 and 4, though you lose the callback |
| 53 |
functionality with giflib3. |
| 54 |
|
| 55 |
=head1 REFERENCE |
| 56 |
|
| 57 |
=over |
| 58 |
|
| 59 |
=cut |
| 60 |
*/ |
| 61 |
|
| 62 |
#ifdef GIFLIB_MAJOR |
| 63 |
#define IMGIFLIB_API_VERSION (GIFLIB_MAJOR * 100 + GIFLIB_MINOR) |
| 64 |
#else |
| 65 |
/* only matters for pre-5.0 which we either reject, or which contains |
| 66 |
no significant API changes */ |
| 67 |
#define IMGIFLIB_API_VERSION 0 |
| 68 |
#endif |
| 69 |
|
| 70 |
#if IMGIFLIB_API_VERSION >= 500 |
| 71 |
#define POST_SET_VERSION |
| 72 |
#define myDGifOpen(userPtr, readFunc, Error) DGifOpen((userPtr), (readFunc), (Error)) |
| 73 |
#define myEGifOpen(userPtr, readFunc, Error) EGifOpen((userPtr), (readFunc), (Error)) |
| 74 |
#define myGifError(gif) ((gif)->Error) |
| 75 |
#define MakeMapObject GifMakeMapObject |
| 76 |
#define FreeMapObject GifFreeMapObject |
| 77 |
#define gif_mutex_lock(mutex) |
| 78 |
#define gif_mutex_unlock(mutex) |
| 79 |
#else |
| 80 |
#define PRE_SET_VERSION |
| 81 |
static GifFileType * |
| 82 |
myDGifOpen(void *userPtr, InputFunc readFunc, int *error) { |
| 83 |
GifFileType *result = DGifOpen(userPtr, readFunc); |
| 84 |
if (!result) |
| 85 |
*error = GifLastError(); |
| 86 |
|
| 87 |
return result; |
| 88 |
} |
| 89 |
static GifFileType * |
| 90 |
myEGifOpen(void *userPtr, OutputFunc outputFunc, int *error) { |
| 91 |
GifFileType *result = EGifOpen(userPtr, outputFunc); |
| 92 |
if (!result) |
| 93 |
*error = GifLastError(); |
| 94 |
|
| 95 |
return result; |
| 96 |
} |
| 97 |
#define myGifError(gif) GifLastError() |
| 98 |
#define gif_mutex_lock(mutex) i_mutex_lock(mutex) |
| 99 |
#define gif_mutex_unlock(mutex) i_mutex_unlock(mutex) |
| 100 |
|
| 101 |
#endif |
| 102 |
|
| 103 |
#if IMGIFLIB_API_VERSION >= 501 |
| 104 |
#define myDGifCloseFile(gif, perror) (DGifCloseFile((gif), (perror))) |
| 105 |
#define myEGifCloseFile(gif, perror) (EGifCloseFile((gif), (perror))) |
| 106 |
#else |
| 107 |
static int |
| 108 |
myDGifCloseFile(GifFileType *GifFile, int *ErrorCode) { |
| 109 |
int result = DGifCloseFile(GifFile); |
| 110 |
if (result == GIF_ERROR) { |
| 111 |
if (ErrorCode) |
| 112 |
*ErrorCode = myGifError(GifFile); |
| 113 |
free(GifFile->Private); |
| 114 |
free(GifFile); |
| 115 |
} |
| 116 |
|
| 117 |
return result; |
| 118 |
} |
| 119 |
|
| 120 |
static int |
| 121 |
myEGifCloseFile(GifFileType *GifFile, int *ErrorCode) { |
| 122 |
int result = EGifCloseFile(GifFile); |
| 123 |
if (result == GIF_ERROR) { |
| 124 |
if (ErrorCode) |
| 125 |
*ErrorCode = myGifError(GifFile); |
| 126 |
free(GifFile->Private); |
| 127 |
free(GifFile); |
| 128 |
} |
| 129 |
|
| 130 |
return result; |
| 131 |
} |
| 132 |
#endif |
| 133 |
|
| 134 |
static char const *gif_error_msg(int code); |
| 135 |
static void gif_push_error(int code); |
| 136 |
|
| 137 |
/* Make some variables global, so we could access them faster: */ |
| 138 |
|
| 139 |
static const int |
| 140 |
InterlacedOffset[] = { 0, 4, 2, 1 }, /* The way Interlaced image should. */ |
| 141 |
InterlacedJumps[] = { 8, 8, 4, 2 }; /* be read - offsets and jumps... */ |
| 142 |
|
| 143 |
#if IMGIFLIB_API_VERSION < 500 |
| 144 |
static i_mutex_t mutex; |
| 145 |
#endif |
| 146 |
|
| 147 |
void |
| 148 |
i_init_gif(void) { |
| 149 |
#if IMGIFLIB_API_VERSION < 500 |
| 150 |
mutex = i_mutex_new(); |
| 151 |
#endif |
| 152 |
} |
| 153 |
|
| 154 |
static |
| 155 |
void |
| 156 |
i_colortable_copy(int **colour_table, int *colours, ColorMapObject *colourmap) { |
| 157 |
GifColorType *mapentry; |
| 158 |
int q; |
| 159 |
int colourmapsize = colourmap->ColorCount; |
| 160 |
|
| 161 |
if(colours) *colours = colourmapsize; |
| 162 |
if(!colour_table) return; |
| 163 |
|
| 164 |
*colour_table = mymalloc(sizeof(int) * colourmapsize * 3); |
| 165 |
memset(*colour_table, 0, sizeof(int) * colourmapsize * 3); |
| 166 |
|
| 167 |
for(q=0; q<colourmapsize; q++) { |
| 168 |
mapentry = &colourmap->Colors[q]; |
| 169 |
(*colour_table)[q*3 + 0] = mapentry->Red; |
| 170 |
(*colour_table)[q*3 + 1] = mapentry->Green; |
| 171 |
(*colour_table)[q*3 + 2] = mapentry->Blue; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
#ifdef GIF_LIB_VERSION |
| 176 |
|
| 177 |
static const |
| 178 |
char gif_version_str[] = GIF_LIB_VERSION; |
| 179 |
|
| 180 |
double |
| 181 |
i_giflib_version(void) { |
| 182 |
const char *p = gif_version_str; |
| 183 |
|
| 184 |
while (*p && (*p < '0' || *p > '9')) |
| 185 |
++p; |
| 186 |
|
| 187 |
if (!*p) |
| 188 |
return 0; |
| 189 |
|
| 190 |
return strtod(p, NULL); |
| 191 |
} |
| 192 |
|
| 193 |
#else |
| 194 |
|
| 195 |
double |
| 196 |
i_giflib_version(void) { |
| 197 |
return GIFLIB_MAJOR + GIFLIB_MINOR * 0.1; |
| 198 |
} |
| 199 |
|
| 200 |
#endif |
| 201 |
|
| 202 |
/* |
| 203 |
=item i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) |
| 204 |
|
| 205 |
Internal. Low-level function for reading a GIF file. The caller must |
| 206 |
create the appropriate GifFileType object and pass it in. |
| 207 |
|
| 208 |
=cut |
| 209 |
*/ |
| 210 |
|
| 211 |
static i_img * |
| 212 |
i_readgif_low(GifFileType *GifFile, int **colour_table, int *colours) { |
| 213 |
i_img *im; |
| 214 |
int i, j, Row, Col, Width, Height, ExtCode, x; |
| 215 |
size_t Size; |
| 216 |
int cmapcnt = 0, ImageNum = 0; |
| 217 |
ColorMapObject *ColorMap; |
| 218 |
|
| 219 |
GifRecordType RecordType; |
| 220 |
GifByteType *Extension; |
| 221 |
|
| 222 |
GifRowType GifRow; |
| 223 |
GifColorType *ColorMapEntry; |
| 224 |
i_color col; |
| 225 |
int error; |
| 226 |
|
| 227 |
mm_log((1,"i_readgif_low(GifFile %p, colour_table %p, colours %p)\n", GifFile, colour_table, colours)); |
| 228 |
|
| 229 |
/* it's possible that the caller has called us with *colour_table being |
| 230 |
non-NULL, but we check that to see if we need to free an allocated |
| 231 |
colour table on error. |
| 232 |
*/ |
| 233 |
if (colour_table) *colour_table = NULL; |
| 234 |
|
| 235 |
ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap); |
| 236 |
|
| 237 |
if (ColorMap) { |
| 238 |
i_colortable_copy(colour_table, colours, ColorMap); |
| 239 |
cmapcnt++; |
| 240 |
} |
| 241 |
|
| 242 |
if (!i_int_check_image_file_limits(GifFile->SWidth, GifFile->SHeight, 3, sizeof(i_sample_t))) { |
| 243 |
if (colour_table && *colour_table) { |
| 244 |
myfree(*colour_table); |
| 245 |
*colour_table = NULL; |
| 246 |
} |
| 247 |
(void)myDGifCloseFile(GifFile, NULL); |
| 248 |
mm_log((1, "i_readgif: image size exceeds limits\n")); |
| 249 |
return NULL; |
| 250 |
} |
| 251 |
|
| 252 |
im = i_img_8_new(GifFile->SWidth, GifFile->SHeight, 3); |
| 253 |
if (!im) { |
| 254 |
if (colour_table && *colour_table) { |
| 255 |
myfree(*colour_table); |
| 256 |
*colour_table = NULL; |
| 257 |
} |
| 258 |
(void)myDGifCloseFile(GifFile, NULL); |
| 259 |
return NULL; |
| 260 |
} |
| 261 |
|
| 262 |
Size = GifFile->SWidth * sizeof(GifPixelType); |
| 263 |
|
| 264 |
GifRow = mymalloc(Size); |
| 265 |
|
| 266 |
for (i = 0; i < GifFile->SWidth; i++) GifRow[i] = GifFile->SBackGroundColor; |
| 267 |
|
| 268 |
/* Scan the content of the GIF file and load the image(s) in: */ |
| 269 |
do { |
| 270 |
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) { |
| 271 |
gif_push_error(myGifError(GifFile)); |
| 272 |
i_push_error(0, "Unable to get record type"); |
| 273 |
if (colour_table && *colour_table) { |
| 274 |
myfree(*colour_table); |
| 275 |
*colour_table = NULL; |
| 276 |
} |
| 277 |
myfree(GifRow); |
| 278 |
i_img_destroy(im); |
| 279 |
(void)myDGifCloseFile(GifFile, NULL); |
| 280 |
return NULL; |
| 281 |
} |
| 282 |
|
| 283 |
switch (RecordType) { |
| 284 |
case IMAGE_DESC_RECORD_TYPE: |
| 285 |
if (DGifGetImageDesc(GifFile) == GIF_ERROR) { |
| 286 |
gif_push_error(myGifError(GifFile)); |
| 287 |
i_push_error(0, "Unable to get image descriptor"); |
| 288 |
if (colour_table && *colour_table) { |
| 289 |
myfree(*colour_table); |
| 290 |
*colour_table = NULL; |
| 291 |
} |
| 292 |
myfree(GifRow); |
| 293 |
i_img_destroy(im); |
| 294 |
(void)myDGifCloseFile(GifFile, NULL); |
| 295 |
return NULL; |
| 296 |
} |
| 297 |
|
| 298 |
if (( ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap) )) { |
| 299 |
mm_log((1, "Adding local colormap\n")); |
| 300 |
if ( cmapcnt == 0) { |
| 301 |
i_colortable_copy(colour_table, colours, ColorMap); |
| 302 |
cmapcnt++; |
| 303 |
} |
| 304 |
} else { |
| 305 |
/* No colormap and we are about to read in the image - abandon for now */ |
| 306 |
mm_log((1, "Going in with no colormap\n")); |
| 307 |
i_push_error(0, "Image does not have a local or a global color map"); |
| 308 |
/* we can't have allocated a colour table here */ |
| 309 |
myfree(GifRow); |
| 310 |
i_img_destroy(im); |
| 311 |
(void)myDGifCloseFile(GifFile, NULL); |
| 312 |
return NULL; |
| 313 |
} |
| 314 |
|
| 315 |
Row = GifFile->Image.Top; /* Image Position relative to Screen. */ |
| 316 |
Col = GifFile->Image.Left; |
| 317 |
Width = GifFile->Image.Width; |
| 318 |
Height = GifFile->Image.Height; |
| 319 |
ImageNum++; |
| 320 |
mm_log((1,"i_readgif_low: Image %d at (%d, %d) [%dx%d]: \n",ImageNum, Col, Row, Width, Height)); |
| 321 |
|
| 322 |
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth || |
| 323 |
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) { |
| 324 |
i_push_errorf(0, "Image %d is not confined to screen dimension, aborted.\n",ImageNum); |
| 325 |
if (colour_table && *colour_table) { |
| 326 |
myfree(*colour_table); |
| 327 |
*colour_table = NULL; |
| 328 |
} |
| 329 |
myfree(GifRow); |
| 330 |
i_img_destroy(im); |
| 331 |
(void)myDGifCloseFile(GifFile, NULL); |
| 332 |
return NULL; |
| 333 |
} |
| 334 |
if (GifFile->Image.Interlace) { |
| 335 |
|
| 336 |
for (i = 0; i < 4; i++) for (j = Row + InterlacedOffset[i]; j < Row + Height; j += InterlacedJumps[i]) { |
| 337 |
if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) { |
| 338 |
gif_push_error(myGifError(GifFile)); |
| 339 |
i_push_error(0, "Reading GIF line"); |
| 340 |
if (colour_table && *colour_table) { |
| 341 |
myfree(*colour_table); |
| 342 |
*colour_table = NULL; |
| 343 |
} |
| 344 |
myfree(GifRow); |
| 345 |
i_img_destroy(im); |
| 346 |
(void)myDGifCloseFile(GifFile, NULL); |
| 347 |
return NULL; |
| 348 |
} |
| 349 |
|
| 350 |
for (x = 0; x < Width; x++) { |
| 351 |
ColorMapEntry = &ColorMap->Colors[GifRow[x]]; |
| 352 |
col.rgb.r = ColorMapEntry->Red; |
| 353 |
col.rgb.g = ColorMapEntry->Green; |
| 354 |
col.rgb.b = ColorMapEntry->Blue; |
| 355 |
i_ppix(im,Col+x,j,&col); |
| 356 |
} |
| 357 |
|
| 358 |
} |
| 359 |
} |
| 360 |
else { |
| 361 |
for (i = 0; i < Height; i++) { |
| 362 |
if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) { |
| 363 |
gif_push_error(myGifError(GifFile)); |
| 364 |
i_push_error(0, "Reading GIF line"); |
| 365 |
if (colour_table && *colour_table) { |
| 366 |
myfree(*colour_table); |
| 367 |
*colour_table = NULL; |
| 368 |
} |
| 369 |
myfree(GifRow); |
| 370 |
i_img_destroy(im); |
| 371 |
(void)myDGifCloseFile(GifFile, NULL); |
| 372 |
return NULL; |
| 373 |
} |
| 374 |
|
| 375 |
for (x = 0; x < Width; x++) { |
| 376 |
ColorMapEntry = &ColorMap->Colors[GifRow[x]]; |
| 377 |
col.rgb.r = ColorMapEntry->Red; |
| 378 |
col.rgb.g = ColorMapEntry->Green; |
| 379 |
col.rgb.b = ColorMapEntry->Blue; |
| 380 |
i_ppix(im, Col+x, Row, &col); |
| 381 |
} |
| 382 |
Row++; |
| 383 |
} |
| 384 |
} |
| 385 |
break; |
| 386 |
case EXTENSION_RECORD_TYPE: |
| 387 |
/* Skip any extension blocks in file: */ |
| 388 |
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) { |
| 389 |
gif_push_error(myGifError(GifFile)); |
| 390 |
i_push_error(0, "Reading extension record"); |
| 391 |
if (colour_table && *colour_table) { |
| 392 |
myfree(*colour_table); |
| 393 |
*colour_table = NULL; |
| 394 |
} |
| 395 |
myfree(GifRow); |
| 396 |
i_img_destroy(im); |
| 397 |
(void)myDGifCloseFile(GifFile, NULL); |
| 398 |
return NULL; |
| 399 |
} |
| 400 |
while (Extension != NULL) { |
| 401 |
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) { |
| 402 |
gif_push_error(myGifError(GifFile)); |
| 403 |
i_push_error(0, "reading next block of extension"); |
| 404 |
if (colour_table && *colour_table) { |
| 405 |
myfree(*colour_table); |
| 406 |
*colour_table = NULL; |
| 407 |
} |
| 408 |
myfree(GifRow); |
| 409 |
i_img_destroy(im); |
| 410 |
(void)myDGifCloseFile(GifFile, NULL); |
| 411 |
return NULL; |
| 412 |
} |
| 413 |
} |
| 414 |
break; |
| 415 |
case TERMINATE_RECORD_TYPE: |
| 416 |
default: /* Should be traps by DGifGetRecordType. */ |
| 417 |
break; |
| 418 |
} |
| 419 |
} while (RecordType != TERMINATE_RECORD_TYPE); |
| 420 |
|
| 421 |
myfree(GifRow); |
| 422 |
|
| 423 |
if (myDGifCloseFile(GifFile, &error) == GIF_ERROR) { |
| 424 |
gif_push_error(error); |
| 425 |
i_push_error(0, "Closing GIF file object"); |
| 426 |
if (colour_table && *colour_table) { |
| 427 |
myfree(*colour_table); |
| 428 |
*colour_table = NULL; |
| 429 |
} |
| 430 |
i_img_destroy(im); |
| 431 |
return NULL; |
| 432 |
} |
| 433 |
|
| 434 |
i_tags_set(&im->tags, "i_format", "gif", -1); |
| 435 |
|
| 436 |
return im; |
| 437 |
} |
| 438 |
|
| 439 |
/* |
| 440 |
|
| 441 |
Internal function called by i_readgif_multi_low() in error handling |
| 442 |
|
| 443 |
*/ |
| 444 |
static void |
| 445 |
free_images(i_img **imgs, int count) { |
| 446 |
int i; |
| 447 |
|
| 448 |
if (count) { |
| 449 |
for (i = 0; i < count; ++i) |
| 450 |
i_img_destroy(imgs[i]); |
| 451 |
myfree(imgs); |
| 452 |
} |
| 453 |
} |
| 454 |
|
| 455 |
/* |
| 456 |
=item i_readgif_multi_low(GifFileType *gf, int *count, int page) |
| 457 |
|
| 458 |
Reads one of more gif images from the given GIF file. |
| 459 |
|
| 460 |
Returns a pointer to an array of i_img *, and puts the count into |
| 461 |
*count. |
| 462 |
|
| 463 |
If page is not -1 then the given image _only_ is returned from the |
| 464 |
file, where the first image is 0, the second 1 and so on. |
| 465 |
|
| 466 |
Unlike the normal i_readgif*() functions the images are paletted |
| 467 |
images rather than a combined RGB image. |
| 468 |
|
| 469 |
This functions sets tags on the images returned: |
| 470 |
|
| 471 |
=over |
| 472 |
|
| 473 |
=item gif_left |
| 474 |
|
| 475 |
the offset of the image from the left of the "screen" ("Image Left |
| 476 |
Position") |
| 477 |
|
| 478 |
=item gif_top |
| 479 |
|
| 480 |
the offset of the image from the top of the "screen" ("Image Top Position") |
| 481 |
|
| 482 |
=item gif_interlace |
| 483 |
|
| 484 |
non-zero if the image was interlaced ("Interlace Flag") |
| 485 |
|
| 486 |
=item gif_screen_width |
| 487 |
|
| 488 |
=item gif_screen_height |
| 489 |
|
| 490 |
the size of the logical screen ("Logical Screen Width", |
| 491 |
"Logical Screen Height") |
| 492 |
|
| 493 |
=item gif_local_map |
| 494 |
|
| 495 |
Non-zero if this image had a local color map. |
| 496 |
|
| 497 |
=item gif_background |
| 498 |
|
| 499 |
The index in the global colormap of the logical screen's background |
| 500 |
color. This is only set if the current image uses the global |
| 501 |
colormap. |
| 502 |
|
| 503 |
=item gif_trans_index |
| 504 |
|
| 505 |
The index of the color in the colormap used for transparency. If the |
| 506 |
image has a transparency then it is returned as a 4 channel image with |
| 507 |
the alpha set to zero in this palette entry. ("Transparent Color Index") |
| 508 |
|
| 509 |
=item gif_delay |
| 510 |
|
| 511 |
The delay until the next frame is displayed, in 1/100 of a second. |
| 512 |
("Delay Time"). |
| 513 |
|
| 514 |
=item gif_user_input |
| 515 |
|
| 516 |
whether or not a user input is expected before continuing (view dependent) |
| 517 |
("User Input Flag"). |
| 518 |
|
| 519 |
=item gif_disposal |
| 520 |
|
| 521 |
how the next frame is displayed ("Disposal Method") |
| 522 |
|
| 523 |
=item gif_loop |
| 524 |
|
| 525 |
the number of loops from the Netscape Loop extension. This may be zero. |
| 526 |
|
| 527 |
=item gif_comment |
| 528 |
|
| 529 |
the first block of the first gif comment before each image. |
| 530 |
|
| 531 |
=back |
| 532 |
|
| 533 |
Where applicable, the ("name") is the name of that field from the GIF89 |
| 534 |
standard. |
| 535 |
|
| 536 |
=cut |
| 537 |
*/ |
| 538 |
|
| 539 |
static i_img ** |
| 540 |
i_readgif_multi_low(GifFileType *GifFile, int *count, int page) { |
| 541 |
i_img *img; |
| 542 |
int i, j, Width, Height, ExtCode; |
| 543 |
size_t Size; |
| 544 |
int ImageNum = 0, ColorMapSize = 0; |
| 545 |
ColorMapObject *ColorMap; |
| 546 |
|
| 547 |
GifRecordType RecordType; |
| 548 |
GifByteType *Extension; |
| 549 |
|
| 550 |
GifRowType GifRow; |
| 551 |
int got_gce = 0; |
| 552 |
int trans_index = 0; /* transparent index if we see a GCE */ |
| 553 |
int gif_delay = 0; /* delay from a GCE */ |
| 554 |
int user_input = 0; /* user input flag from a GCE */ |
| 555 |
int disposal = 0; /* disposal method from a GCE */ |
| 556 |
int got_ns_loop = 0; |
| 557 |
int ns_loop = 0; |
| 558 |
char *comment = NULL; /* a comment */ |
| 559 |
i_img **results = NULL; |
| 560 |
int result_alloc = 0; |
| 561 |
int channels; |
| 562 |
int image_colors = 0; |
| 563 |
i_color black; /* used to expand the palette if needed */ |
| 564 |
int error; |
| 565 |
|
| 566 |
for (i = 0; i < MAXCHANNELS; ++i) |
| 567 |
black.channel[i] = 0; |
| 568 |
|
| 569 |
*count = 0; |
| 570 |
|
| 571 |
mm_log((1,"i_readgif_multi_low(GifFile %p, , count %p)\n", GifFile, count)); |
| 572 |
|
| 573 |
Size = GifFile->SWidth * sizeof(GifPixelType); |
| 574 |
|
| 575 |
GifRow = (GifRowType) mymalloc(Size); |
| 576 |
|
| 577 |
/* Scan the content of the GIF file and load the image(s) in: */ |
| 578 |
do { |
| 579 |
if (DGifGetRecordType(GifFile, &RecordType) == GIF_ERROR) { |
| 580 |
gif_push_error(myGifError(GifFile)); |
| 581 |
i_push_error(0, "Unable to get record type"); |
| 582 |
free_images(results, *count); |
| 583 |
(void)myDGifCloseFile(GifFile, NULL); |
| 584 |
myfree(GifRow); |
| 585 |
if (comment) |
| 586 |
myfree(comment); |
| 587 |
return NULL; |
| 588 |
} |
| 589 |
|
| 590 |
switch (RecordType) { |
| 591 |
case IMAGE_DESC_RECORD_TYPE: |
| 592 |
if (DGifGetImageDesc(GifFile) == GIF_ERROR) { |
| 593 |
gif_push_error(myGifError(GifFile)); |
| 594 |
i_push_error(0, "Unable to get image descriptor"); |
| 595 |
free_images(results, *count); |
| 596 |
(void)myDGifCloseFile(GifFile, NULL); |
| 597 |
myfree(GifRow); |
| 598 |
if (comment) |
| 599 |
myfree(comment); |
| 600 |
return NULL; |
| 601 |
} |
| 602 |
|
| 603 |
Width = GifFile->Image.Width; |
| 604 |
Height = GifFile->Image.Height; |
| 605 |
mm_log((1,"i_readgif_multi_low: Image %d at (%d, %d) [%dx%d]: \n", |
| 606 |
ImageNum, GifFile->Image.Left, GifFile->Image.Top, Width, |
| 607 |
Height)); |
| 608 |
|
| 609 |
if (GifFile->Image.Left + GifFile->Image.Width > GifFile->SWidth || |
| 610 |
GifFile->Image.Top + GifFile->Image.Height > GifFile->SHeight) { |
| 611 |
i_push_errorf(0, "Image %d is not confined to screen dimension, aborted.\n",ImageNum); |
| 612 |
free_images(results, *count); |
| 613 |
(void)myDGifCloseFile(GifFile, NULL); |
| 614 |
myfree(GifRow); |
| 615 |
if (comment) |
| 616 |
myfree(comment); |
| 617 |
return(0); |
| 618 |
} |
| 619 |
|
| 620 |
if (page == -1 || page == ImageNum) { |
| 621 |
if (( ColorMap = (GifFile->Image.ColorMap ? GifFile->Image.ColorMap : GifFile->SColorMap) )) { |
| 622 |
mm_log((1, "Adding local colormap\n")); |
| 623 |
ColorMapSize = ColorMap->ColorCount; |
| 624 |
} else { |
| 625 |
/* No colormap and we are about to read in the image - |
| 626 |
abandon for now */ |
| 627 |
mm_log((1, "Going in with no colormap\n")); |
| 628 |
i_push_error(0, "Image does not have a local or a global color map"); |
| 629 |
free_images(results, *count); |
| 630 |
(void)myDGifCloseFile(GifFile, NULL); |
| 631 |
myfree(GifRow); |
| 632 |
if (comment) |
| 633 |
myfree(comment); |
| 634 |
return NULL; |
| 635 |
} |
| 636 |
|
| 637 |
channels = 3; |
| 638 |
if (got_gce && trans_index >= 0) |
| 639 |
channels = 4; |
| 640 |
if (!i_int_check_image_file_limits(Width, Height, channels, sizeof(i_sample_t))) { |
| 641 |
free_images(results, *count); |
| 642 |
mm_log((1, "i_readgif: image size exceeds limits\n")); |
| 643 |
(void)myDGifCloseFile(GifFile, NULL); |
| 644 |
myfree(GifRow); |
| 645 |
if (comment) |
| 646 |
myfree(comment); |
| 647 |
return NULL; |
| 648 |
} |
| 649 |
img = i_img_pal_new(Width, Height, channels, 256); |
| 650 |
if (!img) { |
| 651 |
free_images(results, *count); |
| 652 |
(void)myDGifCloseFile(GifFile, NULL); |
| 653 |
if (comment) |
| 654 |
myfree(comment); |
| 655 |
myfree(GifRow); |
| 656 |
return NULL; |
| 657 |
} |
| 658 |
/* populate the palette of the new image */ |
| 659 |
mm_log((1, "ColorMapSize %d\n", ColorMapSize)); |
| 660 |
for (i = 0; i < ColorMapSize; ++i) { |
| 661 |
i_color col; |
| 662 |
col.rgba.r = ColorMap->Colors[i].Red; |
| 663 |
col.rgba.g = ColorMap->Colors[i].Green; |
| 664 |
col.rgba.b = ColorMap->Colors[i].Blue; |
| 665 |
if (channels == 4 && trans_index == i) |
| 666 |
col.rgba.a = 0; |
| 667 |
else |
| 668 |
col.rgba.a = 255; |
| 669 |
|
| 670 |
i_addcolors(img, &col, 1); |
| 671 |
} |
| 672 |
image_colors = ColorMapSize; |
| 673 |
++*count; |
| 674 |
if (*count > result_alloc) { |
| 675 |
if (result_alloc == 0) { |
| 676 |
result_alloc = 5; |
| 677 |
results = mymalloc(result_alloc * sizeof(i_img *)); |
| 678 |
} |
| 679 |
else { |
| 680 |
/* myrealloc never fails (it just dies if it can't allocate) */ |
| 681 |
result_alloc *= 2; |
| 682 |
results = myrealloc(results, result_alloc * sizeof(i_img *)); |
| 683 |
} |
| 684 |
} |
| 685 |
results[*count-1] = img; |
| 686 |
i_tags_set(&img->tags, "i_format", "gif", -1); |
| 687 |
i_tags_setn(&img->tags, "gif_left", GifFile->Image.Left); |
| 688 |
/**(char *)0 = 1;*/ |
| 689 |
i_tags_setn(&img->tags, "gif_top", GifFile->Image.Top); |
| 690 |
i_tags_setn(&img->tags, "gif_interlace", GifFile->Image.Interlace); |
| 691 |
i_tags_setn(&img->tags, "gif_screen_width", GifFile->SWidth); |
| 692 |
i_tags_setn(&img->tags, "gif_screen_height", GifFile->SHeight); |
| 693 |
i_tags_setn(&img->tags, "gif_colormap_size", ColorMapSize); |
| 694 |
if (GifFile->SColorMap && !GifFile->Image.ColorMap) { |
| 695 |
i_tags_setn(&img->tags, "gif_background", |
| 696 |
GifFile->SBackGroundColor); |
| 697 |
} |
| 698 |
if (GifFile->Image.ColorMap) { |
| 699 |
i_tags_setn(&img->tags, "gif_localmap", 1); |
| 700 |
} |
| 701 |
if (got_gce) { |
| 702 |
i_color trans; |
| 703 |
if (trans_index >= 0 |
| 704 |
&& i_getcolors(img, trans_index, &trans, 1) == 1) { |
| 705 |
i_tags_setn(&img->tags, "gif_trans_index", trans_index); |
| 706 |
i_tags_set_color(&img->tags, "gif_trans_color", 0, &trans); |
| 707 |
} |
| 708 |
i_tags_setn(&img->tags, "gif_delay", gif_delay); |
| 709 |
i_tags_setn(&img->tags, "gif_user_input", user_input); |
| 710 |
i_tags_setn(&img->tags, "gif_disposal", disposal); |
| 711 |
} |
| 712 |
got_gce = 0; |
| 713 |
if (got_ns_loop) |
| 714 |
i_tags_setn(&img->tags, "gif_loop", ns_loop); |
| 715 |
if (comment) { |
| 716 |
i_tags_set(&img->tags, "gif_comment", comment, strlen(comment)); |
| 717 |
myfree(comment); |
| 718 |
comment = NULL; |
| 719 |
} |
| 720 |
|
| 721 |
if (GifFile->Image.Interlace) { |
| 722 |
for (i = 0; i < 4; i++) { |
| 723 |
for (j = InterlacedOffset[i]; j < Height; |
| 724 |
j += InterlacedJumps[i]) { |
| 725 |
if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) { |
| 726 |
gif_push_error(myGifError(GifFile)); |
| 727 |
i_push_error(0, "Reading GIF line"); |
| 728 |
free_images(results, *count); |
| 729 |
(void)myDGifCloseFile(GifFile, NULL); |
| 730 |
myfree(GifRow); |
| 731 |
if (comment) |
| 732 |
myfree(comment); |
| 733 |
return NULL; |
| 734 |
} |
| 735 |
|
| 736 |
/* range check the scanline if needed */ |
| 737 |
if (image_colors != 256) { |
| 738 |
int x; |
| 739 |
for (x = 0; x < Width; ++x) { |
| 740 |
while (GifRow[x] >= image_colors) { |
| 741 |
/* expand the palette since a palette index is too big */ |
| 742 |
i_addcolors(img, &black, 1); |
| 743 |
++image_colors; |
| 744 |
} |
| 745 |
} |
| 746 |
} |
| 747 |
|
| 748 |
i_ppal(img, 0, Width, j, GifRow); |
| 749 |
} |
| 750 |
} |
| 751 |
} |
| 752 |
else { |
| 753 |
for (i = 0; i < Height; i++) { |
| 754 |
if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) { |
| 755 |
gif_push_error(myGifError(GifFile)); |
| 756 |
i_push_error(0, "Reading GIF line"); |
| 757 |
free_images(results, *count); |
| 758 |
(void)myDGifCloseFile(GifFile, NULL); |
| 759 |
myfree(GifRow); |
| 760 |
if (comment) |
| 761 |
myfree(comment); |
| 762 |
return NULL; |
| 763 |
} |
| 764 |
|
| 765 |
/* range check the scanline if needed */ |
| 766 |
if (image_colors != 256) { |
| 767 |
int x; |
| 768 |
for (x = 0; x < Width; ++x) { |
| 769 |
while (GifRow[x] >= image_colors) { |
| 770 |
/* expand the palette since a palette index is too big */ |
| 771 |
i_addcolors(img, &black, 1); |
| 772 |
++image_colors; |
| 773 |
} |
| 774 |
} |
| 775 |
} |
| 776 |
|
| 777 |
i_ppal(img, 0, Width, i, GifRow); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
/* must be only one image wanted and that was it */ |
| 782 |
if (page != -1) { |
| 783 |
myfree(GifRow); |
| 784 |
(void)myDGifCloseFile(GifFile, NULL); |
| 785 |
if (comment) |
| 786 |
myfree(comment); |
| 787 |
return results; |
| 788 |
} |
| 789 |
} |
| 790 |
else { |
| 791 |
/* skip the image */ |
| 792 |
/* whether interlaced or not, it has the same number of lines */ |
| 793 |
/* giflib does't have an interface to skip the image data */ |
| 794 |
for (i = 0; i < Height; i++) { |
| 795 |
if (DGifGetLine(GifFile, GifRow, Width) == GIF_ERROR) { |
| 796 |
gif_push_error(myGifError(GifFile)); |
| 797 |
i_push_error(0, "Reading GIF line"); |
| 798 |
free_images(results, *count); |
| 799 |
myfree(GifRow); |
| 800 |
(void)myDGifCloseFile(GifFile, NULL); |
| 801 |
if (comment) |
| 802 |
myfree(comment); |
| 803 |
return NULL; |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
/* kill the comment so we get the right comment for the page */ |
| 808 |
if (comment) { |
| 809 |
myfree(comment); |
| 810 |
comment = NULL; |
| 811 |
} |
| 812 |
} |
| 813 |
ImageNum++; |
| 814 |
break; |
| 815 |
case EXTENSION_RECORD_TYPE: |
| 816 |
/* Skip any extension blocks in file: */ |
| 817 |
if (DGifGetExtension(GifFile, &ExtCode, &Extension) == GIF_ERROR) { |
| 818 |
gif_push_error(myGifError(GifFile)); |
| 819 |
i_push_error(0, "Reading extension record"); |
| 820 |
free_images(results, *count); |
| 821 |
myfree(GifRow); |
| 822 |
(void)myDGifCloseFile(GifFile, NULL); |
| 823 |
if (comment) |
| 824 |
myfree(comment); |
| 825 |
return NULL; |
| 826 |
} |
| 827 |
/* possibly this should be an error, but "be liberal in what you accept" */ |
| 828 |
if (!Extension) |
| 829 |
break; |
| 830 |
if (ExtCode == 0xF9) { |
| 831 |
got_gce = 1; |
| 832 |
if (Extension[1] & 1) |
| 833 |
trans_index = Extension[4]; |
| 834 |
else |
| 835 |
trans_index = -1; |
| 836 |
gif_delay = Extension[2] + 256 * Extension[3]; |
| 837 |
user_input = (Extension[1] & 2) != 0; |
| 838 |
disposal = ((unsigned)Extension[1] >> 2) & 7; |
| 839 |
} |
| 840 |
if (ExtCode == 0xFF && *Extension == 11) { |
| 841 |
if (memcmp(Extension+1, "NETSCAPE2.0", 11) == 0) { |
| 842 |
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) { |
| 843 |
gif_push_error(myGifError(GifFile)); |
| 844 |
i_push_error(0, "reading loop extension"); |
| 845 |
free_images(results, *count); |
| 846 |
myfree(GifRow); |
| 847 |
(void)myDGifCloseFile(GifFile, NULL); |
| 848 |
if (comment) |
| 849 |
myfree(comment); |
| 850 |
return NULL; |
| 851 |
} |
| 852 |
if (Extension && *Extension == 3) { |
| 853 |
got_ns_loop = 1; |
| 854 |
ns_loop = Extension[2] + 256 * Extension[3]; |
| 855 |
} |
| 856 |
} |
| 857 |
} |
| 858 |
else if (ExtCode == 0xFE) { |
| 859 |
/* while it's possible for a GIF file to contain more than one |
| 860 |
comment, I'm only implementing a single comment per image, |
| 861 |
with the comment saved into the following image. |
| 862 |
If someone wants more than that they can implement it. |
| 863 |
I also don't handle comments that take more than one block. |
| 864 |
*/ |
| 865 |
if (!comment) { |
| 866 |
comment = mymalloc(*Extension+1); |
| 867 |
memcpy(comment, Extension+1, *Extension); |
| 868 |
comment[*Extension] = '\0'; |
| 869 |
} |
| 870 |
} |
| 871 |
while (Extension != NULL) { |
| 872 |
if (DGifGetExtensionNext(GifFile, &Extension) == GIF_ERROR) { |
| 873 |
gif_push_error(myGifError(GifFile)); |
| 874 |
i_push_error(0, "reading next block of extension"); |
| 875 |
free_images(results, *count); |
| 876 |
myfree(GifRow); |
| 877 |
(void)myDGifCloseFile(GifFile, NULL); |
| 878 |
if (comment) |
| 879 |
myfree(comment); |
| 880 |
return NULL; |
| 881 |
} |
| 882 |
} |
| 883 |
break; |
| 884 |
case TERMINATE_RECORD_TYPE: |
| 885 |
default: /* Should be trapped by DGifGetRecordType. */ |
| 886 |
break; |
| 887 |
} |
| 888 |
} while (RecordType != TERMINATE_RECORD_TYPE); |
| 889 |
|
| 890 |
if (comment) { |
| 891 |
if (*count) { |
| 892 |
i_tags_set(&(results[*count-1]->tags), "gif_comment", comment, |
| 893 |
strlen(comment)); |
| 894 |
} |
| 895 |
myfree(comment); |
| 896 |
} |
| 897 |
|
| 898 |
myfree(GifRow); |
| 899 |
|
| 900 |
if (myDGifCloseFile(GifFile, &error) == GIF_ERROR) { |
| 901 |
gif_push_error(error); |
| 902 |
i_push_error(0, "Closing GIF file object"); |
| 903 |
free_images(results, *count); |
| 904 |
return NULL; |
| 905 |
} |
| 906 |
|
| 907 |
if (!ImageNum) { |
| 908 |
/* there were no images */ |
| 909 |
i_push_error(0, "no images found in file"); |
| 910 |
return NULL; |
| 911 |
} |
| 912 |
|
| 913 |
if (ImageNum && page != -1) { |
| 914 |
/* there were images, but the page selected wasn't found */ |
| 915 |
i_push_errorf(0, "page %d not found (%d total)", page, ImageNum); |
| 916 |
free_images(results, *count); |
| 917 |
return NULL; |
| 918 |
} |
| 919 |
|
| 920 |
return results; |
| 921 |
} |
| 922 |
|
| 923 |
static int io_glue_read_cb(GifFileType *gft, GifByteType *buf, int length); |
| 924 |
|
| 925 |
/* |
| 926 |
=item i_readgif_multi_wiol(ig, int *count) |
| 927 |
|
| 928 |
=cut |
| 929 |
*/ |
| 930 |
|
| 931 |
i_img ** |
| 932 |
i_readgif_multi_wiol(io_glue *ig, int *count) { |
| 933 |
GifFileType *GifFile; |
| 934 |
int gif_error; |
| 935 |
i_img **result; |
| 936 |
|
| 937 |
gif_mutex_lock(mutex); |
| 938 |
|
| 939 |
i_clear_error(); |
| 940 |
|
| 941 |
if ((GifFile = myDGifOpen((void *)ig, io_glue_read_cb, &gif_error )) == NULL) { |
| 942 |
gif_push_error(gif_error); |
| 943 |
i_push_error(0, "Cannot create giflib callback object"); |
| 944 |
mm_log((1,"i_readgif_multi_wiol: Unable to open callback datasource.\n")); |
| 945 |
gif_mutex_unlock(mutex); |
| 946 |
return NULL; |
| 947 |
} |
| 948 |
|
| 949 |
result = i_readgif_multi_low(GifFile, count, -1); |
| 950 |
|
| 951 |
gif_mutex_unlock(mutex); |
| 952 |
|
| 953 |
return result; |
| 954 |
} |
| 955 |
|
| 956 |
static int |
| 957 |
io_glue_read_cb(GifFileType *gft, GifByteType *buf, int length) { |
| 958 |
io_glue *ig = (io_glue *)gft->UserData; |
| 959 |
|
| 960 |
return i_io_read(ig, buf, length); |
| 961 |
} |
| 962 |
|
| 963 |
i_img * |
| 964 |
i_readgif_wiol(io_glue *ig, int **color_table, int *colors) { |
| 965 |
GifFileType *GifFile; |
| 966 |
int gif_error; |
| 967 |
i_img *result; |
| 968 |
|
| 969 |
gif_mutex_lock(mutex); |
| 970 |
|
| 971 |
i_clear_error(); |
| 972 |
|
| 973 |
if ((GifFile = myDGifOpen((void *)ig, io_glue_read_cb, &gif_error )) == NULL) { |
| 974 |
gif_push_error(gif_error); |
| 975 |
i_push_error(0, "Cannot create giflib callback object"); |
| 976 |
mm_log((1,"i_readgif_wiol: Unable to open callback datasource.\n")); |
| 977 |
gif_mutex_unlock(mutex); |
| 978 |
return NULL; |
| 979 |
} |
| 980 |
|
| 981 |
result = i_readgif_low(GifFile, color_table, colors); |
| 982 |
|
| 983 |
gif_mutex_unlock(mutex); |
| 984 |
|
| 985 |
return result; |
| 986 |
} |
| 987 |
|
| 988 |
/* |
| 989 |
=item i_readgif_single_low(GifFile, page) |
| 990 |
|
| 991 |
Lower level function to read a single image from a GIF. |
| 992 |
|
| 993 |
page must be non-negative. |
| 994 |
|
| 995 |
=cut |
| 996 |
*/ |
| 997 |
static i_img * |
| 998 |
i_readgif_single_low(GifFileType *GifFile, int page) { |
| 999 |
int count = 0; |
| 1000 |
i_img **imgs; |
| 1001 |
|
| 1002 |
imgs = i_readgif_multi_low(GifFile, &count, page); |
| 1003 |
|
| 1004 |
if (imgs && count) { |
| 1005 |
i_img *result = imgs[0]; |
| 1006 |
|
| 1007 |
myfree(imgs); |
| 1008 |
return result; |
| 1009 |
} |
| 1010 |
else { |
| 1011 |
/* i_readgif_multi_low() handles the errors appropriately */ |
| 1012 |
return NULL; |
| 1013 |
} |
| 1014 |
} |
| 1015 |
|
| 1016 |
/* |
| 1017 |
=item i_readgif_single_wiol(ig, page) |
| 1018 |
|
| 1019 |
Read a single page from a GIF image file, where the page is indexed |
| 1020 |
from 0. |
| 1021 |
|
| 1022 |
Returns NULL if the page isn't found. |
| 1023 |
|
| 1024 |
=cut |
| 1025 |
*/ |
| 1026 |
|
| 1027 |
i_img * |
| 1028 |
i_readgif_single_wiol(io_glue *ig, int page) { |
| 1029 |
GifFileType *GifFile; |
| 1030 |
int gif_error; |
| 1031 |
i_img *result; |
| 1032 |
|
| 1033 |
i_clear_error(); |
| 1034 |
if (page < 0) { |
| 1035 |
i_push_error(0, "page must be non-negative"); |
| 1036 |
return NULL; |
| 1037 |
} |
| 1038 |
|
| 1039 |
gif_mutex_lock(mutex); |
| 1040 |
|
| 1041 |
if ((GifFile = myDGifOpen((void *)ig, io_glue_read_cb, &gif_error )) == NULL) { |
| 1042 |
gif_push_error(gif_error); |
| 1043 |
i_push_error(0, "Cannot create giflib callback object"); |
| 1044 |
mm_log((1,"i_readgif_wiol: Unable to open callback datasource.\n")); |
| 1045 |
gif_mutex_unlock(mutex); |
| 1046 |
return NULL; |
| 1047 |
} |
| 1048 |
|
| 1049 |
result = i_readgif_single_low(GifFile, page); |
| 1050 |
|
| 1051 |
gif_mutex_unlock(mutex); |
| 1052 |
|
| 1053 |
return result; |
| 1054 |
} |
| 1055 |
|
| 1056 |
/* |
| 1057 |
=item do_write(GifFileType *gf, i_gif_opts *opts, i_img *img, i_palidx *data) |
| 1058 |
|
| 1059 |
Internal. Low level image write function. Writes in interlace if |
| 1060 |
that was requested in the GIF options. |
| 1061 |
|
| 1062 |
Returns non-zero on success. |
| 1063 |
|
| 1064 |
=cut |
| 1065 |
*/ |
| 1066 |
static undef_int |
| 1067 |
do_write(GifFileType *gf, int interlace, i_img *img, i_palidx *data) { |
| 1068 |
if (interlace) { |
| 1069 |
int i, j; |
| 1070 |
for (i = 0; i < 4; ++i) { |
| 1071 |
for (j = InterlacedOffset[i]; j < img->ysize; j += InterlacedJumps[i]) { |
| 1072 |
if (EGifPutLine(gf, data+j*img->xsize, gf->Image.Width) == GIF_ERROR) { |
| 1073 |
gif_push_error(myGifError(gf)); |
| 1074 |
i_push_error(0, "Could not save image data:"); |
| 1075 |
mm_log((1, "Error in EGifPutLine\n")); |
| 1076 |
return 0; |
| 1077 |
} |
| 1078 |
} |
| 1079 |
} |
| 1080 |
} |
| 1081 |
else { |
| 1082 |
int y; |
| 1083 |
for (y = 0; y < img->ysize; ++y) { |
| 1084 |
if (EGifPutLine(gf, data, gf->Image.Width) == GIF_ERROR) { |
| 1085 |
gif_push_error(myGifError(gf)); |
| 1086 |
i_push_error(0, "Could not save image data:"); |
| 1087 |
mm_log((1, "Error in EGifPutLine\n")); |
| 1088 |
return 0; |
| 1089 |
} |
| 1090 |
data += img->xsize; |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
return 1; |
| 1095 |
} |
| 1096 |
|
| 1097 |
/* |
| 1098 |
=item do_gce(GifFileType *gf, int index, i_gif_opts *opts, int want_trans, int trans_index) |
| 1099 |
|
| 1100 |
Internal. Writes the GIF graphics control extension, if necessary. |
| 1101 |
|
| 1102 |
Returns non-zero on success. |
| 1103 |
|
| 1104 |
=cut |
| 1105 |
*/ |
| 1106 |
|
| 1107 |
static int |
| 1108 |
do_gce(GifFileType *gf, i_img *img, int want_trans, int trans_index) |
| 1109 |
{ |
| 1110 |
unsigned char gce[4] = {0}; |
| 1111 |
int want_gce = 0; |
| 1112 |
int delay; |
| 1113 |
int user_input; |
| 1114 |
int disposal_method; |
| 1115 |
|
| 1116 |
if (want_trans) { |
| 1117 |
gce[0] |= 1U; |
| 1118 |
gce[3] = trans_index; |
| 1119 |
++want_gce; |
| 1120 |
} |
| 1121 |
if (i_tags_get_int(&img->tags, "gif_delay", 0, &delay)) { |
| 1122 |
gce[1] = delay % 256; |
| 1123 |
gce[2] = delay / 256; |
| 1124 |
++want_gce; |
| 1125 |
} |
| 1126 |
if (i_tags_get_int(&img->tags, "gif_user_input", 0, &user_input) |
| 1127 |
&& user_input) { |
| 1128 |
gce[0] |= 2U; |
| 1129 |
++want_gce; |
| 1130 |
} |
| 1131 |
if (i_tags_get_int(&img->tags, "gif_disposal", 0, &disposal_method)) { |
| 1132 |
gce[0] |= (disposal_method & 3) << 2; |
| 1133 |
++want_gce; |
| 1134 |
} |
| 1135 |
if (want_gce) { |
| 1136 |
if (EGifPutExtension(gf, 0xF9, sizeof(gce), gce) == GIF_ERROR) { |
| 1137 |
gif_push_error(myGifError(gf)); |
| 1138 |
i_push_error(0, "Could not save GCE"); |
| 1139 |
} |
| 1140 |
} |
| 1141 |
return 1; |
| 1142 |
} |
| 1143 |
|
| 1144 |
/* |
| 1145 |
=item do_comments(gf, img) |
| 1146 |
|
| 1147 |
Write any comments in the image. |
| 1148 |
|
| 1149 |
=cut |
| 1150 |
*/ |
| 1151 |
|
| 1152 |
static int |
| 1153 |
do_comments(GifFileType *gf, i_img *img) { |
| 1154 |
int pos = -1; |
| 1155 |
|
| 1156 |
while (i_tags_find(&img->tags, "gif_comment", pos+1, &pos)) { |
| 1157 |
if (img->tags.tags[pos].data) { |
| 1158 |
if (EGifPutComment(gf, img->tags.tags[pos].data) == GIF_ERROR) { |
| 1159 |
return 0; |
| 1160 |
} |
| 1161 |
} |
| 1162 |
else { |
| 1163 |
char buf[50]; |
| 1164 |
int len = snprintf(buf, sizeof(buf), "%d", img->tags.tags[pos].idata); |
| 1165 |
if ((size_t)len >= sizeof(buf) |
| 1166 |
|| EGifPutComment(gf, buf) == GIF_ERROR) { |
| 1167 |
return 0; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
} |
| 1171 |
|
| 1172 |
return 1; |
| 1173 |
} |
| 1174 |
|
| 1175 |
/* |
| 1176 |
=item do_ns_loop(GifFileType *gf, i_gif_opts *opts) |
| 1177 |
|
| 1178 |
Internal. Add the Netscape2.0 loop extension block, if requested. |
| 1179 |
|
| 1180 |
Giflib/libungif prior to 4.1.1 didn't support writing application |
| 1181 |
extension blocks, so we don't attempt to write them for older versions. |
| 1182 |
|
| 1183 |
Giflib/libungif prior to 4.1.3 used the wrong write mechanism when |
| 1184 |
writing extension blocks so that they could only be written to files. |
| 1185 |
|
| 1186 |
=cut |
| 1187 |
*/ |
| 1188 |
|
| 1189 |
static int |
| 1190 |
do_ns_loop(GifFileType *gf, i_img *img) |
| 1191 |
{ |
| 1192 |
/* EGifPutExtension() doesn't appear to handle application |
| 1193 |
extension blocks in any way |
| 1194 |
Since giflib wraps the fd with a FILE * (and puts that in its |
| 1195 |
private data), we can't do an end-run and write the data |
| 1196 |
directly to the fd. |
| 1197 |
There's no open interface that takes a FILE * either, so we |
| 1198 |
can't workaround it that way either. |
| 1199 |
If giflib's callback interface wasn't broken by default, I'd |
| 1200 |
force file writes to use callbacks, but it is broken by default. |
| 1201 |
*/ |
| 1202 |
/* yes this was another attempt at supporting the loop extension */ |
| 1203 |
int loop_count; |
| 1204 |
if (i_tags_get_int(&img->tags, "gif_loop", 0, &loop_count)) { |
| 1205 |
unsigned char nsle[12] = "NETSCAPE2.0"; |
| 1206 |
unsigned char subblock[3]; |
| 1207 |
|
| 1208 |
subblock[0] = 1; |
| 1209 |
subblock[1] = loop_count % 256; |
| 1210 |
subblock[2] = loop_count / 256; |
| 1211 |
|
| 1212 |
#if IMGIFLIB_API_VERSION >= 500 |
| 1213 |
if (EGifPutExtensionLeader(gf, APPLICATION_EXT_FUNC_CODE) == GIF_ERROR |
| 1214 |
|| EGifPutExtensionBlock(gf, 11, nsle) == GIF_ERROR |
| 1215 |
|| EGifPutExtensionBlock(gf, 3, subblock) == GIF_ERROR |
| 1216 |
|| EGifPutExtensionTrailer(gf) == GIF_ERROR) { |
| 1217 |
gif_push_error(myGifError(gf)); |
| 1218 |
i_push_error(0, "writing loop extension"); |
| 1219 |
return 0; |
| 1220 |
} |
| 1221 |
|
| 1222 |
#else |
| 1223 |
if (EGifPutExtensionFirst(gf, APPLICATION_EXT_FUNC_CODE, 11, nsle) == GIF_ERROR) { |
| 1224 |
gif_push_error(myGifError(gf)); |
| 1225 |
i_push_error(0, "writing loop extension"); |
| 1226 |
return 0; |
| 1227 |
} |
| 1228 |
if (EGifPutExtensionLast(gf, APPLICATION_EXT_FUNC_CODE, 3, subblock) == GIF_ERROR) { |
| 1229 |
gif_push_error(myGifError(gf)); |
| 1230 |
i_push_error(0, "writing loop extension sub-block"); |
| 1231 |
return 0; |
| 1232 |
} |
| 1233 |
#endif |
| 1234 |
} |
| 1235 |
|
| 1236 |
return 1; |
| 1237 |
} |
| 1238 |
|
| 1239 |
/* |
| 1240 |
=item make_gif_map(i_quantize *quant, int want_trans) |
| 1241 |
|
| 1242 |
Create a giflib color map object from an Imager color map. |
| 1243 |
|
| 1244 |
=cut |
| 1245 |
*/ |
| 1246 |
|
| 1247 |
static ColorMapObject * |
| 1248 |
make_gif_map(i_quantize *quant, i_img *img, int want_trans) { |
| 1249 |
GifColorType colors[256]; |
| 1250 |
int i; |
| 1251 |
int size = quant->mc_count; |
| 1252 |
int map_size; |
| 1253 |
ColorMapObject *map; |
| 1254 |
i_color trans; |
| 1255 |
|
| 1256 |
for (i = 0; i < quant->mc_count; ++i) { |
| 1257 |
colors[i].Red = quant->mc_colors[i].rgb.r; |
| 1258 |
colors[i].Green = quant->mc_colors[i].rgb.g; |
| 1259 |
colors[i].Blue = quant->mc_colors[i].rgb.b; |
| 1260 |
} |
| 1261 |
if (want_trans) { |
| 1262 |
if (!i_tags_get_color(&img->tags, "gif_trans_color", 0, &trans)) |
| 1263 |
trans.rgb.r = trans.rgb.g = trans.rgb.b = 0; |
| 1264 |
colors[size].Red = trans.rgb.r; |
| 1265 |
colors[size].Green = trans.rgb.g; |
| 1266 |
colors[size].Blue = trans.rgb.b; |
| 1267 |
++size; |
| 1268 |
} |
| 1269 |
map_size = 1; |
| 1270 |
while (map_size < size) |
| 1271 |
map_size <<= 1; |
| 1272 |
/* giflib spews for 1 colour maps, reasonable, I suppose */ |
| 1273 |
if (map_size == 1) |
| 1274 |
map_size = 2; |
| 1275 |
while (i < map_size) { |
| 1276 |
colors[i].Red = colors[i].Green = colors[i].Blue = 0; |
| 1277 |
++i; |
| 1278 |
} |
| 1279 |
|
| 1280 |
map = MakeMapObject(map_size, colors); |
| 1281 |
if (!map) { |
| 1282 |
i_push_error(0, "Could not create color map object"); |
| 1283 |
return NULL; |
| 1284 |
} |
| 1285 |
mm_log((1, "XXX map is at %p and colors at %p\n", map, map->Colors)); |
| 1286 |
#if IMGIFLIB_API_VERSION >= 500 |
| 1287 |
map->SortFlag = 0; |
| 1288 |
#endif |
| 1289 |
return map; |
| 1290 |
} |
| 1291 |
|
| 1292 |
/* |
| 1293 |
=item need_version_89a(i_quantize *quant, i_img *imgs, int count) |
| 1294 |
|
| 1295 |
Return true if the file we're creating on these images needs a GIF89a |
| 1296 |
header. |
| 1297 |
|
| 1298 |
=cut |
| 1299 |
*/ |
| 1300 |
|
| 1301 |
static int |
| 1302 |
need_version_89a(i_quantize *quant, i_img **imgs, int count) { |
| 1303 |
int need_89a = 0; |
| 1304 |
int temp; |
| 1305 |
int i; |
| 1306 |
|
| 1307 |
for (i = 0; i < count; ++i) { |
| 1308 |
if (quant->transp != tr_none && |
| 1309 |
(imgs[i]->channels == 2 || imgs[i]->channels == 4)) { |
| 1310 |
need_89a = 1; |
| 1311 |
break; |
| 1312 |
} |
| 1313 |
if (i_tags_get_int(&imgs[i]->tags, "gif_delay", 0, &temp)) { |
| 1314 |
need_89a = 1; |
| 1315 |
break; |
| 1316 |
} |
| 1317 |
if (i_tags_get_int(&imgs[i]->tags, "gif_user_input", 0, &temp) && temp) { |
| 1318 |
need_89a = 1; |
| 1319 |
break; |
| 1320 |
} |
| 1321 |
if (i_tags_get_int(&imgs[i]->tags, "gif_disposal", 0, &temp)) { |
| 1322 |
need_89a = 1; |
| 1323 |
break; |
| 1324 |
} |
| 1325 |
if (i_tags_get_int(&imgs[i]->tags, "gif_loop", 0, &temp)) { |
| 1326 |
need_89a = 1; |
| 1327 |
break; |
| 1328 |
} |
| 1329 |
} |
| 1330 |
|
| 1331 |
return need_89a; |
| 1332 |
} |
| 1333 |
|
| 1334 |
static int |
| 1335 |
in_palette(i_color *c, i_quantize *quant, int size) { |
| 1336 |
int i; |
| 1337 |
|
| 1338 |
for (i = 0; i < size; ++i) { |
| 1339 |
if (c->channel[0] == quant->mc_colors[i].channel[0] |
| 1340 |
&& c->channel[1] == quant->mc_colors[i].channel[1] |
| 1341 |
&& c->channel[2] == quant->mc_colors[i].channel[2]) { |
| 1342 |
return i; |
| 1343 |
} |
| 1344 |
} |
| 1345 |
|
| 1346 |
return -1; |
| 1347 |
} |
| 1348 |
|
| 1349 |
/* |
| 1350 |
=item has_common_palette(imgs, count, quant) |
| 1351 |
|
| 1352 |
Tests if all the given images are paletted and their colors are in the |
| 1353 |
palette produced. |
| 1354 |
|
| 1355 |
Previously this would build a consolidated palette from the source, |
| 1356 |
but that meant that if the caller supplied a static palette (or |
| 1357 |
specified a fixed palette like "webmap") then we wouldn't be |
| 1358 |
quantizing to the caller specified palette. |
| 1359 |
|
| 1360 |
=cut |
| 1361 |
*/ |
| 1362 |
|
| 1363 |
static int |
| 1364 |
has_common_palette(i_img **imgs, int count, i_quantize *quant) { |
| 1365 |
int i; |
| 1366 |
int imgn; |
| 1367 |
char used[256]; |
| 1368 |
int col_count; |
| 1369 |
|
| 1370 |
/* we try to build a common palette here, if we can manage that, then |
| 1371 |
that's the palette we use */ |
| 1372 |
for (imgn = 0; imgn < count; ++imgn) { |
| 1373 |
int eliminate_unused; |
| 1374 |
if (imgs[imgn]->type != i_palette_type) |
| 1375 |
return 0; |
| 1376 |
|
| 1377 |
if (!i_tags_get_int(&imgs[imgn]->tags, "gif_eliminate_unused", 0, |
| 1378 |
&eliminate_unused)) { |
| 1379 |
eliminate_unused = 1; |
| 1380 |
} |
| 1381 |
|
| 1382 |
if (eliminate_unused) { |
| 1383 |
i_palidx *line = mymalloc(sizeof(i_palidx) * imgs[imgn]->xsize); |
| 1384 |
int x, y; |
| 1385 |
memset(used, 0, sizeof(used)); |
| 1386 |
|
| 1387 |
for (y = 0; y < imgs[imgn]->ysize; ++y) { |
| 1388 |
i_gpal(imgs[imgn], 0, imgs[imgn]->xsize, y, line); |
| 1389 |
for (x = 0; x < imgs[imgn]->xsize; ++x) |
| 1390 |
used[line[x]] = 1; |
| 1391 |
} |
| 1392 |
|
| 1393 |
myfree(line); |
| 1394 |
} |
| 1395 |
else { |
| 1396 |
/* assume all are in use */ |
| 1397 |
memset(used, 1, sizeof(used)); |
| 1398 |
} |
| 1399 |
|
| 1400 |
col_count = i_colorcount(imgs[imgn]); |
| 1401 |
for (i = 0; i < col_count; ++i) { |
| 1402 |
i_color c; |
| 1403 |
|
| 1404 |
if (used[i] && i_getcolors(imgs[imgn], i, &c, 1) == 1) { |
| 1405 |
if (in_palette(&c, quant, quant->mc_count) < 0) { |
| 1406 |
mm_log((1, " color not found in palette, no palette shortcut\n")); |
| 1407 |
|
| 1408 |
return 0; |
| 1409 |
} |
| 1410 |
} |
| 1411 |
} |
| 1412 |
} |
| 1413 |
|
| 1414 |
mm_log((1, " all colors found in palette, palette shortcut\n")); |
| 1415 |
|
| 1416 |
return 1; |
| 1417 |
} |
| 1418 |
|
| 1419 |
static i_palidx * |
| 1420 |
quant_paletted(i_quantize *quant, i_img *img) { |
| 1421 |
i_palidx *data = mymalloc(sizeof(i_palidx) * img->xsize * img->ysize); |
| 1422 |
i_palidx *p = data; |
| 1423 |
i_palidx trans[256]; |
| 1424 |
int i; |
| 1425 |
i_img_dim x, y; |
| 1426 |
|
| 1427 |
/* build a translation table */ |
| 1428 |
for (i = 0; i < i_colorcount(img); ++i) { |
| 1429 |
i_color c; |
| 1430 |
i_getcolors(img, i, &c, 1); |
| 1431 |
trans[i] = in_palette(&c, quant, quant->mc_count); |
| 1432 |
} |
| 1433 |
|
| 1434 |
for (y = 0; y < img->ysize; ++y) { |
| 1435 |
i_gpal(img, 0, img->xsize, y, data+img->xsize * y); |
| 1436 |
for (x = 0; x < img->xsize; ++x) { |
| 1437 |
*p = trans[*p]; |
| 1438 |
++p; |
| 1439 |
} |
| 1440 |
} |
| 1441 |
|
| 1442 |
return data; |
| 1443 |
} |
| 1444 |
|
| 1445 |
/* |
| 1446 |
=item i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count, i_gif_opts *opts) |
| 1447 |
|
| 1448 |
Internal. Low-level function that does the high-level GIF processing |
| 1449 |
:) |
| 1450 |
|
| 1451 |
Returns non-zero on success. |
| 1452 |
|
| 1453 |
=cut |
| 1454 |
*/ |
| 1455 |
|
| 1456 |
static undef_int |
| 1457 |
i_writegif_low(i_quantize *quant, GifFileType *gf, i_img **imgs, int count) { |
| 1458 |
unsigned char *result = NULL; |
| 1459 |
int color_bits; |
| 1460 |
ColorMapObject *map; |
| 1461 |
int scrw = 0, scrh = 0; |
| 1462 |
int imgn, orig_count, orig_size; |
| 1463 |
int posx, posy; |
| 1464 |
int trans_index = -1; |
| 1465 |
int *localmaps = NULL; |
| 1466 |
int anylocal; |
| 1467 |
i_img **glob_imgs = NULL; /* images that will use the global color map */ |
| 1468 |
int glob_img_count; |
| 1469 |
i_color *orig_colors = quant->mc_colors; |
| 1470 |
i_color *glob_colors = NULL; |
| 1471 |
int glob_color_count = 0; |
| 1472 |
int glob_want_trans; |
| 1473 |
int glob_paletted = 0; /* the global map was made from the image palettes */ |
| 1474 |
int colors_paletted = 0; |
| 1475 |
int want_trans = 0; |
| 1476 |
int interlace; |
| 1477 |
int gif_background; |
| 1478 |
int error; |
| 1479 |
|
| 1480 |
mm_log((1, "i_writegif_low(quant %p, gf %p, imgs %p, count %d)\n", |
| 1481 |
quant, gf, imgs, count)); |
| 1482 |
|
| 1483 |
/* *((char *)0) = 1; */ /* used to break into the debugger */ |
| 1484 |
|
| 1485 |
if (count <= 0) { |
| 1486 |
i_push_error(0, "No images provided to write"); |
| 1487 |
goto fail_cleanup; |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* sanity is nice */ |
| 1491 |
if (quant->mc_size > 256) |
| 1492 |
quant->mc_size = 256; |
| 1493 |
if (quant->mc_count > quant->mc_size) |
| 1494 |
quant->mc_count = quant->mc_size; |
| 1495 |
|
| 1496 |
if (!i_tags_get_int(&imgs[0]->tags, "gif_screen_width", 0, &scrw)) |
| 1497 |
scrw = 0; |
| 1498 |
if (!i_tags_get_int(&imgs[0]->tags, "gif_screen_height", 0, &scrh)) |
| 1499 |
scrh = 0; |
| 1500 |
|
| 1501 |
anylocal = 0; |
| 1502 |
localmaps = mymalloc(sizeof(int) * count); |
| 1503 |
glob_imgs = mymalloc(sizeof(i_img *) * count); |
| 1504 |
glob_img_count = 0; |
| 1505 |
glob_want_trans = 0; |
| 1506 |
for (imgn = 0; imgn < count; ++imgn) { |
| 1507 |
i_img *im = imgs[imgn]; |
| 1508 |
if (im->xsize > 0xFFFF || im->ysize > 0xFFFF) { |
| 1509 |
i_push_error(0, "image too large for GIF"); |
| 1510 |
goto fail_cleanup; |
| 1511 |
} |
| 1512 |
|
| 1513 |
posx = posy = 0; |
| 1514 |
i_tags_get_int(&im->tags, "gif_left", 0, &posx); |
| 1515 |
if (posx < 0) posx = 0; |
| 1516 |
i_tags_get_int(&im->tags, "gif_top", 0, &posy); |
| 1517 |
if (posy < 0) posy = 0; |
| 1518 |
if (im->xsize + posx > scrw) |
| 1519 |
scrw = im->xsize + posx; |
| 1520 |
if (im->ysize + posy > scrh) |
| 1521 |
scrh = im->ysize + posy; |
| 1522 |
if (!i_tags_get_int(&im->tags, "gif_local_map", 0, localmaps+imgn)) |
| 1523 |
localmaps[imgn] = 0; |
| 1524 |
if (localmaps[imgn]) |
| 1525 |
anylocal = 1; |
| 1526 |
else { |
| 1527 |
if (im->channels == 4) { |
| 1528 |
glob_want_trans = 1; |
| 1529 |
} |
| 1530 |
glob_imgs[glob_img_count++] = im; |
| 1531 |
} |
| 1532 |
} |
| 1533 |
glob_want_trans = glob_want_trans && quant->transp != tr_none ; |
| 1534 |
|
| 1535 |
if (scrw > 0xFFFF || scrh > 0xFFFF) { |
| 1536 |
i_push_error(0, "screen size too large for GIF"); |
| 1537 |
goto fail_cleanup; |
| 1538 |
} |
| 1539 |
|
| 1540 |
orig_count = quant->mc_count; |
| 1541 |
orig_size = quant->mc_size; |
| 1542 |
|
| 1543 |
if (glob_img_count) { |
| 1544 |
/* this is ugly */ |
| 1545 |
glob_colors = mymalloc(sizeof(i_color) * quant->mc_size); |
| 1546 |
quant->mc_colors = glob_colors; |
| 1547 |
memcpy(glob_colors, orig_colors, sizeof(i_color) * quant->mc_count); |
| 1548 |
/* we have some images that want to use the global map */ |
| 1549 |
if (glob_want_trans && quant->mc_count == 256) { |
| 1550 |
mm_log((2, " disabling transparency for global map - no space\n")); |
| 1551 |
glob_want_trans = 0; |
| 1552 |
} |
| 1553 |
if (glob_want_trans && quant->mc_size == 256) { |
| 1554 |
mm_log((2, " reserving color for transparency\n")); |
| 1555 |
--quant->mc_size; |
| 1556 |
} |
| 1557 |
|
| 1558 |
i_quant_makemap(quant, glob_imgs, glob_img_count); |
| 1559 |
glob_paletted = has_common_palette(glob_imgs, glob_img_count, quant); |
| 1560 |
glob_color_count = quant->mc_count; |
| 1561 |
quant->mc_colors = orig_colors; |
| 1562 |
} |
| 1563 |
|
| 1564 |
/* use the global map if we have one, otherwise use the local map */ |
| 1565 |
gif_background = 0; |
| 1566 |
if (glob_colors) { |
| 1567 |
quant->mc_colors = glob_colors; |
| 1568 |
quant->mc_count = glob_color_count; |
| 1569 |
want_trans = glob_want_trans && imgs[0]->channels == 4; |
| 1570 |
|
| 1571 |
if (!i_tags_get_int(&imgs[0]->tags, "gif_background", 0, &gif_background)) |
| 1572 |
gif_background = 0; |
| 1573 |
if (gif_background < 0) |
| 1574 |
gif_background = 0; |
| 1575 |
if (gif_background >= glob_color_count) |
| 1576 |
gif_background = 0; |
| 1577 |
} |
| 1578 |
else { |
| 1579 |
want_trans = quant->transp != tr_none && imgs[0]->channels == 4; |
| 1580 |
i_quant_makemap(quant, imgs, 1); |
| 1581 |
colors_paletted = has_common_palette(imgs, 1, quant); |
| 1582 |
} |
| 1583 |
|
| 1584 |
if ((map = make_gif_map(quant, imgs[0], want_trans)) == NULL) { |
| 1585 |
mm_log((1, "Error in MakeMapObject")); |
| 1586 |
goto fail_cleanup; |
| 1587 |
} |
| 1588 |
color_bits = 1; |
| 1589 |
if (anylocal) { |
| 1590 |
/* since we don't know how big some the local palettes could be |
| 1591 |
we need to base the bits on the maximum number of colors */ |
| 1592 |
while (orig_size > (1 << color_bits)) |
| 1593 |
++color_bits; |
| 1594 |
} |
| 1595 |
else { |
| 1596 |
int count = quant->mc_count; |
| 1597 |
if (want_trans) |
| 1598 |
++count; |
| 1599 |
while (count > (1 << color_bits)) |
| 1600 |
++color_bits; |
| 1601 |
} |
| 1602 |
|
| 1603 |
if (EGifPutScreenDesc(gf, scrw, scrh, color_bits, |
| 1604 |
gif_background, map) == GIF_ERROR) { |
| 1605 |
gif_push_error(myGifError(gf)); |
| 1606 |
i_push_error(0, "Could not save screen descriptor"); |
| 1607 |
FreeMapObject(map); |
| 1608 |
mm_log((1, "Error in EGifPutScreenDesc.")); |
| 1609 |
goto fail_cleanup; |
| 1610 |
} |
| 1611 |
FreeMapObject(map); |
| 1612 |
|
| 1613 |
if (!i_tags_get_int(&imgs[0]->tags, "gif_left", 0, &posx)) |
| 1614 |
posx = 0; |
| 1615 |
if (!i_tags_get_int(&imgs[0]->tags, "gif_top", 0, &posy)) |
| 1616 |
posy = 0; |
| 1617 |
|
| 1618 |
if (!localmaps[0]) { |
| 1619 |
map = NULL; |
| 1620 |
colors_paletted = glob_paletted; |
| 1621 |
} |
| 1622 |
else { |
| 1623 |
/* if this image has a global map the colors in quant don't |
| 1624 |
belong to this image, so build a palette */ |
| 1625 |
if (glob_colors) { |
| 1626 |
/* generate the local map for this image */ |
| 1627 |
quant->mc_colors = orig_colors; |
| 1628 |
quant->mc_size = orig_size; |
| 1629 |
quant->mc_count = orig_count; |
| 1630 |
want_trans = quant->transp != tr_none && imgs[0]->channels == 4; |
| 1631 |
|
| 1632 |
/* if the caller gives us too many colours we can't do transparency */ |
| 1633 |
if (want_trans && quant->mc_count == 256) |
| 1634 |
want_trans = 0; |
| 1635 |
/* if they want transparency but give us a big size, make it smaller |
| 1636 |
to give room for a transparency colour */ |
| 1637 |
if (want_trans && quant->mc_size == 256) |
| 1638 |
--quant->mc_size; |
| 1639 |
i_quant_makemap(quant, imgs, 1); |
| 1640 |
colors_paletted = has_common_palette(imgs, 1, quant); |
| 1641 |
if ((map = make_gif_map(quant, imgs[0], want_trans)) == NULL) { |
| 1642 |
mm_log((1, "Error in MakeMapObject")); |
| 1643 |
goto fail_cleanup; |
| 1644 |
} |
| 1645 |
} |
| 1646 |
else { |
| 1647 |
/* the map we wrote was the map for this image - don't set the local |
| 1648 |
map */ |
| 1649 |
map = NULL; |
| 1650 |
} |
| 1651 |
} |
| 1652 |
|
| 1653 |
if (colors_paletted) |
| 1654 |
result = quant_paletted(quant, imgs[0]); |
| 1655 |
else |
| 1656 |
result = i_quant_translate(quant, imgs[0]); |
| 1657 |
if (!result) { |
| 1658 |
goto fail_cleanup; |
| 1659 |
} |
| 1660 |
if (want_trans) { |
| 1661 |
i_quant_transparent(quant, result, imgs[0], quant->mc_count); |
| 1662 |
trans_index = quant->mc_count; |
| 1663 |
} |
| 1664 |
|
| 1665 |
if (!do_ns_loop(gf, imgs[0])) { |
| 1666 |
goto fail_cleanup; |
| 1667 |
} |
| 1668 |
|
| 1669 |
if (!do_gce(gf, imgs[0], want_trans, trans_index)) { |
| 1670 |
goto fail_cleanup; |
| 1671 |
} |
| 1672 |
|
| 1673 |
if (!do_comments(gf, imgs[0])) { |
| 1674 |
goto fail_cleanup; |
| 1675 |
} |
| 1676 |
|
| 1677 |
if (!i_tags_get_int(&imgs[0]->tags, "gif_interlace", 0, &interlace)) |
| 1678 |
interlace = 0; |
| 1679 |
if (EGifPutImageDesc(gf, posx, posy, imgs[0]->xsize, imgs[0]->ysize, |
| 1680 |
interlace, map) == GIF_ERROR) { |
| 1681 |
if (map) |
| 1682 |
FreeMapObject(map); |
| 1683 |
gif_push_error(myGifError(gf)); |
| 1684 |
i_push_error(0, "Could not save image descriptor"); |
| 1685 |
mm_log((1, "Error in EGifPutImageDesc.")); |
| 1686 |
goto fail_cleanup; |
| 1687 |
} |
| 1688 |
if (map) |
| 1689 |
FreeMapObject(map); |
| 1690 |
|
| 1691 |
if (!do_write(gf, interlace, imgs[0], result)) { |
| 1692 |
goto fail_cleanup; |
| 1693 |
} |
| 1694 |
myfree(result); |
| 1695 |
result = NULL; |
| 1696 |
|
| 1697 |
/* that first awful image is out of the way, do the rest */ |
| 1698 |
for (imgn = 1; imgn < count; ++imgn) { |
| 1699 |
if (localmaps[imgn]) { |
| 1700 |
quant->mc_colors = orig_colors; |
| 1701 |
quant->mc_count = orig_count; |
| 1702 |
quant->mc_size = orig_size; |
| 1703 |
|
| 1704 |
want_trans = quant->transp != tr_none |
| 1705 |
&& imgs[imgn]->channels == 4; |
| 1706 |
/* if the caller gives us too many colours we can't do transparency */ |
| 1707 |
if (want_trans && quant->mc_count == 256) |
| 1708 |
want_trans = 0; |
| 1709 |
/* if they want transparency but give us a big size, make it smaller |
| 1710 |
to give room for a transparency colour */ |
| 1711 |
if (want_trans && quant->mc_size == 256) |
| 1712 |
--quant->mc_size; |
| 1713 |
|
| 1714 |
if (has_common_palette(imgs+imgn, 1, quant)) { |
| 1715 |
result = quant_paletted(quant, imgs[imgn]); |
| 1716 |
} |
| 1717 |
else { |
| 1718 |
i_quant_makemap(quant, imgs+imgn, 1); |
| 1719 |
result = i_quant_translate(quant, imgs[imgn]); |
| 1720 |
} |
| 1721 |
if (!result) { |
| 1722 |
mm_log((1, "error in i_quant_translate()")); |
| 1723 |
goto fail_cleanup; |
| 1724 |
} |
| 1725 |
if (want_trans) { |
| 1726 |
i_quant_transparent(quant, result, imgs[imgn], quant->mc_count); |
| 1727 |
trans_index = quant->mc_count; |
| 1728 |
} |
| 1729 |
|
| 1730 |
if ((map = make_gif_map(quant, imgs[imgn], want_trans)) == NULL) { |
| 1731 |
mm_log((1, "Error in MakeMapObject.")); |
| 1732 |
goto fail_cleanup; |
| 1733 |
} |
| 1734 |
} |
| 1735 |
else { |
| 1736 |
quant->mc_colors = glob_colors; |
| 1737 |
quant->mc_count = glob_color_count; |
| 1738 |
if (glob_paletted) |
| 1739 |
result = quant_paletted(quant, imgs[imgn]); |
| 1740 |
else |
| 1741 |
result = i_quant_translate(quant, imgs[imgn]); |
| 1742 |
want_trans = glob_want_trans && imgs[imgn]->channels == 4; |
| 1743 |
if (want_trans) { |
| 1744 |
i_quant_transparent(quant, result, imgs[imgn], quant->mc_count); |
| 1745 |
trans_index = quant->mc_count; |
| 1746 |
} |
| 1747 |
map = NULL; |
| 1748 |
} |
| 1749 |
|
| 1750 |
if (!do_gce(gf, imgs[imgn], want_trans, trans_index)) { |
| 1751 |
goto fail_cleanup; |
| 1752 |
} |
| 1753 |
|
| 1754 |
if (!do_comments(gf, imgs[imgn])) { |
| 1755 |
goto fail_cleanup; |
| 1756 |
} |
| 1757 |
|
| 1758 |
if (!i_tags_get_int(&imgs[imgn]->tags, "gif_left", 0, &posx)) |
| 1759 |
posx = 0; |
| 1760 |
if (!i_tags_get_int(&imgs[imgn]->tags, "gif_top", 0, &posy)) |
| 1761 |
posy = 0; |
| 1762 |
|
| 1763 |
if (!i_tags_get_int(&imgs[imgn]->tags, "gif_interlace", 0, &interlace)) |
| 1764 |
interlace = 0; |
| 1765 |
if (EGifPutImageDesc(gf, posx, posy, imgs[imgn]->xsize, |
| 1766 |
imgs[imgn]->ysize, interlace, map) == GIF_ERROR) { |
| 1767 |
gif_push_error(myGifError(gf)); |
| 1768 |
i_push_error(0, "Could not save image descriptor"); |
| 1769 |
if (map) |
| 1770 |
FreeMapObject(map); |
| 1771 |
mm_log((1, "Error in EGifPutImageDesc.")); |
| 1772 |
goto fail_cleanup; |
| 1773 |
} |
| 1774 |
if (map) |
| 1775 |
FreeMapObject(map); |
| 1776 |
|
| 1777 |
if (!do_write(gf, interlace, imgs[imgn], result)) { |
| 1778 |
goto fail_cleanup; |
| 1779 |
} |
| 1780 |
myfree(result); |
| 1781 |
result = NULL; |
| 1782 |
} |
| 1783 |
|
| 1784 |
if (myEGifCloseFile(gf, &error) == GIF_ERROR) { |
| 1785 |
gif_push_error(error); |
| 1786 |
i_push_error(0, "Could not close GIF file"); |
| 1787 |
goto fail_cleanup; |
| 1788 |
} |
| 1789 |
if (glob_colors) { |
| 1790 |
int i; |
| 1791 |
for (i = 0; i < glob_color_count; ++i) |
| 1792 |
orig_colors[i] = glob_colors[i]; |
| 1793 |
} |
| 1794 |
|
| 1795 |
myfree(glob_colors); |
| 1796 |
myfree(localmaps); |
| 1797 |
myfree(glob_imgs); |
| 1798 |
quant->mc_colors = orig_colors; |
| 1799 |
|
| 1800 |
return 1; |
| 1801 |
|
| 1802 |
fail_cleanup: |
| 1803 |
quant->mc_colors = orig_colors; |
| 1804 |
myfree(result); |
| 1805 |
myfree(glob_colors); |
| 1806 |
myfree(localmaps); |
| 1807 |
myfree(glob_imgs); |
| 1808 |
(void)myEGifCloseFile(gf, &error); |
| 1809 |
return 0; |
| 1810 |
} |
| 1811 |
|
| 1812 |
static int |
| 1813 |
io_glue_write_cb(GifFileType *gft, const GifByteType *data, int length) { |
| 1814 |
io_glue *ig = (io_glue *)gft->UserData; |
| 1815 |
|
| 1816 |
return i_io_write(ig, data, length); |
| 1817 |
} |
| 1818 |
|
| 1819 |
|
| 1820 |
/* |
| 1821 |
=item i_writegif_wiol(ig, quant, opts, imgs, count) |
| 1822 |
|
| 1823 |
=cut |
| 1824 |
*/ |
| 1825 |
undef_int |
| 1826 |
i_writegif_wiol(io_glue *ig, i_quantize *quant, i_img **imgs, |
| 1827 |
int count) { |
| 1828 |
GifFileType *GifFile; |
| 1829 |
int gif_error; |
| 1830 |
int result; |
| 1831 |
|
| 1832 |
gif_mutex_lock(mutex); |
| 1833 |
|
| 1834 |
i_clear_error(); |
| 1835 |
|
| 1836 |
#ifdef PRE_SET_VERSION |
| 1837 |
EGifSetGifVersion(need_version_89a(quant, imgs, count) ? "89a" : "87a"); |
| 1838 |
#endif |
| 1839 |
|
| 1840 |
if ((GifFile = myEGifOpen((void *)ig, io_glue_write_cb, &gif_error )) == NULL) { |
| 1841 |
gif_push_error(gif_error); |
| 1842 |
i_push_error(0, "Cannot create giflib callback object"); |
| 1843 |
mm_log((1,"i_writegif_wiol: Unable to open callback datasource.\n")); |
| 1844 |
gif_mutex_unlock(mutex); |
| 1845 |
return 0; |
| 1846 |
} |
| 1847 |
|
| 1848 |
#ifdef POST_SET_VERSION |
| 1849 |
EGifSetGifVersion(GifFile, need_version_89a(quant, imgs, count)); |
| 1850 |
#endif |
| 1851 |
|
| 1852 |
result = i_writegif_low(quant, GifFile, imgs, count); |
| 1853 |
|
| 1854 |
gif_mutex_unlock(mutex); |
| 1855 |
|
| 1856 |
if (i_io_close(ig)) |
| 1857 |
return 0; |
| 1858 |
|
| 1859 |
return result; |
| 1860 |
} |
| 1861 |
|
| 1862 |
/* |
| 1863 |
=item gif_error_msg(int code) |
| 1864 |
|
| 1865 |
Grabs the most recent giflib error code from GifLastError() and |
| 1866 |
returns a string that describes that error. |
| 1867 |
|
| 1868 |
Returns NULL for unknown error codes. |
| 1869 |
|
| 1870 |
=cut |
| 1871 |
*/ |
| 1872 |
|
| 1873 |
static char const * |
| 1874 |
gif_error_msg(int code) { |
| 1875 |
#if IMGIFLIB_API_VERSION >= 500 |
| 1876 |
return GifErrorString(code); |
| 1877 |
#else |
| 1878 |
switch (code) { |
| 1879 |
case E_GIF_ERR_OPEN_FAILED: /* should not see this */ |
| 1880 |
return "Failed to open given file"; |
| 1881 |
|
| 1882 |
case E_GIF_ERR_WRITE_FAILED: |
| 1883 |
return "Write failed"; |
| 1884 |
|
| 1885 |
case E_GIF_ERR_HAS_SCRN_DSCR: /* should not see this */ |
| 1886 |
return "Screen descriptor already passed to giflib"; |
| 1887 |
|
| 1888 |
case E_GIF_ERR_HAS_IMAG_DSCR: /* should not see this */ |
| 1889 |
return "Image descriptor already passed to giflib"; |
| 1890 |
|
| 1891 |
case E_GIF_ERR_NO_COLOR_MAP: /* should not see this */ |
| 1892 |
return "Neither global nor local color map set"; |
| 1893 |
|
| 1894 |
case E_GIF_ERR_DATA_TOO_BIG: /* should not see this */ |
| 1895 |
return "Too much pixel data passed to giflib"; |
| 1896 |
|
| 1897 |
case E_GIF_ERR_NOT_ENOUGH_MEM: |
| 1898 |
return "Out of memory"; |
| 1899 |
|
| 1900 |
case E_GIF_ERR_DISK_IS_FULL: |
| 1901 |
return "Disk is full"; |
| 1902 |
|
| 1903 |
case E_GIF_ERR_CLOSE_FAILED: /* should not see this */ |
| 1904 |
return "File close failed"; |
| 1905 |
|
| 1906 |
case E_GIF_ERR_NOT_WRITEABLE: /* should not see this */ |
| 1907 |
return "File not writable"; |
| 1908 |
|
| 1909 |
case D_GIF_ERR_OPEN_FAILED: |
| 1910 |
return "Failed to open file"; |
| 1911 |
|
| 1912 |
case D_GIF_ERR_READ_FAILED: |
| 1913 |
return "Failed to read from file"; |
| 1914 |
|
| 1915 |
case D_GIF_ERR_NOT_GIF_FILE: |
| 1916 |
return "File is not a GIF file"; |
| 1917 |
|
| 1918 |
case D_GIF_ERR_NO_SCRN_DSCR: |
| 1919 |
return "No screen descriptor detected - invalid file"; |
| 1920 |
|
| 1921 |
case D_GIF_ERR_NO_IMAG_DSCR: |
| 1922 |
return "No image descriptor detected - invalid file"; |
| 1923 |
|
| 1924 |
case D_GIF_ERR_NO_COLOR_MAP: |
| 1925 |
return "No global or local color map found"; |
| 1926 |
|
| 1927 |
case D_GIF_ERR_WRONG_RECORD: |
| 1928 |
return "Wrong record type detected - invalid file?"; |
| 1929 |
|
| 1930 |
case D_GIF_ERR_DATA_TOO_BIG: |
| 1931 |
return "Data in file too big for image"; |
| 1932 |
|
| 1933 |
case D_GIF_ERR_NOT_ENOUGH_MEM: |
| 1934 |
return "Out of memory"; |
| 1935 |
|
| 1936 |
case D_GIF_ERR_CLOSE_FAILED: |
| 1937 |
return "Close failed"; |
| 1938 |
|
| 1939 |
case D_GIF_ERR_NOT_READABLE: |
| 1940 |
return "File not opened for read"; |
| 1941 |
|
| 1942 |
case D_GIF_ERR_IMAGE_DEFECT: |
| 1943 |
return "Defective image"; |
| 1944 |
|
| 1945 |
case D_GIF_ERR_EOF_TOO_SOON: |
| 1946 |
return "Unexpected EOF - invalid file"; |
| 1947 |
|
| 1948 |
default: |
| 1949 |
return NULL; |
| 1950 |
} |
| 1951 |
#endif |
| 1952 |
} |
| 1953 |
|
| 1954 |
/* |
| 1955 |
=item gif_push_error(code) |
| 1956 |
|
| 1957 |
Utility function that takes the current GIF error code, converts it to |
| 1958 |
an error message and pushes it on the error stack. |
| 1959 |
|
| 1960 |
=cut |
| 1961 |
*/ |
| 1962 |
|
| 1963 |
static void |
| 1964 |
gif_push_error(int code) { |
| 1965 |
const char *msg = gif_error_msg(code); |
| 1966 |
if (msg) |
| 1967 |
i_push_error(code, msg); |
| 1968 |
else |
| 1969 |
i_push_errorf(code, "Unknown GIF error %d", code); |
| 1970 |
} |
| 1971 |
|
| 1972 |
/* |
| 1973 |
=head1 AUTHOR |
| 1974 |
|
| 1975 |
Arnar M. Hrafnkelsson, [email protected] |
| 1976 |
|
| 1977 |
Tony Cook <[email protected]> |
| 1978 |
|
| 1979 |
=head1 SEE ALSO |
| 1980 |
|
| 1981 |
perl(1), Imager(3) |
| 1982 |
|
| 1983 |
=cut |
| 1984 |
|
| 1985 |
*/ |