15#ifndef LIBCARLA_IMAGE_WITH_PNG_SUPPORT
16# if defined(__has_include) && __has_include("png.h")
17# define LIBCARLA_IMAGE_WITH_PNG_SUPPORT true
19# define LIBCARLA_IMAGE_WITH_PNG_SUPPORT false
24#ifndef LIBCARLA_IMAGE_WITH_JPEG_SUPPORT
25# if defined(__has_include) && __has_include("jpeglib.h")
26# define LIBCARLA_IMAGE_WITH_JPEG_SUPPORT true
28# define LIBCARLA_IMAGE_WITH_JPEG_SUPPORT false
33#ifndef LIBCARLA_IMAGE_WITH_TIFF_SUPPORT
34# if defined(__has_include) && __has_include("tiffio.h")
35# define LIBCARLA_IMAGE_WITH_TIFF_SUPPORT true
37# define LIBCARLA_IMAGE_WITH_TIFF_SUPPORT false
42# pragma clang diagnostic push
43# pragma clang diagnostic ignored "-Wunused-parameter"
46#if LIBCARLA_IMAGE_WITH_PNG_SUPPORT == true
47# ifndef png_infopp_NULL
48# define png_infopp_NULL (png_infopp)NULL
51# define int_p_NULL (int*)NULL
53# if defined(__clang__)
54# pragma clang diagnostic push
55# pragma clang diagnostic ignored "-Wignored-qualifiers"
56# pragma clang diagnostic ignored "-Wparentheses"
58# include <boost/gil/extension/io/png.hpp>
59# if defined(__clang__)
60# pragma clang diagnostic pop
64#if LIBCARLA_IMAGE_WITH_JPEG_SUPPORT == true
65# include <boost/gil/extension/io/jpeg.hpp>
68#if LIBCARLA_IMAGE_WITH_TIFF_SUPPORT == true
69# include <boost/gil/extension/io/tiff.hpp>
73# pragma clang diagnostic pop
80 constexpr bool has_png_support() {
84 constexpr bool has_jpeg_support() {
88 constexpr bool has_tiff_support() {
92 static_assert(has_png_support() || has_jpeg_support() || has_tiff_support(),
93 "No image format supported, please compile with at least one of "
94 "LIBCARLA_IMAGE_WITH_PNG_SUPPORT, LIBCARLA_IMAGE_WITH_JPEG_SUPPORT, "
95 "or LIBCARLA_IMAGE_WITH_TIFF_SUPPORT");
99 template <
typename ViewT,
typename IOTag>
100 struct is_write_supported {
101 static constexpr bool value = boost::gil::is_write_supported<typename boost::gil::get_pixel_type<ViewT>::type, IOTag>::value;
105#if LIBCARLA_IMAGE_WITH_PNG_SUPPORT
107 static constexpr const char *get_default_extension() {
111 template <
typename Str>
112 static bool match_extension(
const Str &str) {
113 return StringUtil::EndsWith(str, get_default_extension());
116 template <
typename Str,
typename ImageT>
117 static void read_image(Str &&in_filename, ImageT &image) {
118 boost::gil::read_and_convert_image(std::forward<Str>(in_filename), image, boost::gil::png_tag());
121 template <
typename Str,
typename ViewT>
122 static void write_view(Str &&out_filename,
const ViewT &view) {
123 boost::gil::write_view(std::forward<Str>(out_filename), view, boost::gil::png_tag());
132 static constexpr bool is_supported = has_jpeg_support();
134#if LIBCARLA_IMAGE_WITH_JPEG_SUPPORT
136 static constexpr const char *get_default_extension() {
140 template <
typename Str>
141 static bool match_extension(
const Str &str) {
142 return StringUtil::EndsWith(str, get_default_extension()) ||
143 StringUtil::EndsWith(str,
"jpg");
146 template <
typename Str,
typename ImageT>
147 static void read_image(Str &&in_filename, ImageT &image) {
148 boost::gil::read_image(std::forward<Str>(in_filename), image, boost::gil::jpeg_tag());
151 template <
typename Str,
typename ViewT>
152 static typename std::enable_if<is_write_supported<ViewT, boost::gil::jpeg_tag>::value>::type
153 write_view(Str &&out_filename,
const ViewT &view) {
154 boost::gil::write_view(std::forward<Str>(out_filename), view, boost::gil::jpeg_tag());
157 template <
typename Str,
typename ViewT>
158 static typename std::enable_if<!is_write_supported<ViewT, boost::gil::jpeg_tag>::value>::type
159 write_view(Str &&out_filename,
const ViewT &view) {
160 boost::gil::write_view(
161 std::forward<Str>(out_filename),
162 boost::gil::color_converted_view<boost::gil::rgb8_pixel_t>(view),
163 boost::gil::jpeg_tag());
169 static constexpr bool is_supported = has_tiff_support();
171#if LIBCARLA_IMAGE_WITH_TIFF_SUPPORT
173 static constexpr const char *get_default_extension() {
177 template <
typename Str>
178 static bool match_extension(
const Str &str) {
179 return StringUtil::EndsWith(str, get_default_extension());
182 template <
typename Str,
typename ImageT>
183 static void read_image(Str &&in_filename, ImageT &image) {
184 boost::gil::read_and_convert_image(std::forward<Str>(in_filename), image, boost::gil::tiff_tag());
187 template <
typename Str,
typename ViewT>
188 static typename std::enable_if<is_write_supported<ViewT, boost::gil::tiff_tag>::value>::type
189 write_view(Str &&out_filename,
const ViewT &view) {
190 boost::gil::write_view(std::forward<Str>(out_filename), view, boost::gil::tiff_tag());
193 template <
typename Str,
typename ViewT>
194 static typename std::enable_if<!is_write_supported<ViewT, boost::gil::tiff_tag>::value>::type
195 write_view(Str &&out_filename,
const ViewT &view) {
196 boost::gil::write_view(
197 std::forward<Str>(out_filename),
198 boost::gil::color_converted_view<boost::gil::rgb8_pixel_t>(view),
199 boost::gil::tiff_tag());
207 template <
typename IO,
typename Str>
208 static typename std::enable_if<IO::is_supported, bool>::type match_extension(
const Str &str) {
209 return IO::match_extension(str);
212 template <
typename IO,
typename Str>
213 static typename std::enable_if<!IO::is_supported, bool>::type match_extension(
const Str &) {
217 template <
typename IO,
typename Str,
typename... Args>
218 static typename std::enable_if<IO::is_supported>::type read_image(
const Str &path, Args &&... args) {
219 log_debug(
"reading", path,
"as", IO::get_default_extension());
220 IO::read_image(path, std::forward<Args>(args)...);
223 template <
typename IO,
typename... Args>
224 static typename std::enable_if<!IO::is_supported>::type read_image(Args &&...) {
228 template <
typename IO,
typename... Args>
229 static typename std::enable_if<IO::is_supported>::type write_view(std::string &path, Args &&... args) {
230 FileSystem::ValidateFilePath(path, IO::get_default_extension());
231 log_debug(
"writing", path,
"as", IO::get_default_extension());
232 IO::write_view(path, std::forward<Args>(args)...);
235 template <
typename IO,
typename... Args>
236 static typename std::enable_if<!IO::is_supported>::type write_view(Args &&...) {
241template <
typename... IOs>
244template <
typename IO>
246 constexpr static bool is_supported = IO::is_supported;
248 template <
typename... Args>
249 static void read_image(Args &&... args) {
250 io_resolver::read_image<IO>(std::forward<Args>(args)...);
253 template <
typename... Args>
254 static void write_view(Args &&... args) {
255 io_resolver::write_view<IO>(std::forward<Args>(args)...);
258 template <
typename Str,
typename... Args>
259 static bool try_read_image(
const Str &filename, Args &&... args) {
260 if (io_resolver::match_extension<IO>(filename)) {
261 io_resolver::read_image<IO>(filename, std::forward<Args>(args)...);
267 template <
typename Str,
typename... Args>
268 static bool try_write_view(Str &filename, Args &&... args) {
269 if (io_resolver::match_extension<IO>(filename)) {
270 io_resolver::write_view<IO>(filename, std::forward<Args>(args)...);
276 template <
typename IO,
typename... IOs>
277struct io_impl<IO, IOs...> {
279 using self = io_impl<IO>;
280 using recursive = io_impl<IOs...>;
283 constexpr static bool is_supported = self::is_supported || recursive::is_supported;
285 template <
typename... Args>
286 static void read_image(Args &... args) {
287 if (!recursive::try_read_image(args...)) {
288 self::read_image(args...);
292 template <
typename... Args>
293 static bool try_read_image(Args &... args) {
294 return recursive::try_read_image(args...) || self::try_read_image(args...);
297 template <
typename... Args>
298 static void write_view(Args &... args) {
299 if (!recursive::try_write_view(args...)) {
300 self::write_view(args...);
304 template <
typename... Args>
305 static bool try_write_view(Args &... args) {
306 return recursive::try_write_view(args...) || self::try_write_view(args...);
310template <
typename DefaultIO,
typename... IOs>
311struct io_any : detail::io_impl<DefaultIO, IOs...> {
312 static_assert(DefaultIO::is_supported,
"Default IO needs to be supported.");
317struct png : detail::io_impl<detail::io_png> {};
319struct jpeg : detail::io_impl<detail::io_jpeg> {};
321struct tiff : detail::io_impl<detail::io_tiff> {};
323#if LIBCARLA_IMAGE_WITH_PNG_SUPPORT
325struct any : detail::io_any<detail::io_png, detail::io_tiff, detail::io_jpeg> {};
327#elif LIBCARLA_IMAGE_WITH_TIFF_SUPPORT
329struct any : detail::io_any<detail::io_tiff, detail::io_jpeg> {};
333struct any : detail::io_any<detail::io_jpeg> {};
#define DEBUG_ASSERT(predicate)
#define LIBCARLA_IMAGE_WITH_TIFF_SUPPORT
#define LIBCARLA_IMAGE_WITH_JPEG_SUPPORT
#define LIBCARLA_IMAGE_WITH_PNG_SUPPORT