Skip to content

include/cvw/pixel_format.hpp

Compile-time pixel format descriptors for type-safe image I/O. More...

Namespaces

Name
cvw

Classes

Name
structcvw::YUYV
Blue-Green-Red, 8-bit per channel. 3 channels.

Defines

Name
MAKE_PIXEL_FORMAT(name, ch, type)
Declares a pixel format tag struct with static channel count and value type.

Detailed Description

Compile-time pixel format descriptors for type-safe image I/O.

Author: Charliechen114514

Version: 1.0.0

Since: 1.0.0

Date: 2026-05-15

Defines a set of tag structs that encode channel count and value type for each supported pixel format. A C++20 concept constrains template parameters to valid formats, and a constexpr variable computes the byte size of a single pixel at compile time.

Macros Documentation

define MAKE_PIXEL_FORMAT

cpp
#define MAKE_PIXEL_FORMAT(
    _name_,
    _ch_,
    _type_
)
    struct _name_ {                             \
        static constexpr int channels = _ch_;   \
        using value_type = _type_;              \
    };

Declares a pixel format tag struct with static channel count and value type.

Parameters:

  • name Struct name to declare.
  • ch Number of channels (compile-time constant).
  • type Scalar type of each channel element.

Source code

cpp

#pragma once
#include <concepts>
#include <cstddef>
#include <cstdint>

namespace cvw {

#define MAKE_PIXEL_FORMAT(_name_, _ch_, _type_) \
    struct _name_ {                             \
        static constexpr int channels = _ch_;   \
        using value_type = _type_;              \
    };

MAKE_PIXEL_FORMAT(BGR, 3, uint8_t)


MAKE_PIXEL_FORMAT(RGB, 3, uint8_t)


MAKE_PIXEL_FORMAT(BGRA, 4, uint8_t)


MAKE_PIXEL_FORMAT(RGBA, 4, uint8_t)


MAKE_PIXEL_FORMAT(Gray, 1, uint8_t)


MAKE_PIXEL_FORMAT(Gray16, 1, uint16_t)


MAKE_PIXEL_FORMAT(Float1, 1, float)


MAKE_PIXEL_FORMAT(Float3, 3, float)

#undef MAKE_PIXEL_FORMAT

struct YUYV {
    static constexpr int channels = 2;

    using value_type = uint8_t;

    static constexpr int bytes_per_pixel = 2;
};

template <typename format>
concept is_pixel_format = requires {
    { format::channels } -> std::convertible_to<int>;
    typename format::value_type;
};

template <is_pixel_format pixel_format> constexpr size_t bytes_size =
    pixel_format::channels * sizeof(typename pixel_format::value_type);

} // namespace cvw

Updated on 2026-05-17 at 13:22:38 +0000

Built with VitePress