From patchwork Tue Dec 8 20:44:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 10616 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id F401DBDB20 for ; Tue, 8 Dec 2020 20:44:49 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id A198667F12; Tue, 8 Dec 2020 21:44:49 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=raspberrypi.com header.i=@raspberrypi.com header.b="NlUkXIQ1"; dkim-atps=neutral Received: from mail-ed1-x532.google.com (mail-ed1-x532.google.com [IPv6:2a00:1450:4864:20::532]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 651CE67E6F for ; Tue, 8 Dec 2020 21:44:48 +0100 (CET) Received: by mail-ed1-x532.google.com with SMTP id cm17so19040349edb.4 for ; Tue, 08 Dec 2020 12:44:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=raspberrypi.com; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=Zy5kG8Dx19vajqkeNd3AVwmfxp8E+gdkVwFyJzoeJFo=; b=NlUkXIQ1f1A74pK7VGzEdKAUWdf/tS4vkfEe93EyRoRG/WDC13Hid+cJGiOdMpWxau N8eAPDd9DHGkFNC6atKLAgbAkidSsVHDiO5SQ3rTjAE2gBcDZACTH+4AJcoVWGmcXlOv IswuGbOd8b0gQYK61+0K4b9xpAFknNEygHhBMWsCgrA1U6bHcwDFSoxu03EE7Z9APr/6 XTqvEQ9SxkIn8hHqEjVFoChy8NdXBQDu7pXko1JVF6rvQZFuHtq6hvjabNr/bcmIenb8 4eY8a+gLyW5zYCvpNLhOfA6GPxWc31crhAEnG4Q0Vqi3P1n5JzpaP1ItXjMOds/QMUuR OfRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=Zy5kG8Dx19vajqkeNd3AVwmfxp8E+gdkVwFyJzoeJFo=; b=SH0xSWAQfSL1n7MjxoZ4jYFAiBOgXQ/UFsN0iT4zp/tX+QB0kBJ01lWDuP/jNfWNls kcQ/R02i8/E2pmnpebd+TENKypK0J6C7T9cqdU/eNKuZOFdJLklKP+5I8lHepJ7WL2Aq 2oxPQR914YdJavfVazneBax8qoHHKbjERX87OrRfdkr9PW7Fz4kkyV7BQYwvzVzGiLZw x/WspTS7N7YIEEHCptcN9stgkATq3vp/sgVpGWIJgdNzwVP7vunLcHjjsBBWbRLufm96 DNnStLMHe+wAl8Irwf/ZOnC25zPeWNX3mc0m0MYEOYXkkf322jDcgHcIHwP2BMJmLbo0 YI1w== X-Gm-Message-State: AOAM531oe2vFVtTGiwMNhD9czA8838HdTJNRSnGQ/xXPx31ZiLBvPEHn KWBIC5UK0lXRLpXh4Q4ayOLwuz9Lm6dwdaSu X-Google-Smtp-Source: ABdhPJy57bJJIYrfggRqJwv17gF4JEUiyrBUXEMkMw36r++eQo3Fk4S0belR9VRLctck1Kih0ChQfA== X-Received: by 2002:a05:6402:1caa:: with SMTP id cz10mr14565766edb.345.1607460287756; Tue, 08 Dec 2020 12:44:47 -0800 (PST) Received: from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72]) by smtp.gmail.com with ESMTPSA id be6sm18441471edb.29.2020.12.08.12.44.46 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Dec 2020 12:44:47 -0800 (PST) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Tue, 8 Dec 2020 20:44:39 +0000 Message-Id: <20201208204441.9356-5-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20201208204441.9356-1-david.plowman@raspberrypi.com> References: <20201208204441.9356-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 4/6] src: ipa: raspberrypi: Compute inverse of piecewise linear function X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" Add a method to the piecewise linear function (Pwl) class to compute the inverse of a given Pwl. If the input function is non-monotonic we can only produce a best effort "pseudo" inverse, and we signal this to the caller. Signed-off-by: David Plowman Reviewed-by: Naushir Patuck Reviewed-by: Laurent Pinchart --- src/ipa/raspberrypi/controller/pwl.cpp | 30 ++++++++++++++++++++++++++ src/ipa/raspberrypi/controller/pwl.hpp | 3 +++ 2 files changed, 33 insertions(+) diff --git a/src/ipa/raspberrypi/controller/pwl.cpp b/src/ipa/raspberrypi/controller/pwl.cpp index aa134a1f..130c820b 100644 --- a/src/ipa/raspberrypi/controller/pwl.cpp +++ b/src/ipa/raspberrypi/controller/pwl.cpp @@ -114,6 +114,36 @@ Pwl::PerpType Pwl::Invert(Point const &xy, Point &perp, int &span, return PerpType::None; } +Pwl Pwl::Inverse(bool *true_inverse, const double eps) const +{ + bool appended = false, prepended = false, neither = false; + Pwl inverse; + + for (Point const &p : points_) { + if (inverse.Empty()) + inverse.Append(p.y, p.x, eps); + else if (std::abs(inverse.points_.back().x - p.y) <= eps || + std::abs(inverse.points_.front().x - p.y) <= eps) + /* do nothing */; + else if (p.y > inverse.points_.back().x) { + inverse.Append(p.y, p.x, eps); + appended = true; + } else if (p.y < inverse.points_.front().x) { + inverse.Prepend(p.y, p.x, eps); + prepended = true; + } else + neither = true; + } + + // This is not a proper inverse if we found ourselves putting points + // onto both ends of the inverse, or if there were points that couldn't + // go on either. + if (true_inverse) + *true_inverse = !(neither || (appended && prepended)); + + return inverse; +} + Pwl Pwl::Compose(Pwl const &other, const double eps) const { double this_x = points_[0].x, this_y = points_[0].y; diff --git a/src/ipa/raspberrypi/controller/pwl.hpp b/src/ipa/raspberrypi/controller/pwl.hpp index 4f168551..484672f6 100644 --- a/src/ipa/raspberrypi/controller/pwl.hpp +++ b/src/ipa/raspberrypi/controller/pwl.hpp @@ -80,6 +80,9 @@ public: }; PerpType Invert(Point const &xy, Point &perp, int &span, const double eps = 1e-6) const; + // Compute the inverse function. Indicate if it is a proper (true) + // inverse, or only a best effort (e.g. input was non-monotonic). + Pwl Inverse(bool *true_inverse = nullptr, const double eps = 1e-6) const; // Compose two Pwls together, doing "this" first and "other" after. Pwl Compose(Pwl const &other, const double eps = 1e-6) const; // Apply function to (x,y) values at every control point.