[{"id":32226,"web_url":"https://patchwork.libcamera.org/comment/32226/","msgid":"<87mshwhcom.fsf@redhat.com>","date":"2024-11-18T13:52:09","subject":"Re: [RFC PATCH v2 12/12] ipa: rkisp1: awb: Replace manual\n\tcalculations with Vector and Matrix","submitter":{"id":177,"url":"https://patchwork.libcamera.org/api/people/177/","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"content":"Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:\n\n> Processing of the statistics and estimation of the colour temperature\n> involve linear algebra. Replace the manual calculations with usage of\n> the Vector and Matrix classes.\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Milan Zamazal <mzamazal@redhat.com>\n\n> ---\n> Changes since v1:\n>\n> - Adapt to Vector::normalize() being dropped\n> ---\n>  src/ipa/rkisp1/algorithms/awb.cpp | 44 +++++++++++++++++++------------\n>  1 file changed, 27 insertions(+), 17 deletions(-)\n>\n> diff --git a/src/ipa/rkisp1/algorithms/awb.cpp b/src/ipa/rkisp1/algorithms/awb.cpp\n> index 1c572055acdd..97163e0fa45c 100644\n> --- a/src/ipa/rkisp1/algorithms/awb.cpp\n> +++ b/src/ipa/rkisp1/algorithms/awb.cpp\n> @@ -169,17 +169,21 @@ void Awb::prepare(IPAContext &context, const uint32_t frame,\n>  \n>  uint32_t Awb::estimateCCT(const RGB<double> &rgb)\n>  {\n> -\t/* Convert the RGB values to CIE tristimulus values (XYZ) */\n> -\tdouble X = -0.14282 * rgb.r() + 1.54924 * rgb.g() - 0.95641 * rgb.b();\n> -\tdouble Y = -0.32466 * rgb.r() + 1.57837 * rgb.g() - 0.73191 * rgb.b();\n> -\tdouble Z = -0.68202 * rgb.r() + 0.77073 * rgb.g() + 0.56332 * rgb.b();\n> +\t/*\n> +\t * Convert the RGB values to CIE tristimulus values (XYZ) and divide by\n> +\t * the sum of X, Y and Z to calculate the CIE xy chromaticity.\n> +\t */\n> +\tstatic const Matrix<double, 3, 3> rgb2xyz({\n> +\t\t-0.14282, 1.54924, -0.95641,\n> +\t\t-0.32466, 1.57837, -0.73191,\n> +\t\t-0.68202, 0.77073,  0.56332\n> +\t});\n>  \n> -\t/* Calculate the normalized chromaticity values */\n> -\tdouble x = X / (X + Y + Z);\n> -\tdouble y = Y / (X + Y + Z);\n> +\tVector<double, 3> xyz = rgb2xyz * rgb;\n> +\txyz /= xyz.sum();\n>  \n>  \t/* Calculate CCT */\n> -\tdouble n = (x - 0.3320) / (0.1858 - y);\n> +\tdouble n = (xyz.x() - 0.3320) / (0.1858 - xyz.y());\n>  \treturn 449 * n * n * n + 3525 * n * n + 6823.3 * n + 5520.33;\n>  }\n>  \n> @@ -215,9 +219,11 @@ void Awb::process(IPAContext &context,\n>  \t\trgbMeans.b() = awb->awb_mean[0].mean_cb_or_b;\n>  \t} else {\n>  \t\t/* Get the YCbCr mean values */\n> -\t\tdouble yMean = awb->awb_mean[0].mean_y_or_g;\n> -\t\tdouble cbMean = awb->awb_mean[0].mean_cb_or_b;\n> -\t\tdouble crMean = awb->awb_mean[0].mean_cr_or_r;\n> +\t\tVector<double, 3> yuvMeans({\n> +\t\t\tstatic_cast<double>(awb->awb_mean[0].mean_y_or_g),\n> +\t\t\tstatic_cast<double>(awb->awb_mean[0].mean_cb_or_b),\n> +\t\t\tstatic_cast<double>(awb->awb_mean[0].mean_cr_or_r)\n> +\t\t});\n>  \n>  \t\t/*\n>  \t\t * Convert from YCbCr to RGB.\n> @@ -231,12 +237,16 @@ void Awb::process(IPAContext &context,\n>  \t\t *  [1,1636, -0,4045, -0,7949]\n>  \t\t *  [1,1636,  1,9912, -0,0250]]\n>  \t\t */\n> -\t\tyMean -= 16;\n> -\t\tcbMean -= 128;\n> -\t\tcrMean -= 128;\n> -\t\trgbMeans.r() = 1.1636 * yMean - 0.0623 * cbMean + 1.6008 * crMean;\n> -\t\trgbMeans.g() = 1.1636 * yMean - 0.4045 * cbMean - 0.7949 * crMean;\n> -\t\trgbMeans.b() = 1.1636 * yMean + 1.9912 * cbMean - 0.0250 * crMean;\n> +\t\tstatic const Matrix<double, 3, 3> yuv2rgbMatrix({\n> +\t\t\t1.1636, -0.0623,  1.6008,\n> +\t\t\t1.1636, -0.4045, -0.7949,\n> +\t\t\t1.1636,  1.9912, -0.0250\n> +\t\t});\n> +\t\tstatic const Vector<double, 3> yuv2rgbOffset({\n> +\t\t\t16, 128, 128\n> +\t\t});\n> +\n> +\t\trgbMeans = yuv2rgbMatrix * (yuvMeans - yuv2rgbOffset);\n>  \n>  \t\t/*\n>  \t\t * Due to hardware rounding errors in the YCbCr means, the","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 EAA3AC32DD\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 18 Nov 2024 13:52:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 9EA58658D8;\n\tMon, 18 Nov 2024 14:52:16 +0100 (CET)","from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.129.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 5DF0860532\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2024 14:52:15 +0100 (CET)","from mail-wm1-f70.google.com (mail-wm1-f70.google.com\n\t[209.85.128.70]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-611-kpqzGtdhOFifSNetK1xTAA-1; Mon, 18 Nov 2024 08:52:13 -0500","by mail-wm1-f70.google.com with SMTP id\n\t5b1f17b1804b1-4315cefda02so22920175e9.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Nov 2024 05:52:13 -0800 (PST)","from nuthatch ([2a00:102a:400a:489a:ffe9:aa41:63cd:fc2b])\n\tby smtp.gmail.com with ESMTPSA id\n\t5b1f17b1804b1-432dab80ad9sm154542645e9.25.2024.11.18.05.52.10\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tMon, 18 Nov 2024 05:52:10 -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=\"SgicQY+G\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1731937934;\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=u5Csn/WU1CiodOOmug+9ftqTVnDCLi/QBTqFxeSCBtk=;\n\tb=SgicQY+GnHT6h6lOysC0Axv/HkIpsixsQmP4ti3E3Pi7DCcS2+Dhjz00CHFEGNRCzD52i/\n\t9iwfTumxn9Fjz4jTvM4l5Q3okLXvc4OZV0efMrbLdhRwlt3epopntUi4E7fUEliKWyhGM1\n\tJijelp6xbN0E1ncxpKHpfr821Zh9TRg=","X-MC-Unique":"kpqzGtdhOFifSNetK1xTAA-1","X-Mimecast-MFC-AGG-ID":"kpqzGtdhOFifSNetK1xTAA","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20230601; t=1731937932; x=1732542732;\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=u5Csn/WU1CiodOOmug+9ftqTVnDCLi/QBTqFxeSCBtk=;\n\tb=MMc5ciaJg2Z+28DNVLR+N8mnGnOReuOQ82S19aP/eZCSrmla9oT4k1PhfWVsXv65Sr\n\tLpDft7Ker9OzOcTXnlrrPCI08ZSXoqAuHwHRqkgKqmoRDqFMXoPrg12oy9ww3D5znfXg\n\tHqLCVh3yA3kBUGUNd2mpDkOuMpFVpR1PyqN1h0RozxSAaNIXegwJ4AN9V8AU81AlutWV\n\tc2Ct3hCigjuHqQzFUaaoZnVmM+vFqVVR8uKUwOwTIKVNIVpM+e/EjJWzrjI/wfQaqh3S\n\tUoLSxx5AhpHoESurng0WoaVDawY3OXDmNK6O5Pv/gQCZNsk8uSjmdO7sHiLev7Ei7rpG\n\t/kiQ==","X-Gm-Message-State":"AOJu0YyzQyee0RWjyf/aPdURUoZUcQRsQyStv3MmMw/4Ey9L6N/j5NYv\n\tc4gNvbe/rqYuciuSEUqXfW4GeFmP0cVSCiQcYXH2ppfEoAa5pAmDZyoeR18YVc9KVxTKz9dhBB8\n\tJ6nvR/qITQJHLb4j4kjUXwHTvbBopvHtJFa5EHGsKzCIwlzNmDSARZG+KlmQ94Hzz1ZqfVR5myS\n\t+DTh2ztH1W+lYjVCrRs8pubGn5Ezr3tdEY0jAhz+6IpJg/ozPg5Qqo1ac=","X-Received":["by 2002:a05:600c:3b96:b0:431:53db:3d29 with SMTP id\n\t5b1f17b1804b1-432df74d944mr98771485e9.18.1731937931845; \n\tMon, 18 Nov 2024 05:52:11 -0800 (PST)","by 2002:a05:600c:3b96:b0:431:53db:3d29 with SMTP id\n\t5b1f17b1804b1-432df74d944mr98771195e9.18.1731937931344; \n\tMon, 18 Nov 2024 05:52:11 -0800 (PST)"],"X-Google-Smtp-Source":"AGHT+IH+4VDnZ98PSlGccIXlDO2m7xQ+/klSr1Pigu7PycmMveMddpIL4rUbwbXCj1boJLgER1+Axw==","From":"Milan Zamazal <mzamazal@redhat.com>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Cc":"libcamera-devel@lists.libcamera.org","Subject":"Re: [RFC PATCH v2 12/12] ipa: rkisp1: awb: Replace manual\n\tcalculations with Vector and Matrix","In-Reply-To":"<20241118000738.18977-13-laurent.pinchart@ideasonboard.com>\n\t(Laurent Pinchart's message of \"Mon, 18 Nov 2024 02:07:38 +0200\")","References":"<20241118000738.18977-1-laurent.pinchart@ideasonboard.com>\n\t<20241118000738.18977-13-laurent.pinchart@ideasonboard.com>","Date":"Mon, 18 Nov 2024 14:52:09 +0100","Message-ID":"<87mshwhcom.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":"pumcxo3g19b0fZ07n0vGB2TYsDai3Tm5SvhEKDHQ8ZY_1731937932","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>"}}]