[gtk/gamma-shenanigans] Add a jpeg loader



commit 39fe08c326557b34952223f2900b6fabaad44fd1
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Sep 9 23:52:39 2021 -0400

    Add a jpeg loader

 gdk/gdkjpeg.c   | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 gdk/gdkjpeg.h   |  55 +++++++++++++++++
 gdk/meson.build |   3 +-
 meson.build     |   1 +
 4 files changed, 246 insertions(+), 1 deletion(-)
---
diff --git a/gdk/gdkjpeg.c b/gdk/gdkjpeg.c
new file mode 100644
index 0000000000..a51364b73f
--- /dev/null
+++ b/gdk/gdkjpeg.c
@@ -0,0 +1,188 @@
+/* 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 <stdio.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
+};
+
+/* }}} */
+/* {{{ Public API */
+
+GdkTexture *
+gdk_load_jpeg (GInputStream  *stream,
+               GError       **error)
+{
+  FILE *file;
+  struct jpeg_decompress_struct info;
+  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 (&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..bdaab8b50a
--- /dev/null
+++ b/gdk/gdkjpeg.h
@@ -0,0 +1,55 @@
+/* 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);
+
+gboolean    gdk_save_jpeg         (GOutputStream    *stream,
+                                   const guchar     *data,
+                                   int               width,
+                                   int               height,
+                                   GdkMemoryFormat   format,
+                                   GError          **error);
+
+void        gdk_save_jpeg_async  (GOutputStream          *stream,
+                                  const guchar           *data,
+                                  int                     width,
+                                  int                     height,
+                                  GdkMemoryFormat         format,
+                                  GCancellable           *cancellable,
+                                  GAsyncReadyCallback     callback,
+                                  gpointer                user_data);
+
+gboolean    gdk_save_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]