[{"id":32179,"web_url":"https://patchwork.libcamera.org/comment/32179/","msgid":"<173167121676.4187655.13394700896813267524@ping.linuxembedded.co.uk>","date":"2024-11-15T11:46:56","subject":"Re: [PATCH v3 1/6] ipa: libipa: Add colour helpers","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Quoting Daniel Scally (2024-11-15 07:46:23)\n> We start to have some functions relating to colour that are\n> effectively identical crop up across the IPA modules. Add a file\n> allowing those to be centralised within libipa so that a single\n> implementation can be used in all of the IPAs.\n> \n> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n> Changes in v3:\n> \n>         - Named the file \"colours.h\" and \"colours.cpp\" instead of \"helpers\"\n>         - Made the rec601LuminanceFromRGB() arguments doubles\n> \n> Changes in v2:\n> \n>         - Dropped the Q(m, n) format helpers until they're needed.\n>         - \\return statements after long description\n>         - Switched rec601LuminanceFromRGB() to return a double\n> \n>  src/ipa/libipa/colours.cpp | 77 ++++++++++++++++++++++++++++++++++++++\n>  src/ipa/libipa/colours.h   | 21 +++++++++++\n>  src/ipa/libipa/meson.build |  2 +\n>  3 files changed, 100 insertions(+)\n>  create mode 100644 src/ipa/libipa/colours.cpp\n>  create mode 100644 src/ipa/libipa/colours.h\n> \n> diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp\n> new file mode 100644\n> index 00000000..9fcb53b0\n> --- /dev/null\n> +++ b/src/ipa/libipa/colours.cpp\n> @@ -0,0 +1,77 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * libipa miscellaneous colour helpers\n> + */\n> +\n> +#include \"colours.h\"\n> +\n> +#include <algorithm>\n> +#include <cmath>\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\file colours.h\n> + * \\brief Functions to reduce code duplication between IPA modules\n> + */\n> +\n> +/**\n> + * \\brief Estimate luminance from RGB values following ITU-R BT.601\n> + * \\param[in] r The red value\n> + * \\param[in] g The green value\n> + * \\param[in] b The blue value\n> + *\n> + * This function estimates a luminance value from a triplet of Red, Green and\n> + * Blue values, following the formula defined by ITU-R Recommendation BT.601-7\n> + * which can be found at https://www.itu.int/rec/R-REC-BT.601\n> + *\n> + * \\return The estimated luminance value\n> + */\n> +double rec601LuminanceFromRGB(double r, double g, double b)\n> +{\n> +       return (r * .299) + (g * .587) + (b * .114);\n> +}\n> +\n> +/**\n> + * \\brief Estimate correlated colour temperature from RGB color space input\n> + * \\param[in] red The input red value\n> + * \\param[in] green The input green value\n> + * \\param[in] blue The input blue value\n> + *\n> + * This function estimates the correlated color temperature RGB color space\n> + * input. In physics and color science, the Planckian locus or black body locus\n> + * is the path or locus that the color of an incandescent black body would take\n> + * in a particular chromaticity space as the blackbody temperature changes.\n> + *\n> + * If a narrow range of color temperatures is considered (those encapsulating\n> + * daylight being the most practical case) one can approximate the Planckian\n> + * locus in order to calculate the CCT in terms of chromaticity coordinates.\n> + *\n> + * More detailed information can be found in:\n> + * https://en.wikipedia.org/wiki/Color_temperature#Approximation\n> + *\n> + * \\return The estimated color temperature\n> + */\n> +uint32_t estimateCCT(double red, double green, double blue)\n> +{\n> +       /* Convert the RGB values to CIE tristimulus values (XYZ) */\n> +       double X = (-0.14282) * (red) + (1.54924) * (green) + (-0.95641) * (blue);\n> +       double Y = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);\n> +       double Z = (-0.68202) * (red) + (0.77073) * (green) + (0.56332) * (blue);\n> +\n> +       /* Calculate the normalized chromaticity values */\n> +       double x = X / (X + Y + Z);\n> +       double y = Y / (X + Y + Z);\n> +\n> +       /* Calculate CCT */\n> +       double n = (x - 0.3320) / (0.1858 - y);\n> +       return 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;\n> +}\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/colours.h b/src/ipa/libipa/colours.h\n> new file mode 100644\n> index 00000000..b42ed0ac\n> --- /dev/null\n> +++ b/src/ipa/libipa/colours.h\n> @@ -0,0 +1,21 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * libipa miscellaneous colour helpers\n> + */\n> +\n> +#pragma once\n> +\n> +#include <stdint.h>\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +double rec601LuminanceFromRGB(double r, double g, double b);\n> +uint32_t estimateCCT(double red, double green, double blue);\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index e78cbcd6..788d037a 100644\n> --- a/src/ipa/libipa/meson.build\n> +++ b/src/ipa/libipa/meson.build\n> @@ -4,6 +4,7 @@ libipa_headers = files([\n>      'agc_mean_luminance.h',\n>      'algorithm.h',\n>      'camera_sensor_helper.h',\n> +    'colours.h',\n>      'exposure_mode_helper.h',\n>      'fc_queue.h',\n>      'histogram.h',\n> @@ -19,6 +20,7 @@ libipa_sources = files([\n>      'agc_mean_luminance.cpp',\n>      'algorithm.cpp',\n>      'camera_sensor_helper.cpp',\n> +    'colours.cpp',\n>      'exposure_mode_helper.cpp',\n>      'fc_queue.cpp',\n>      'histogram.cpp',\n> -- \n> 2.30.2\n>","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 21A4CC3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Nov 2024 11:47:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 5001C6586E;\n\tFri, 15 Nov 2024 12:47:02 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2EEA86580A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Nov 2024 12:47:00 +0100 (CET)","from pendragon.ideasonboard.com\n\t(cpc89244-aztw30-2-0-cust6594.18-1.cable.virginm.net [86.31.185.195])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 56F8E496;\n\tFri, 15 Nov 2024 12:46:45 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"pr7AuKCD\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1731671205;\n\tbh=Zok/Pb7TYMZZuEBmAB75cQOjuZT03aJlVLX6q94z1+s=;\n\th=In-Reply-To:References:Subject:From:Cc:To:Date:From;\n\tb=pr7AuKCDkwkSd0DRVS9Dh1lT1PE7wpxZkDk+nsY3jb8DfmdytydIlTHkf3Rimr7AR\n\tfgJ/bMnign/Ud7L6MV9JhRrQuOsC4F/18+f5RuHkFX+BT/5AkrvzYUAqs1Rs8gG190\n\tdt5SD8d7Hqf7gBso+GAY0VrqYsTBmcyIySMnQx4I=","Content-Type":"text/plain; charset=\"utf-8\"","MIME-Version":"1.0","Content-Transfer-Encoding":"quoted-printable","In-Reply-To":"<20241115074628.417215-2-dan.scally@ideasonboard.com>","References":"<20241115074628.417215-1-dan.scally@ideasonboard.com>\n\t<20241115074628.417215-2-dan.scally@ideasonboard.com>","Subject":"Re: [PATCH v3 1/6] ipa: libipa: Add colour helpers","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Cc":"mike.rudenko@gmail.com, Daniel Scally <dan.scally@ideasonboard.com>","To":"Daniel Scally <dan.scally@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","Date":"Fri, 15 Nov 2024 11:46:56 +0000","Message-ID":"<173167121676.4187655.13394700896813267524@ping.linuxembedded.co.uk>","User-Agent":"alot/0.10","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>"}},{"id":32195,"web_url":"https://patchwork.libcamera.org/comment/32195/","msgid":"<87ldxklhxe.fsf@redhat.com>","date":"2024-11-15T13:56:13","subject":"Re: [PATCH v3 1/6] ipa: libipa: Add colour helpers","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"content":"Hi Dan,\n\nthank you for this work.\n\nDaniel Scally <dan.scally@ideasonboard.com> writes:\n\n> We start to have some functions relating to colour that are\n> effectively identical crop up across the IPA modules. Add a file\n> allowing those to be centralised within libipa so that a single\n> implementation can be used in all of the IPAs.\n>\n> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n> ---\n> Changes in v3:\n>\n> \t- Named the file \"colours.h\" and \"colours.cpp\" instead of \"helpers\"\n> \t- Made the rec601LuminanceFromRGB() arguments doubles\n>\n> Changes in v2:\n>\n> \t- Dropped the Q(m, n) format helpers until they're needed.\n> \t- \\return statements after long description\n> \t- Switched rec601LuminanceFromRGB() to return a double\n>\n>  src/ipa/libipa/colours.cpp | 77 ++++++++++++++++++++++++++++++++++++++\n>  src/ipa/libipa/colours.h   | 21 +++++++++++\n>  src/ipa/libipa/meson.build |  2 +\n>  3 files changed, 100 insertions(+)\n>  create mode 100644 src/ipa/libipa/colours.cpp\n>  create mode 100644 src/ipa/libipa/colours.h\n>\n> diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp\n> new file mode 100644\n> index 00000000..9fcb53b0\n> --- /dev/null\n> +++ b/src/ipa/libipa/colours.cpp\n> @@ -0,0 +1,77 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * libipa miscellaneous colour helpers\n> + */\n> +\n> +#include \"colours.h\"\n> +\n> +#include <algorithm>\n> +#include <cmath>\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\file colours.h\n> + * \\brief Functions to reduce code duplication between IPA modules\n> + */\n> +\n> +/**\n> + * \\brief Estimate luminance from RGB values following ITU-R BT.601\n> + * \\param[in] r The red value\n> + * \\param[in] g The green value\n> + * \\param[in] b The blue value\n> + *\n> + * This function estimates a luminance value from a triplet of Red, Green and\n> + * Blue values, following the formula defined by ITU-R Recommendation BT.601-7\n> + * which can be found at https://www.itu.int/rec/R-REC-BT.601\n> + *\n> + * \\return The estimated luminance value\n> + */\n> +double rec601LuminanceFromRGB(double r, double g, double b)\n> +{\n> +\treturn (r * .299) + (g * .587) + (b * .114);\n> +}\n> +\n> +/**\n> + * \\brief Estimate correlated colour temperature from RGB color space input\n> + * \\param[in] red The input red value\n> + * \\param[in] green The input green value\n> + * \\param[in] blue The input blue value\n\nIt might be worth to mention what the \"input value\" is.  Namely that it\ndoesn't matter and it can be any \"unit\", as long as it is the same\n(linear) unit for all the colors.  That means it can be\ne.g. mean/average or sum from stats, without need to convert it.\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\n\n> + *\n> + * This function estimates the correlated color temperature RGB color space\n> + * input. In physics and color science, the Planckian locus or black body locus\n> + * is the path or locus that the color of an incandescent black body would take\n> + * in a particular chromaticity space as the blackbody temperature changes.\n> + *\n> + * If a narrow range of color temperatures is considered (those encapsulating\n> + * daylight being the most practical case) one can approximate the Planckian\n> + * locus in order to calculate the CCT in terms of chromaticity coordinates.\n> + *\n> + * More detailed information can be found in:\n> + * https://en.wikipedia.org/wiki/Color_temperature#Approximation\n> + *\n> + * \\return The estimated color temperature\n> + */\n> +uint32_t estimateCCT(double red, double green, double blue)\n> +{\n> +\t/* Convert the RGB values to CIE tristimulus values (XYZ) */\n> +\tdouble X = (-0.14282) * (red) + (1.54924) * (green) + (-0.95641) * (blue);\n> +\tdouble Y = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);\n> +\tdouble Z = (-0.68202) * (red) + (0.77073) * (green) + (0.56332) * (blue);\n> +\n> +\t/* Calculate the normalized chromaticity values */\n> +\tdouble x = X / (X + Y + Z);\n> +\tdouble y = Y / (X + Y + Z);\n> +\n> +\t/* Calculate CCT */\n> +\tdouble n = (x - 0.3320) / (0.1858 - y);\n> +\treturn 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;\n> +}\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/colours.h b/src/ipa/libipa/colours.h\n> new file mode 100644\n> index 00000000..b42ed0ac\n> --- /dev/null\n> +++ b/src/ipa/libipa/colours.h\n> @@ -0,0 +1,21 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * libipa miscellaneous colour helpers\n> + */\n> +\n> +#pragma once\n> +\n> +#include <stdint.h>\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +double rec601LuminanceFromRGB(double r, double g, double b);\n> +uint32_t estimateCCT(double red, double green, double blue);\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index e78cbcd6..788d037a 100644\n> --- a/src/ipa/libipa/meson.build\n> +++ b/src/ipa/libipa/meson.build\n> @@ -4,6 +4,7 @@ libipa_headers = files([\n>      'agc_mean_luminance.h',\n>      'algorithm.h',\n>      'camera_sensor_helper.h',\n> +    'colours.h',\n>      'exposure_mode_helper.h',\n>      'fc_queue.h',\n>      'histogram.h',\n> @@ -19,6 +20,7 @@ libipa_sources = files([\n>      'agc_mean_luminance.cpp',\n>      'algorithm.cpp',\n>      'camera_sensor_helper.cpp',\n> +    'colours.cpp',\n>      'exposure_mode_helper.cpp',\n>      'fc_queue.cpp',\n>      'histogram.cpp',","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 44C45C3257\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 15 Nov 2024 13:56:22 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 73EE26580A;\n\tFri, 15 Nov 2024 14:56:21 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8191C6580A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Nov 2024 14:56:19 +0100 (CET)","from mail-wm1-f69.google.com (mail-wm1-f69.google.com\n\t[209.85.128.69]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-138-I5OOEXsoPCyYbkrzScXyqw-1; Fri, 15 Nov 2024 08:56:17 -0500","by mail-wm1-f69.google.com with SMTP id\n\t5b1f17b1804b1-4316300bb15so12940935e9.2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 15 Nov 2024 05:56:16 -0800 (PST)","from nuthatch (ip-77-48-47-2.net.vodafone.cz. [77.48.47.2])\n\tby smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-432daafc6b3sm56440265e9.0.2024.11.15.05.56.14\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 15 Nov 2024 05:56:14 -0800 (PST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"VyKtHgnV\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1731678978;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=sC0SkwwgTdaCQIZVz//T29ppHd88NDxE2nelMdgt9rs=;\n\tb=VyKtHgnV82OntJv2P+n1bU+CYVFrvQk9C1r42O3ckvmcX1dn+zh/xrL4mS8BqVGlVoUZFm\n\tUFYo9A8hx3+yLY2+7rE95mYzJ98u713cMbAUvl2I5sIq5FO+/b4hYo5MXgMX7zb1NzsWAv\n\tfmdvs+RSfXkpH/pvFlDR4qMRuby3bxQ=","X-MC-Unique":"I5OOEXsoPCyYbkrzScXyqw-1","X-Mimecast-MFC-AGG-ID":"I5OOEXsoPCyYbkrzScXyqw","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1731678976; x=1732283776;\n\th=mime-version:user-agent:message-id:date:references:in-reply-to\n\t:subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date\n\t:message-id:reply-to;\n\tbh=sC0SkwwgTdaCQIZVz//T29ppHd88NDxE2nelMdgt9rs=;\n\tb=cEm71K39fhoykAfuUostlzBaCp5uW0fvoa1awPioGvIZVOT7v8R01ySCrXLymAY15F\n\tZ7LNys/ERURDWF7zn8d+3CVWmm/UuyiOvPeiQcX7BL+WVxe4gw+JeS/t7BTm2xhP9hnM\n\t+3DjdUirvI2PVCCyaPBhQxb7a9Hi/33i27aUEA9fzjkJX/MZ3ITApzZ+JV0yRZcJNVbb\n\tfftlR8wZhgTHDVafDBf3m87FbBRux8D4RdhMYMQIhEihkvl2MjTxHeNEEoAEzyZb+eLy\n\tMIDQgay+EMZGe9rF7sILJhLaoBkO1ABxkGlwOc+5xfL7WsMndLP21pyzxADZMwRkVCBT\n\tTqXQ==","X-Gm-Message-State":"AOJu0Ywf7yFhvJanAPY4Jxu8ctL75BaleiEZwzghf3Z3E733NrejKe+7\n\tDfN3dGIZRJX4I9wr835NJZFrW4lvaiTJIT3kqCqfCF6xD4H9Rfh/S9yaCh4/SKWVXp6l+rHc8Cw\n\txqKa7rCnGvp2k8rdhApXwXGv8Ju2kqjPgtyHr22QI+Mo+cO07vETVr90f/u+T2C40MfGEDU4=","X-Received":["by 2002:a05:600c:4e12:b0:431:5d4f:73b9 with SMTP id\n\t5b1f17b1804b1-432df77c7ccmr19441935e9.26.1731678975816; \n\tFri, 15 Nov 2024 05:56:15 -0800 (PST)","by 2002:a05:600c:4e12:b0:431:5d4f:73b9 with SMTP id\n\t5b1f17b1804b1-432df77c7ccmr19441735e9.26.1731678975425; \n\tFri, 15 Nov 2024 05:56:15 -0800 (PST)"],"X-Google-Smtp-Source":"AGHT+IHe0Qq1T3stwkIZ3RIEvo3CSU7UO35aKRiIshZBR7A3VFScnqRwI9R+yk0DBKSy43C3TYGqMA==","From":"Milan Zamazal <mzamazal@redhat.com>","To":"Daniel Scally <dan.scally@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org,  mike.rudenko@gmail.com","Subject":"Re: [PATCH v3 1/6] ipa: libipa: Add colour helpers","In-Reply-To":"<20241115074628.417215-2-dan.scally@ideasonboard.com> (Daniel\n\tScally's message of \"Fri, 15 Nov 2024 07:46:23 +0000\")","References":"<20241115074628.417215-1-dan.scally@ideasonboard.com>\n\t<20241115074628.417215-2-dan.scally@ideasonboard.com>","Date":"Fri, 15 Nov 2024 14:56:13 +0100","Message-ID":"<87ldxklhxe.fsf@redhat.com>","User-Agent":"Gnus/5.13 (Gnus v5.13)","MIME-Version":"1.0","X-Mimecast-Spam-Score":"0","X-Mimecast-MFC-PROC-ID":"cphzHHT35FLT6IQ8OZq-oGuzzIPaqacXYEnYGobudIQ_1731678976","X-Mimecast-Originator":"redhat.com","Content-Type":"text/plain","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>"}},{"id":32203,"web_url":"https://patchwork.libcamera.org/comment/32203/","msgid":"<20241118003834.GE30787@pendragon.ideasonboard.com>","date":"2024-11-18T00:38:34","subject":"Re: [PATCH v3 1/6] ipa: libipa: Add colour helpers","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Dan,\n\nThank you for the patch.\n\nOn Fri, Nov 15, 2024 at 07:46:23AM +0000, Daniel Scally wrote:\n> We start to have some functions relating to colour that are\n> effectively identical crop up across the IPA modules. Add a file\n> allowing those to be centralised within libipa so that a single\n> implementation can be used in all of the IPAs.\n> \n> Signed-off-by: Daniel Scally <dan.scally@ideasonboard.com>\n> ---\n> Changes in v3:\n> \n> \t- Named the file \"colours.h\" and \"colours.cpp\" instead of \"helpers\"\n> \t- Made the rec601LuminanceFromRGB() arguments doubles\n> \n> Changes in v2:\n> \n> \t- Dropped the Q(m, n) format helpers until they're needed.\n> \t- \\return statements after long description\n> \t- Switched rec601LuminanceFromRGB() to return a double\n> \n>  src/ipa/libipa/colours.cpp | 77 ++++++++++++++++++++++++++++++++++++++\n>  src/ipa/libipa/colours.h   | 21 +++++++++++\n>  src/ipa/libipa/meson.build |  2 +\n>  3 files changed, 100 insertions(+)\n>  create mode 100644 src/ipa/libipa/colours.cpp\n>  create mode 100644 src/ipa/libipa/colours.h\n> \n> diff --git a/src/ipa/libipa/colours.cpp b/src/ipa/libipa/colours.cpp\n> new file mode 100644\n> index 00000000..9fcb53b0\n> --- /dev/null\n> +++ b/src/ipa/libipa/colours.cpp\n> @@ -0,0 +1,77 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * libipa miscellaneous colour helpers\n> + */\n> +\n> +#include \"colours.h\"\n> +\n> +#include <algorithm>\n> +#include <cmath>\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +/**\n> + * \\file colours.h\n> + * \\brief Functions to reduce code duplication between IPA modules\n> + */\n> +\n> +/**\n> + * \\brief Estimate luminance from RGB values following ITU-R BT.601\n> + * \\param[in] r The red value\n> + * \\param[in] g The green value\n> + * \\param[in] b The blue value\n> + *\n> + * This function estimates a luminance value from a triplet of Red, Green and\n> + * Blue values, following the formula defined by ITU-R Recommendation BT.601-7\n> + * which can be found at https://www.itu.int/rec/R-REC-BT.601\n> + *\n> + * \\return The estimated luminance value\n> + */\n> +double rec601LuminanceFromRGB(double r, double g, double b)\n> +{\n> +\treturn (r * .299) + (g * .587) + (b * .114);\n> +}\n\nI've sent \"[RFC PATCH v2 00/12] Improve linear algebra helpers in\nlibipa\" which would allow rewriting this function as\n\ndouble rec601LuminanceFromRGB(RGB<double> rgb)\n{\n\tstatic constexpr RGB<double> rgb2y{{ 0.299, 0.587, 0.114 }};\n\n\treturn rgb.dot(rgb2y);\n}\n\nBut I think we could take it one step further, and instead define RGB to\nYUV conversion matrices (and their inverse matrices) in colour.h for\ncommon colour spaces.\n\nThe Agc::estimateLuminance() function could then be written as\n\ndouble Agc::estimateLuminance(double gain) const\n{\n\tRGB<double> sum;\n\n\tfor (const auto rgb : rgbTriples_) {\n\t\tRGB<double> rgbf = rgb;\n\t\trgbf *= gain;\n\t\tsum += rgbf.min(255.0);\n\t}\n\n\tVector<3, double> yuv = kBt601Rgb2YuvMatrix * (sum * gain_);\n\treturn yuv[0] / (bdsGrid_.height * bdsGrid_.width) / 255;\n}\n\nThe rgbf variable is needed as rgbTriples_ would store RGB<uint8_t>\nvalues. We may need further improvements to the Vector class operators,\nwhen multiplying a Vector<integer-type> by a double, we may want to\nproduce a Vector<double>.\n\nWe don't have to wait until \"[RFC PATCH v2 00/12] Improve linear algebra\nhelpers in libipa\" gets reviewed and merged, so\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nCould you then improve the code on top ?\n\n> +\n> +/**\n> + * \\brief Estimate correlated colour temperature from RGB color space input\n> + * \\param[in] red The input red value\n> + * \\param[in] green The input green value\n> + * \\param[in] blue The input blue value\n> + *\n> + * This function estimates the correlated color temperature RGB color space\n> + * input. In physics and color science, the Planckian locus or black body locus\n> + * is the path or locus that the color of an incandescent black body would take\n> + * in a particular chromaticity space as the blackbody temperature changes.\n> + *\n> + * If a narrow range of color temperatures is considered (those encapsulating\n> + * daylight being the most practical case) one can approximate the Planckian\n> + * locus in order to calculate the CCT in terms of chromaticity coordinates.\n> + *\n> + * More detailed information can be found in:\n> + * https://en.wikipedia.org/wiki/Color_temperature#Approximation\n> + *\n> + * \\return The estimated color temperature\n> + */\n> +uint32_t estimateCCT(double red, double green, double blue)\n> +{\n> +\t/* Convert the RGB values to CIE tristimulus values (XYZ) */\n> +\tdouble X = (-0.14282) * (red) + (1.54924) * (green) + (-0.95641) * (blue);\n> +\tdouble Y = (-0.32466) * (red) + (1.57837) * (green) + (-0.73191) * (blue);\n> +\tdouble Z = (-0.68202) * (red) + (0.77073) * (green) + (0.56332) * (blue);\n> +\n> +\t/* Calculate the normalized chromaticity values */\n> +\tdouble x = X / (X + Y + Z);\n> +\tdouble y = Y / (X + Y + Z);\n> +\n> +\t/* Calculate CCT */\n> +\tdouble n = (x - 0.3320) / (0.1858 - y);\n> +\treturn 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;\n> +}\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/colours.h b/src/ipa/libipa/colours.h\n> new file mode 100644\n> index 00000000..b42ed0ac\n> --- /dev/null\n> +++ b/src/ipa/libipa/colours.h\n> @@ -0,0 +1,21 @@\n> +/* SPDX-License-Identifier: BSD-2-Clause */\n> +/*\n> + * Copyright (C) 2024, Ideas on Board Oy\n> + *\n> + * libipa miscellaneous colour helpers\n> + */\n> +\n> +#pragma once\n> +\n> +#include <stdint.h>\n> +\n> +namespace libcamera {\n> +\n> +namespace ipa {\n> +\n> +double rec601LuminanceFromRGB(double r, double g, double b);\n> +uint32_t estimateCCT(double red, double green, double blue);\n> +\n> +} /* namespace ipa */\n> +\n> +} /* namespace libcamera */\n> diff --git a/src/ipa/libipa/meson.build b/src/ipa/libipa/meson.build\n> index e78cbcd6..788d037a 100644\n> --- a/src/ipa/libipa/meson.build\n> +++ b/src/ipa/libipa/meson.build\n> @@ -4,6 +4,7 @@ libipa_headers = files([\n>      'agc_mean_luminance.h',\n>      'algorithm.h',\n>      'camera_sensor_helper.h',\n> +    'colours.h',\n>      'exposure_mode_helper.h',\n>      'fc_queue.h',\n>      'histogram.h',\n> @@ -19,6 +20,7 @@ libipa_sources = files([\n>      'agc_mean_luminance.cpp',\n>      'algorithm.cpp',\n>      'camera_sensor_helper.cpp',\n> +    'colours.cpp',\n>      'exposure_mode_helper.cpp',\n>      'fc_queue.cpp',\n>      'histogram.cpp',","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 C62EEC32DD\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 18 Nov 2024 00:38:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D5759658AD;\n\tMon, 18 Nov 2024 01:38:46 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8CC15600F2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2024 01:38:45 +0100 (CET)","from pendragon.ideasonboard.com (81-175-209-231.bb.dnainternet.fi\n\t[81.175.209.231])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E768E75A;\n\tMon, 18 Nov 2024 01:38:28 +0100 (CET)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"BiNXLwhk\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1731890309;\n\tbh=lWNu90B1rs/0Hq13pIeaL+loaGtocoGWcxGDR+roLfo=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=BiNXLwhknAZ+X9T49HdrBApw40GT2heJcLlr6DdATurAqj7gwxc9xaeUWTF1ODM8f\n\tPbhQmGdnjmawKHFTu0DSRGKHJqAt2b88h6ttEMRhnjOfKuGGTQbDOaVVqr5iK9PnAJ\n\tgLnJhuDtg2JGXNXLvQVohrrWOWoX/7rLthBARjyY=","Date":"Mon, 18 Nov 2024 02:38:34 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Daniel Scally <dan.scally@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org, mike.rudenko@gmail.com","Subject":"Re: [PATCH v3 1/6] ipa: libipa: Add colour helpers","Message-ID":"<20241118003834.GE30787@pendragon.ideasonboard.com>","References":"<20241115074628.417215-1-dan.scally@ideasonboard.com>\n\t<20241115074628.417215-2-dan.scally@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20241115074628.417215-2-dan.scally@ideasonboard.com>","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>"}}]