Skip to content

include/cvw/algorithms/io.hpp

Image file I/O operations. More...

Namespaces

Name
cvw

Detailed Description

Image file I/O operations.

Author: Charliechen114514

Version: 1.0.0

Since: 1.0.0

Date: 2026-05-15

Provides load and save functions for reading and writing images with compile-time pixel format selection and expected-based errors.

Source code

cpp

#pragma once
#include "detail.hpp"
#include "../image_builder.hpp"
#include <opencv2/imgcodecs.hpp>

namespace cvw {

template <is_pixel_format F = BGR>
[[nodiscard]] inline expected<Image<F>, AlgorithmError>
load(std::string_view path) {
    auto result = ImageBuilder<F>{}.load(path);
    if (!result) {
        return unexpected(AlgorithmError::LoadFailed);
    }
    return std::move(*result);
}

template <is_pixel_format F> [[nodiscard]] inline expected<void, AlgorithmError>
save(const Image<F>& img, std::string_view path) {
    if (img.empty()) {
        return unexpected(AlgorithmError::EmptyInput);
    }
    if (!cv::imwrite(std::string{path}, img.mat())) {
        return unexpected(AlgorithmError::SaveFailed);
    }
    return {};
}

} // namespace cvw

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

Built with VitePress