Skip to content

include/cvw/image.hpp

Owning image container with compile-time pixel format safety. More...

Namespaces

Name
cvw

Classes

Name
classcvw::Image
Owning, type-safe image container parameterized by pixel format.

Detailed Description

Owning image container with compile-time pixel format safety.

Author: Charliechen114514

Version: 1.0.0

Since: 1.0.0

Date: 2026-05-15

Declares the Image template, ConversionalError enum, and forward declarations for ImageView and ImageMutView. Image owns its pixel data via cv::Mat and enforces move-only semantics.

Source code

cpp

#pragma once
#include "base/expected.hpp"
#include "pixel_format.hpp"
#include <cstdint>
#include <opencv2/core/mat.hpp>

namespace cvw {

template <is_pixel_format F> class ImageView;
template <is_pixel_format F> class ImageMutView;

enum class ConversionalError : uint8_t {
    InternalError, 
};

template <is_pixel_format format_type> class Image {
  public:
    using value_type = typename format_type::value_type;

    Image() = default;

    Image(int width, int height);

    Image(int width, int height, value_type fill_value);

    explicit Image(cv::Mat image);

    [[nodiscard]] cv::Mat& mat() noexcept;

    [[nodiscard]] const cv::Mat& mat() const noexcept;

    [[nodiscard("Calling for width call should get the width!")]]
    int width() const noexcept;

    [[nodiscard("Calling for height call should get the height!")]]
    int height() const noexcept;

    [[nodiscard("Calling for channels call should get the channels!")]]
    constexpr int channels() noexcept {
        return format_type::channels;
    }

    [[nodiscard]] size_t size_bytes() const noexcept;

    [[nodiscard]] size_t stride() const noexcept;

    [[nodiscard]] bool empty() const noexcept;

    [[nodiscard]] value_type* data() noexcept;

    [[nodiscard]] const value_type* data() const noexcept;

    [[nodiscard]] value_type& at(int row, int col, int ch = 0);

    [[nodiscard("View should never be thrown away!")]]
    ImageView<format_type> view() const noexcept;

    [[nodiscard("View should never be thrown away!")]]
    ImageMutView<format_type> mutable_view() const noexcept;

    [[nodiscard("Const View should never be thrown away!")]] ImageView<const format_type>
    const_view() const noexcept;

    template <is_pixel_format target_format>
    [[nodiscard("if you discard it, why call this?")]] expected<
        Image<target_format>, ConversionalError>
    convert_to() const;

    Image(Image&&) noexcept = default;

    Image& operator=(Image&&) noexcept = default;

    Image(const Image&) = delete;            
    Image& operator=(const Image&) = delete; 

    [[nodiscard]] Image clone() const;

  private:
    cv::Mat mat_;
};

} // namespace cvw

#include "details/image_impl.hpp"

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

Built with VitePress