diff options
| author | Felix Morgner <felix.morgner@gmail.com> | 2021-02-10 15:43:01 +0100 |
|---|---|---|
| committer | Felix Morgner <felix.morgner@gmail.com> | 2021-02-10 15:56:18 +0100 |
| commit | d70aaf0ef92cac0781dabb60411bbd907e7f0caa (patch) | |
| tree | 949115e3b3d0c117d83af7c0924322084e178fbf /include | |
| parent | 4fc2b7acffb9699c3ef4fbe5027124e589735be0 (diff) | |
| download | wanda-d70aaf0ef92cac0781dabb60411bbd907e7f0caa.tar.xz wanda-d70aaf0ef92cac0781dabb60411bbd907e7f0caa.zip | |
wallpaper: calculate average color
Diffstat (limited to 'include')
| -rw-r--r-- | include/wanda/magic.hpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/include/wanda/magic.hpp b/include/wanda/magic.hpp new file mode 100644 index 0000000..fcb153e --- /dev/null +++ b/include/wanda/magic.hpp @@ -0,0 +1,58 @@ +#ifndef WANDA_MAGIC_HPP +#define WANDA_MAGIC_HPP + +#include <magic.h> + +#include <filesystem> +#include <memory> +#include <string> +#include <type_traits> + +namespace wanda +{ + struct magic + { + struct closer + { + auto operator()(magic_t handle) const noexcept -> void + { + magic_close(handle); + } + }; + + enum struct mime_type + { + unknown, + image_jpeg, + image_png, + }; + + magic() + : m_handle{magic_open(MAGIC_MIME_TYPE)} + { + magic_load(m_handle.get(), nullptr); + } + + auto type(std::filesystem::path path) -> mime_type + { + auto magic_type = std::string{magic_file(m_handle.get(), path.native().c_str())}; + + if (magic_type == "image/jpeg") + { + return mime_type::image_jpeg; + } + else if (magic_type == "image/png") + { + return mime_type::image_png; + } + + return mime_type::unknown; + } + + private: + std::unique_ptr<std::remove_pointer_t<magic_t>, closer> m_handle; + }; + +} // namespace wanda + +#endif
\ No newline at end of file |
