From 192698b7bdfe04e9b6ef4ad4e022422f34a00a53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C5=99emysl=20Eric=20Janouch?= Date: Wed, 10 Nov 2021 21:33:30 +0100 Subject: [PATCH] Add support for defaulting to gdk-pixbuf --- README.adoc | 2 +- fastiv-io.c | 33 +++++++++++++++++++++++++++++++++ fastiv.c | 5 ++++- meson.build | 2 ++ meson_options.txt | 2 ++ 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/README.adoc b/README.adoc index 0b1b04f..853e0f0 100644 --- a/README.adoc +++ b/README.adoc @@ -2,7 +2,7 @@ fastiv ====== 'fastiv' is a fast image viewer, supporting BMP, PNG, GIF, JPEG, and optionally -RAW and SVG pictures. Currently, it's not particularly usable. +RAW and SVG pictures, or whatever gdk-pixbuf loads. Currently, it's very basic. Non-goals --------- diff --git a/fastiv-io.c b/fastiv-io.c index 66b7010..a1811a7 100644 --- a/fastiv-io.c +++ b/fastiv-io.c @@ -26,6 +26,10 @@ #ifdef HAVE_LIBRSVG #include #endif // HAVE_LIBRSVG +#ifdef HAVE_GDKPIXBUF +#include +#include +#endif // HAVE_GDKPIXBUF #define WUFFS_IMPLEMENTATION #define WUFFS_CONFIG__MODULES @@ -479,6 +483,24 @@ open_librsvg(const gchar *data, gsize len, const gchar *path, GError **error) } #endif // HAVE_LIBRSVG -------------------------------------------------------- +#ifdef HAVE_GDKPIXBUF // ------------------------------------------------------ + +static cairo_surface_t * +open_gdkpixbuf(const gchar *data, gsize len, GError **error) +{ + GInputStream *is = g_memory_input_stream_new_from_data(data, len, NULL); + GdkPixbuf *pixbuf = gdk_pixbuf_new_from_stream(is, NULL, error); + g_object_unref(is); + if (!pixbuf) + return NULL; + + cairo_surface_t *surface = + gdk_cairo_surface_create_from_pixbuf(pixbuf, 1, NULL); + g_object_unref(pixbuf); + return surface; +} + +#endif // HAVE_GDKPIXBUF ------------------------------------------------------ cairo_surface_t * fastiv_io_open(const gchar *path, GError **error) @@ -550,6 +572,17 @@ fastiv_io_open_from_data(const char *data, size_t len, const gchar *path, g_clear_error(error); } #endif // HAVE_LIBRSVG -------------------------------------------------------- +#ifdef HAVE_GDKPIXBUF // ------------------------------------------------------ + // This is only used as a last resort, the rest above is special-cased. + if ((surface = open_gdkpixbuf(data, len, error)) || + (error && (*error)->code != GDK_PIXBUF_ERROR_UNKNOWN_TYPE)) + break; + + if (error) { + g_debug("%s", (*error)->message); + g_clear_error(error); + } +#endif // HAVE_GDKPIXBUF ------------------------------------------------------ // TODO(p): Integrate gdk-pixbuf as a fallback (optional dependency). set_error(error, "unsupported file type"); diff --git a/fastiv.c b/fastiv.c index 273e63a..27093fb 100644 --- a/fastiv.c +++ b/fastiv.c @@ -182,10 +182,12 @@ on_open(void) "_Cancel", GTK_RESPONSE_CANCEL, "_Open", GTK_RESPONSE_ACCEPT, NULL); - // NOTE: gdk-pixbuf has gtk_file_filter_add_pixbuf_formats(). GtkFileFilter *filter = gtk_file_filter_new(); for (const char **p = fastiv_io_supported_media_types; *p; p++) gtk_file_filter_add_mime_type(filter, *p); +#ifdef HAVE_GDKPIXBUF + gtk_file_filter_add_pixbuf_formats(filter); +#endif // HAVE_GDKPIXBUF gtk_file_filter_set_name(filter, "Supported images"); gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), filter); @@ -389,6 +391,7 @@ main(int argc, char *argv[]) G_CALLBACK(on_key_press), NULL); gtk_container_add(GTK_CONTAINER(g.window), g.stack); + // TODO(p): Also milk gdk-pixbuf, if linked in, needs to be done in runtime. g.supported_globs = extract_mime_globs(fastiv_io_supported_media_types); g.files = g_ptr_array_new_full(16, g_free); gchar *cwd = g_get_current_dir(); diff --git a/meson.build b/meson.build index d5acea1..aaa33a8 100644 --- a/meson.build +++ b/meson.build @@ -3,6 +3,7 @@ project('fastiv', 'c', default_options : ['c_std=gnu99'], version : '0.1.0') # TODO(p): Use libraw_r later, when we start parallelizing/preloading. libraw = dependency('libraw', required : get_option('libraw')) librsvg = dependency('librsvg-2.0', required : get_option('librsvg')) +gdkpixbuf = dependency('gdk-pixbuf-2.0', required : get_option('gdk-pixbuf')) dependencies = [ dependency('gtk+-3.0'), dependency('libturbojpeg'), @@ -18,6 +19,7 @@ conf.set_quoted('PROJECT_NAME', meson.project_name()) conf.set_quoted('PROJECT_VERSION', meson.project_version()) conf.set('HAVE_LIBRAW', libraw.found()) conf.set('HAVE_LIBRSVG', librsvg.found()) +conf.set('HAVE_GDKPIXBUF', gdkpixbuf.found()) configure_file( output : 'config.h', configuration : conf, diff --git a/meson_options.txt b/meson_options.txt index 3d89ed8..fc53ac5 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -2,3 +2,5 @@ option('libraw', type : 'feature', value : 'auto', description : 'Build with RAW support, requires LibRaw') option('librsvg', type : 'feature', value : 'auto', description : 'Build with SVG support, requires librsvg') +option('gdk-pixbuf', type : 'feature', value : 'auto', + description : 'Build with a fallback to the gdk-pixbuf library')