FlatImage
A configurable Linux containerization system
Loading...
Searching...
No Matches
image.hpp
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <filesystem>
12#include <boost/gil.hpp>
13#include <boost/gil/extension/io/jpeg.hpp>
14#include <boost/gil/extension/io/png.hpp>
15#include <boost/gil/extension/numeric/sampler.hpp>
16#include <boost/gil/extension/numeric/resample.hpp>
17
18#include "env.hpp"
19#include "log.hpp"
20#include "subprocess.hpp"
21#include "../std/enum.hpp"
22
31namespace ns_image
32{
33
34namespace
35{
36
37namespace fs = std::filesystem;
38
39ENUM(ImageFormat, JPG, PNG);
40
55inline Value<void> resize_impl(fs::path const& path_file_src
56 , fs::path const& path_file_dst
57 , uint32_t width
58 , uint32_t height)
59{
60 namespace gil = boost::gil;
61
62 // Create icon directory and set file name
63 logger("I::Reading image {}", path_file_src);
64 return_if(not Try(fs::is_regular_file(path_file_src))
65 , Error("E::File '{}' does not exist or is not a regular file", path_file_src)
66 );
67
68 // Determine image format
69 std::string ext = Try(path_file_src.extension().string());
70 std::ranges::transform(ext, ext.begin(), [](unsigned char c){ return static_cast<char>(std::tolower(c)); });
71
72 ImageFormat format = Pop(
73 (ext == ".jpg" || ext == ".jpeg") ? Value<ImageFormat>(ImageFormat::JPG) :
74 (ext == ".png") ? Value<ImageFormat>(ImageFormat::PNG) :
75 Error("E::Input image of invalid format: '{}'", ext)
76 );
77
78 // Read image into memory
79 gil::rgba8_image_t img;
80 if (format == ImageFormat::JPG)
81 {
82 Try(gil::read_and_convert_image(path_file_src, img, gil::jpeg_tag()));
83 }
84 else if (format == ImageFormat::PNG)
85 {
86 Try(gil::read_and_convert_image(path_file_src, img, gil::png_tag()));
87 }
88 logger("I::Image size is {}x{}", std::to_string(img.width()), std::to_string(img.height()));
89 logger("I::Saving image to {}", path_file_dst);
90 // Search for imagemagick
91 fs::path path_bin_magick = Pop(ns_env::search_path("magick"));
92 // Resize
93 Pop(ns_subprocess::Subprocess(path_bin_magick)
94 .with_args(path_file_src)
95 .with_args("-resize", (img.width() > img.height())? std::format("{}x", width) : std::format("x{}", height))
96 .with_args(path_file_dst)
97 .spawn()->wait());
98 return {};
99}
100
101} // namespace
102
103
104
105
115inline Value<void> resize(fs::path const& path_file_src
116 , fs::path const& path_file_dst
117 , uint32_t width
118 , uint32_t height)
119{
120 return resize_impl(path_file_src, path_file_dst, width, height);
121}
122
123} // namespace ns_image
124
125/* vim: set expandtab fdm=marker ts=2 sw=2 tw=100 et :*/
std::unique_ptr< Child > spawn()
Spawns (forks) the child process and begins execution.
Subprocess & with_args(Args &&... args)
Arguments forwarded as the process' arguments.
Custom enumeration class.
A library for manipulating environment variables.
A library for file logging.
#define logger(fmt,...)
Compile-time log level dispatch macro with automatic location capture.
Definition log.hpp:682
Value< fs::path > search_path(std::string const &query)
Search the directories in the PATH variable for the given input file name.
Definition env.hpp:150
Value< void > resize_impl(fs::path const &path_file_src, fs::path const &path_file_dst, uint32_t width, uint32_t height)
Internal implementation for resizing an image.
Definition image.hpp:55
Image processing utilities.
Value< void > resize(fs::path const &path_file_src, fs::path const &path_file_dst, uint32_t width, uint32_t height)
Resizes an input image to the specified width and height.
Definition image.hpp:115
Enhanced expected type with integrated logging capabilities.
Definition expected.hpp:44
A library to spawn sub-processes in linux.