[v4,6/9] software-isp: Map input crops to native sensor coordinates
diff mbox series

Message ID 20261029132631.370507-7-halfline@gmail.com
State Superseded
Headers show
Series
  • software-isp: Align metering and report the configured crop
Related show

Commit Message

Ray Strode Oct. 29, 2026, 1:26 p.m. UTC
From: Ray Strode <rstrode@redhat.com>

This commit adds the coordinate conversion needed to report a debayer crop
as ScalerCrop metadata.

Debayer crops use captured-buffer pixels. ScalerCrop uses physical pixel
array coordinates in native sensor pixels, so sensor binning, analog crop
offsets and sensor transforms must be accounted for.

The conversion reverses the sensor transform and maps the input rectangle
into the analog crop. Integer coordinates and dimensions follow the
truncation used by the existing rectangle transformation.

Signed-off-by: Ray Strode <rstrode@redhat.com>
---
 .../internal/software_isp/debayer_geometry.h  | 25 +++++++++++++++++++
 1 file changed, 25 insertions(+)

Patch
diff mbox series

diff --git a/include/libcamera/internal/software_isp/debayer_geometry.h b/include/libcamera/internal/software_isp/debayer_geometry.h
index 8812a3cf..bf6c6771 100644
--- a/include/libcamera/internal/software_isp/debayer_geometry.h
+++ b/include/libcamera/internal/software_isp/debayer_geometry.h
@@ -1,23 +1,48 @@ 
 /* SPDX-License-Identifier: LGPL-2.1-or-later */
 /*
  * Copyright (C) 2026, Red Hat Inc.
  *
  * Geometry for scaling and cropping debayered images
  */
 
 #pragma once
 
+#include <utility>
 
 #include <libcamera/geometry.h>
+#include <libcamera/transform.h>
 
 namespace libcamera {
 
 /* Return the visible sensor rectangle, before statistics Bayer alignment. */
 inline Rectangle debayerCrop(const Size &inputSize, const Size &nativeSize,
 			     const Size &outputSize)
 {
 	const Size visibleSize = nativeSize.boundedToAspectRatio(outputSize);
 	return visibleSize.centeredTo(Rectangle(inputSize).center());
 }
 
+/*
+ * Map an image crop to physical pixel-array coordinates in native sensor pixels.
+ * The input must cover the whole analog crop. Undo the sensor transform
+ * before accounting for binning and the analog crop offset. Integer
+ * coordinates and dimensions are truncated as in transformedBetween().
+ */
+inline Rectangle debayerCropToSensor(Rectangle crop, Size inputSize,
+				     const Rectangle &analogCrop,
+				     Transform transform)
+{
+	if (!!(transform & Transform::Transpose)) {
+		std::swap(crop.x, crop.y);
+		std::swap(crop.width, crop.height);
+		std::swap(inputSize.width, inputSize.height);
+	}
+	if (!!(transform & Transform::HFlip))
+		crop.x = inputSize.width - crop.x - crop.width;
+	if (!!(transform & Transform::VFlip))
+		crop.y = inputSize.height - crop.y - crop.height;
+
+	return crop.transformedBetween(Rectangle(inputSize), analogCrop);
+}
+
 } /* namespace libcamera */