42#ifndef UFO_IO_JPEG_HPP
43#define UFO_IO_JPEG_HPP
46#include <ufo/io/file_handler.hpp>
47#include <ufo/io/image_properties.hpp>
48#include <ufo/vision/color.hpp>
49#include <ufo/vision/image.hpp>
60bool readJPEG(FileHandler fp, std::uint8_t* image);
62bool writeJPEG(FileHandler& fp, std::uint8_t
const* image, std::uint32_t width,
63 std::uint32_t height,
int num_channels,
int quality);
66[[nodiscard]] ImageProperties imagePropertiesJPEG(std::filesystem::path
const& file);
68template <ColorType CT,
class T,
bool Alpha,
bool Weight>
69bool readJPEG(std::filesystem::path
const& file,
70 Image<Color<CT, T, Alpha, Weight>>& image)
72 FileHandler fp(file.c_str(),
"rb");
75 std::println(stderr,
"[UFO | Read JPEG] Failed to open file: {}", file.string());
79 auto fun = [&image](FileHandler& fp,
auto& tmp) {
80 using C =
typename std::remove_cvref_t<
decltype(tmp)>::value_type;
81 if (detail::readJPEG(fp,
reinterpret_cast<std::uint8_t*
>(tmp.data()))) {
82 image = convert<Color<CT, T, Alpha, Weight>>(tmp);
88 auto prop = imagePropertiesJPEG(file);
90 Image<Color<ColorType::GRAY, std::uint8_t, false, false>> tmp(prop.height,
93 }
else if (!prop.grayscale) {
94 Image<Color<ColorType::RGB, std::uint8_t, false, false>> tmp(prop.height, prop.width);
97 std::println(stderr,
"[UFO | Read JPEG] Unknown JPEG type");
103template <ColorType CT,
class T,
bool Alpha,
bool Weight>
104bool writeJPEG(std::filesystem::path
const& file,
105 Image<Color<CT, T, Alpha, Weight>>
const& image,
int quality = 90)
107 FileHandler fp(file.c_str(),
"wb");
110 std::println(stderr,
"[UFO | Write JPEG] Failed to create file: {}", file.string());
114 std::uint32_t width = image.cols();
115 std::uint32_t height = image.rows();
116 int num_channels = ColorType::GRAY == CT ? 1 : 3;
118 constexpr ColorType
const CT2 =
119 ColorType::GRAY == CT ? ColorType::GRAY : ColorType::RGB;
121 if constexpr (CT == CT2 && std::is_same_v<T, std::uint8_t> && !
Alpha && !
Weight) {
122 return detail::writeJPEG(fp,
reinterpret_cast<std::uint8_t const*
>(image.data()),
123 width, height, num_channels, quality);
125 auto tmp = convert<Color<CT2, std::uint8_t, false, false>>(image);
127 return detail::writeJPEG(fp,
reinterpret_cast<std::uint8_t const*
>(image.data()),
128 width, height, num_channels, quality);
@ Alpha
Include an alpha channel.
@ Weight
Include an accumulation weight.
All vision-related classes and functions.