[gnome-photos/wip/rishi/buffer-decoder: 2/5] Add PhotosGeglBufferCodecPng
- From: Debarshi Ray <debarshir src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-photos/wip/rishi/buffer-decoder: 2/5] Add PhotosGeglBufferCodecPng
- Date: Fri, 21 Sep 2018 07:17:02 +0000 (UTC)
commit 6cfa7f15e7e1fbea16bb94d2b20244f003058328
Author: Debarshi Ray <debarshir gnome org>
Date: Sat Sep 8 01:02:14 2018 +0200
Add PhotosGeglBufferCodecPng
Bump minimum libpng version to 1.6.11.
https://gitlab.gnome.org/GNOME/gnome-photos/issues/63
configure.ac | 3 +-
meson.build | 2 +-
src/Makefile.am | 4 +
src/meson.build | 1 +
src/photos-gegl-buffer-codec-png.c | 744 +++++++++++++++++++++++++++++++++++++
src/photos-gegl-buffer-codec-png.h | 39 ++
6 files changed, 791 insertions(+), 2 deletions(-)
---
diff --git a/configure.ac b/configure.ac
index e318efc8..50cd2070 100644
--- a/configure.ac
+++ b/configure.ac
@@ -51,6 +51,7 @@ GOA_MIN_VERSION=3.8.0
GRILO_MIN_VERSION=0.3.5
GTK_MIN_VERSION=3.22.16
JPEG_MIN_VERSION=2.0.0
+PNG_MIN_VERSION=1.6.11
G_LOG_DOMAIN=AC_PACKAGE_TARNAME
AC_DEFINE_UNQUOTED([G_LOG_DOMAIN], ["$G_LOG_DOMAIN"], [Log domain])
@@ -104,7 +105,7 @@ PKG_CHECK_MODULES(GSETTINGS_DESKTOP_SCHEMAS, [gsettings-desktop-schemas])
PKG_CHECK_MODULES(GTK, [gtk+-3.0 >= $GTK_MIN_VERSION])
PKG_CHECK_MODULES(GTK_UNIX_PRINT, [gtk+-unix-print-3.0])
PKG_CHECK_MODULES(JPEG, [libjpeg >= $JPEG_MIN_VERSION])
-PKG_CHECK_MODULES(PNG, [libpng16])
+PKG_CHECK_MODULES(PNG, [libpng16 >= $PNG_MIN_VERSION])
PKG_CHECK_MODULES(TRACKER, [tracker-control-2.0 tracker-sparql-2.0])
LIBGD_INIT([
diff --git a/meson.build b/meson.build
index f6134287..f206bfc9 100644
--- a/meson.build
+++ b/meson.build
@@ -145,7 +145,7 @@ libdazzle_dep = dependency('libdazzle-1.0', version: '>= 3.26.0')
libgdata_dep = dependency('libgdata', version: '>= 0.15.2')
libgfgraph_dep = dependency('libgfbgraph-0.2', version: '>= 0.2.1')
libjpeg_dep = dependency('libjpeg', version: '>= 2.0.0')
-libpng_dep = dependency('libpng16')
+libpng_dep = dependency('libpng16', version: '>= 1.6.11')
tracker_control_dep = dependency('tracker-control-2.0')
tracker_sparql_dep = dependency('tracker-sparql-2.0')
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..fc5710a4
--- /dev/null
+++ b/src/photos-gegl-buffer-codec-png.c
@@ -0,0 +1,744 @@
+/*
+ * 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;
+ GeglBuffer *buffer_original;
+ gboolean can_set_size;
+ 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;
+ gpointer line;
+ guint stride_original;
+ 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 (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 = 0;
+ 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;
+
+ g_return_val_if_fail (space != NULL, NULL);
+ g_return_val_if_fail (gamma > 0.0, NULL);
+
+ ret_val = space;
+
+ 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 = PHOTOS_GEGL_BUFFER_CODEC_PNG (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: %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)
+{
+ PhotosGeglBufferCodecPng *self;
+ const Babl *format = NULL;
+ const Babl *space = NULL;
+ const Babl *space_sRGB;
+ GeglRectangle bbox;
+ gboolean ret_val = FALSE;
+ gdouble gamma;
+ gdouble target_height;
+ gdouble target_width;
+ gint bit_depth;
+ gint bpp;
+ gint color_type;
+ gint format_components;
+ gint interlace_type;
+ guint target_height_rounded;
+ guint target_width_rounded;
+ png_byte channels;
+ png_uint_32 has_transparency;
+ png_uint_32 height;
+ png_uint_32 width;
+
+ self = PHOTOS_GEGL_BUFFER_CODEC_PNG (png_get_progressive_ptr (png_read_ptr));
+
+ if (self->fatal_error_occurred)
+ return;
+
+ space_sRGB = babl_space ("sRGB");
+
+ png_get_IHDR (png_read_ptr,
+ png_info_ptr,
+ NULL,
+ NULL,
+ &bit_depth,
+ &color_type,
+ &interlace_type,
+ NULL,
+ &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, self->error);
+ if (space == NULL)
+ {
+ self->fatal_error_occurred = TRUE;
+ 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,
+ NULL,
+ &filter_type);
+
+ g_return_val_if_fail (bit_depth == 8 || bit_depth == 16, FALSE);
+
+ photos_debug (PHOTOS_DEBUG_GEGL, "GeglBufferCodecPng: Original size: %u×%u", (guint) width, (guint)
height);
+ if (height == 0 || width == 0)
+ {
+ g_set_error (self->error, PHOTOS_ERROR, 0, _("Original PNG has zero width or height"));
+ self->fatal_error_occurred = TRUE;
+ goto out;
+ }
+
+ self->can_set_size = TRUE;
+ g_signal_emit_by_name (self, "size-prepared", (guint) width, (guint) height);
+ self->can_set_size = FALSE;
+
+ target_height = photos_gegl_buffer_codec_get_height (PHOTOS_GEGL_BUFFER_CODEC (self));
+ target_width = photos_gegl_buffer_codec_get_width (PHOTOS_GEGL_BUFFER_CODEC (self));
+
+ photos_debug (PHOTOS_DEBUG_GEGL, "GeglBufferCodecPng: Target size: %f×%f", target_width, target_height);
+
+ if (target_height <= 0 || target_width <= 0)
+ {
+ g_set_error (self->error, PHOTOS_ERROR, 0, _("Transformed PNG has invalid width or height"));
+ self->fatal_error_occurred = TRUE;
+ goto out;
+ }
+
+ target_height_rounded = (guint) (target_height + 0.5);
+ target_width_rounded = (guint) (target_width + 0.5);
+
+ photos_debug (PHOTOS_DEBUG_GEGL,
+ "GeglBufferCodecPng: Target size rounded: %u×%u",
+ target_width_rounded,
+ target_height_rounded);
+
+ format = photos_gegl_buffer_codec_png_get_format (space, bit_depth, color_type, self->error);
+ if (format == NULL)
+ {
+ self->fatal_error_occurred = TRUE;
+ goto out;
+ }
+
+ format_components = babl_format_get_n_components (format);
+ channels = png_get_channels (png_read_ptr, png_info_ptr);
+ g_assert_cmpint (format_components, ==, (gint) channels);
+
+ bpp = babl_format_get_bytes_per_pixel (format);
+ g_assert_cmpint (bpp, ==, bit_depth * (gint) channels / 8);
+
+ g_assert_null (self->buffer);
+ g_assert_null (self->buffer_original);
+
+ gegl_rectangle_set (&bbox, 0, 0, (guint) width, (guint) height);
+ self->buffer_original = gegl_buffer_new (&bbox, format);
+
+ if ((guint) height == target_height_rounded && (guint) width == target_width_rounded)
+ {
+ self->buffer = g_object_ref (self->buffer_original);
+ }
+ else
+ {
+ gdouble aspect_ratio = (gdouble) width / (gdouble) height;
+ gdouble target_aspect_ratio = target_width / target_height;
+
+ if (G_APPROX_VALUE (aspect_ratio, target_aspect_ratio, PHOTOS_EPSILON))
+ {
+ }
+ }
+
+ g_object_notify (G_OBJECT (self), "buffer");
+
+ g_assert_null (self->line);
+
+ /* Row strides are represented as signed integers (ie. gint)
+ * in GEGL. Even though the output_width is an unsigned
+ * integer (ie. guint or JDIMENSION), it must be ensured
+ * that the row stride can fit into the expected signed
+ * integer type (ie. gint).
+ *
+ * The comparison is made using the same unsigned integer
+ * type to avoid triggering -Wsign-compare.
+ */
+ if (bpp > 0 && width > 0 && (guint) width > G_MAXINT / (guint) bpp)
+ {
+ g_set_error (error,
+ PHOTOS_ERROR,
+ 0,
+ _("Overflow calculating row stride (%d×%u)"),
+ bpp,
+ (guint) width);
+ goto out;
+ }
+
+ self->stride_original = (guint) bpp * (guint) width;
+ self->line = g_malloc_n (width, bpp);
+
+ out:
+ return;
+}
+
+
+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)
+{
+ PhotosGeglBufferCodecPng *self;
+ const Babl *format;
+ GeglRectangle bbox_scanline;
+ gint buffer_height;
+ png_uint_32 width;
+
+ self = PHOTOS_GEGL_BUFFER_CODEC_PNG (png_get_progressive_ptr (png_read_ptr));
+
+ if (self->fatal_error_occurred)
+ goto out;
+
+ if (pass_num < 0)
+ {
+ g_set_error (self->error, PHOTOS_ERROR, 0, _("Invalid deinterlacing pass over PNG"));
+ self->fatal_error_occurred = TRUE;
+ goto out;
+ }
+
+ buffer_height = gegl_buffer_get_height (self->buffer_original);
+ if (row_num >= (png_uint_32) buffer_height)
+ {
+ g_set_error (self->error, PHOTOS_ERROR, 0, _("PNG has too many rows"));
+ self->fatal_error_occurred = TRUE;
+ goto out;
+ }
+
+ format = gegl_buffer_get_format (self->buffer_original);
+
+ width = png_get_image_width (png_read_ptr, self->png_info_ptr);
+ gegl_rectangle_set (&bbox_scanline, 0, (gint) row_num, (guint) width, 1);
+
+ if (pass_num > 0)
+ {
+ gegl_buffer_get (self->buffer_original,
+ &bbox_scanline,
+ 1.0,
+ format,
+ self->line,
+ (gint) self->stride_original,
+ GEGL_ABYSS_NONE);
+ }
+
+ png_progressive_combine_row (png_read_ptr, self->line, new_row);
+ gegl_buffer_set (self->buffer_original, &bbox_scanline, 0, format, self->line, (gint)
self->stride_original);
+
+ out:
+ return;
+}
+
+
+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->error = error;
+ 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 (self->error && *self->error == NULL)
+ g_set_error (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 (self->error && *self->error == NULL)
+ g_set_error (self->error, PHOTOS_ERROR, 0, _("Couldn’t allocate memory for loading PNG"));
+
+ self->decoding = FALSE;
+ goto out;
+ }
+
+ /* See:
+ * https://bugzilla.gnome.org/show_bug.cgi?id=721135
+ * https://bugzilla.gnome.org/show_bug.cgi?id=765850
+ */
+ png_set_benign_errors (self->png_read_ptr, TRUE);
+ png_set_option (self->png_read_ptr, PNG_SKIP_sRGB_CHECK_PROFILE, PNG_OPTION_ON);
+
+ 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->error = error;
+ 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;
+
+ 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 (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_clear_object (&self->buffer_original);
+
+ 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->line);
+ 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]