[v3,4/4] ipa: rpi: controller: Use libipa's Pwl class
diff mbox series

Message ID 20240529192612.814515-5-paul.elder@ideasonboard.com
State Superseded
Headers show
Series
  • ipa: Move Pwl from Raspberry Pi to libipa
Related show

Commit Message

Paul Elder May 29, 2024, 7:26 p.m. UTC
To reduce code duplication, use the Pwl class from libipa. This also
removes the Pwl class from the Raspberry Pi IPA.

Signed-off-by: Paul Elder <paul.elder@ideasonboard.com>
Reviewed-by: Stefan Klug <stefan.klug@ideasonboard.com>

---
No change in v3

Changes in v2:
- s/FPoint/PointF/g
---
 src/ipa/rpi/controller/cac_status.h        |  2 --
 src/ipa/rpi/controller/contrast_status.h   |  4 +--
 src/ipa/rpi/controller/meson.build         |  2 +-
 src/ipa/rpi/controller/rpi/af.cpp          |  4 +--
 src/ipa/rpi/controller/rpi/af.h            |  5 +--
 src/ipa/rpi/controller/rpi/agc_channel.cpp |  8 ++---
 src/ipa/rpi/controller/rpi/agc_channel.h   |  7 ++--
 src/ipa/rpi/controller/rpi/awb.cpp         | 40 +++++++++++-----------
 src/ipa/rpi/controller/rpi/awb.h           | 23 +++++++------
 src/ipa/rpi/controller/rpi/ccm.cpp         |  4 +--
 src/ipa/rpi/controller/rpi/ccm.h           |  5 +--
 src/ipa/rpi/controller/rpi/contrast.cpp    | 14 ++++----
 src/ipa/rpi/controller/rpi/contrast.h      |  5 +--
 src/ipa/rpi/controller/rpi/geq.cpp         |  5 ++-
 src/ipa/rpi/controller/rpi/geq.h           |  4 ++-
 src/ipa/rpi/controller/rpi/hdr.cpp         |  8 ++---
 src/ipa/rpi/controller/rpi/hdr.h           |  9 ++---
 src/ipa/rpi/controller/rpi/tonemap.cpp     |  2 +-
 src/ipa/rpi/controller/rpi/tonemap.h       |  5 +--
 src/ipa/rpi/controller/tonemap_status.h    |  4 +--
 20 files changed, 84 insertions(+), 76 deletions(-)

Patch
diff mbox series

diff --git a/src/ipa/rpi/controller/cac_status.h b/src/ipa/rpi/controller/cac_status.h
index 475d4c5cc..adffce411 100644
--- a/src/ipa/rpi/controller/cac_status.h
+++ b/src/ipa/rpi/controller/cac_status.h
@@ -6,8 +6,6 @@ 
  */
 #pragma once
 
-#include "pwl.h"
-
 struct CacStatus {
 	std::vector<double> lutRx;
 	std::vector<double> lutRy;
diff --git a/src/ipa/rpi/controller/contrast_status.h b/src/ipa/rpi/controller/contrast_status.h
index 7c67f0547..1f175872a 100644
--- a/src/ipa/rpi/controller/contrast_status.h
+++ b/src/ipa/rpi/controller/contrast_status.h
@@ -6,7 +6,7 @@ 
  */
 #pragma once
 
-#include "pwl.h"
+#include "libipa/pwl.h"
 
 /*
  * The "contrast" algorithm creates a gamma curve, optionally doing a little bit
@@ -14,7 +14,7 @@ 
  */
 
 struct ContrastStatus {
-	RPiController::Pwl gammaCurve;
+	libcamera::ipa::Pwl gammaCurve;
 	double brightness;
 	double contrast;
 };
diff --git a/src/ipa/rpi/controller/meson.build b/src/ipa/rpi/controller/meson.build
index 32a4d31cf..74b74888b 100644
--- a/src/ipa/rpi/controller/meson.build
+++ b/src/ipa/rpi/controller/meson.build
@@ -5,7 +5,6 @@  rpi_ipa_controller_sources = files([
     'controller.cpp',
     'device_status.cpp',
     'histogram.cpp',
-    'pwl.cpp',
     'rpi/af.cpp',
     'rpi/agc.cpp',
     'rpi/agc_channel.cpp',
@@ -32,4 +31,5 @@  rpi_ipa_controller_deps = [
 ]
 
 rpi_ipa_controller_lib = static_library('rpi_ipa_controller', rpi_ipa_controller_sources,
+                                        include_directories : libipa_includes,
                                         dependencies : rpi_ipa_controller_deps)
diff --git a/src/ipa/rpi/controller/rpi/af.cpp b/src/ipa/rpi/controller/rpi/af.cpp
index c5fd84826..304629d6d 100644
--- a/src/ipa/rpi/controller/rpi/af.cpp
+++ b/src/ipa/rpi/controller/rpi/af.cpp
@@ -139,7 +139,7 @@  int Af::CfgParams::read(const libcamera::YamlObject &params)
 	readNumber<uint32_t>(skipFrames, params, "skip_frames");
 
 	if (params.contains("map"))
-		map.read(params["map"]);
+		map.readYaml(params["map"]);
 	else
 		LOG(RPiAf, Warning) << "No map defined";
 
@@ -721,7 +721,7 @@  bool Af::setLensPosition(double dioptres, int *hwpos)
 
 	if (mode_ == AfModeManual) {
 		LOG(RPiAf, Debug) << "setLensPosition: " << dioptres;
-		ftarget_ = cfg_.map.domain().clip(dioptres);
+		ftarget_ = cfg_.map.domain().clamp(dioptres);
 		changed = !(initted_ && fsmooth_ == ftarget_);
 		updateLensPosition();
 	}
diff --git a/src/ipa/rpi/controller/rpi/af.h b/src/ipa/rpi/controller/rpi/af.h
index 2617e2ace..317a51f3e 100644
--- a/src/ipa/rpi/controller/rpi/af.h
+++ b/src/ipa/rpi/controller/rpi/af.h
@@ -9,7 +9,8 @@ 
 #include "../af_algorithm.h"
 #include "../af_status.h"
 #include "../pdaf_data.h"
-#include "../pwl.h"
+
+#include "libipa/pwl.h"
 
 /*
  * This algorithm implements a hybrid of CDAF and PDAF, favouring PDAF.
@@ -100,7 +101,7 @@  private:
 		uint32_t confThresh;	       	/* PDAF confidence cell min (sensor-specific) */
 		uint32_t confClip;	       	/* PDAF confidence cell max (sensor-specific) */
 		uint32_t skipFrames;	       	/* frames to skip at start or modeswitch */
-		Pwl map;		       	/* converts dioptres -> lens driver position */
+		libcamera::ipa::Pwl map;       	/* converts dioptres -> lens driver position */
 
 		CfgParams();
 		int read(const libcamera::YamlObject &params);
diff --git a/src/ipa/rpi/controller/rpi/agc_channel.cpp b/src/ipa/rpi/controller/rpi/agc_channel.cpp
index a77ccec36..a381dd972 100644
--- a/src/ipa/rpi/controller/rpi/agc_channel.cpp
+++ b/src/ipa/rpi/controller/rpi/agc_channel.cpp
@@ -130,7 +130,7 @@  int AgcConstraint::read(const libcamera::YamlObject &params)
 		return -EINVAL;
 	qHi = *value;
 
-	return yTarget.read(params["y_target"]);
+	return yTarget.readYaml(params["y_target"]);
 }
 
 static std::tuple<int, AgcConstraintMode>
@@ -237,7 +237,7 @@  int AgcConfig::read(const libcamera::YamlObject &params)
 			return ret;
 	}
 
-	ret = yTarget.read(params["y_target"]);
+	ret = yTarget.readYaml(params["y_target"]);
 	if (ret)
 		return ret;
 
@@ -715,7 +715,7 @@  static constexpr double EvGainYTargetLimit = 0.9;
 static double constraintComputeGain(AgcConstraint &c, const Histogram &h, double lux,
 				    double evGain, double &targetY)
 {
-	targetY = c.yTarget.eval(c.yTarget.domain().clip(lux));
+	targetY = c.yTarget.eval(c.yTarget.domain().clamp(lux));
 	targetY = std::min(EvGainYTargetLimit, targetY * evGain);
 	double iqm = h.interQuantileMean(c.qLo, c.qHi);
 	return (targetY * h.bins()) / iqm;
@@ -734,7 +734,7 @@  void AgcChannel::computeGain(StatisticsPtr &statistics, Metadata *imageMetadata,
 	 * The initial gain and target_Y come from some of the regions. After
 	 * that we consider the histogram constraints.
 	 */
-	targetY = config_.yTarget.eval(config_.yTarget.domain().clip(lux.lux));
+	targetY = config_.yTarget.eval(config_.yTarget.domain().clamp(lux.lux));
 	targetY = std::min(EvGainYTargetLimit, targetY * evGain);
 
 	/*
diff --git a/src/ipa/rpi/controller/rpi/agc_channel.h b/src/ipa/rpi/controller/rpi/agc_channel.h
index 99033e23e..58368889e 100644
--- a/src/ipa/rpi/controller/rpi/agc_channel.h
+++ b/src/ipa/rpi/controller/rpi/agc_channel.h
@@ -12,10 +12,11 @@ 
 
 #include <libcamera/base/utils.h>
 
+#include <libipa/pwl.h>
+
 #include "../agc_status.h"
 #include "../awb_status.h"
 #include "../controller.h"
-#include "../pwl.h"
 
 /* This is our implementation of AGC. */
 
@@ -40,7 +41,7 @@  struct AgcConstraint {
 	Bound bound;
 	double qLo;
 	double qHi;
-	Pwl yTarget;
+	libcamera::ipa::Pwl yTarget;
 	int read(const libcamera::YamlObject &params);
 };
 
@@ -61,7 +62,7 @@  struct AgcConfig {
 	std::map<std::string, AgcExposureMode> exposureModes;
 	std::map<std::string, AgcConstraintMode> constraintModes;
 	std::vector<AgcChannelConstraint> channelConstraints;
-	Pwl yTarget;
+	libcamera::ipa::Pwl yTarget;
 	double speed;
 	uint16_t startupFrames;
 	unsigned int convergenceFrames;
diff --git a/src/ipa/rpi/controller/rpi/awb.cpp b/src/ipa/rpi/controller/rpi/awb.cpp
index abe5906e9..db9937cd4 100644
--- a/src/ipa/rpi/controller/rpi/awb.cpp
+++ b/src/ipa/rpi/controller/rpi/awb.cpp
@@ -49,10 +49,10 @@  int AwbPrior::read(const libcamera::YamlObject &params)
 		return -EINVAL;
 	lux = *value;
 
-	return prior.read(params["prior"]);
+	return prior.readYaml(params["prior"]);
 }
 
-static int readCtCurve(Pwl &ctR, Pwl &ctB, const libcamera::YamlObject &params)
+static int readCtCurve(ipa::Pwl &ctR, ipa::Pwl &ctB, const libcamera::YamlObject &params)
 {
 	if (params.size() % 3) {
 		LOG(RPiAwb, Error) << "AwbConfig: incomplete CT curve entry";
@@ -207,7 +207,7 @@  void Awb::initialise()
 	 * them.
 	 */
 	if (!config_.ctR.empty() && !config_.ctB.empty()) {
-		syncResults_.temperatureK = config_.ctR.domain().clip(4000);
+		syncResults_.temperatureK = config_.ctR.domain().clamp(4000);
 		syncResults_.gainR = 1.0 / config_.ctR.eval(syncResults_.temperatureK);
 		syncResults_.gainG = 1.0;
 		syncResults_.gainB = 1.0 / config_.ctB.eval(syncResults_.temperatureK);
@@ -273,8 +273,8 @@  void Awb::setManualGains(double manualR, double manualB)
 		syncResults_.gainB = prevSyncResults_.gainB = manualB_;
 		if (config_.bayes) {
 			/* Also estimate the best corresponding colour temperature from the curves. */
-			double ctR = config_.ctRInverse.eval(config_.ctRInverse.domain().clip(1 / manualR_));
-			double ctB = config_.ctBInverse.eval(config_.ctBInverse.domain().clip(1 / manualB_));
+			double ctR = config_.ctRInverse.eval(config_.ctRInverse.domain().clamp(1 / manualR_));
+			double ctB = config_.ctBInverse.eval(config_.ctBInverse.domain().clamp(1 / manualB_));
 			prevSyncResults_.temperatureK = (ctR + ctB) / 2;
 			syncResults_.temperatureK = prevSyncResults_.temperatureK;
 		}
@@ -468,7 +468,7 @@  double Awb::computeDelta2Sum(double gainR, double gainB)
 	return delta2Sum;
 }
 
-Pwl Awb::interpolatePrior()
+ipa::Pwl Awb::interpolatePrior()
 {
 	/*
 	 * Interpolate the prior log likelihood function for our current lux
@@ -485,7 +485,7 @@  Pwl Awb::interpolatePrior()
 			idx++;
 		double lux0 = config_.priors[idx].lux,
 		       lux1 = config_.priors[idx + 1].lux;
-		return Pwl::combine(config_.priors[idx].prior,
+		return ipa::Pwl::combine(config_.priors[idx].prior,
 				    config_.priors[idx + 1].prior,
 				    [&](double /*x*/, double y0, double y1) {
 					    return y0 + (y1 - y0) *
@@ -494,15 +494,15 @@  Pwl Awb::interpolatePrior()
 	}
 }
 
-static double interpolateQuadatric(Pwl::Point const &a, Pwl::Point const &b,
-				   Pwl::Point const &c)
+static double interpolateQuadatric(PointF const &a, PointF const &b,
+				   PointF const &c)
 {
 	/*
 	 * Given 3 points on a curve, find the extremum of the function in that
 	 * interval by fitting a quadratic.
 	 */
 	const double eps = 1e-3;
-	Pwl::Point ca = c - a, ba = b - a;
+	PointF ca = c - a, ba = b - a;
 	double denominator = 2 * (ba.y * ca.x - ca.y * ba.x);
 	if (abs(denominator) > eps) {
 		double numerator = ba.y * ca.x * ca.x - ca.y * ba.x * ba.x;
@@ -513,7 +513,7 @@  static double interpolateQuadatric(Pwl::Point const &a, Pwl::Point const &b,
 	return a.y < c.y - eps ? a.x : (c.y < a.y - eps ? c.x : b.x);
 }
 
-double Awb::coarseSearch(Pwl const &prior)
+double Awb::coarseSearch(ipa::Pwl const &prior)
 {
 	points_.clear(); /* assume doesn't deallocate memory */
 	size_t bestPoint = 0;
@@ -525,14 +525,14 @@  double Awb::coarseSearch(Pwl const &prior)
 		double b = config_.ctB.eval(t, &spanB);
 		double gainR = 1 / r, gainB = 1 / b;
 		double delta2Sum = computeDelta2Sum(gainR, gainB);
-		double priorLogLikelihood = prior.eval(prior.domain().clip(t));
+		double priorLogLikelihood = prior.eval(prior.domain().clamp(t));
 		double finalLogLikelihood = delta2Sum - priorLogLikelihood;
 		LOG(RPiAwb, Debug)
 			<< "t: " << t << " gain R " << gainR << " gain B "
 			<< gainB << " delta2_sum " << delta2Sum
 			<< " prior " << priorLogLikelihood << " final "
 			<< finalLogLikelihood;
-		points_.push_back(Pwl::Point(t, finalLogLikelihood));
+		points_.push_back(PointF(t, finalLogLikelihood));
 		if (points_.back().y < points_[bestPoint].y)
 			bestPoint = points_.size() - 1;
 		if (t == mode_->ctHi)
@@ -559,7 +559,7 @@  double Awb::coarseSearch(Pwl const &prior)
 	return t;
 }
 
-void Awb::fineSearch(double &t, double &r, double &b, Pwl const &prior)
+void Awb::fineSearch(double &t, double &r, double &b, ipa::Pwl const &prior)
 {
 	int spanR = -1, spanB = -1;
 	config_.ctR.eval(t, &spanR);
@@ -570,7 +570,7 @@  void Awb::fineSearch(double &t, double &r, double &b, Pwl const &prior)
 		       config_.ctR.eval(t - nsteps * step, &spanR);
 	double bDiff = config_.ctB.eval(t + nsteps * step, &spanB) -
 		       config_.ctB.eval(t - nsteps * step, &spanB);
-	Pwl::Point transverse(bDiff, -rDiff);
+	PointF transverse(bDiff, -rDiff);
 	if (transverse.len2() < 1e-6)
 		return;
 	/*
@@ -592,17 +592,17 @@  void Awb::fineSearch(double &t, double &r, double &b, Pwl const &prior)
 	for (int i = -nsteps; i <= nsteps; i++) {
 		double tTest = t + i * step;
 		double priorLogLikelihood =
-			prior.eval(prior.domain().clip(tTest));
+			prior.eval(prior.domain().clamp(tTest));
 		double rCurve = config_.ctR.eval(tTest, &spanR);
 		double bCurve = config_.ctB.eval(tTest, &spanB);
 		/* x will be distance off the curve, y the log likelihood there */
-		Pwl::Point points[maxNumDeltas];
+		PointF points[maxNumDeltas];
 		int bestPoint = 0;
 		/* Take some measurements transversely *off* the CT curve. */
 		for (int j = 0; j < numDeltas; j++) {
 			points[j].x = -config_.transverseNeg +
 				      (transverseRange * j) / (numDeltas - 1);
-			Pwl::Point rbTest = Pwl::Point(rCurve, bCurve) +
+			PointF rbTest = PointF(rCurve, bCurve) +
 					    transverse * points[j].x;
 			double rTest = rbTest.x, bTest = rbTest.y;
 			double gainR = 1 / rTest, gainB = 1 / bTest;
@@ -619,7 +619,7 @@  void Awb::fineSearch(double &t, double &r, double &b, Pwl const &prior)
 		 * now let's do a quadratic interpolation for the best result.
 		 */
 		bestPoint = std::max(1, std::min(bestPoint, numDeltas - 2));
-		Pwl::Point rbTest = Pwl::Point(rCurve, bCurve) +
+		PointF rbTest = PointF(rCurve, bCurve) +
 					transverse * interpolateQuadatric(points[bestPoint - 1],
 									points[bestPoint],
 									points[bestPoint + 1]);
@@ -653,7 +653,7 @@  void Awb::awbBayes()
 	 * Get the current prior, and scale according to how many zones are
 	 * valid... not entirely sure about this.
 	 */
-	Pwl prior = interpolatePrior();
+	ipa::Pwl prior = interpolatePrior();
 	prior *= zones_.size() / (double)(statistics_->awbRegions.numRegions());
 	prior.map([](double x, double y) {
 		LOG(RPiAwb, Debug) << "(" << x << "," << y << ")";
diff --git a/src/ipa/rpi/controller/rpi/awb.h b/src/ipa/rpi/controller/rpi/awb.h
index 499b4519c..7869d630a 100644
--- a/src/ipa/rpi/controller/rpi/awb.h
+++ b/src/ipa/rpi/controller/rpi/awb.h
@@ -10,11 +10,14 @@ 
 #include <condition_variable>
 #include <thread>
 
+#include <libcamera/geometry.h>
+
 #include "../awb_algorithm.h"
-#include "../pwl.h"
 #include "../awb_status.h"
 #include "../statistics.h"
 
+#include "libipa/pwl.h"
+
 namespace RPiController {
 
 /* Control algorithm to perform AWB calculations. */
@@ -28,7 +31,7 @@  struct AwbMode {
 struct AwbPrior {
 	int read(const libcamera::YamlObject &params);
 	double lux; /* lux level */
-	Pwl prior; /* maps CT to prior log likelihood for this lux level */
+	libcamera::ipa::Pwl prior; /* maps CT to prior log likelihood for this lux level */
 };
 
 struct AwbConfig {
@@ -41,10 +44,10 @@  struct AwbConfig {
 	unsigned int convergenceFrames; /* approx number of frames to converge */
 	double speed; /* IIR filter speed applied to algorithm results */
 	bool fast; /* "fast" mode uses a 16x16 rather than 32x32 grid */
-	Pwl ctR; /* function maps CT to r (= R/G) */
-	Pwl ctB; /* function maps CT to b (= B/G) */
-	Pwl ctRInverse; /* inverse of ctR */
-	Pwl ctBInverse; /* inverse of ctB */
+	libcamera::ipa::Pwl ctR; /* function maps CT to r (= R/G) */
+	libcamera::ipa::Pwl ctB; /* function maps CT to b (= B/G) */
+	libcamera::ipa::Pwl ctRInverse; /* inverse of ctR */
+	libcamera::ipa::Pwl ctBInverse; /* inverse of ctB */
 	/* table of illuminant priors at different lux levels */
 	std::vector<AwbPrior> priors;
 	/* AWB "modes" (determines the search range) */
@@ -161,11 +164,11 @@  private:
 	void awbGrey();
 	void prepareStats();
 	double computeDelta2Sum(double gainR, double gainB);
-	Pwl interpolatePrior();
-	double coarseSearch(Pwl const &prior);
-	void fineSearch(double &t, double &r, double &b, Pwl const &prior);
+	libcamera::ipa::Pwl interpolatePrior();
+	double coarseSearch(libcamera::ipa::Pwl const &prior);
+	void fineSearch(double &t, double &r, double &b, libcamera::ipa::Pwl const &prior);
 	std::vector<RGB> zones_;
-	std::vector<Pwl::Point> points_;
+	std::vector<libcamera::PointF> points_;
 	/* manual r setting */
 	double manualR_;
 	/* manual b setting */
diff --git a/src/ipa/rpi/controller/rpi/ccm.cpp b/src/ipa/rpi/controller/rpi/ccm.cpp
index c55880296..3272a1416 100644
--- a/src/ipa/rpi/controller/rpi/ccm.cpp
+++ b/src/ipa/rpi/controller/rpi/ccm.cpp
@@ -71,7 +71,7 @@  int Ccm::read(const libcamera::YamlObject &params)
 	int ret;
 
 	if (params.contains("saturation")) {
-		ret = config_.saturation.read(params["saturation"]);
+		ret = config_.saturation.readYaml(params["saturation"]);
 		if (ret)
 			return ret;
 	}
@@ -172,7 +172,7 @@  void Ccm::prepare(Metadata *imageMetadata)
 	ccmStatus.saturation = saturation;
 	if (!config_.saturation.empty())
 		saturation *= config_.saturation.eval(
-			config_.saturation.domain().clip(lux.lux));
+			config_.saturation.domain().clamp(lux.lux));
 	ccm = applySaturation(ccm, saturation);
 	for (int j = 0; j < 3; j++)
 		for (int i = 0; i < 3; i++)
diff --git a/src/ipa/rpi/controller/rpi/ccm.h b/src/ipa/rpi/controller/rpi/ccm.h
index b3abeddf6..4e5b33fef 100644
--- a/src/ipa/rpi/controller/rpi/ccm.h
+++ b/src/ipa/rpi/controller/rpi/ccm.h
@@ -8,8 +8,9 @@ 
 
 #include <vector>
 
+#include <libipa/pwl.h>
+
 #include "../ccm_algorithm.h"
-#include "../pwl.h"
 
 namespace RPiController {
 
@@ -54,7 +55,7 @@  struct CtCcm {
 
 struct CcmConfig {
 	std::vector<CtCcm> ccms;
-	Pwl saturation;
+	libcamera::ipa::Pwl saturation;
 };
 
 class Ccm : public CcmAlgorithm
diff --git a/src/ipa/rpi/controller/rpi/contrast.cpp b/src/ipa/rpi/controller/rpi/contrast.cpp
index 9eef792d3..66871a61e 100644
--- a/src/ipa/rpi/controller/rpi/contrast.cpp
+++ b/src/ipa/rpi/controller/rpi/contrast.cpp
@@ -53,7 +53,7 @@  int Contrast::read(const libcamera::YamlObject &params)
 	config_.hiHistogram = params["hi_histogram"].get<double>(0.95);
 	config_.hiLevel = params["hi_level"].get<double>(0.95);
 	config_.hiMax = params["hi_max"].get<double>(2000);
-	return config_.gammaCurve.read(params["gamma_curve"]);
+	return config_.gammaCurve.readYaml(params["gamma_curve"]);
 }
 
 void Contrast::setBrightness(double brightness)
@@ -92,10 +92,10 @@  void Contrast::prepare(Metadata *imageMetadata)
 	imageMetadata->set("contrast.status", status_);
 }
 
-Pwl computeStretchCurve(Histogram const &histogram,
+ipa::Pwl computeStretchCurve(Histogram const &histogram,
 			ContrastConfig const &config)
 {
-	Pwl enhance;
+	ipa::Pwl enhance;
 	enhance.append(0, 0);
 	/*
 	 * If the start of the histogram is rather empty, try to pull it down a
@@ -136,10 +136,10 @@  Pwl computeStretchCurve(Histogram const &histogram,
 	return enhance;
 }
 
-Pwl applyManualContrast(Pwl const &gammaCurve, double brightness,
-			double contrast)
+ipa::Pwl applyManualContrast(ipa::Pwl const &gammaCurve, double brightness,
+			     double contrast)
 {
-	Pwl newGammaCurve;
+	ipa::Pwl newGammaCurve;
 	LOG(RPiContrast, Debug)
 		<< "Manual brightness " << brightness << " contrast " << contrast;
 	gammaCurve.map([&](double x, double y) {
@@ -160,7 +160,7 @@  void Contrast::process(StatisticsPtr &stats,
 	 * ways: 1. Adjust the gamma curve so as to pull the start of the
 	 * histogram down, and possibly push the end up.
 	 */
-	Pwl gammaCurve = config_.gammaCurve;
+	ipa::Pwl gammaCurve = config_.gammaCurve;
 	if (ceEnable_) {
 		if (config_.loMax != 0 || config_.hiMax != 0)
 			gammaCurve = computeStretchCurve(histogram, config_).compose(gammaCurve);
diff --git a/src/ipa/rpi/controller/rpi/contrast.h b/src/ipa/rpi/controller/rpi/contrast.h
index a9d9bbc99..c0f7db981 100644
--- a/src/ipa/rpi/controller/rpi/contrast.h
+++ b/src/ipa/rpi/controller/rpi/contrast.h
@@ -8,8 +8,9 @@ 
 
 #include <mutex>
 
+#include <libipa/pwl.h>
+
 #include "../contrast_algorithm.h"
-#include "../pwl.h"
 
 namespace RPiController {
 
@@ -26,7 +27,7 @@  struct ContrastConfig {
 	double hiHistogram;
 	double hiLevel;
 	double hiMax;
-	Pwl gammaCurve;
+	libcamera::ipa::Pwl gammaCurve;
 };
 
 class Contrast : public ContrastAlgorithm
diff --git a/src/ipa/rpi/controller/rpi/geq.cpp b/src/ipa/rpi/controller/rpi/geq.cpp
index fb539d1f2..c9c38ebff 100644
--- a/src/ipa/rpi/controller/rpi/geq.cpp
+++ b/src/ipa/rpi/controller/rpi/geq.cpp
@@ -9,7 +9,6 @@ 
 
 #include "../device_status.h"
 #include "../lux_status.h"
-#include "../pwl.h"
 
 #include "geq.h"
 
@@ -45,7 +44,7 @@  int Geq::read(const libcamera::YamlObject &params)
 	}
 
 	if (params.contains("strength")) {
-		int ret = config_.strength.read(params["strength"]);
+		int ret = config_.strength.readYaml(params["strength"]);
 		if (ret)
 			return ret;
 	}
@@ -67,7 +66,7 @@  void Geq::prepare(Metadata *imageMetadata)
 	GeqStatus geqStatus = {};
 	double strength = config_.strength.empty()
 			? 1.0
-			: config_.strength.eval(config_.strength.domain().clip(luxStatus.lux));
+			: config_.strength.eval(config_.strength.domain().clamp(luxStatus.lux));
 	strength *= deviceStatus.analogueGain;
 	double offset = config_.offset * strength;
 	double slope = config_.slope * strength;
diff --git a/src/ipa/rpi/controller/rpi/geq.h b/src/ipa/rpi/controller/rpi/geq.h
index 2c8400c2f..e8b9f4270 100644
--- a/src/ipa/rpi/controller/rpi/geq.h
+++ b/src/ipa/rpi/controller/rpi/geq.h
@@ -6,6 +6,8 @@ 
  */
 #pragma once
 
+#include <libipa/pwl.h>
+
 #include "../algorithm.h"
 #include "../geq_status.h"
 
@@ -16,7 +18,7 @@  namespace RPiController {
 struct GeqConfig {
 	uint16_t offset;
 	double slope;
-	Pwl strength; /* lux to strength factor */
+	libcamera::ipa::Pwl strength; /* lux to strength factor */
 };
 
 class Geq : public Algorithm
diff --git a/src/ipa/rpi/controller/rpi/hdr.cpp b/src/ipa/rpi/controller/rpi/hdr.cpp
index 34cf360e9..d533a4ea4 100644
--- a/src/ipa/rpi/controller/rpi/hdr.cpp
+++ b/src/ipa/rpi/controller/rpi/hdr.cpp
@@ -42,7 +42,7 @@  void HdrConfig::read(const libcamera::YamlObject &params, const std::string &mod
 
 	/* Lens shading related parameters. */
 	if (params.contains("spatial_gain_curve")) {
-		spatialGainCurve.read(params["spatial_gain_curve"]);
+		spatialGainCurve.readYaml(params["spatial_gain_curve"]);
 	} else if (params.contains("spatial_gain")) {
 		double spatialGain = params["spatial_gain"].get<double>(2.0);
 		spatialGainCurve.append(0.0, spatialGain);
@@ -66,7 +66,7 @@  void HdrConfig::read(const libcamera::YamlObject &params, const std::string &mod
 	iirStrength = params["iir_strength"].get<double>(8.0);
 	strength = params["strength"].get<double>(1.5);
 	if (tonemapEnable)
-		tonemap.read(params["tonemap"]);
+		tonemap.readYaml(params["tonemap"]);
 	speed = params["speed"].get<double>(1.0);
 	if (params.contains("hi_quantile_targets")) {
 		hiQuantileTargets = params["hi_quantile_targets"].getList<double>().value();
@@ -212,7 +212,7 @@  bool Hdr::updateTonemap([[maybe_unused]] StatisticsPtr &stats, HdrConfig &config
 	/* When there's a change of HDR mode we start over with a new tonemap curve. */
 	if (delayedStatus_.mode != previousMode_) {
 		previousMode_ = delayedStatus_.mode;
-		tonemap_ = Pwl();
+		tonemap_ = ipa::Pwl();
 	}
 
 	/* No tonemapping. No need to output a tonemap.status. */
@@ -275,7 +275,7 @@  bool Hdr::updateTonemap([[maybe_unused]] StatisticsPtr &stats, HdrConfig &config
 	double power = std::clamp(min_power, config.powerMin, config.powerMax);
 
 	/* Generate the tonemap, including the contrast adjustment factors. */
-	Pwl tonemap;
+	libcamera::ipa::Pwl tonemap;
 	tonemap.append(0, 0);
 	for (unsigned int i = 0; i <= 6; i++) {
 		double x = 1 << (i + 9); /* x loops from 512 to 32768 inclusive */
diff --git a/src/ipa/rpi/controller/rpi/hdr.h b/src/ipa/rpi/controller/rpi/hdr.h
index 9b7327f8b..5c2f3988d 100644
--- a/src/ipa/rpi/controller/rpi/hdr.h
+++ b/src/ipa/rpi/controller/rpi/hdr.h
@@ -12,9 +12,10 @@ 
 
 #include <libcamera/geometry.h>
 
+#include <libipa/pwl.h>
+
 #include "../hdr_algorithm.h"
 #include "../hdr_status.h"
-#include "../pwl.h"
 
 /* This is our implementation of an HDR algorithm. */
 
@@ -26,7 +27,7 @@  struct HdrConfig {
 	std::map<unsigned int, std::string> channelMap;
 
 	/* Lens shading related parameters. */
-	Pwl spatialGainCurve; /* Brightness to gain curve for different image regions. */
+	libcamera::ipa::Pwl spatialGainCurve; /* Brightness to gain curve for different image regions. */
 	unsigned int diffusion; /* How much to diffuse the gain spatially. */
 
 	/* Tonemap related parameters. */
@@ -35,7 +36,7 @@  struct HdrConfig {
 	double detailSlope;
 	double iirStrength;
 	double strength;
-	Pwl tonemap;
+	libcamera::ipa::Pwl tonemap;
 	/* These relate to adaptive tonemap calculation. */
 	double speed;
 	std::vector<double> hiQuantileTargets; /* quantiles to check for unsaturated images */
@@ -75,7 +76,7 @@  private:
 	HdrStatus status_; /* track the current HDR mode and channel */
 	HdrStatus delayedStatus_; /* track the delayed HDR mode and channel */
 	std::string previousMode_;
-	Pwl tonemap_;
+	libcamera::ipa::Pwl tonemap_;
 	libcamera::Size regions_; /* stats regions */
 	unsigned int numRegions_; /* total number of stats regions */
 	std::vector<double> gains_[2];
diff --git a/src/ipa/rpi/controller/rpi/tonemap.cpp b/src/ipa/rpi/controller/rpi/tonemap.cpp
index 0426e9722..2dc50dfc8 100644
--- a/src/ipa/rpi/controller/rpi/tonemap.cpp
+++ b/src/ipa/rpi/controller/rpi/tonemap.cpp
@@ -33,7 +33,7 @@  int Tonemap::read(const libcamera::YamlObject &params)
 	config_.detailSlope = params["detail_slope"].get<double>(0.1);
 	config_.iirStrength = params["iir_strength"].get<double>(1.0);
 	config_.strength = params["strength"].get<double>(1.0);
-	config_.tonemap.read(params["tone_curve"]);
+	config_.tonemap.readYaml(params["tone_curve"]);
 	return 0;
 }
 
diff --git a/src/ipa/rpi/controller/rpi/tonemap.h b/src/ipa/rpi/controller/rpi/tonemap.h
index f25aa47f8..ba0cf5c40 100644
--- a/src/ipa/rpi/controller/rpi/tonemap.h
+++ b/src/ipa/rpi/controller/rpi/tonemap.h
@@ -6,8 +6,9 @@ 
  */
 #pragma once
 
+#include <libipa/pwl.h>
+
 #include "algorithm.h"
-#include "pwl.h"
 
 namespace RPiController {
 
@@ -16,7 +17,7 @@  struct TonemapConfig {
 	double detailSlope;
 	double iirStrength;
 	double strength;
-	Pwl tonemap;
+	libcamera::ipa::Pwl tonemap;
 };
 
 class Tonemap : public Algorithm
diff --git a/src/ipa/rpi/controller/tonemap_status.h b/src/ipa/rpi/controller/tonemap_status.h
index 41a7bf2ff..0364ff66f 100644
--- a/src/ipa/rpi/controller/tonemap_status.h
+++ b/src/ipa/rpi/controller/tonemap_status.h
@@ -6,12 +6,12 @@ 
  */
 #pragma once
 
-#include "pwl.h"
+#include <libipa/pwl.h>
 
 struct TonemapStatus {
 	uint16_t detailConstant;
 	double detailSlope;
 	double iirStrength;
 	double strength;
-	RPiController::Pwl tonemap;
+	libcamera::ipa::Pwl tonemap;
 };