Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:CARLA simulator Carla ImageIOConfig

From Leeroopedia
Knowledge Sources
Domains Image Processing, Sensor Data
Last Updated 2026-02-15 05:00 GMT

Overview

ImageIOConfig provides a compile-time configurable image I/O framework built on Boost.GIL that supports PNG, JPEG, and TIFF formats with automatic format detection and dispatching.

Description

The ImageIOConfig.h header defines the image reading and writing infrastructure within the carla::image::io namespace. It is organized around a set of compile-time switchable format backends controlled by preprocessor macros:

  • LIBCARLA_IMAGE_SUPPORT_PNG - Enables PNG support via boost::gil::png_tag()
  • LIBCARLA_IMAGE_SUPPORT_JPEG - Enables JPEG support via boost::gil::jpeg_tag()
  • LIBCARLA_IMAGE_SUPPORT_TIFF - Enables TIFF support via boost::gil::tiff_tag()

A static_assert enforces that at least one format must be enabled at compile time.

The architecture uses a layered template design:

  • detail::io_png, detail::io_jpeg, detail::io_tiff - Per-format structs that wrap the Boost.GIL read/write calls and provide extension matching. Each handles pixel type conversion automatically when the view type is not directly supported by the codec (e.g., converting to rgb8_pixel_t for JPEG output).
  • detail::io_resolver - A static dispatcher that conditionally calls read/write operations based on whether a given format is supported (via std::enable_if SFINAE).
  • detail::io_impl<IOs...> - A variadic template that chains multiple format handlers together, trying each format in order until one matches the file extension.
  • detail::io_any<DefaultIO, IOs...> - A wrapper that requires the default format to be supported and provides a unified read/write interface across all available formats.

The public API exposes four convenience types:

  • io::png - PNG-only I/O
  • io::jpeg - JPEG-only I/O
  • io::tiff - TIFF-only I/O
  • io::any - Automatic format selection based on file extension, with PNG as the preferred default

Usage

Use ImageIOConfig when you need to read or write sensor image data (e.g., from camera sensors) to disk in CARLA. The io::any type is the most common entry point, as it automatically selects the correct format based on the file extension. Use format-specific types (io::png, io::jpeg, io::tiff) when you need to force a particular output format.

Code Reference

Source Location

  • Repository: CARLA
  • File: LibCarla/source/carla/image/ImageIOConfig.h
  • Lines: 1-331

Signature

namespace carla {
namespace image {
namespace io {

  constexpr bool has_png_support();
  constexpr bool has_jpeg_support();
  constexpr bool has_tiff_support();

namespace detail {

  struct io_png {
    static constexpr bool is_supported = has_png_support();
    static constexpr const char *get_default_extension();
    template <typename Str> static bool match_extension(const Str &str);
    template <typename Str, typename ImageT> static void read_image(Str &&in_filename, ImageT &image);
    template <typename Str, typename ViewT> static void write_view(Str &&out_filename, const ViewT &view);
  };

  struct io_jpeg { /* analogous to io_png for JPEG */ };
  struct io_tiff { /* analogous to io_png for TIFF */ };

  struct io_resolver {
    template <typename IO, typename Str> static bool match_extension(const Str &str);
    template <typename IO, typename Str, typename... Args> static void read_image(const Str &path, Args &&... args);
    template <typename IO, typename... Args> static void write_view(std::string &path, Args &&... args);
  };

  template <typename... IOs> struct io_impl;
  template <typename DefaultIO, typename... IOs> struct io_any;

} // namespace detail

  struct png  : detail::io_impl<detail::io_png> {};
  struct jpeg : detail::io_impl<detail::io_jpeg> {};
  struct tiff : detail::io_impl<detail::io_tiff> {};
  struct any  : detail::io_any<detail::io_png, detail::io_tiff, detail::io_jpeg> {};

} // namespace io
} // namespace image
} // namespace carla

Import

#include "carla/image/ImageIOConfig.h"

I/O Contract

Inputs

Name Type Required Description
in_filename Str (string-like) Yes File path to read an image from; extension determines format when using io::any
image ImageT & Yes Boost.GIL image object to populate with loaded pixel data
out_filename std::string & Yes File path to write an image to; extension is validated and appended if missing
view const ViewT & Yes Boost.GIL image view containing pixel data to write

Outputs

Name Type Description
image (populated) ImageT & The Boost.GIL image filled with pixel data from the file
File on disk Image file The written image file in the detected or specified format

Usage Examples

Writing a Sensor Image to Disk

#include "carla/image/ImageIOConfig.h"

// Assuming 'view' is a boost::gil view of sensor output
std::string path = "/tmp/camera_output.png";

// Write using automatic format detection
carla::image::io::any::write_view(path, view);

Reading an Image File

#include "carla/image/ImageIOConfig.h"
#include "carla/image/BoostGil.h"

boost::gil::rgba8_image_t image;
std::string path = "/tmp/camera_output.png";

// Read using automatic format detection
carla::image::io::any::read_image(path, image);

Forcing a Specific Format

#include "carla/image/ImageIOConfig.h"

std::string path = "/tmp/output";

// Force TIFF output regardless of extension
carla::image::io::tiff::write_view(path, view);
// path will be validated/corrected to have .tiff extension

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment