[gtk/gamma-shenanigans: 7/8] Add a jpeg loader
- From: Matthias Clasen <matthiasc src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gtk/gamma-shenanigans: 7/8] Add a jpeg loader
- Date: Fri, 10 Sep 2021 16:22:44 +0000 (UTC)
commit c2229736a86c35e805843f0cc3dc1b5797f97eae
Author: Matthias Clasen <mclasen redhat com>
Date: Thu Sep 9 23:52:39 2021 -0400
Add a jpeg loader
gdk/gdkjpeg.c | 230 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
gdk/gdkjpeg.h | 35 +++++++++
gdk/meson.build | 3 +-
meson.build | 1 +
4 files changed, 268 insertions(+), 1 deletion(-)
---
diff --git a/gdk/gdkjpeg.c b/gdk/gdkjpeg.c
new file mode 100644
index 0000000000..066ecdcb8f
--- /dev/null
+++ b/gdk/gdkjpeg.c
@@ -0,0 +1,230 @@
+/* GDK - The GIMP Drawing Kit
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include "gdkjpeg.h"
+
+#include "gdktexture.h"
+#include "gdkmemorytextureprivate.h"
+#include <jpeglib.h>
+#include <jerror.h>
+#include <stdio.h>
+#include <setjmp.h>
+
+/* {{{ IO handling */
+
+static ssize_t
+read_stream (void *cookie,
+ char *buf,
+ size_t size)
+{
+ GInputStream *stream = cookie;
+
+ return g_input_stream_read (stream, buf, size, NULL, NULL);
+}
+
+static ssize_t
+write_stream (void *cookie,
+ const char *buf,
+ size_t size)
+{
+ GOutputStream *stream = cookie;
+
+ return g_output_stream_write (stream, buf, size, NULL, NULL);
+}
+
+static int
+seek_stream (void *cookie,
+ off64_t *offset,
+ int whence)
+{
+ GSeekable *seekable = cookie;
+ GSeekType seek_type;
+
+ if (whence == SEEK_SET)
+ seek_type = G_SEEK_SET;
+ else if (whence == SEEK_CUR)
+ seek_type = G_SEEK_CUR;
+ else if (whence == SEEK_END)
+ seek_type = G_SEEK_END;
+ else
+ g_assert_not_reached ();
+
+ if (g_seekable_seek (seekable, *offset, seek_type, NULL, NULL))
+ {
+ *offset = g_seekable_tell (seekable);
+ return 0;
+ }
+
+ return -1;
+}
+
+static int
+close_stream (void *cookie)
+{
+ GInputStream *stream = cookie;
+
+ g_object_unref (stream);
+
+ return 0;
+}
+
+static cookie_io_functions_t cookie_funcs = {
+ read_stream,
+ write_stream,
+ seek_stream,
+ close_stream
+};
+
+/* }}} */
+/* {{{ Error handling */
+
+struct error_handler_data {
+ struct jpeg_error_mgr pub;
+ sigjmp_buf setjmp_buffer;
+ GError **error;
+};
+
+static void
+fatal_error_handler (j_common_ptr cinfo)
+{
+ struct error_handler_data *errmgr;
+
+ errmgr = (struct error_handler_data *) cinfo->err;
+
+ siglongjmp (errmgr->setjmp_buffer, 1);
+
+ g_assert_not_reached ();
+}
+
+static void
+output_message_handler (j_common_ptr cinfo)
+{
+ /* do nothing */
+}
+
+/* }}} */
+/* {{{ Public API */
+
+GdkTexture *
+gdk_load_jpeg (GInputStream *stream,
+ GError **error)
+{
+ FILE *file;
+ struct jpeg_decompress_struct info;
+ struct error_handler_data jerr;
+ struct jpeg_error_mgr err;
+ int width, height;
+ int size;
+ unsigned char *data;
+ unsigned char *row[1];
+ GBytes *bytes;
+ GdkTexture *texture;
+
+ file = fopencookie (g_object_ref (stream), "r", cookie_funcs);
+
+ info.err = jpeg_std_error (&jerr.pub);
+ jerr.pub.error_exit = fatal_error_handler;
+ jerr.pub.output_message = output_message_handler;
+ jerr.error = error;
+
+ if (sigsetjmp (jerr.setjmp_buffer, 1))
+ {
+ fclose (file);
+ jpeg_destroy_decompress (&info);
+ return NULL;
+ }
+
+ info.err = jpeg_std_error (&err);
+ jpeg_create_decompress (&info);
+
+ jpeg_stdio_src (&info, file);
+ jpeg_read_header (&info, TRUE);
+ jpeg_start_decompress (&info);
+
+ width = info.output_width;
+ height = info.output_height;
+
+ size = width * height * 3;
+ data = g_malloc (size);
+
+ while (info.output_scanline < info.output_height)
+ {
+ row[0] = (unsigned char *)(&data[3 *info.output_width * info.output_scanline]);
+ jpeg_read_scanlines (&info, row, 1);
+ }
+
+ jpeg_finish_decompress (&info);
+ jpeg_destroy_decompress (&info);
+ fclose (file);
+
+ bytes = g_bytes_new_take (data, size);
+
+ texture = gdk_memory_texture_new (width, height,
+ GDK_MEMORY_R8G8B8,
+ bytes, width * 3);
+
+ g_bytes_unref (bytes);
+
+ return texture;
+}
+
+/* }}} */
+/* {{{ Async code */
+
+static void
+load_jpeg_in_thread (GTask *task,
+ gpointer source_object,
+ gpointer task_data,
+ GCancellable *cancellable)
+{
+ GInputStream *stream = source_object;
+ GdkTexture *texture;
+ GError *error = NULL;
+
+ texture = gdk_load_jpeg (stream, &error);
+
+ if (texture)
+ g_task_return_pointer (task, texture, g_object_unref);
+ else
+ g_task_return_error (task, error);
+}
+
+void
+gdk_load_jpeg_async (GInputStream *stream,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data)
+{
+ GTask *task;
+
+ task = g_task_new (stream, cancellable, callback, user_data);
+ g_task_run_in_thread (task, load_jpeg_in_thread);
+ g_object_unref (task);
+}
+
+GdkTexture *
+gdk_load_jpeg_finish (GAsyncResult *result,
+ GError **error)
+{
+ return g_task_propagate_pointer (G_TASK (result), error);
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/gdk/gdkjpeg.h b/gdk/gdkjpeg.h
new file mode 100644
index 0000000000..3b865b1fb6
--- /dev/null
+++ b/gdk/gdkjpeg.h
@@ -0,0 +1,35 @@
+/* GDK - The GIMP Drawing Kit
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GDK_JPEG_H__
+#define __GDK_JPEG_H__
+
+#include "gdkmemorytexture.h"
+#include <gio/gio.h>
+
+GdkTexture *gdk_load_jpeg (GInputStream *stream,
+ GError **error);
+
+void gdk_load_jpeg_async (GInputStream *stream,
+ GCancellable *cancellable,
+ GAsyncReadyCallback callback,
+ gpointer user_data);
+
+GdkTexture *gdk_load_jpeg_finish (GAsyncResult *result,
+ GError **error);
+
+#endif
diff --git a/gdk/meson.build b/gdk/meson.build
index d1295af5c8..da8e5324a9 100644
--- a/gdk/meson.build
+++ b/gdk/meson.build
@@ -52,6 +52,7 @@ gdk_public_sources = files([
'gdkdragsurface.c',
'gdkpng.c',
'gdktiff.c',
+ 'gdkjpeg.c',
])
gdk_public_headers = files([
@@ -256,7 +257,7 @@ endif
libgdk = static_library('gdk',
sources: [gdk_sources, gdk_backends_gen_headers, gdkconfig],
- dependencies: gdk_deps + [libgtk_css_dep, png_dep, tiff_dep],
+ dependencies: gdk_deps + [libgtk_css_dep, png_dep, tiff_dep, jpeg_dep],
link_with: [libgtk_css],
include_directories: [confinc, gdkx11_inc, wlinc],
c_args: libgdk_c_args + common_cflags,
diff --git a/meson.build b/meson.build
index 0a7132011c..1afff0c3eb 100644
--- a/meson.build
+++ b/meson.build
@@ -396,6 +396,7 @@ pixbuf_dep = dependency('gdk-pixbuf-2.0', version: gdk_pixbuf_req,
default_options: ['png=enabled', 'jpeg=enabled', 'builtin_loaders=png,jpeg',
'man=false'])
png_dep = dependency('libpng16')
tiff_dep = dependency('libtiff-4')
+jpeg_dep = dependency('libjpeg')
epoxy_dep = dependency('epoxy', version: epoxy_req,
fallback: ['libepoxy', 'libepoxy_dep'])
harfbuzz_dep = dependency('harfbuzz', version: '>= 2.1.0', required: false,
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]