]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libungif.c
Conf: Rewritten configuration parser to support multiple contexts (step 1)
[libucw.git] / images / io-libungif.c
index 45940edee2f052f9f5d4edd0f937717c50d0bc86..7e6038945627a9ed47e3ed62d00c3b7814fb3577 100644 (file)
@@ -9,12 +9,14 @@
 
 #undef LOCAL_DEBUG
 
-#include "lib/lib.h"
-#include "lib/mempool.h"
-#include "lib/fastbuf.h"
-#include "images/images.h"
-#include "images/color.h"
-#include "images/io-main.h"
+#include <ucw/lib.h>
+#include <ucw/mempool.h>
+#include <ucw/fastbuf.h>
+#include <images/images.h>
+#include <images/error.h>
+#include <images/color.h>
+#include <images/io-main.h>
+
 #include <gif_lib.h>
 
 struct libungif_read_data {
@@ -47,7 +49,7 @@ libungif_read_header(struct image_io *io)
   GifFileType *gif;
   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;
     }
 
@@ -57,7 +59,7 @@ libungif_read_header(struct image_io *io)
   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;
     }
@@ -65,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;
     }
@@ -73,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;
     }
@@ -90,7 +92,7 @@ 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;
     }
@@ -106,7 +108,7 @@ libungif_read_header(struct image_io *io)
          DBG("Found graphics control extension");
          if (unlikely(e->ByteCount != 4))
            {
-              image_thread_err(io->thread, IMAGE_ERR_READ_FAILED, "Invalid graphics control extension.");
+              IMAGE_ERROR(io->context, IMAGE_ERROR_READ_FAILED, "Invalid graphics control extension.");
               DGifCloseFile(gif);
               return 0;
            }
@@ -145,7 +147,11 @@ libungif_read_data(struct image_io *io)
 
   /* Prepare image */
   struct image_io_read_data_internals rdi;
-  if (unlikely(!image_io_read_data_prepare(&rdi, io, image->ImageDesc.Width, image->ImageDesc.Height, io->flags)))
+  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;
@@ -178,10 +184,14 @@ libungif_read_data(struct image_io *io)
          if (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
          if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
-           color_put_grayscale(pal + rd->transparent_index, &io->background_color);
+           if (!color_put(io->context, &io->background_color, pal + rd->transparent_index, COLOR_SPACE_GRAYSCALE))
+             {
+               DGifCloseFile(gif);
+               return 0;
+             }
 #        define DO_ROW_END do{ \
-             walk_row_start += dein_step; \
-             if (walk_row_start >= img_end) \
+             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
@@ -192,7 +202,7 @@ libungif_read_data(struct image_io *io)
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_DO_STEP do{ *walk_pos = pal[*pixels++]; }while(0)
 #        define IMAGE_WALK_DO_ROW_END DO_ROW_END
-#        include "images/image-walk.h"
+#        include <images/image-walk.h>
          break;
        }
       case 2:
@@ -215,7 +225,7 @@ libungif_read_data(struct image_io *io)
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_DO_STEP do{ *(u16 *)walk_pos = ((u16 *)pal)[*pixels++]; }while(0)
 #        define IMAGE_WALK_DO_ROW_END DO_ROW_END
-#        include "images/image-walk.h"
+#        include <images/image-walk.h>
          break;
        }
       case 3:
@@ -230,7 +240,11 @@ libungif_read_data(struct image_io *io)
          if (pal_pos != pal_end)
            bzero(pal_pos, pal_end - pal_pos);
          if (rd->transparent_index >= 0 && (io->flags & IMAGE_IO_USE_BACKGROUND))
-           color_put_rgb(pal + 4 * rd->transparent_index, &io->background_color);
+           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_IMAGE (rdi.image)
@@ -239,7 +253,7 @@ libungif_read_data(struct image_io *io)
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_DO_STEP do{ byte *p = pal + 4 * (*pixels++); walk_pos[0] = p[0]; walk_pos[1] = p[1]; walk_pos[2] = p[2]; }while(0)
 #        define IMAGE_WALK_DO_ROW_END DO_ROW_END
-#        include "images/image-walk.h"
+#        include <images/image-walk.h>
          break;
        }
       case 4:
@@ -264,7 +278,7 @@ libungif_read_data(struct image_io *io)
 #        define IMAGE_WALK_ROW_STEP 0
 #        define IMAGE_WALK_DO_STEP do{ *(u32 *)walk_pos = ((u32 *)pal)[*pixels++]; }while(0)
 #        define IMAGE_WALK_DO_ROW_END DO_ROW_END
-#        include "images/image-walk.h"
+#        include <images/image-walk.h>
          break;
        }
       default: