CaptureMoment 0.1.0
Free and open-source non-destructive photo editor
Loading...
Searching...
No Matches
color_space_utils.cpp
Go to the documentation of this file.
1
9
10#include <OpenImageIO/imagebufalgo.h>
11#include <spdlog/spdlog.h>
12
14
15std::string getColorSpace(const OIIO::ImageBuf& buf) noexcept
16{
17 const auto cs { buf.spec().get_string_attribute("oiio:ColorSpace") };
18 return cs.empty() ? std::string{} : std::string(cs);
19}
20
21bool isLinearToken(std::string_view colorspace) noexcept
22{
23 if (colorspace.empty()) {
24 return false;
25 }
26
27 // All CIF linear tokens start with "lin_"
28 // Examples: lin_rec709_scene, lin_ap1_scene, lin_ap0_scene, lin_srgb
29 if (colorspace.starts_with("lin_")) {
30 return true;
31 }
32
33 // OCIO role for scene-linear data
34 if (colorspace.starts_with("scene_linear")) {
35 return true;
36 }
37
38 // "Linear" is OIIO's shorthand for lin_rec709_scene
39 if (colorspace == "Linear") {
40 return true;
41 }
42
43 return false;
44}
45
46ColorSpaceInfo analyzeColorSpace(const OIIO::ImageSpec& spec) noexcept
47{
48 const auto cs { spec.get_string_attribute("oiio:ColorSpace") };
49
50 if (cs.empty()) {
51 return {false, ""};
52 }
53
54 return {isLinearToken(cs), std::string(cs)};
55}
56
57std::expected<void, ErrorHandling::CoreError>
58transformToColorSpace(OIIO::ImageBuf& image, std::string_view target_cs)
59{
60 if (!image.initialized()) {
61 spdlog::warn("[transformToColorSpace]: Buffer is not initialized");
63 }
64
65 const std::string source_cs { getColorSpace(image) };
66
67 if (source_cs.empty()) {
68 spdlog::warn("[transformToColorSpace]: No color space metadata found");
69 return std::unexpected(ErrorHandling::CoreError::DecodingError);
70 }
71
72 if (!OIIO::ImageBufAlgo::colorconvert(image, image,
73 source_cs,
74 std::string(target_cs))) {
75 spdlog::error("[transformToColorSpace]: Conversion from {} to {} failed: {}",
76 source_cs, target_cs, image.geterror());
77 return std::unexpected(ErrorHandling::CoreError::DecodingError);
78 }
79
80 return {};
81}
82
83} // namespace CaptureMoment::Core::Utils
Utilities for analyzing and converting image color spaces.
@ DecodingError
Failed to decode the image data.
@ InvalidWorkingImage
The working image state is invalid or corrupted.
bool isLinearToken(std::string_view colorspace) noexcept
Checks whether an OIIO-detected color space token represents linear data.
ColorSpaceInfo analyzeColorSpace(const OIIO::ImageSpec &spec) noexcept
Analyzes whether an image's pixel data is stored in a linear color space.
std::expected< void, ErrorHandling::CoreError > transformToColorSpace(OIIO::ImageBuf &image, std::string_view target_cs)
Converts an image buffer to a target color space (in-place).
std::pair< bool, std::string > ColorSpaceInfo
Result of a color space analysis.
std::string getColorSpace(const OIIO::ImageBuf &buf) noexcept
Extracts the color space name from an OIIO image buffer metadata.
CaptureMoment — footer