X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;f=images%2Fio-libungif.c;h=c46484311cbe980a86b6a5032a57972ee56d4e1a;hb=62eda8640605c19ecd817e274e75717022349732;hp=a3bfc397eea43daf1c859e6dcbb676e765b300cc;hpb=abfc26f5cde53d1664f03ff49358ae9cc9037556;p=libucw.git diff --git a/images/io-libungif.c b/images/io-libungif.c index a3bfc397..c4648431 100644 --- a/images/io-libungif.c +++ b/images/io-libungif.c @@ -13,10 +13,17 @@ #include "lib/mempool.h" #include "lib/fastbuf.h" #include "images/images.h" +#include "images/error.h" #include "images/color.h" #include "images/io-main.h" + #include +struct libungif_read_data { + GifFileType *gif; + int transparent_index; +}; + static int libungif_read_func(GifFileType *gif, GifByteType *ptr, int len) { @@ -29,7 +36,8 @@ libungif_read_cancel(struct image_io *io) { DBG("libungif_read_cancel()"); - DGifCloseFile(io->read_data); + struct libungif_read_data *rd = io->read_data; + DGifCloseFile(rd->gif); } int @@ -39,16 +47,19 @@ libungif_read_header(struct image_io *io) /* 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."); + IMAGE_ERROR(io->context, IMAGE_ERROR_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."); + IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Gif read failed."); DGifCloseFile(gif); return 0; } @@ -56,7 +67,7 @@ libungif_read_header(struct image_io *io) 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."); + IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "There are no images in gif file."); DGifCloseFile(gif); return 0; } @@ -64,16 +75,16 @@ libungif_read_header(struct image_io *io) /* Read image parameters */ SavedImage *image = gif->SavedImages; if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 || - image->ImageDesc.Width > (int)IMAGE_MAX_SIZE || image->ImageDesc.Height > (int)IMAGE_MAX_SIZE)) + image->ImageDesc.Width > (int)image_max_dim || image->ImageDesc.Height > (int)image_max_dim)) { - image_thread_err(io->thread, IMAGE_ERR_INVALID_DIMENSIONS, "Invalid gif dimensions."); + IMAGE_ERROR(io->context, IMAGE_ERROR_INVALID_DIMENSIONS, "Invalid gif dimensions."); DGifCloseFile(gif); return 0; } ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap; if (unlikely(!color_map)) { - image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Missing palette."); + IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Missing palette."); DGifCloseFile(gif); return 0; } @@ -81,20 +92,44 @@ libungif_read_header(struct image_io *io) io->rows = image->ImageDesc.Height; if (unlikely((io->number_of_colors = color_map->ColorCount) > 256)) { - image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Too many gif colors."); + IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Too many gif colors."); DGifCloseFile(gif); return 0; } io->flags = COLOR_SPACE_RGB | IMAGE_IO_HAS_PALETTE; - /* FIXME transparent GIFs disabled */ -#if 0 - if (gif->SColorMap && !image->ImageDesc.ColorMap && (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_ERROR(io->context, IMAGE_ERROR_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); } -#endif /* Success */ io->read_cancel = libungif_read_cancel; @@ -106,7 +141,8 @@ libungif_read_data(struct image_io *io) { 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 */ @@ -121,18 +157,15 @@ libungif_read_data(struct image_io *io) byte *pixels = (byte *)image->RasterBits; ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap; GifColorType *palette = color_map->Colors; - uns background = ( - /* FIXME: transparent GIFs disabled */ -#if 1 - 0 && -#endif - gif->SColorMap && !image->ImageDesc.ColorMap) ? gif->SBackGroundColor : 256; 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; @@ -146,11 +179,11 @@ libungif_read_data(struct image_io *io) *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 @@ -174,8 +207,8 @@ libungif_read_data(struct image_io *io) } 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) @@ -198,8 +231,8 @@ libungif_read_data(struct image_io *io) } 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) @@ -223,8 +256,8 @@ libungif_read_data(struct image_io *io) } 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)