[libcamera-devel] [PATCH 3/3] libcamera: qcam: Improve colour information in DNG files

David Plowman david.plowman at raspberrypi.com
Sat Jul 4 11:59:14 CEST 2020


This patch improves the colour information recorded in DNG files using
the ColourCorrectionMatrix metadata for the image. Note that we are
not supplying a full calibration using two illuminants, nonetheless
the single matrix here appears to be respected by a number of tools.

Signed-off-by: David Plowman <david.plowman at raspberrypi.com>
---
 src/qcam/dng_writer.cpp | 93 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 93 insertions(+)

diff --git a/src/qcam/dng_writer.cpp b/src/qcam/dng_writer.cpp
index 61505d3..222df9f 100644
--- a/src/qcam/dng_writer.cpp
+++ b/src/qcam/dng_writer.cpp
@@ -34,6 +34,63 @@ struct FormatInfo {
 			      unsigned int stride);
 };
 
+struct Matrix {
+	Matrix(float m0, float m1, float m2,
+	       float m3, float m4, float m5,
+	       float m6, float m7, float m8)
+	{
+		m[0] = m0, m[1] = m1, m[2] = m2;
+		m[3] = m3, m[4] = m4, m[5] = m5;
+		m[6] = m6, m[7] = m7, m[8] = m8;
+	}
+	Matrix(float diag0, float diag1, float diag2)
+		: Matrix(diag0, 0, 0, 0, diag1, 0, 0, 0, diag2) {}
+	Matrix() {}
+	float m[9];
+	Matrix transpose() const
+	{
+		return Matrix(m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8]);
+	}
+	Matrix cofactors() const
+	{
+		return Matrix(m[4] * m[8] - m[5] * m[7],
+			      -(m[3] * m[8] - m[5] * m[6]),
+			      m[3] * m[7] - m[4] * m[6],
+			      -(m[1] * m[8] - m[2] * m[7]),
+			      m[0] * m[8] - m[2] * m[6],
+			      -(m[0] * m[7] - m[1] * m[6]),
+			      m[1] * m[5] - m[2] * m[4],
+			      -(m[0] * m[5] - m[2] * m[3]),
+			      m[0] * m[4] - m[1] * m[3]);
+	}
+	Matrix adjugate() const { return cofactors().transpose(); }
+	float determinant() const
+	{
+		return (m[0] * (m[4] * m[8] - m[5] * m[7]) -
+			m[1] * (m[3] * m[8] - m[5] * m[6]) +
+			m[2] * (m[3] * m[7] - m[4] * m[6]));
+	}
+	Matrix inverse() const { return adjugate() * (1.0 / determinant()); }
+	Matrix operator*(Matrix const &other) const
+	{
+		Matrix result;
+		for (int i = 0; i < 3; i++)
+			for (int j = 0; j < 3; j++)
+				result.m[i * 3 + j] =
+					m[i * 3 + 0] * other.m[0 + j] +
+					m[i * 3 + 1] * other.m[3 + j] +
+					m[i * 3 + 2] * other.m[6 + j];
+		return result;
+	}
+	Matrix operator*(float const &f) const
+	{
+		Matrix result;
+		for (int i = 0; i < 9; i++)
+			result.m[i] = m[i] * f;
+		return result;
+	}
+};
+
 void packScanlineSBGGR10P(void *output, const void *input, unsigned int width)
 {
 	const uint8_t *in = static_cast<const uint8_t *>(input);
@@ -315,6 +372,42 @@ int DNGWriter::write(const char *filename, const Camera *camera,
 	TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
 	TIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);
 
+	/*
+	 * Fill in some reasonable colour information in the DNG. We supply
+	 * the "neutral" colour values which determine the white balance, and the
+	 * "ColorMatrix1" which converts XYZ to (un-white-balanced) camera RGB.
+	 * Note that this is not a "proper" colour calibration for the DNG,
+	 * nonetheless, many tools should be able to render the colours better.
+	 */
+	float neutral[3] = { 1, 1, 1 };
+	Matrix wbGain(1, 1, 1);
+	/* From http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html */
+	Matrix rgb2xyz(0.4124564, 0.3575761, 0.1804375,
+		       0.2126729, 0.7151522, 0.0721750,
+		       0.0193339, 0.1191920, 0.9503041);
+	Matrix ccm(1, 1, 1);
+	const double eps = 1e-2;
+
+	if (metadata.contains(controls::ColourGains)) {
+		Span<const float> colour_gains = metadata.get(controls::ColourGains);
+		if (colour_gains[0] > eps && colour_gains[1] > eps) {
+			wbGain = Matrix(colour_gains[0], 1, colour_gains[1]);
+			neutral[0] = 1.0 / colour_gains[0]; /* red */
+			neutral[2] = 1.0 / colour_gains[1]; /* blue */
+		}
+	}
+	if (metadata.contains(controls::ColourCorrectionMatrix)) {
+		Span<const float> m = metadata.get(controls::ColourCorrectionMatrix);
+		Matrix tmp = Matrix(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);
+		if (tmp.determinant() > eps)
+			ccm = tmp;
+	}
+	/* This is guaranteed to be invertible because all the bits in it are. */
+	Matrix colorMatrix1 = (rgb2xyz * ccm * wbGain).inverse();
+
+	TIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colorMatrix1.m);
+	TIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);
+
 	/*
 	 * Reserve space for the SubIFD and ExifIFD tags, pointing to the IFD
 	 * for the raw image and EXIF data respectively. The real offsets will
-- 
2.20.1



More information about the libcamera-devel mailing list