Skip to content

Commit

Permalink
Add supportsFormat to check for support of additional image formats.
Browse files Browse the repository at this point in the history
  • Loading branch information
gijsbers committed Oct 26, 2023
1 parent 5b5632f commit 6866ef8
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/yimage.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class YImage: public refcounted {
static ref<YImage> 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; }
Expand Down
28 changes: 28 additions & 0 deletions src/yimage2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
21 changes: 21 additions & 0 deletions src/yimage_gdk.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
16 changes: 16 additions & 0 deletions src/yximage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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> YImage::load(upath filename)
{
ref<YImage> image;
Expand Down

0 comments on commit 6866ef8

Please sign in to comment.