#include "images/io-main.h"
#include <gif_lib.h>
+struct libungif_read_data {
+ GifFileType *gif;
+ int transparent_index;
+};
+
static int
libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
{
{
DBG("libungif_read_cancel()");
- DGifCloseFile(io->read_data);
+ struct libungif_read_data *rd = io->read_data;
+ DGifCloseFile(rd->gif);
}
int
/* Create libungif structure */
GifFileType *gif;
- if (unlikely(!(gif = io->read_data = DGifOpen(io->fastbuf, libungif_read_func))))
+ if (unlikely(!(gif = DGifOpen(io->fastbuf, libungif_read_func))))
{
image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Cannot create libungif structure.");
return 0;
}
+ struct libungif_read_data *rd = io->read_data = mp_alloc(io->internal_pool, sizeof(*rd));
+ rd->gif = gif;
+
+ DBG("executing DGifSlurp()");
if (unlikely(DGifSlurp(gif) != GIF_OK))
{
image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Gif read failed.");
return 0;
}
+ DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
if (unlikely(!gif->ImageCount))
{
image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "There are no images in gif file.");
return 0;
}
io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE;
- if ((uns)gif->SBackGroundColor < (uns)color_map->ColorCount)
+
+ /* Search extension blocks */
+ rd->transparent_index = -1;
+ for (int i = 0; i < image->ExtensionBlockCount; i++)
{
- io->flags |= IMAGE_ALPHA | IMAGE_IO_HAS_BACKGROUND;
- GifColorType *background = color_map->Colors + gif->SBackGroundColor;
- color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
+ ExtensionBlock *e = image->ExtensionBlocks + i;
+ if (e->Function == 0xF9)
+ {
+ DBG("Found graphics control extension");
+ if (unlikely(e->ByteCount != 4))
+ {
+ image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Invalid graphics control extension.");
+ DGifCloseFile(gif);
+ return 0;
+ }
+ byte *b = e->Bytes;
+ /* transparent color present */
+ if (b[0] & 1)
+ {
+ rd->transparent_index = b[3];
+ io->flags |= IMAGE_ALPHA;
+ if (gif->SColorMap)
+ {
+ GifColorType *background = color_map->Colors + gif->SBackGroundColor;
+ color_make_rgb(&io->background_color, background->Red, background->Green, background->Blue);
+ }
+ }
+ /* We've got everything we need :-) */
+ break;
+ }
+ else
+ DBG("Found unknown extension: type=%d size=%d", e->Function, e->ByteCount);
}
/* Success */
{
DBG("libungif_read_data()");
- GifFileType *gif = io->read_data;
+ struct libungif_read_data *rd = io->read_data;
+ GifFileType *gif = rd->gif;
SavedImage *image = gif->SavedImages;
/* Prepare image */
byte *pixels = (byte *)image->RasterBits;
ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
GifColorType *palette = color_map->Colors;
- uns background = gif->SBackGroundColor;
byte *img_end = rdi.image->pixels + rdi.image->image_size;
/* Handle deinterlacing */
uns dein_step, dein_next;
if (image->ImageDesc.Interlace)
- dein_step = dein_next = rdi.image->row_size << 3;
+ {
+ DBG("Deinterlaced image");
+ dein_step = dein_next = rdi.image->row_size << 3;
+ }
else
dein_step = dein_next = rdi.image->row_size;
*pal_pos = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
if (pal_pos != pal_end)
bzero(pal_pos, pal_end - pal_pos);
- if (io->flags & IMAGE_IO_USE_BACKGROUND)
- color_put_grayscale(pal + background, &io->background_color);
+ if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
+ color_put_grayscale(pal + rd->transparent_index, &io->background_color);
# define DO_ROW_END do{ \
walk_row_start += dein_step; \
- if (walk_row_start > img_end) \
+ if (walk_row_start >= img_end) \
{ uns n = dein_next >> 1; walk_row_start = rdi.image->pixels + n, dein_step = dein_next; dein_next = n; } \
}while(0)
# define IMAGE_WALK_PREFIX(x) walk_##x
}
if (pal_pos != pal_end)
bzero(pal_pos, pal_end - pal_pos);
- if (background < 256)
- pal[background * 2 + 1] = 0;
+ if (rd->transparent_index >= 0)
+ pal[rd->transparent_index * 2 + 1] = 0;
# define IMAGE_WALK_PREFIX(x) walk_##x
# define IMAGE_WALK_INLINE
# define IMAGE_WALK_IMAGE (rdi.image)
}
if (pal_pos != pal_end)
bzero(pal_pos, pal_end - pal_pos);
- if (io->flags & IMAGE_IO_USE_BACKGROUND)
- color_put_rgb(pal + background, &io->background_color);
+ if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
+ color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
# define IMAGE_WALK_PREFIX(x) walk_##x
# define IMAGE_WALK_INLINE
# define IMAGE_WALK_IMAGE (rdi.image)
}
if (pal_pos != pal_end)
bzero(pal_pos, pal_end - pal_pos);
- if (background < 256)
- pal[background * 4 + 3] = 0;
+ if (rd->transparent_index >= 0)
+ pal[rd->transparent_index * 4 + 3] = 0;
# define IMAGE_WALK_PREFIX(x) walk_##x
# define IMAGE_WALK_INLINE
# define IMAGE_WALK_IMAGE (rdi.image)