From patchwork Sat Jul 4 09:59:14 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Plowman X-Patchwork-Id: 8620 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 43C83BD790 for ; Sat, 4 Jul 2020 09:59:30 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 07F3660DD3; Sat, 4 Jul 2020 11:59:30 +0200 (CEST) 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="Q/hhBLVl"; dkim-atps=neutral Received: from mail-wm1-x342.google.com (mail-wm1-x342.google.com [IPv6:2a00:1450:4864:20::342]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id A14D360DF3 for ; Sat, 4 Jul 2020 11:59:27 +0200 (CEST) Received: by mail-wm1-x342.google.com with SMTP id l17so34215073wmj.0 for ; Sat, 04 Jul 2020 02:59:27 -0700 (PDT) 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=iY+GHfBzn33kHSONBRFpZ/briIH9MQ/8QlpghUiYqCk=; b=Q/hhBLVlBMIuxOMNRBysA43OEbsUx5wl6UaldaG8ImeBO3EUTxxJiph4ffrgSGvN32 MrgWK82pebg24quB7eJVCRj21KO6D0MXOGNjKOX+D0+1YJDSX11zFiYa1qeXqM2JJiCA xZYYhxQuQjPr13dAHxbrGZ2qqBO5nNSLJGHYayqSuu87Mfae9z9ynGprnFYbJqXeoiG9 gZ9V72LdNm59qosyZ4o1NZ2tQnoA1uDGjtc01tnDXi7rDToESgu6sAC29PcYYnsBMrw1 6FcBjJLm1HHWEBhwJKCrlVUN+u1uf70y4O+aIn1kHBe2qJogpRX0jJOFukbfJyhou+ll NR6Q== 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=iY+GHfBzn33kHSONBRFpZ/briIH9MQ/8QlpghUiYqCk=; b=i00bEqd+PafuTAaOQmm1xwZL8ox1cIXMruhU/BKlrWc0l9z+H7NhAUYZ1TYA24j1eE VReqBYHpJaqLmCpfkgFdBKtcCB0tMBxVSk7M+jqiMdL7NE38yuKHkxT91fhALrJMsmA1 ZxBZjq+iAxoQozE0yCEENSb7dbkqCmZMBCn8ij3LtYSkNXZM+IfXP+B0TmSXWsWlALNM yzkfxy9em/JJ1vu0rP6SZUS28y/L+tuVQSv58Fyexim4QP+BS89Jo/z0+xYjwJFzD1af LoHbmOWTIGxZ2nm/KDTG/pn+K4emB/vPQulO3O8/iAWue2gbIACfPjAEX7mSGqm0t7+L jYDg== X-Gm-Message-State: AOAM532vyfKX24Nt1NtmoxazOJsvfZ/eoohoCqo8gtslCUbqAMDBrN0h TuHL/3jL0V5lvoXwFN57MiM9gACrW/o= X-Google-Smtp-Source: ABdhPJxIwNObF395vUAUZfW3kBDCuG/LMeqmTwih/Rr2BkoPFIliOV5/jtI/gx7XULD4JXa8zReJgg== X-Received: by 2002:a1c:2543:: with SMTP id l64mr18043956wml.31.1593856766953; Sat, 04 Jul 2020 02:59:26 -0700 (PDT) Received: from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72]) by smtp.gmail.com with ESMTPSA id z10sm11477681wrm.21.2020.07.04.02.59.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 04 Jul 2020 02:59:26 -0700 (PDT) From: David Plowman To: libcamera-devel@lists.libcamera.org Date: Sat, 4 Jul 2020 10:59:14 +0100 Message-Id: <20200704095914.17344-4-david.plowman@raspberrypi.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200704095914.17344-1-david.plowman@raspberrypi.com> References: <20200704095914.17344-1-david.plowman@raspberrypi.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 3/3] libcamera: qcam: Improve colour information in DNG files 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" 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 --- 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(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 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 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