]> mj.ucw.cz Git - libucw.git/blobdiff - images/io-libpng.c
rest of bbuf printf
[libucw.git] / images / io-libpng.c
index dbf33d511dd359f97b32dda431782f3e244f0855..f15955a64cf83c045bd953cb1afa33fd96908b96 100644 (file)
@@ -13,6 +13,7 @@
 #include "lib/mempool.h"
 #include "lib/fastbuf.h"
 #include "images/images.h"
 #include "lib/mempool.h"
 #include "lib/fastbuf.h"
 #include "images/images.h"
+#include "images/io-main.h"
 #include <png.h>
 #include <setjmp.h>
 
 #include <png.h>
 #include <setjmp.h>
 
@@ -143,29 +144,28 @@ libpng_read_header(struct image_io *io)
   switch (rd->color_type)
     {
       case PNG_COLOR_TYPE_GRAY:
   switch (rd->color_type)
     {
       case PNG_COLOR_TYPE_GRAY:
-        io->flags |= COLOR_SPACE_GRAYSCALE;
+        io->flags = COLOR_SPACE_GRAYSCALE;
        io->number_of_colors = 1 << 8;
         break;
       case PNG_COLOR_TYPE_GRAY_ALPHA:
        io->number_of_colors = 1 << 8;
         break;
       case PNG_COLOR_TYPE_GRAY_ALPHA:
-        io->flags |= COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
+        io->flags = COLOR_SPACE_GRAYSCALE | IMAGE_ALPHA;
        io->number_of_colors = 1 << 8;
         break;
       case PNG_COLOR_TYPE_RGB:
        io->number_of_colors = 1 << 8;
         break;
       case PNG_COLOR_TYPE_RGB:
-        io->flags |= COLOR_SPACE_RGB;
+        io->flags = COLOR_SPACE_RGB;
        io->number_of_colors = 1 << 24;
         break;
       case PNG_COLOR_TYPE_RGB_ALPHA:
        io->number_of_colors = 1 << 24;
        io->number_of_colors = 1 << 24;
         break;
       case PNG_COLOR_TYPE_RGB_ALPHA:
        io->number_of_colors = 1 << 24;
-        io->flags |= COLOR_SPACE_RGB | IMAGE_ALPHA;
+        io->flags = COLOR_SPACE_RGB | IMAGE_ALPHA;
         break;
       case PNG_COLOR_TYPE_PALETTE:
         break;
       case PNG_COLOR_TYPE_PALETTE:
-        io->flags |= COLOR_SPACE_RGB | IMAGE_ALPHA;
+        io->flags = COLOR_SPACE_RGB | IMAGE_ALPHA | IMAGE_IO_HAS_PALETTE;
        int num_palette;
        if (png_get_PLTE(rd->png_ptr, rd->info_ptr, NULL, &num_palette))
          io->number_of_colors = num_palette;
        else
          io->number_of_colors = 1 << rd->bit_depth;
        int num_palette;
        if (png_get_PLTE(rd->png_ptr, rd->info_ptr, NULL, &num_palette))
          io->number_of_colors = num_palette;
        else
          io->number_of_colors = 1 << rd->bit_depth;
-       io->has_palette = 1;
         break;
       default:
         png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
         break;
       default:
         png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
@@ -197,25 +197,20 @@ libpng_read_data(struct image_io *io)
         return 0;
     }
 
         return 0;
     }
 
-  volatile int need_scale = io->cols != rd->cols || io->rows != rd->rows;
-  struct image * volatile img = need_scale ?
-    image_new(io->thread, rd->cols, rd->rows, io->flags & IMAGE_PIXEL_FORMAT, NULL) :
-    image_new(io->thread, rd->cols, rd->rows, io->flags, io->pool);
-  if (!img)
-    {
-      png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
-      return 0;
-    }
+  struct image_io_read_data_internals rdi;
+  rdi.image = NULL;
 
   if (setjmp(png_jmpbuf(rd->png_ptr)))
     {
       DBG("Libpng failed to read the image, longjump saved us");
       png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
 
   if (setjmp(png_jmpbuf(rd->png_ptr)))
     {
       DBG("Libpng failed to read the image, longjump saved us");
       png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
-      if (need_scale || !io->pool)
-       image_destroy(img);
+      if (rdi.image)
+        image_io_read_data_break(&rdi, io);
       return 0;
     }
 
       return 0;
     }
 
+  uns read_flags = io->flags;
+  
   /* Apply transformations */
   if (rd->bit_depth == 16)
     png_set_strip_16(rd->png_ptr);
   /* Apply transformations */
   if (rd->bit_depth == 16)
     png_set_strip_16(rd->png_ptr);
@@ -229,10 +224,20 @@ libpng_read_data(struct image_io *io)
          }
        else
          png_set_palette_to_rgb(rd->png_ptr);
          }
        else
          png_set_palette_to_rgb(rd->png_ptr);
-       if ((io->flags & IMAGE_ALPHA) || (io->flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED))
-          png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER);
+       if (!(io->flags & IMAGE_ALPHA))
+         {
+           if (io->flags & IMAGE_IO_USE_BACKGROUND)
+             {
+                png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER);
+               read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT;
+             }
+           else if ((io->flags & IMAGE_PIXEL_FORMAT) == (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED))
+              png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER);
+            else
+             png_set_strip_alpha(rd->png_ptr);
+         }
        else
        else
-         png_set_strip_alpha(rd->png_ptr);
+          png_set_add_alpha(rd->png_ptr, 255, PNG_FILLER_AFTER);
        break;
       case PNG_COLOR_TYPE_GRAY:
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_RGB)
        break;
       case PNG_COLOR_TYPE_GRAY:
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_RGB)
@@ -244,7 +249,12 @@ libpng_read_data(struct image_io *io)
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_RGB)
           png_set_gray_to_rgb(rd->png_ptr);
        if (!(io->flags & IMAGE_ALPHA))
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_RGB)
           png_set_gray_to_rgb(rd->png_ptr);
        if (!(io->flags & IMAGE_ALPHA))
-          png_set_strip_alpha(rd->png_ptr);
+         {
+           if (io->flags & IMAGE_IO_USE_BACKGROUND)
+             read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT;
+           else
+              png_set_strip_alpha(rd->png_ptr);
+         }  
        break;
       case PNG_COLOR_TYPE_RGB:
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE)
        break;
       case PNG_COLOR_TYPE_RGB:
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE)
@@ -255,16 +265,26 @@ libpng_read_data(struct image_io *io)
       case PNG_COLOR_TYPE_RGB_ALPHA:
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE)
          png_set_rgb_to_gray_fixed(rd->png_ptr, 1, 21267, 71514);
       case PNG_COLOR_TYPE_RGB_ALPHA:
        if ((io->flags & IMAGE_COLOR_SPACE) == COLOR_SPACE_GRAYSCALE)
          png_set_rgb_to_gray_fixed(rd->png_ptr, 1, 21267, 71514);
-       if (!(io->flags & IMAGE_ALPHA) && (io->flags & IMAGE_PIXEL_FORMAT) != (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED))
-          png_set_strip_alpha(rd->png_ptr);
+       if (!(io->flags & IMAGE_ALPHA))
+         if (io->flags & IMAGE_IO_USE_BACKGROUND)
+           read_flags = (read_flags | IMAGE_ALPHA) & IMAGE_CHANNELS_FORMAT;
+         else if ((io->flags & IMAGE_PIXEL_FORMAT) != (COLOR_SPACE_RGB | IMAGE_PIXELS_ALIGNED))
+            png_set_strip_alpha(rd->png_ptr);
        break;
       default:
        ASSERT(0);
     }
   png_read_update_info(rd->png_ptr, rd->info_ptr);
 
        break;
       default:
        ASSERT(0);
     }
   png_read_update_info(rd->png_ptr, rd->info_ptr);
 
+  /* Prepare the image */
+  if (unlikely(!image_io_read_data_prepare(&rdi, io, rd->cols, rd->rows, read_flags)))
+    {
+      png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
+      return 0;
+    }
   /* Read image data */
   DBG("Reading image data");
   /* Read image data */
   DBG("Reading image data");
+  struct image *img = rdi.image;
   byte *pixels = img->pixels;
   png_bytep rows[img->rows];
   for (uns r = 0; r < img->rows; r++, pixels += img->row_size)
   byte *pixels = img->pixels;
   png_bytep rows[img->rows];
   for (uns r = 0; r < img->rows; r++, pixels += img->row_size)
@@ -275,29 +295,8 @@ libpng_read_data(struct image_io *io)
   /* Destroy libpng read structure */
   png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
 
   /* Destroy libpng read structure */
   png_destroy_read_struct(&rd->png_ptr, &rd->info_ptr, &rd->end_ptr);
 
-  /* Scale and store the resulting image */
-  if (need_scale)
-    {
-      struct image *dest = image_new(io->thread, io->cols, io->rows, io->flags, io->pool);
-      if (!dest)
-        {
-         image_destroy(img);
-         return 0;
-       }
-      if (!image_scale(io->thread, dest, img))
-        {
-         image_destroy(img);
-         if (!io->pool)
-           image_destroy(dest);
-         return 0;
-       }
-      io->image = dest;
-    }
-  else
-    io->image = img;
-  io->image_destroy = !io->pool;
-
-  return 1;
+  /* Finish the image  */
+  return image_io_read_data_finish(&rdi, io);
 }
 
 int
 }
 
 int