From fbfdaa2d205b8898527609fb18d641429f0e0710 Mon Sep 17 00:00:00 2001 From: Laurent Pinchart Date: Tue, 23 Aug 2022 17:16:34 +0300 Subject: libcamera: color_space: Add fromString() function Add a ColorSpace:fromString() function to parse a string into a color space. The string can either contain the name of a well-known color space, or four color space components separate by a '/' character. Signed-off-by: Laurent Pinchart Reviewed-by: Umang Jain Reviewed-by: Paul Elder --- include/libcamera/color_space.h | 2 + src/libcamera/color_space.cpp | 153 ++++++++++++++++++++++++++++++++-------- 2 files changed, 125 insertions(+), 30 deletions(-) diff --git a/include/libcamera/color_space.h b/include/libcamera/color_space.h index 8030a264..f493f72d 100644 --- a/include/libcamera/color_space.h +++ b/include/libcamera/color_space.h @@ -59,6 +59,8 @@ public: std::string toString() const; static std::string toString(const std::optional &colorSpace); + + static std::optional fromString(const std::string &str); }; bool operator==(const ColorSpace &lhs, const ColorSpace &rhs); diff --git a/src/libcamera/color_space.cpp b/src/libcamera/color_space.cpp index 1b2dd240..944d0e98 100644 --- a/src/libcamera/color_space.cpp +++ b/src/libcamera/color_space.cpp @@ -12,6 +12,9 @@ #include #include #include +#include + +#include /** * \file color_space.h @@ -208,6 +211,44 @@ const ColorSpace ColorSpace::Rec2020 = { * \brief The pixel range used with by color space */ +namespace { + +const std::array, 6> colorSpaceNames = { { + { ColorSpace::Raw, "RAW" }, + { ColorSpace::Srgb, "sRGB" }, + { ColorSpace::Sycc, "sYCC" }, + { ColorSpace::Smpte170m, "SMPTE170M" }, + { ColorSpace::Rec709, "Rec709" }, + { ColorSpace::Rec2020, "Rec2020" }, +} }; + +const std::map primariesNames = { + { ColorSpace::Primaries::Raw, "RAW" }, + { ColorSpace::Primaries::Smpte170m, "SMPTE170M" }, + { ColorSpace::Primaries::Rec709, "Rec709" }, + { ColorSpace::Primaries::Rec2020, "Rec2020" }, +}; + +const std::map transferNames = { + { ColorSpace::TransferFunction::Linear, "Linear" }, + { ColorSpace::TransferFunction::Srgb, "sRGB" }, + { ColorSpace::TransferFunction::Rec709, "Rec709" }, +}; + +const std::map encodingNames = { + { ColorSpace::YcbcrEncoding::None, "None" }, + { ColorSpace::YcbcrEncoding::Rec601, "Rec601" }, + { ColorSpace::YcbcrEncoding::Rec709, "Rec709" }, + { ColorSpace::YcbcrEncoding::Rec2020, "Rec2020" }, +}; + +const std::map rangeNames = { + { ColorSpace::Range::Full, "Full" }, + { ColorSpace::Range::Limited, "Limited" }, +}; + +} /* namespace */ + /** * \brief Assemble and return a readable string representation of the * ColorSpace @@ -223,14 +264,6 @@ std::string ColorSpace::toString() const { /* Print out a brief name only for standard color spaces. */ - static const std::array, 6> colorSpaceNames = { { - { ColorSpace::Raw, "RAW" }, - { ColorSpace::Srgb, "sRGB" }, - { ColorSpace::Sycc, "sYCC" }, - { ColorSpace::Smpte170m, "SMPTE170M" }, - { ColorSpace::Rec709, "Rec709" }, - { ColorSpace::Rec2020, "Rec2020" }, - } }; auto it = std::find_if(colorSpaceNames.begin(), colorSpaceNames.end(), [this](const auto &item) { return *this == item.first; @@ -240,28 +273,6 @@ std::string ColorSpace::toString() const /* Assemble a name made of the constituent fields. */ - static const std::map primariesNames = { - { Primaries::Raw, "RAW" }, - { Primaries::Smpte170m, "SMPTE170M" }, - { Primaries::Rec709, "Rec709" }, - { Primaries::Rec2020, "Rec2020" }, - }; - static const std::map transferNames = { - { TransferFunction::Linear, "Linear" }, - { TransferFunction::Srgb, "sRGB" }, - { TransferFunction::Rec709, "Rec709" }, - }; - static const std::map encodingNames = { - { YcbcrEncoding::None, "None" }, - { YcbcrEncoding::Rec601, "Rec601" }, - { YcbcrEncoding::Rec709, "Rec709" }, - { YcbcrEncoding::Rec2020, "Rec2020" }, - }; - static const std::map rangeNames = { - { Range::Full, "Full" }, - { Range::Limited, "Limited" }, - }; - auto itPrimaries = primariesNames.find(primaries); std::string primariesName = itPrimaries == primariesNames.end() ? "Invalid" : itPrimaries->second; @@ -303,6 +314,88 @@ std::string ColorSpace::toString(const std::optional &colorSpace) return colorSpace->toString(); } +/** + * \brief Construct a color space from a string + * \param[in] str The string + * + * The string \a str can contain the name of a well-known color space, or be + * made of the four color space components separated by a '/' character, ordered + * as + * + * \verbatim primaries '/' transferFunction '/' ycbcrEncoding '/' range \endverbatim + * + * Any failure to parse the string, either because it doesn't match the expected + * format, or because the one of the names isn't recognized, will cause this + * function to return std::nullopt. + * + * \return The ColorSpace corresponding to the string, or std::nullopt if the + * string doesn't describe a known color space + */ +std::optional ColorSpace::fromString(const std::string &str) +{ + /* First search for a standard color space name match. */ + auto itColorSpace = std::find_if(colorSpaceNames.begin(), colorSpaceNames.end(), + [&str](const auto &item) { + return str == item.second; + }); + if (itColorSpace != colorSpaceNames.end()) + return itColorSpace->first; + + /* + * If not found, the string must contain the four color space + * components separated by a '/' character. + */ + const auto &split = utils::split(str, "/"); + std::vector components{ split.begin(), split.end() }; + + if (components.size() != 4) + return std::nullopt; + + ColorSpace colorSpace = ColorSpace::Raw; + + /* Color primaries */ + auto itPrimaries = std::find_if(primariesNames.begin(), primariesNames.end(), + [&components](const auto &item) { + return components[0] == item.second; + }); + if (itPrimaries == primariesNames.end()) + return std::nullopt; + + colorSpace.primaries = itPrimaries->first; + + /* Transfer function */ + auto itTransfer = std::find_if(transferNames.begin(), transferNames.end(), + [&components](const auto &item) { + return components[1] == item.second; + }); + if (itTransfer == transferNames.end()) + return std::nullopt; + + colorSpace.transferFunction = itTransfer->first; + + /* YCbCr encoding */ + auto itEncoding = std::find_if(encodingNames.begin(), encodingNames.end(), + [&components](const auto &item) { + return components[2] == item.second; + }); + if (itEncoding == encodingNames.end()) + return std::nullopt; + + colorSpace.ycbcrEncoding = itEncoding->first; + + /* Quantization range */ + auto itRange = std::find_if(rangeNames.begin(), rangeNames.end(), + [&components](const auto &item) { + return components[3] == item.second; + }); + if (itRange == rangeNames.end()) + return std::nullopt; + + colorSpace.range = itRange->first; + + return colorSpace; +} + /** * \brief Compare color spaces for equality * \return True if the two color spaces are identical, false otherwise -- cgit v1.2.1