CARLA
Loading...
Searching...
No Matches
ImageIOConfig.h
Go to the documentation of this file.
1// Copyright (c) 2026 Computer Vision Center (CVC) at the Universitat Autonoma
2// de Barcelona (UAB).
3//
4// This work is licensed under the terms of the MIT license.
5// For a copy, see <https://opensource.org/licenses/MIT>.
6
7#pragma once
8
9#include "carla/FileSystem.h"
10#include "carla/Logging.h"
11#include "carla/StringUtil.h"
13
14#ifndef LIBCARLA_IMAGE_SUPPORT_PNG
15 #define LIBCARLA_IMAGE_SUPPORT_PNG false
16#endif
17
18#ifndef LIBCARLA_IMAGE_SUPPORT_JPEG
19 #define LIBCARLA_IMAGE_SUPPORT_JPEG false
20#endif
21
22#ifndef LIBCARLA_IMAGE_SUPPORT_TIFF
23 #define LIBCARLA_IMAGE_SUPPORT_TIFF false
24#endif
25
26#if defined(__clang__)
27# pragma clang diagnostic push
28# pragma clang diagnostic ignored "-Wunused-parameter"
29#endif
30
31#if LIBCARLA_IMAGE_SUPPORT_PNG
32# ifndef png_infopp_NULL
33# define png_infopp_NULL (png_infopp)NULL
34# endif // png_infopp_NULL
35# ifndef int_p_NULL
36# define int_p_NULL (int*)NULL
37# endif // int_p_NULL
38# if defined(__clang__)
39# pragma clang diagnostic push
40# pragma clang diagnostic ignored "-Wignored-qualifiers"
41# pragma clang diagnostic ignored "-Wparentheses"
42# endif
43# include <boost/gil/extension/io/png.hpp>
44# if defined(__clang__)
45# pragma clang diagnostic pop
46# endif
47#endif
48
49#if LIBCARLA_IMAGE_WITH_JPEG_SUPPORT
50# include <boost/gil/extension/io/jpeg.hpp>
51#endif
52
53#if LIBCARLA_IMAGE_WITH_TIFF_SUPPORT
54# include <boost/gil/extension/io/tiff.hpp>
55#endif
56
57#if defined(__clang__)
58# pragma clang diagnostic pop
59#endif
60
61namespace carla {
62namespace image {
63namespace io {
64
65 constexpr bool has_png_support() {
67 }
68
69 constexpr bool has_jpeg_support() {
71 }
72
73 constexpr bool has_tiff_support() {
75 }
76
77 static_assert(
79 "No image format supported, please compile with at least one of "
80 "LIBCARLA_IMAGE_SUPPORT_PNG, "
81 "LIBCARLA_IMAGE_SUPPORT_JPEG "
82 "or LIBCARLA_IMAGE_SUPPORT_TIFF");
83
84namespace detail {
85
86 template <typename ViewT, typename IOTag>
88 static constexpr bool value = boost::gil::is_write_supported<typename boost::gil::get_pixel_type<ViewT>::type, IOTag>::value;
89 };
90
91 struct io_png {
92
93 static constexpr bool is_supported = has_png_support();
94
95#if LIBCARLA_IMAGE_SUPPORT_PNG
96
97 static constexpr const char *get_default_extension() {
98 return "png";
99 }
100
101 template <typename Str>
102 static bool match_extension(const Str &str) {
103 return StringUtil::EndsWith(str, get_default_extension());
104 }
105
106 template <typename Str, typename ImageT>
107 static void read_image(Str &&in_filename, ImageT &image) {
108 boost::gil::read_and_convert_image(std::forward<Str>(in_filename), image, boost::gil::png_tag());
109 }
110
111 template <typename Str, typename ViewT>
112 static void write_view(Str &&out_filename, const ViewT &view) {
113 boost::gil::write_view(std::forward<Str>(out_filename), view, boost::gil::png_tag());
114 }
115
116#endif // LIBCARLA_IMAGE_SUPPORT_PNG
117 };
118
119 struct io_jpeg {
120
121 static constexpr bool is_supported = has_jpeg_support();
122
123#if LIBCARLA_IMAGE_WITH_JPEG_SUPPORT
124
125 static constexpr const char *get_default_extension() {
126 return "jpeg";
127 }
128
129 template <typename Str>
130 static bool match_extension(const Str &str) {
131 return StringUtil::EndsWith(str, get_default_extension()) ||
132 StringUtil::EndsWith(str, "jpg");
133 }
134
135 template <typename Str, typename ImageT>
136 static void read_image(Str &&in_filename, ImageT &image) {
137 boost::gil::read_image(std::forward<Str>(in_filename), image, boost::gil::jpeg_tag());
138 }
139
140 template <typename Str, typename ViewT>
141 static typename std::enable_if<is_write_supported<ViewT, boost::gil::jpeg_tag>::value>::type
142 write_view(Str &&out_filename, const ViewT &view) {
143 boost::gil::write_view(std::forward<Str>(out_filename), view, boost::gil::jpeg_tag());
144 }
145
146 template <typename Str, typename ViewT>
147 static typename std::enable_if<!is_write_supported<ViewT, boost::gil::jpeg_tag>::value>::type
148 write_view(Str &&out_filename, const ViewT &view) {
149 boost::gil::write_view(
150 std::forward<Str>(out_filename),
151 boost::gil::color_converted_view<boost::gil::rgb8_pixel_t>(view),
152 boost::gil::jpeg_tag());
153 }
154
155#endif // LIBCARLA_IMAGE_WITH_JPEG_SUPPORT
156 };
157
158 struct io_tiff {
159
160 static constexpr bool is_supported = has_tiff_support();
161
162#if LIBCARLA_IMAGE_SUPPORT_TIFF
163
164 static constexpr const char *get_default_extension() {
165 return "tiff";
166 }
167
168 template <typename Str>
169 static bool match_extension(const Str &str) {
170 return StringUtil::EndsWith(str, get_default_extension());
171 }
172
173 template <typename Str, typename ImageT>
174 static void read_image(Str &&in_filename, ImageT &image) {
175 boost::gil::read_and_convert_image(std::forward<Str>(in_filename), image, boost::gil::tiff_tag());
176 }
177
178 template <typename Str, typename ViewT>
179 static typename std::enable_if<is_write_supported<ViewT, boost::gil::tiff_tag>::value>::type
180 write_view(Str &&out_filename, const ViewT &view) {
181 boost::gil::write_view(std::forward<Str>(out_filename), view, boost::gil::tiff_tag());
182 }
183
184 template <typename Str, typename ViewT>
185 static typename std::enable_if<!is_write_supported<ViewT, boost::gil::tiff_tag>::value>::type
186 write_view(Str &&out_filename, const ViewT &view) {
187 boost::gil::write_view(
188 std::forward<Str>(out_filename),
189 boost::gil::color_converted_view<boost::gil::rgb8_pixel_t>(view),
190 boost::gil::tiff_tag());
191 }
192
193#endif // LIBCARLA_IMAGE_SUPPORT_TIFF
194 };
195
196 struct io_resolver {
197
198 template <typename IO, typename Str>
199 static typename std::enable_if<IO::is_supported, bool>::type match_extension(const Str &str) {
200 return IO::match_extension(str);
201 }
202
203 template <typename IO, typename Str>
204 static typename std::enable_if<!IO::is_supported, bool>::type match_extension(const Str &) {
205 return false;
206 }
207
208 template <typename IO, typename Str, typename... Args>
209 static typename std::enable_if<IO::is_supported>::type read_image(const Str &path, Args &&... args) {
210 log_debug("reading", path, "as", IO::get_default_extension());
211 IO::read_image(path, std::forward<Args>(args)...);
212 }
213
214 template <typename IO, typename... Args>
215 static typename std::enable_if<!IO::is_supported>::type read_image(Args &&...) {
216 DEBUG_ASSERT(false);
217 }
218
219 template <typename IO, typename... Args>
220 static typename std::enable_if<IO::is_supported>::type write_view(std::string &path, Args &&... args) {
221 FileSystem::ValidateFilePath(path, IO::get_default_extension());
222 log_debug("writing", path, "as", IO::get_default_extension());
223 IO::write_view(path, std::forward<Args>(args)...);
224 }
225
226 template <typename IO, typename... Args>
227 static typename std::enable_if<!IO::is_supported>::type write_view(Args &&...) {
228 DEBUG_ASSERT(false);
229 }
230 };
231
232 template <typename... IOs>
233 struct io_impl;
234
235 template <typename IO>
236 struct io_impl<IO> {
237 constexpr static bool is_supported = IO::is_supported;
238
239 template <typename... Args>
240 static void read_image(Args &&... args) {
241 io_resolver::read_image<IO>(std::forward<Args>(args)...);
242 }
243
244 template <typename... Args>
245 static void write_view(Args &&... args) {
246 io_resolver::write_view<IO>(std::forward<Args>(args)...);
247 }
248
249 template <typename Str, typename... Args>
250 static bool try_read_image(const Str &filename, Args &&... args) {
251 if (io_resolver::match_extension<IO>(filename)) {
252 io_resolver::read_image<IO>(filename, std::forward<Args>(args)...);
253 return true;
254 }
255 return false;
256 }
257
258 template <typename Str, typename... Args>
259 static bool try_write_view(Str &filename, Args &&... args) {
260 if (io_resolver::match_extension<IO>(filename)) {
261 io_resolver::write_view<IO>(filename, std::forward<Args>(args)...);
262 return true;
263 }
264 return false;
265 }
266 };
267
268 template <typename IO, typename... IOs>
269 struct io_impl<IO, IOs...> {
270 private:
272 using recursive = io_impl<IOs...>;
273 public:
274
275 constexpr static bool is_supported = self::is_supported || recursive::is_supported;
276
277 template <typename... Args>
278 static void read_image(Args &... args) {
279 if (!recursive::try_read_image(args...)) {
280 self::read_image(args...);
281 }
282 }
283
284 template <typename... Args>
285 static bool try_read_image(Args &... args) {
286 return recursive::try_read_image(args...) || self::try_read_image(args...);
287 }
288
289 template <typename... Args>
290 static void write_view(Args &... args) {
291 if (!recursive::try_write_view(args...)) {
292 self::write_view(args...);
293 }
294 }
295
296 template <typename... Args>
297 static bool try_write_view(Args &... args) {
298 return recursive::try_write_view(args...) || self::try_write_view(args...);
299 }
300 };
301
302 template <typename DefaultIO, typename... IOs>
303 struct io_any : detail::io_impl<DefaultIO, IOs...> {
304 static_assert(DefaultIO::is_supported, "Default IO needs to be supported.");
305 };
306
307} // namespace detail
308
309 struct png : detail::io_impl<detail::io_png> {};
310
311 struct jpeg : detail::io_impl<detail::io_jpeg> {};
312
313 struct tiff : detail::io_impl<detail::io_tiff> {};
314
315#if LIBCARLA_IMAGE_SUPPORT_PNG
316
317 struct any : detail::io_any<detail::io_png, detail::io_tiff, detail::io_jpeg> {};
318
319#elif LIBCARLA_IMAGE_SUPPORT_TIFF
320
321 struct any : detail::io_any<detail::io_tiff, detail::io_jpeg> {};
322
323#else // Then for sure this one is available.
324
325 struct any : detail::io_any<detail::io_jpeg> {};
326
327#endif
328
329} // namespace io
330} // namespace image
331} // namespace carla
#define DEBUG_ASSERT(predicate)
Definition Debug.h:66
#define LIBCARLA_IMAGE_SUPPORT_JPEG
#define LIBCARLA_IMAGE_SUPPORT_TIFF
#define LIBCARLA_IMAGE_SUPPORT_PNG
static void ValidateFilePath(std::string &filepath, const std::string &default_extension="")
Convenient function to validate a path before creating a file.
static bool EndsWith(const Range1T &input, const Range2T &test)
Definition StringUtil.h:31
constexpr bool has_jpeg_support()
constexpr bool has_tiff_support()
constexpr bool has_png_support()
This file contains definitions of common data structures used in traffic manager.
static void log_debug(Args &&... args)
Definition Logging.h:68
static bool try_write_view(Str &filename, Args &&... args)
static bool try_read_image(const Str &filename, Args &&... args)
static void write_view(Args &&... args)
static void read_image(Args &&... args)
static constexpr bool is_supported
static constexpr bool is_supported
static std::enable_if< IO::is_supported >::type read_image(const Str &path, Args &&... args)
static std::enable_if<!IO::is_supported >::type read_image(Args &&...)
static std::enable_if<!IO::is_supported >::type write_view(Args &&...)
static std::enable_if< IO::is_supported >::type write_view(std::string &path, Args &&... args)
static std::enable_if< IO::is_supported, bool >::type match_extension(const Str &str)
static std::enable_if<!IO::is_supported, bool >::type match_extension(const Str &)
static constexpr bool is_supported