{"id":14425,"url":"https://patchwork.libcamera.org/api/patches/14425/?format=json","web_url":"https://patchwork.libcamera.org/patch/14425/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20211029132343.1629-2-david.plowman@raspberrypi.com>","date":"2021-10-29T13:23:37","name":"[libcamera-devel,v4,1/7] libcamera: Add ColorSpace class","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ec021c9e9456c6a191e235b0a725cace9ae03063","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/14425/mbox/","series":[{"id":2682,"url":"https://patchwork.libcamera.org/api/series/2682/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2682","date":"2021-10-29T13:23:36","name":"Colour spaces","version":4,"mbox":"https://patchwork.libcamera.org/series/2682/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/14425/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/14425/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 93AB5BF415\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 29 Oct 2021 13:23:52 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9B0EF600BF;\n\tFri, 29 Oct 2021 15:23:49 +0200 (CEST)","from mail-wr1-x436.google.com (mail-wr1-x436.google.com\n\t[IPv6:2a00:1450:4864:20::436])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 20FBF600B8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Oct 2021 15:23:48 +0200 (CEST)","by mail-wr1-x436.google.com with SMTP id d13so16130648wrf.11\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 29 Oct 2021 06:23:48 -0700 (PDT)","from pi4-davidp.pitowers.org\n\t([2a00:1098:3142:14:1ce1:9965:4328:89c4])\n\tby smtp.gmail.com with ESMTPSA id\n\tl7sm111934wrf.31.2021.10.29.06.23.46\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 29 Oct 2021 06:23:46 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"bFQ0C6Gl\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=tlUI2V+eGxAjDG+xyKZcONVDwUS7KK1blhVHHWSuJ+I=;\n\tb=bFQ0C6GlwnyeRxQF0oT5KPpd01Illu80o8W1N5IDykPecxZvqokq5IMB7lImYmvwu/\n\tzg3EvA48kXE2yLpnJJvEL9heuP+0/XSZYMfhfQbhIgu1vbqj0PAAXXa//XTfOyQ27RXH\n\tGCb1qRd2VxtMF9Wtgj7zK3naQQeNgY7nBPh8HRiEzb3vmKoSgOCPZA0pkmxRi2lVMyco\n\t9Ondwju0PqyqG6ZOLvWUoid0sSmG2Z+9vVwtdTOXSs+mpre8n5kc4f15Gr1NFdZHJv0F\n\tSRBykv8FvdF3RTYPzKtyXFiZ++htLLBhjvT73PgJhETzgSmTCn2sOgjK6TMp0iXpOdgq\n\tVMtg==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20210112;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=tlUI2V+eGxAjDG+xyKZcONVDwUS7KK1blhVHHWSuJ+I=;\n\tb=0ZSn6Hm1jFYpQpdrX27le8O02p7Y06qIQnLdRR45d7RarCGWlJHCO0nDT2MOFemL5b\n\tbFKy9kbcTr45tFIJwxQgqbHZrT5aQVuhXls0QDuKqrNGW+qpp7Bw0TJxXphyWvU6i6fG\n\t3GrjG1AwiRTEkTU1t+Bt0DLH4ZZonA4Cp98MhtLPYYdTFqrJT4Nx2vJzN5IYr5pSChBu\n\tJhB681UVd/wq0CAvVbO3HG12PjbbqOzqdeo8uT53m8PFUH5wS13ai9Dt67em6pIYKIi0\n\tmgEdNVTlI8a2nQcZybK/1aiEUIUvzSUAGp3lI6ISlC8yunPlUXNzIo7Qpgqq/KjFuydl\n\tZ1Ng==","X-Gm-Message-State":"AOAM533wfvMzg1XQqb73SxCcYxFx1hVbZ7YxV+VJyNHAdqKPvzS5EZFV\n\tApLORsCZaHKw8E4/cWwbE0ZX1/gitAKjtQ==","X-Google-Smtp-Source":"ABdhPJznY56VuXjVujmfsrD2bMa9wvPOw9VUhag7avAWUEPhsNkelfjpZIIxHpvClcjAPs1pUmWn0w==","X-Received":"by 2002:a05:6000:1c2:: with SMTP id\n\tt2mr12388105wrx.378.1635513827423; \n\tFri, 29 Oct 2021 06:23:47 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 29 Oct 2021 14:23:37 +0100","Message-Id":"<20211029132343.1629-2-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20211029132343.1629-1-david.plowman@raspberrypi.com>","References":"<20211029132343.1629-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 1/7] libcamera: Add ColorSpace class","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"This class represents a color space by defining its color primaries,\nYCbCr encoding, the transfer (gamma) function it uses, and whether the\noutput is full or limited range.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\nReviewed-by: Naushir Patuck <naush@raspberrypi.com>\n---\n include/libcamera/color_space.h |  88 +++++++++++\n include/libcamera/meson.build   |   1 +\n src/libcamera/color_space.cpp   | 257 ++++++++++++++++++++++++++++++++\n src/libcamera/meson.build       |   1 +\n 4 files changed, 347 insertions(+)\n create mode 100644 include/libcamera/color_space.h\n create mode 100644 src/libcamera/color_space.cpp","diff":"diff --git a/include/libcamera/color_space.h b/include/libcamera/color_space.h\nnew file mode 100644\nindex 00000000..2af9da31\n--- /dev/null\n+++ b/include/libcamera/color_space.h\n@@ -0,0 +1,88 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Raspberry Pi (Trading) Limited\n+ *\n+ * color_space.h - color space definitions\n+ */\n+\n+#ifndef __LIBCAMERA_COLOR_SPACE_H__\n+#define __LIBCAMERA_COLOR_SPACE_H__\n+\n+#include <string>\n+\n+namespace libcamera {\n+\n+class ColorSpace\n+{\n+public:\n+\tenum class Primaries : int {\n+\t\tUndefined,\n+\t\tRaw,\n+\t\tSmpte170m,\n+\t\tRec709,\n+\t\tRec2020,\n+\t};\n+\n+\tenum class YcbcrEncoding : int {\n+\t\tUndefined,\n+\t\tRec601,\n+\t\tRec709,\n+\t\tRec2020,\n+\t};\n+\n+\tenum class TransferFunction : int {\n+\t\tUndefined,\n+\t\tLinear,\n+\t\tSrgb,\n+\t\tRec709,\n+\t};\n+\n+\tenum class Range : int {\n+\t\tUndefined,\n+\t\tFull,\n+\t\tLimited,\n+\t};\n+\n+\tconstexpr ColorSpace()\n+\t\t: ColorSpace(Primaries::Undefined, YcbcrEncoding::Undefined, TransferFunction::Undefined, Range::Undefined)\n+\t{\n+\t}\n+\n+\tconstexpr ColorSpace(Primaries p, YcbcrEncoding e, TransferFunction t, Range r)\n+\t\t: primaries(p), ycbcrEncoding(e), transferFunction(t), range(r)\n+\t{\n+\t}\n+\n+\tstatic const ColorSpace Undefined;\n+\tstatic const ColorSpace Raw;\n+\tstatic const ColorSpace Jpeg;\n+\tstatic const ColorSpace Smpte170m;\n+\tstatic const ColorSpace Rec709;\n+\tstatic const ColorSpace Rec2020;\n+\n+\tPrimaries primaries;\n+\tYcbcrEncoding ycbcrEncoding;\n+\tTransferFunction transferFunction;\n+\tRange range;\n+\n+\tbool isFullyDefined() const;\n+\n+\tconst std::string toString() const;\n+};\n+\n+constexpr ColorSpace ColorSpace::Undefined = { Primaries::Undefined, YcbcrEncoding::Undefined, TransferFunction::Undefined, Range::Undefined };\n+constexpr ColorSpace ColorSpace::Raw = { Primaries::Raw, YcbcrEncoding::Rec601, TransferFunction::Linear, Range::Full };\n+constexpr ColorSpace ColorSpace::Jpeg = { Primaries::Rec709, YcbcrEncoding::Rec601, TransferFunction::Srgb, Range::Full };\n+constexpr ColorSpace ColorSpace::Smpte170m = { Primaries::Smpte170m, YcbcrEncoding::Rec601, TransferFunction::Rec709, Range::Limited };\n+constexpr ColorSpace ColorSpace::Rec709 = { Primaries::Rec709, YcbcrEncoding::Rec709, TransferFunction::Rec709, Range::Limited };\n+constexpr ColorSpace ColorSpace::Rec2020 = { Primaries::Rec2020, YcbcrEncoding::Rec2020, TransferFunction::Rec709, Range::Limited };\n+\n+bool operator==(const ColorSpace &lhs, const ColorSpace &rhs);\n+static inline bool operator!=(const ColorSpace &lhs, const ColorSpace &rhs)\n+{\n+\treturn !(lhs == rhs);\n+}\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_COLOR_SPACE_H__ */\ndiff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\nindex 7155ff20..131e1740 100644\n--- a/include/libcamera/meson.build\n+++ b/include/libcamera/meson.build\n@@ -5,6 +5,7 @@ libcamera_include_dir = 'libcamera' / 'libcamera'\n libcamera_public_headers = files([\n     'camera.h',\n     'camera_manager.h',\n+    'color_space.h',\n     'compiler.h',\n     'controls.h',\n     'file_descriptor.h',\ndiff --git a/src/libcamera/color_space.cpp b/src/libcamera/color_space.cpp\nnew file mode 100644\nindex 00000000..dfd9fa1d\n--- /dev/null\n+++ b/src/libcamera/color_space.cpp\n@@ -0,0 +1,257 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Raspberry Pi (Trading) Limited\n+ *\n+ * color_space.cpp - color spaces.\n+ */\n+\n+#include <libcamera/color_space.h>\n+\n+#include <algorithm>\n+#include <sstream>\n+#include <vector>\n+\n+/**\n+ * \\file color_space.h\n+ * \\brief Class and enums to represent color spaces\n+ */\n+\n+namespace libcamera {\n+\n+/**\n+ * \\class ColorSpace\n+ * \\brief Class to describe a color space\n+ *\n+ * The ColorSpace class defines the color primaries, the Y'CbCr encoding,\n+ * the transfer function associated with the color space, and the range\n+ * (sometimes also referred to as the quantisation) of the color space.\n+ *\n+ * Certain combinations of these fields form well-known standard color\n+ * spaces such as \"JPEG\" or \"REC709\". Applications must not request color\n+ * spaces with undefined fields, but the \"Undefined\" value may be\n+ * returned if the camera drivers decide to use a color space that is\n+ * not recognised by the ColorSpace class.\n+ *\n+ * For more information on the specific color spaces described here, please\n+ * see:\n+ *\n+ * <a href=\"https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-007.html#col-srgb\">sRGB</a> and <a href=\"https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-007.html#col-jpeg\">JPEG</a>\n+>* <a href=\"https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-007.html#col-smpte-170m\">SMPTE 170M</a>\n+ * <a href=\"https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-007.html#col-rec709\">Rec.709</a>\n+ * <a href=\"https://www.kernel.org/doc/html/v4.8/media/uapi/v4l/pixfmt-007.html#col-bt2020\">Rec.2020</a>\n+ */\n+\n+/**\n+ * \\enum ColorSpace::Primaries\n+ * \\brief The color primaries for this color space\n+ *\n+ * \\var ColorSpace::Primaries::Undefined\n+ * \\brief The color primaries are undefined\n+ * \\var ColorSpace::Primaries::Raw\n+ * \\brief These are raw colors directly from a sensor\n+ * \\var ColorSpace::Primaries::Smpte170m\n+ * \\brief SMPTE 170M color primaries\n+ * \\var ColorSpace::Primaries::Rec709\n+ * \\brief Rec.709 color primaries\n+ * \\var ColorSpace::Primaries::Rec2020\n+ * \\brief Rec.2020 color primaries\n+ */\n+\n+/**\n+ * \\enum ColorSpace::YcbcrEncoding\n+ * \\brief The Y'CbCr encoding\n+ *\n+ * \\var ColorSpace::YcbcrEncoding::Undefined\n+ * \\brief The Y'CbCr encoding is undefined\n+ * \\var ColorSpace::YcbcrEncoding::Rec601\n+ * \\brief Rec.601 Y'CbCr encoding\n+ * \\var ColorSpace::YcbcrEncoding::Rec709\n+ * \\brief Rec.709 Y'CbCr encoding\n+ * \\var ColorSpace::YcbcrEncoding::Rec2020\n+ * \\brief Rec.2020 Y'CbCr encoding\n+ */\n+\n+/**\n+ * \\enum ColorSpace::TransferFunction\n+ * \\brief The transfer function used for this color space\n+ *\n+ * \\var ColorSpace::TransferFunction::Undefined\n+ * \\brief The transfer function is not specified\n+ * \\var ColorSpace::TransferFunction::Linear\n+ * \\brief This color space uses a linear (identity) transfer function\n+ * \\var ColorSpace::TransferFunction::Srgb\n+ * \\brief sRGB transfer function\n+ * \\var ColorSpace::TransferFunction::Rec709\n+ * \\brief Rec709 transfer function\n+ */\n+\n+/**\n+ * \\enum ColorSpace::Range\n+ * \\brief The range (sometimes \"quantisation\") for this color space\n+ *\n+ * \\var ColorSpace::Range::Undefined\n+ * \\brief The range is not specified\n+ * \\var ColorSpace::Range::Full\n+ * \\brief This color space uses full range pixel values\n+ * \\var ColorSpace::Range::Limited\n+ * \\brief This color space uses limited range pixel values, being\n+ * 16 to 235 for Y' and 16 to 240 for Cb and Cr (8 bits per sample)\n+ * or 64 to 940 for Y' and 16 to 960 for Cb and Cr (10 bits)\n+ */\n+\n+/**\n+ * \\fn ColorSpace::ColorSpace(Encoding e, TransferFunction t, Range r)\n+ * \\brief Construct a ColorSpace from explicit values\n+ * \\param[in] e The Y'CbCr encoding\n+ * \\param[in] t The transfer function for the color space\n+ * \\param[in] r The range of the pixel values in this color space\n+ */\n+\n+/**\n+ * \\fn ColorSpace::ColorSpace()\n+ * \\brief Construct a color space with undefined encoding, transfer function\n+ * and range\n+ */\n+\n+/**\n+ * \\brief Check if all the fields of the color space are defined\n+ * \\return Return true if all the fields of the color space are defined,\n+ * otherwise false\n+ */\n+bool ColorSpace::isFullyDefined() const\n+{\n+\treturn primaries != Primaries::Undefined &&\n+\t       ycbcrEncoding != YcbcrEncoding::Undefined &&\n+\t       transferFunction != TransferFunction::Undefined &&\n+\t       range != Range::Undefined;\n+}\n+\n+/**\n+ * \\brief Assemble and return a readable string representation of the\n+ * ColorSpace\n+ * \\return A string describing the ColorSpace\n+ */\n+const std::string ColorSpace::toString() const\n+{\n+\t/* Print out a brief name only for standard color sapces. */\n+\n+\tstatic const std::vector<std::pair<ColorSpace, const char *>> colorSpaceNames = {\n+\t\t{ ColorSpace::Undefined, \"Undefined\" },\n+\t\t{ ColorSpace::Raw, \"Raw\" },\n+\t\t{ ColorSpace::Jpeg, \"Jpeg\" },\n+\t\t{ ColorSpace::Smpte170m, \"Smpte170m\" },\n+\t\t{ ColorSpace::Rec709, \"Rec709\" },\n+\t\t{ ColorSpace::Rec2020, \"Rec2020\" },\n+\t};\n+\tauto it = std::find_if(colorSpaceNames.begin(), colorSpaceNames.end(),\n+\t\t\t       [this](const auto &item) {\n+\t\t\t\t       return *this == item.first;\n+\t\t\t       });\n+\tif (it != colorSpaceNames.end())\n+\t\treturn std::string(it->second);\n+\n+\tstatic const char *primariesNames[] = {\n+\t\t\"Undefined\",\n+\t\t\"Raw\",\n+\t\t\"Smpte170m\",\n+\t\t\"Rec709\",\n+\t\t\"Rec2020\",\n+\t};\n+\tstatic const char *encodingNames[] = {\n+\t\t\"Undefined\",\n+\t\t\"Rec601\",\n+\t\t\"Rec709\",\n+\t\t\"Rec2020\",\n+\t};\n+\tstatic const char *transferFunctionNames[] = {\n+\t\t\"Undefined\",\n+\t\t\"Linear\",\n+\t\t\"Srgb\",\n+\t\t\"Rec709\",\n+\t};\n+\tstatic const char *rangeNames[] = {\n+\t\t\"Undefined\",\n+\t\t\"Full\",\n+\t\t\"Limited\",\n+\t};\n+\n+\tstd::stringstream ss;\n+\tss << std::string(primariesNames[static_cast<int>(primaries)]) << \"/\"\n+\t   << std::string(encodingNames[static_cast<int>(ycbcrEncoding)]) << \"/\"\n+\t   << std::string(transferFunctionNames[static_cast<int>(transferFunction)]) << \"/\"\n+\t   << std::string(rangeNames[static_cast<int>(range)]);\n+\n+\treturn ss.str();\n+}\n+\n+/**\n+ * \\var ColorSpace::primaries\n+ * \\brief The color primaries\n+ */\n+\n+/**\n+ * \\var ColorSpace::ycbcrEncoding\n+ * \\brief The Y'CbCr encoding\n+ */\n+\n+/**\n+ * \\var ColorSpace::transferFunction\n+ * \\brief The transfer function for this color space\n+ */\n+\n+/**\n+ * \\var ColorSpace::range\n+ * \\brief The pixel range used by this color space\n+ */\n+\n+/**\n+ * \\var ColorSpace::Undefined\n+ * \\brief A constant representing a fully undefined color space\n+ */\n+\n+/**\n+ * \\var ColorSpace::Raw\n+ * \\brief A constant representing a raw color space (from a sensor)\n+ */\n+\n+/**\n+ * \\var ColorSpace::Jpeg\n+ * \\brief A constant representing the JPEG color space used for\n+ * encoding JPEG images (and regarded as being the same as the sRGB\n+ * color space)\n+ */\n+\n+/**\n+ * \\var ColorSpace::Smpte170m\n+ * \\brief A constant representing the SMPTE170M color space\n+ */\n+\n+/**\n+ * \\var ColorSpace::Rec709\n+ * \\brief A constant representing the Rec.709 color space\n+ */\n+\n+/**\n+ * \\var ColorSpace::Rec2020\n+ * \\brief A constant representing the Rec.2020 color space\n+ */\n+\n+/**\n+ * \\brief Compare color spaces for equality\n+ * \\return True if the two color spaces are identical, false otherwise\n+ */\n+bool operator==(const ColorSpace &lhs, const ColorSpace &rhs)\n+{\n+\treturn lhs.primaries == rhs.primaries &&\n+\t       lhs.ycbcrEncoding == rhs.ycbcrEncoding &&\n+\t       lhs.transferFunction == rhs.transferFunction &&\n+\t       lhs.range == rhs.range;\n+}\n+\n+/**\n+ * \\fn bool operator!=(const ColorSpace &lhs, const ColorSpace &rhs)\n+ * \\brief Compare color spaces for inequality\n+ * \\return True if the two color spaces are not identical, false otherwise\n+ */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 6727a777..e7371d20 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -8,6 +8,7 @@ libcamera_sources = files([\n     'camera_manager.cpp',\n     'camera_sensor.cpp',\n     'camera_sensor_properties.cpp',\n+    'color_space.cpp',\n     'controls.cpp',\n     'control_serializer.cpp',\n     'control_validator.cpp',\n","prefixes":["libcamera-devel","v4","1/7"]}