From 56158a42818946853c97b556e5803f84e5c35d43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=BCseyin=20ASLIT=C3=9CRK?= Date: Mon, 15 Jun 2020 15:13:12 +0300 Subject: [PATCH] LibGfx: Add a new Bitmap::is_path_a_supported_image_format() method Move the image file detection code to the File class to prevent code duplication. --- Libraries/LibGfx/Bitmap.cpp | 11 ++++++----- Libraries/LibGfx/Bitmap.h | 14 ++++++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/Libraries/LibGfx/Bitmap.cpp b/Libraries/LibGfx/Bitmap.cpp index 0391b9b201..c9236e2c96 100644 --- a/Libraries/LibGfx/Bitmap.cpp +++ b/Libraries/LibGfx/Bitmap.cpp @@ -29,8 +29,8 @@ #include #include #include -#include #include +#include #include #include #include @@ -86,10 +86,11 @@ RefPtr Bitmap::create_wrapper(BitmapFormat format, const IntSize& size, RefPtr Bitmap::load_from_file(const StringView& path) { - if(path.ends_with(".png")) - return load_png(path); - if(path.ends_with(".gif")) - return load_gif(path); +#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \ + if (path.ends_with(Ext)) \ + return load_##Name(path); + ENUMERATE_IMAGE_FORMATS +#undef __ENUMERATE_IMAGE_FORMAT return nullptr; } diff --git a/Libraries/LibGfx/Bitmap.h b/Libraries/LibGfx/Bitmap.h index 4d1a0ef90b..4e313a402a 100644 --- a/Libraries/LibGfx/Bitmap.h +++ b/Libraries/LibGfx/Bitmap.h @@ -33,6 +33,10 @@ #include #include +#define ENUMERATE_IMAGE_FORMATS \ + __ENUMERATE_IMAGE_FORMAT(png, ".png") \ + __ENUMERATE_IMAGE_FORMAT(gif, ".gif") + namespace Gfx { enum class BitmapFormat { @@ -54,6 +58,16 @@ public: static RefPtr create_wrapper(BitmapFormat, const IntSize&, size_t pitch, RGBA32*); static RefPtr load_from_file(const StringView& path); static RefPtr create_with_shared_buffer(BitmapFormat, NonnullRefPtr&&, const IntSize&); + static bool is_path_a_supported_image_format(const StringView& path) + { +#define __ENUMERATE_IMAGE_FORMAT(Name, Ext) \ + if (path.ends_with(Ext)) \ + return true; + ENUMERATE_IMAGE_FORMATS +#undef __ENUMERATE_IMAGE_FORMAT + + return false; + } RefPtr rotated(Gfx::RotationDirection) const; RefPtr flipped(Gfx::Orientation) const;