[gnome-photos/wip/rishi/buffer-decoder: 7/10] Add PhotosGeglBufferCodecPng



commit d9831e62bf1ba4219d2072d487426f9649db89b8
Author: Debarshi Ray <debarshir gnome org>
Date:   Sat Sep 8 01:02:14 2018 +0200

    Add PhotosGeglBufferCodecPng
    
    https://gitlab.gnome.org/GNOME/gnome-photos/issues/63

 src/Makefile.am                    |   4 +
 src/meson.build                    |   1 +
 src/photos-gegl-buffer-codec-png.c | 596 +++++++++++++++++++++++++++++++++++++
 src/photos-gegl-buffer-codec-png.h |  39 +++
 4 files changed, 640 insertions(+)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index 9174762f..3fbf8dde 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -108,6 +108,8 @@ gnome_photos_SOURCES = \
        photos-gegl-buffer-codec.h \
        photos-gegl-buffer-codec-jpeg.c \
        photos-gegl-buffer-codec-jpeg.h \
+       photos-gegl-buffer-codec-png.c \
+       photos-gegl-buffer-codec-png.h \
        photos-gegl-buffer-loader.c \
        photos-gegl-buffer-loader.h \
        photos-gegl-buffer-loader-builder.c \
@@ -334,6 +336,8 @@ gnome_photos_thumbnailer_SOURCES = \
        photos-gegl-buffer-codec.h \
        photos-gegl-buffer-codec-jpeg.c \
        photos-gegl-buffer-codec-jpeg.h \
+       photos-gegl-buffer-codec-png.c \
+       photos-gegl-buffer-codec-png.h \
        photos-gegl-buffer-loader.c \
        photos-gegl-buffer-loader.h \
        photos-gegl-buffer-loader-builder.c \
diff --git a/src/meson.build b/src/meson.build
index 2a6b9222..3412a5d1 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -4,6 +4,7 @@ common_sources = files(
   'photos-gegl.c',
   'photos-gegl-buffer-codec.c',
   'photos-gegl-buffer-codec-jpeg.c',
+  'photos-gegl-buffer-codec-png.c',
   'photos-gegl-buffer-loader.c',
   'photos-gegl-buffer-loader-builder.c',
   'photos-glib.c',
diff --git a/src/photos-gegl-buffer-codec-png.c b/src/photos-gegl-buffer-codec-png.c
new file mode 100644
index 00000000..5897b596
--- /dev/null
+++ b/src/photos-gegl-buffer-codec-png.c
@@ -0,0 +1,596 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 1999 The Free Software Foundation
+ * Copyright © 1999 Mark Crichton
+ * Copyright © 2006 Øyvind Kolås
+ * Copyright © 2018 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Based on code from:
+ *   + GdkPixbuf
+ *   + GEGL
+ */
+
+
+#include "config.h"
+
+#include <gegl.h>
+#include <glib/gi18n.h>
+#include <png.h>
+
+#include "photos-debug.h"
+#include "photos-error.h"
+#include "photos-gegl.h"
+#include "photos-gegl-buffer-codec-png.h"
+
+
+struct _PhotosGeglBufferCodecPng
+{
+  PhotosGeglBufferCodec parent_instance;
+  GError **error;
+  GeglBuffer *buffer;
+  gboolean decoding;
+  gboolean fatal_error_occurred;
+  gint first_pass_seen_in_chunk;
+  gint first_row_seen_in_chunk;
+  gint last_pass_seen_in_chunk;
+  gint last_row_seen_in_chunk;
+  gint max_row_seen_in_chunk;
+  png_infop png_info_ptr;
+  png_structp png_read_ptr;
+};
+
+enum
+{
+  PROP_0,
+  PROP_BUFFER,
+  PROP_CAN_SET_SIZE
+};
+
+
+G_DEFINE_TYPE_WITH_CODE (PhotosGeglBufferCodecPng, photos_gegl_buffer_codec_png, 
PHOTOS_TYPE_GEGL_BUFFER_CODEC,
+                         photos_gegl_ensure_extension_points ();
+                         g_io_extension_point_implement (PHOTOS_GEGL_BUFFER_CODEC_EXTENSION_POINT_NAME,
+                                                         g_define_type_id,
+                                                         "png",
+                                                         0));
+
+
+static const gchar *MIME_TYPES[] =
+{
+  "image/png",
+  NULL
+};
+
+
+static const Babl *
+photos_gegl_buffer_codec_png_get_format (const Babl *space, gint bit_depth, gint color_type, GError **error)
+{
+  const Babl *ret_val = NULL;
+  g_autoptr (GString) format_name_str = NULL;
+
+  g_return_val_if_fail (space != NULL, NULL);
+  g_return_val_if_fail (bit_depth == 8 || bit_depth == 16, NULL);
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  format_name_str = g_string_new (NULL);
+
+  switch (color_type)
+    {
+    case PNG_COLOR_TYPE_GRAY:
+      format_name_str = g_string_append (string, "Y'");
+      break;
+
+    case PNG_COLOR_TYPE_GRAY_ALPHA:
+      format_name_str = g_string_append (string, "Y'A");
+      break;
+
+    case PNG_COLOR_TYPE_PALETTE:
+    case PNG_COLOR_TYPE_RGB:
+      format_name_str = g_string_append (string, "R'G'B'");
+      break;
+
+    case PNG_COLOR_TYPE_PALETTE | PNG_COLOR_MASK_ALPHA:
+    case PNG_COLOR_TYPE_RGB_ALPHA:
+      format_name_str = g_string_append (string, "R'G'B'A");
+      break;
+
+    default:
+      g_set_error_literal (error, PHOTOS_ERROR, 0, _("Unsupported PNG color space"));
+      goto out;
+      break;
+    }
+
+  format_name_str = g_string_append_c (string, ' ');
+
+  switch (bit_depth)
+    {
+    case 8:
+      format_name_str = g_string_append (string, "u8");
+      break;
+
+    case 16:
+      format_name_str = g_string_append (string, "u16");
+      break;
+
+    default:
+      g_assert_not_reached ();
+      break;
+    }
+
+  ret_val = babl_format_with_space (format_name_str->str, space);
+
+ out:
+  return ret_val;
+}
+
+
+static const Babl *
+photos_gegl_buffer_codec_png_get_space (png_structp png_read_ptr,
+                                        png_infop png_info_ptr,
+                                        gdouble *out_gamma,
+                                        GError **error)
+{
+  const Babl *ret_val = NULL;
+  const Babl *space = NULL;
+  const Babl *space_sRGB;
+  gdouble gamma = 0.45455;
+  gint icc_profile_compression_type;
+  png_bytep icc_profile = NULL;
+  png_charp icc_profile_name;
+  png_uint_32 has_gamma;
+  png_uint_32 icc_profile_len;
+
+  g_return_val_if_fail (error == NULL || *error == NULL, NULL);
+
+  space_sRGB = babl_space ("sRGB");
+
+  has_gamma = png_get_valid (png_read_ptr, png_info_ptr, PNG_INFO_gAMA);
+  if (has_gamma != 0)
+    {
+      png_get_gAMA (png_read_ptr, png_info_ptr, &gamma);
+      if (gamma <= 0.0)
+        {
+          g_set_error (error, PHOTOS_ERROR, 0, _("Invalid gamma in PNG: %f"), gamma);
+          goto out;
+        }
+    }
+
+  png_get_iCCP (png_read_ptr,
+                png_info_ptr,
+                &icc_profile_name,
+                &icc_profile_compression_type,
+                &icc_profile,
+                &icc_profile_len);
+
+  if (icc_profile != NULL)
+    {
+      const gchar *error_message = NULL;
+
+      space = babl_space_from_icc ((char *) icc_profile,
+                                   (gint) icc_profile_len,
+                                   BABL_ICC_INTENT_RELATIVE_COLORIMETRIC,
+                                   &error_message);
+      if (space == NULL)
+        {
+          g_warning ("Unable to create Babl space from ICC profile: %s", error_message);
+          space = space_sRGB;
+        }
+    }
+
+  if (space == NULL)
+    {
+      png_uint_32 has_sRGB;
+
+      has_sRGB = png_get_valid (png_read_ptr, png_info_ptr, PNG_INFO_sRGB);
+      if (has_sRGB != 0)
+        space = space_sRGB;
+    }
+
+  if (space == NULL && has_gamma != 0)
+    {
+      const Babl *trc;
+
+      /* default to sRGB */
+      gdouble blue[2] = {0.1500, 0.0600};
+      gdouble green[2] = {0.3000, 0.6000};
+      gdouble red[2] = {0.6400, 0.3300};
+      gdouble white_point[2] = {0.3127, 0.3290};
+
+      has_chromaticities = png_get_valid (png_read_ptr, png_info_ptr, PNG_INFO_cHRM);
+      if (has_chromaticities != 0)
+        {
+          png_get_cHRM (png_read_ptr,
+                        png_info_ptr,
+                        &white_point[0],
+                        &white_point[1],
+                        &red[0],
+                        &red[1],
+                        &green[0],
+                        &green[1],
+                        &blue[0],
+                        &blue[1]);
+        }
+
+      trc = babl_trc_gamma (1.0 / gamma);
+      space = babl_space_from_chromaticities (NULL,
+                                              white_point[0],
+                                              white_point[1],
+                                              red[0],
+                                              red[1],
+                                              green[0],
+                                              green[1],
+                                              blue[0],
+                                              blue[1],
+                                              trc,
+                                              trc,
+                                              trc,
+                                              BABL_SPACE_FLAG_EQUALIZE);
+    }
+
+  if (space == NULL)
+    space = space_sRGB;
+
+  if (out_gamma != NULL)
+    *out_gamma = gamma;
+
+  ret_val = space;
+
+  g_return_val_if_fail (ret_val != NULL, NULL);
+  g_return_val_if_fail (gamma > 0.0, NULL);
+
+ out:
+  return ret_val;
+}
+
+static gboolean
+photos_gegl_buffer_codec_png_setup_transformations (png_structp png_read_ptr,
+                                                    png_infop png_info_ptr,
+                                                    png_uint_32 *out_width,
+                                                    png_uint_32 *out_height,
+                                                    gint *out_color_type,
+                                                    GError **error)
+{
+  const Babl *space;
+  const Babl *space_sRGB;
+  gboolean ret_val = FALSE;
+  gdouble gamma;
+  gint bit_depth;
+  gint color_type;
+  gint interlace_type;
+  png_uint_32 has_transparency;
+  png_uint_32 height;
+  png_uint_32 width;
+
+  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
+
+  space_sRGB = babl_space ("sRGB");
+
+  png_get_IHDR (png_read_ptr,
+                png_info_ptr,
+                &width,
+                &height,
+                &bit_depth,
+                &color_type,
+                &interlace_type,
+                &compression_type,
+                &filter_type);
+
+  if (bit_depth < 8)
+    png_set_expand (png_read_ptr);
+
+#if BYTE_ORDER == LITTLE_ENDIAN
+  else if (bit_depth == 16)
+    png_set_swap (png_ptr);
+#endif
+
+  if (color_type == PNG_COLOR_TYPE_PALETTE)
+    png_set_expand (png_read_ptr);
+
+  has_transparency = png_get_valid (png_read_ptr, png_info_ptr, PNG_INFO_tRNS);
+  if (has_transparency != 0)
+    png_set_expand (png_read_ptr);
+
+  if (interlace_type != PNG_INTERLACE_NONE)
+    png_set_interlace_handling (png_read_ptr);
+
+  space = photos_gegl_buffer_codec_png_get_space (png_read_ptr, png_info_ptr, &gamma, error);
+  if (space == NULL)
+    goto out;
+
+  if (space == sRGB)
+    png_set_gamma (png_read_ptr, 2.2, gamma);
+
+  png_read_update_info (png_read_ptr, png_info_ptr);
+
+  png_get_IHDR (png_read_ptr,
+                png_info_ptr,
+                &width,
+                &height,
+                &bit_depth,
+                &color_type,
+                &interlace_type,
+                &compression_type,
+                &filter_type);
+
+  g_return_val_if_fail (bit_depth == 8 || bit_depth == 16, FALSE);
+
+  if (height == 0 || width == 0)
+    {
+      g_set_error_literal (error, PHOTOS_ERROR, 0, _("Transformed PNG has zero width or height"));
+      goto out;
+    }
+
+  ret_val = TRUE;
+
+ out:
+  return ret_val;
+}
+
+
+static void
+photos_gegl_buffer_codec_png_end (png_structp png_read_ptr, png_infop png_info_ptr)
+{
+}
+
+
+static void
+photos_gegl_buffer_codec_png_error (png_structp png_read_ptr, png_const_charp error_message)
+{
+  PhotosGeglBufferCodecPng *self;
+
+  self = png_get_error_ptr (png_read_ptr);
+  self->fatal_error_occurred = TRUE;
+
+  /* Check for *error == NULL for robustness against crappy PNG
+   * library.
+   */
+  if (self->error && *self->error == NULL)
+    g_set_error (self->error, PHOTOS_ERROR, 0, _("Error reading PNG image: %s"), error_message);
+
+  longjmp (png_jmpbuf (png_read_ptr), 1);
+  g_assert_not_reached ();
+}
+
+
+static void
+photos_gegl_buffer_codec_png_info (png_structp png_read_ptr, png_infop png_info_ptr)
+{
+}
+
+
+static void
+photos_gegl_buffer_codec_png_row (png_structp png_read_ptr,
+                                  png_bytep new_row,
+                                  png_uint_32 row_num,
+                                  gint pass_num)
+{
+}
+
+
+static void
+photos_gegl_buffer_codec_png_warning (png_structp png_read_ptr, png_const_charp warning_msg)
+{
+}
+
+
+static GeglBuffer *
+photos_gegl_buffer_codec_png_get_buffer (PhotosGeglBufferCodec *codec)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (codec);
+  return self->buffer;
+}
+
+
+static gboolean
+photos_gegl_buffer_codec_png_load_begin (PhotosGeglBufferCodec *codec, GError **error)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (codec);
+
+  g_return_val_if_fail (!self->decoding, FALSE);
+
+  self->fatal_error_occurred = FALSE;
+  self->first_pass_seen_in_chunk = -1;
+  self->first_row_seen_in_chunk = -1;
+  self->last_pass_seen_in_chunk = -1;
+  self->last_row_seen_in_chunk = -1;
+  self->max_row_seen_in_chunk = -1;
+
+  self->png_read_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING,
+                                               self,
+                                               photos_gegl_buffer_codec_png_error,
+                                               photos_gegl_buffer_codec_png_warning);
+  if (self->png_read_ptr == NULL)
+    {
+      /* A failure here isn't supposed to call the error callback, but
+       * it doesn't hurt to be careful.
+       */
+      if (error && *error == NULL)
+        g_set_error_literal (self->error, PHOTOS_ERROR, 0, _("Couldn’t allocate memory for loading PNG"));
+
+      self->decoding = FALSE;
+      goto out;
+    }
+
+  self->png_info_ptr = png_create_info_struct (self->png_read_ptr);
+  if (self->png_info_ptr == NULL)
+    {
+      /* A failure here isn't supposed to call the error callback, but
+       * it doesn't hurt to be careful.
+       */
+      if (error && *error == NULL)
+        g_set_error_literal (self->error, PHOTOS_ERROR, 0, _("Couldn’t allocate memory for loading PNG"));
+
+      self->decoding = FALSE;
+      goto out;
+    }
+
+  self->error = error;
+  if (setjmp (png_jmpbuf (self->png_read_ptr)))
+    {
+      self->decoding = FALSE;
+      goto out;
+    }
+
+  png_set_progressive_read_fn (self->png_read_ptr,
+                               self,
+                               photos_gegl_buffer_codec_png_info,
+                               photos_gegl_buffer_codec_png_row,
+                               photos_gegl_buffer_codec_png_end);
+
+  self->decoding = TRUE;
+
+ out:
+  self->error = NULL;
+  return self->decoding;
+}
+
+
+static gboolean
+photos_gegl_buffer_codec_png_load_increment (PhotosGeglBufferCodec *codec,
+                                             const guchar *buf,
+                                             gsize count,
+                                             GError **error)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (codec);
+  gboolean ret_val = FALSE;
+
+  g_return_val_if_fail (self->decoding, FALSE);
+
+  self->first_pass_seen_in_chunk = -1;
+  self->first_row_seen_in_chunk = -1;
+  self->last_pass_seen_in_chunk = -1;
+  self->last_row_seen_in_chunk = -1;
+  self->max_row_seen_in_chunk = -1;
+
+  self->error = error;
+  if (setjmp (png_jmpbuf (self->png_read_ptr)))
+    {
+      ret_val = FALSE;
+      goto out;
+    }
+
+  png_process_data (self->png_read_ptr, self->png_info_ptr, (guchar *) buf, count);
+  if (self->fatal_error_occurred)
+    {
+      ret_val = FALSE;
+      goto out;
+    }
+
+  ret_val = TRUE;
+
+ out:
+  self->error = NULL;
+  return ret_val;
+}
+
+
+static gboolean
+photos_gegl_buffer_codec_png_load_stop (PhotosGeglBufferCodec *codec, GError **error)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (codec);
+  gboolean ret_val = FALSE;
+
+  g_return_val_if_fail (self->decoding, FALSE);
+
+  /* FIXME: This thing needs to report errors if we have unused image
+   * data.
+   */
+
+  if (self->buffer == NULL)
+    {
+      g_set_error_literal (error, PHOTOS_ERROR, 0, _("Premature end-of-file encountered"));
+      goto out;
+    }
+
+  ret_val = TRUE;
+
+ out:
+  self->decoding = FALSE;
+  return ret_val;
+}
+
+
+static void
+photos_gegl_buffer_codec_png_dispose (GObject *object)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (object);
+
+  g_clear_object (&self->buffer);
+
+  G_OBJECT_CLASS (photos_gegl_buffer_codec_png_parent_class)->dispose (object);
+}
+
+
+static void
+photos_gegl_buffer_codec_png_finalize (GObject *object)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (object);
+
+  //g_free (self->tile_memory);
+  png_destroy_read_struct (&self->png_read_ptr, &self->png_info_ptr, NULL);
+
+  G_OBJECT_CLASS (photos_gegl_buffer_codec_png_parent_class)->finalize (object);
+}
+
+
+static void
+photos_gegl_buffer_codec_png_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec *pspec)
+{
+  PhotosGeglBufferCodecPng *self = PHOTOS_GEGL_BUFFER_CODEC_PNG (object);
+
+  switch (prop_id)
+    {
+    case PROP_BUFFER:
+      g_value_set_object (value, self->buffer);
+      break;
+
+    case PROP_CAN_SET_SIZE:
+      //g_value_set_boolean (value, self->can_set_size);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_gegl_buffer_codec_png_init (PhotosGeglBufferCodecPng *self)
+{
+}
+
+
+static void
+photos_gegl_buffer_codec_png_class_init (PhotosGeglBufferCodecPngClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PhotosGeglBufferCodecClass *buffer_codec_class = PHOTOS_GEGL_BUFFER_CODEC_CLASS (class);
+
+  buffer_codec_class->mime_types = (GStrv) MIME_TYPES;
+
+  object_class->dispose = photos_gegl_buffer_codec_png_dispose;
+  object_class->finalize = photos_gegl_buffer_codec_png_finalize;
+  object_class->get_property = photos_gegl_buffer_codec_png_get_property;
+  buffer_codec_class->get_buffer = photos_gegl_buffer_codec_png_get_buffer;
+  buffer_codec_class->load_begin = photos_gegl_buffer_codec_png_load_begin;
+  buffer_codec_class->load_increment = photos_gegl_buffer_codec_png_load_increment;
+  buffer_codec_class->load_stop = photos_gegl_buffer_codec_png_load_stop;
+
+  g_object_class_override_property (object_class, PROP_BUFFER, "buffer");
+  g_object_class_override_property (object_class, PROP_CAN_SET_SIZE, "can-set-size");
+}
diff --git a/src/photos-gegl-buffer-codec-png.h b/src/photos-gegl-buffer-codec-png.h
new file mode 100644
index 00000000..08c8749b
--- /dev/null
+++ b/src/photos-gegl-buffer-codec-png.h
@@ -0,0 +1,39 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2018 Red Hat, Inc.
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+/* Based on code from:
+ *   + GdkPixbuf
+ */
+
+#ifndef PHOTOS_GEGL_BUFFER_CODEC_PNG_H
+#define PHOTOS_GEGL_BUFFER_CODEC_PNG_H
+
+#include "photos-gegl-buffer-codec.h"
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_GEGL_BUFFER_CODEC_PNG (photos_gegl_buffer_codec_png_get_type ())
+G_DECLARE_FINAL_TYPE (PhotosGeglBufferCodecPng,
+                      photos_gegl_buffer_codec_png,
+                      PHOTOS,
+                      GEGL_BUFFER_CODEC_PNG,
+                      PhotosGeglBufferCodec);
+
+G_END_DECLS
+
+#endif /* PHOTOS_GEGL_BUFFER_CODEC_JPEG_H */


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]