From 6866ef82bac864a1fb45fc7868804ceb350a94bb Mon Sep 17 00:00:00 2001 From: Bert Gijsbers Date: Thu, 26 Oct 2023 18:33:44 +0200 Subject: [PATCH] Add `supportsFormat` to check for support of additional image formats. --- src/yimage.h | 1 + src/yimage2.cc | 28 ++++++++++++++++++++++++++++ src/yimage_gdk.cc | 21 +++++++++++++++++++++ src/yximage.cc | 16 ++++++++++++++++ 4 files changed, 66 insertions(+) diff --git a/src/yimage.h b/src/yimage.h index 344ae4b8e..426926540 100644 --- a/src/yimage.h +++ b/src/yimage.h @@ -26,6 +26,7 @@ class YImage: public refcounted { static ref createFromIconProperty(long *pixels, unsigned width, unsigned height); static bool supportsDepth(unsigned depth); + static bool supportsFormat(const char* format); static const char* renderName(); unsigned width() const { return fWidth; } diff --git a/src/yimage2.cc b/src/yimage2.cc index a687c483b..e53eb603c 100644 --- a/src/yimage2.cc +++ b/src/yimage2.cc @@ -38,6 +38,34 @@ bool YImage::supportsDepth(unsigned depth) { return depth == xapp->depth() || depth == 32; } +#ifdef IMLIB2_VERSION +extern "C" { + typedef struct _ImlibLoader ImlibLoader; +#if IMLIB2_VERSION >= 10801 + ImlibLoader* __imlib_FindBestLoader + (const char *file, const char *format, int for_save); +#else + ImlibLoader* __imlib_FindBestLoaderForFormat + (const char *format, int for_save); +#endif +} +#endif + +bool YImage::supportsFormat(const char* format) { + if ( !strcmp(format, "xpm") || + !strcmp(format, "png") || + !strcmp(format, "jpg") ) + return true; +#ifdef IMLIB2_VERSION +#if IMLIB2_VERSION >= 10801 + return __imlib_FindBestLoader(nullptr, format, 0) != nullptr; +#else + return __imlib_FindBestLoaderForFormat(format, 0) != nullptr; +#endif +#endif + return false; +} + bool YImage2::hasAlpha() const { context(); return imlib_image_has_alpha(); diff --git a/src/yimage_gdk.cc b/src/yimage_gdk.cc index bc1b243a3..11dcd2731 100644 --- a/src/yimage_gdk.cc +++ b/src/yimage_gdk.cc @@ -14,6 +14,27 @@ bool YImage::supportsDepth(unsigned depth) { return depth == unsigned(xlib_rgb_get_depth()); } +bool YImage::supportsFormat(const char* format) { + if ( !strcmp(format, "xpm") || + !strcmp(format, "png") || + !strcmp(format, "jpg") ) + return true; + + bool supports = false; + GSList* formats = gdk_pixbuf_get_formats(); + int length = g_slist_length(formats); + for (int i = 0; i < length && supports == false; i++) { + GdkPixbufFormat* info = (GdkPixbufFormat *) g_slist_nth_data(formats, i); + gchar** exts = gdk_pixbuf_format_get_extensions(info); + for (gchar** p = exts; *p && supports == false; ++p) { + supports = (strcmp(*p, format) == 0); + } + g_strfreev(exts); + } + g_slist_free(formats); + return supports; +} + bool YImageGDK::hasAlpha() const { return gdk_pixbuf_get_has_alpha(fPixbuf); } diff --git a/src/yximage.cc b/src/yximage.cc index d9fa564b6..fd294d526 100644 --- a/src/yximage.cc +++ b/src/yximage.cc @@ -136,6 +136,22 @@ bool YImage::supportsDepth(unsigned depth) { return depth == 32 || depth == xapp->depth(); } +bool YImage::supportsFormat(const char* format) { + if (strcmp(format, "xbm") == 0) + return true; + else if (strcmp(format, "xpm") == 0) + return true; +#ifdef CONFIG_LIBPNG + else if (strcmp(format, "png") == 0) + return true; +#endif +#ifdef CONFIG_LIBJPEG + else if (strcmp(format, "jpg") == 0 || strcmp(format, "jpeg") == 0) + return true; +#endif + return false; +} + ref YImage::load(upath filename) { ref image;