UFO 1.0.0
An Efficient Probabilistic 3D Mapping Framework That Embraces the Unknown
Loading...
Searching...
No Matches
jpeg.hpp
1
42#ifndef UFO_IO_JPEG_HPP
43#define UFO_IO_JPEG_HPP
44
45// UFO
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>
50
51// STL
52#include <cstdio>
53#include <filesystem>
54#include <print>
55
56namespace ufo
57{
58namespace detail
59{
60bool readJPEG(FileHandler fp, std::uint8_t* image);
61
62bool writeJPEG(FileHandler& fp, std::uint8_t const* image, std::uint32_t width,
63 std::uint32_t height, int num_channels, int quality);
64} // namespace detail
65
66[[nodiscard]] ImageProperties imagePropertiesJPEG(std::filesystem::path const& file);
67
68template <ColorType CT, class T, bool Alpha, bool Weight>
69bool readJPEG(std::filesystem::path const& file,
70 Image<Color<CT, T, Alpha, Weight>>& image)
71{
72 FileHandler fp(file.c_str(), "rb");
73
74 if (!fp) {
75 std::println(stderr, "[UFO | Read JPEG] Failed to open file: {}", file.string());
76 return false;
77 }
78
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);
83 return true;
84 }
85 return false;
86 };
87
88 auto prop = imagePropertiesJPEG(file);
89 if (prop.grayscale) {
90 Image<Color<ColorType::GRAY, std::uint8_t, false, false>> tmp(prop.height,
91 prop.width);
92 return fun(fp, tmp);
93 } else if (!prop.grayscale) {
94 Image<Color<ColorType::RGB, std::uint8_t, false, false>> tmp(prop.height, prop.width);
95 return fun(fp, tmp);
96 } else {
97 std::println(stderr, "[UFO | Read JPEG] Unknown JPEG type");
98 return false;
99 }
100}
101
102// Compression quality (0..100; 5-95 is most useful range,\n"
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)
106{
107 FileHandler fp(file.c_str(), "wb");
108
109 if (!fp) {
110 std::println(stderr, "[UFO | Write JPEG] Failed to create file: {}", file.string());
111 return false;
112 }
113
114 std::uint32_t width = image.cols();
115 std::uint32_t height = image.rows();
116 int num_channels = ColorType::GRAY == CT ? 1 : 3;
117
118 constexpr ColorType const CT2 =
119 ColorType::GRAY == CT ? ColorType::GRAY : ColorType::RGB;
120
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);
124 } else {
125 auto tmp = convert<Color<CT2, std::uint8_t, false, false>>(image);
126
127 return detail::writeJPEG(fp, reinterpret_cast<std::uint8_t const*>(image.data()),
128 width, height, num_channels, quality);
129 }
130}
131} // namespace ufo
132
133#endif // UFO_IO_JPEG_HPP
@ Alpha
Include an alpha channel.
@ Weight
Include an accumulation weight.
All vision-related classes and functions.
Definition cloud.hpp:49