[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: Wed, 12 Sep 2018 11:55:22 +0000 (UTC)
commit fa451e0d2933d2f1faa638c3c00220b435ff2393
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 | 356 +++++++++++++++++++++++++++++++++++++
src/photos-gegl-buffer-codec-png.h | 39 ++++
4 files changed, 400 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..d39d4a4b
--- /dev/null
+++ b/src/photos-gegl-buffer-codec-png.c
@@ -0,0 +1,356 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 1999 The Free Software Foundation
+ * Copyright © 1999 Mark Crichton
+ * 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 gboolean
+photos_gegl_buffer_codec_png_setup_transformations (png_structp png_read_ptr,
+ png_infop png_info_ptr,
+ png_uint_32 *out_width_p,
+ png_uint_32 *out_height_p,
+ gint *out_color_type_p,
+ GError **error)
+{
+ gboolean ret_val = FALSE;
+ gint bit_depth;
+
+ bit_depth = png_get_bit_depth (png_read_ptr, png_info_ptr);
+ if (bit_depth < 1 || bit_depth > 16)
+ {
+ /* Must check bit depth, since png_get_IHDR generates an FPE on
+ * bit_depth == 0.
+ */
+ g_set_error_literal (error, PHOTOS_ERROR, 0, _("Bits per channel of PNG image is invalid"));
+ goto out;
+ }
+
+ 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]