Skip to content

include/cvw/image_view.hpp

Non-owning, zero-copy views over image pixel data. More...

Namespaces

Name
cvw

Classes

Name
classcvw::ImageView
Read-only, non-owning view over image pixel data.
classcvw::ImageMutView
Mutable, non-owning view over image pixel data.

Detailed Description

Non-owning, zero-copy views over image pixel data.

Author: Charliechen114514

Version: 1.0.0

Since: 1.0.0

Date: 2026-05-15

Provides ImageView (read-only) and ImageMutView (read-write) templates that reference an external pixel buffer without managing its lifetime.

Source code

cpp

#pragma once
#include "cvw/pixel_format.hpp"
#include <cstddef>
#include <cstdint>

namespace cvw {

template <is_pixel_format F> class ImageView {
  public:
    using value_type = typename F::value_type;

    ImageView() = default;

    ImageView(const value_type* data, int width, int height, size_t stride)
        : data_(data), width_(width), height_(height), stride_(stride) {}

    [[nodiscard]] int width() const noexcept { return width_; }

    [[nodiscard]] int height() const noexcept { return height_; }

    [[nodiscard]] constexpr int channels() const noexcept {
        return F::channels;
    }

    [[nodiscard]] size_t stride() const noexcept { return stride_; }

    [[nodiscard]] bool empty() const noexcept { return data_ == nullptr; }

    [[nodiscard]] const value_type* data() const noexcept { return data_; }

    [[nodiscard]] const value_type& at(int row, int col,
                                       int ch = 0) const noexcept {
        return data_[row * stride_ + col * F::channels + ch];
    }

  private:
    const value_type* data_ = nullptr;

    int width_ = 0;

    int height_ = 0;

    size_t stride_ = 0;
};

template <is_pixel_format F> class ImageMutView {
  public:
    using value_type = typename F::value_type;

    ImageMutView() = default;

    ImageMutView(value_type* data, int width, int height, size_t stride)
        : data_(data), width_(width), height_(height), stride_(stride) {}

    [[nodiscard]] int width() const noexcept { return width_; }

    [[nodiscard]] int height() const noexcept { return height_; }

    [[nodiscard]] constexpr int channels() const noexcept {
        return F::channels;
    }

    [[nodiscard]] size_t stride() const noexcept { return stride_; }

    [[nodiscard]] bool empty() const noexcept { return data_ == nullptr; }

    [[nodiscard]] value_type* data() noexcept { return data_; }

    [[nodiscard]] const value_type* data() const noexcept { return data_; }

    [[nodiscard]] value_type& at(int row, int col, int ch = 0) noexcept {
        return data_[row * stride_ + col * F::channels + ch];
    }

    [[nodiscard]] ImageView<F> as_const_view() const noexcept {
        return ImageView<F>(data_, width_, height_, stride_);
    }

  private:
    value_type* data_ = nullptr;

    int width_ = 0;

    int height_ = 0;

    size_t stride_ = 0;
};

} // namespace cvw

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

Built with VitePress