]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libungif.c
ucw docs: Array sorter
[libucw.git] / images / io-libungif.c
index 7c9850e4a04ec681b615354aff464bb5c4286baf..ac0f51f29b4e94a2892264dbce61e35f9163e93e 100644 (file)
@@ -9,12 +9,21 @@
 
 #undef LOCAL_DEBUG
 
 
 #undef LOCAL_DEBUG
 
-#include "lib/lib.h"
-#include "lib/mempool.h"
-#include "lib/fastbuf.h"
+#include "ucw/lib.h"
+#include "ucw/mempool.h"
+#include "ucw/fastbuf.h"
 #include "images/images.h"
 #include "images/images.h"
+#include "images/error.h"
+#include "images/color.h"
+#include "images/io-main.h"
+
 #include <gif_lib.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)
 {
 static int
 libungif_read_func(GifFileType *gif, GifByteType *ptr, int len)
 {
@@ -27,7 +36,8 @@ libungif_read_cancel(struct image_io *io)
 {
   DBG("libungif_read_cancel()");
 
 {
   DBG("libungif_read_cancel()");
 
-  DGifCloseFile(io->read_data);
+  struct libungif_read_data *rd = io->read_data;
+  DGifCloseFile(rd->gif);
 }
 
 int
 }
 
 int
@@ -37,22 +47,27 @@ libungif_read_header(struct image_io *io)
 
   /* Create libungif structure */
   GifFileType *gif;
 
   /* 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;
     }
 
       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))
     {
   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;
     }
 
       DGifCloseFile(gif);
       return 0;
     }
 
+  DBG("ImageCount=%d ColorResolution=%d SBackGroundColor=%d SColorMap=%p", gif->ImageCount, gif->SColorResolution, gif->SBackGroundColor, gif->SColorMap);
   if (unlikely(!gif->ImageCount))
     {
   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;
     }
       DGifCloseFile(gif);
       return 0;
     }
@@ -60,87 +75,128 @@ libungif_read_header(struct image_io *io)
   /* Read image parameters */
   SavedImage *image = gif->SavedImages;
   if (unlikely(image->ImageDesc.Width <= 0 || image->ImageDesc.Height <= 0 ||
   /* 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))
     {
       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;
     }
   io->cols = image->ImageDesc.Width;
   io->rows = image->ImageDesc.Height;
       DGifCloseFile(gif);
       return 0;
     }
   io->cols = image->ImageDesc.Width;
   io->rows = image->ImageDesc.Height;
-  io->has_palette = 1;
-  io->number_of_colors = color_map->ColorCount;
-  io->flags = COLOR_SPACE_RGB;
-  if ((uns)gif->SBackGroundColor < (uns)color_map->ColorCount)
-    io->flags |= IMAGE_ALPHA;
+  if (unlikely((io->number_of_colors = color_map->ColorCount) > 256))
+    {
+      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;
+
+  /* Search extension blocks */
+  rd->transparent_index = -1;
+  for (int i = 0; i < image->ExtensionBlockCount; i++)
+    {
+      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);
+    }
 
   /* Success */
   io->read_cancel = libungif_read_cancel;
   return 1;
 }
 
 
   /* Success */
   io->read_cancel = libungif_read_cancel;
   return 1;
 }
 
-static inline byte
-libungif_pixel_to_gray(GifColorType *pixel)
-{
-  return ((uns)pixel->Red * 19660 + (uns)pixel->Green * 38666 + (uns)pixel->Blue * 7210) >> 16;
-}
-
 int
 libungif_read_data(struct image_io *io)
 {
   DBG("libungif_read_data()");
 
 int
 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;
 
   SavedImage *image = gif->SavedImages;
 
-  /* Allocate image */
-  int need_scale = io->cols != (uns)image->ImageDesc.Width || io->rows != (uns)image->ImageDesc.Height;
-  int need_destroy = need_scale || !io->pool;
-  struct image *img = need_scale ?
-    image_new(io->thread, image->ImageDesc.Width, image->ImageDesc.Height, io->flags & IMAGE_CHANNELS_FORMAT, NULL) :
-    image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
-  if (unlikely(!img))
-    goto err;
+  /* Prepare image */
+  struct image_io_read_data_internals rdi;
+  uns read_flags = io->flags;
+  uns cs = read_flags & IMAGE_COLOR_SPACE;
+  if (cs != COLOR_SPACE_GRAYSCALE && cs != COLOR_SPACE_RGB)
+    read_flags = (read_flags & ~IMAGE_COLOR_SPACE & IMAGE_CHANNELS_FORMAT) | COLOR_SPACE_RGB;
+  if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, read_flags)))
+    {
+      DGifCloseFile(gif);
+      return 0;
+    }
 
   /* Get pixels and palette */
   byte *pixels = (byte *)image->RasterBits;
   ColorMapObject *color_map = image->ImageDesc.ColorMap ? : gif->SColorMap;
   GifColorType *palette = color_map->Colors;
 
   /* Get pixels and palette */
   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 = img->pixels + img->image_size;
+  byte *img_end = rdi.image->pixels + rdi.image->image_size;
 
   /* Handle deinterlacing */
   uns dein_step, dein_next;
   if (image->ImageDesc.Interlace)
 
   /* Handle deinterlacing */
   uns dein_step, dein_next;
   if (image->ImageDesc.Interlace)
-    dein_step = dein_next = img->row_size << 3;
+    {
+      DBG("Deinterlaced image");
+      dein_step = dein_next = rdi.image->row_size << 3;
+    }
   else
   else
-    dein_step = dein_next = img->row_size;
+    dein_step = dein_next = rdi.image->row_size;
 
   /* Convert pixels */
 
   /* Convert pixels */
-  switch (img->pixel_size)
+  switch (rdi.image->pixel_size)
     {
       case 1:
        {
          byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
          for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
     {
       case 1:
        {
          byte pal[256], *pal_pos = pal, *pal_end = pal + 256;
          for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos++, palette++)
-           *pal_pos = libungif_pixel_to_gray(palette);
+           *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 (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
+         if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
+           if (!color_put(io->context, &io->background_color, pal + rd->transparent_index, COLOR_SPACE_GRAYSCALE))
+             {
+               DGifCloseFile(gif);
+               return 0;
+             }
 #        define DO_ROW_END do{ \
 #        define DO_ROW_END do{ \
-             walk_row_start += dein_step; \
-             if (walk_row_start > img_end) \
-               { uns n = dein_next >> 1; walk_row_start = img->pixels + n, dein_step = dein_next; dein_next = n; } \
+             walk_row_start += dein_step; \
+             while (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
 #        define IMAGE_WALK_INLINE
            }while(0)
 #        define IMAGE_WALK_PREFIX(x) walk_##x
 #        define IMAGE_WALK_INLINE
-#        define IMAGE_WALK_IMAGE img
+#        define IMAGE_WALK_IMAGE (rdi.image)
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 1
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 1
 #        define IMAGE_WALK_ROW_STEP 0
@@ -154,16 +210,16 @@ libungif_read_data(struct image_io *io)
          byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
          for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
            {
          byte pal[256 * 2], *pal_pos = pal, *pal_end = pal + 256 * 2;
          for (uns i = 0; i < (uns)color_map->ColorCount; i++, pal_pos += 2, palette++)
            {
-             pal_pos[0] = libungif_pixel_to_gray(palette);
+             pal_pos[0] = rgb_to_gray_func(palette->Red, palette->Green, palette->Blue);
              pal_pos[1] = 255;
            }
          if (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
              pal_pos[1] = 255;
            }
          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_PREFIX(x) walk_##x
 #        define IMAGE_WALK_INLINE
-#        define IMAGE_WALK_IMAGE img
+#        define IMAGE_WALK_IMAGE (rdi.image)
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 2
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 2
 #        define IMAGE_WALK_ROW_STEP 0
@@ -183,9 +239,15 @@ libungif_read_data(struct image_io *io)
            }
          if (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
            }
          if (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
+         if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
+           if (!color_put(io->context, &io->background_color, pal + 4 * rd->transparent_index, COLOR_SPACE_RGB))
+             {
+               DGifCloseFile(gif);
+               return 0;
+             }
 #        define IMAGE_WALK_PREFIX(x) walk_##x
 #        define IMAGE_WALK_INLINE
 #        define IMAGE_WALK_PREFIX(x) walk_##x
 #        define IMAGE_WALK_INLINE
-#        define IMAGE_WALK_IMAGE img
+#        define IMAGE_WALK_IMAGE (rdi.image)
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 3
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 3
 #        define IMAGE_WALK_ROW_STEP 0
@@ -206,11 +268,11 @@ libungif_read_data(struct image_io *io)
            }
          if (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
            }
          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_PREFIX(x) walk_##x
 #        define IMAGE_WALK_INLINE
-#        define IMAGE_WALK_IMAGE img
+#        define IMAGE_WALK_IMAGE (rdi.image)
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 4
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_UNROLL 4
 #        define IMAGE_WALK_COL_STEP 4
 #        define IMAGE_WALK_ROW_STEP 0
@@ -226,30 +288,6 @@ libungif_read_data(struct image_io *io)
   /* Destroy libungif structure */
   DGifCloseFile(gif);
 
   /* Destroy libungif structure */
   DGifCloseFile(gif);
 
-  /* Scale image */
-  if (need_scale)
-    {
-      struct image *img2 = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
-      if (unlikely(!img2))
-        goto err2;
-      int result = image_scale(io->thread, img2, img);
-      image_destroy(img);
-      img = img2;
-      need_destroy = !io->pool;
-      if (unlikely(!result))
-        goto err2;
-    }
-
-  /* Success */
-  io->image = img;
-  io->image_destroy = need_destroy;
-  return 1;
-
-  /* Free structures */
-err:
-  DGifCloseFile(gif);
-err2:
-  if (need_destroy)
-    image_destroy(img);
-  return 0;
+  /* Finish image */
+  return image_io_read_data_finish(&rdi, io);
 }
 }