{"id":8620,"url":"https://patchwork.libcamera.org/api/1.1/patches/8620/?format=json","web_url":"https://patchwork.libcamera.org/patch/8620/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200704095914.17344-4-david.plowman@raspberrypi.com>","date":"2020-07-04T09:59:14","name":"[libcamera-devel,3/3] libcamera: qcam: Improve colour information in DNG files","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"e40c51f33789d795257f6eb6d9c8b7819ba54e27","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/1.1/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/8620/mbox/","series":[{"id":1082,"url":"https://patchwork.libcamera.org/api/1.1/series/1082/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1082","date":"2020-07-04T09:59:11","name":"ColourCorrectionMatrixControl","version":1,"mbox":"https://patchwork.libcamera.org/series/1082/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8620/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8620/checks/","tags":{},"headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 43C83BD790\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSat,  4 Jul 2020 09:59:30 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 07F3660DD3;\n\tSat,  4 Jul 2020 11:59:30 +0200 (CEST)","from mail-wm1-x342.google.com (mail-wm1-x342.google.com\n\t[IPv6:2a00:1450:4864:20::342])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id A14D360DF3\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  4 Jul 2020 11:59:27 +0200 (CEST)","by mail-wm1-x342.google.com with SMTP id l17so34215073wmj.0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 04 Jul 2020 02:59:27 -0700 (PDT)","from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72])\n\tby smtp.gmail.com with ESMTPSA id\n\tz10sm11477681wrm.21.2020.07.04.02.59.26\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tSat, 04 Jul 2020 02:59:26 -0700 (PDT)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (2048-bit key;\n\tunprotected) header.d=raspberrypi.com header.i=@raspberrypi.com\n\theader.b=\"Q/hhBLVl\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=raspberrypi.com; s=google;\n\th=from:to:cc:subject:date:message-id:in-reply-to:references\n\t:mime-version:content-transfer-encoding;\n\tbh=iY+GHfBzn33kHSONBRFpZ/briIH9MQ/8QlpghUiYqCk=;\n\tb=Q/hhBLVlBMIuxOMNRBysA43OEbsUx5wl6UaldaG8ImeBO3EUTxxJiph4ffrgSGvN32\n\tMrgWK82pebg24quB7eJVCRj21KO6D0MXOGNjKOX+D0+1YJDSX11zFiYa1qeXqM2JJiCA\n\txZYYhxQuQjPr13dAHxbrGZ2qqBO5nNSLJGHYayqSuu87Mfae9z9ynGprnFYbJqXeoiG9\n\tgZ9V72LdNm59qosyZ4o1NZ2tQnoA1uDGjtc01tnDXi7rDToESgu6sAC29PcYYnsBMrw1\n\t6FcBjJLm1HHWEBhwJKCrlVUN+u1uf70y4O+aIn1kHBe2qJogpRX0jJOFukbfJyhou+ll\n\tNR6Q==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n\td=1e100.net; s=20161025;\n\th=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to\n\t:references:mime-version:content-transfer-encoding;\n\tbh=iY+GHfBzn33kHSONBRFpZ/briIH9MQ/8QlpghUiYqCk=;\n\tb=i00bEqd+PafuTAaOQmm1xwZL8ox1cIXMruhU/BKlrWc0l9z+H7NhAUYZ1TYA24j1eE\n\tVReqBYHpJaqLmCpfkgFdBKtcCB0tMBxVSk7M+jqiMdL7NE38yuKHkxT91fhALrJMsmA1\n\tZxBZjq+iAxoQozE0yCEENSb7dbkqCmZMBCn8ij3LtYSkNXZM+IfXP+B0TmSXWsWlALNM\n\tyzkfxy9em/JJ1vu0rP6SZUS28y/L+tuVQSv58Fyexim4QP+BS89Jo/z0+xYjwJFzD1af\n\tLoHbmOWTIGxZ2nm/KDTG/pn+K4emB/vPQulO3O8/iAWue2gbIACfPjAEX7mSGqm0t7+L\n\tjYDg==","X-Gm-Message-State":"AOAM532vyfKX24Nt1NtmoxazOJsvfZ/eoohoCqo8gtslCUbqAMDBrN0h\n\tTuHL/3jL0V5lvoXwFN57MiM9gACrW/o=","X-Google-Smtp-Source":"ABdhPJxIwNObF395vUAUZfW3kBDCuG/LMeqmTwih/Rr2BkoPFIliOV5/jtI/gx7XULD4JXa8zReJgg==","X-Received":"by 2002:a1c:2543:: with SMTP id\n\tl64mr18043956wml.31.1593856766953; \n\tSat, 04 Jul 2020 02:59:26 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","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\n\tinformation in DNG files","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"This patch improves the colour information recorded in DNG files using\nthe ColourCorrectionMatrix metadata for the image. Note that we are\nnot supplying a full calibration using two illuminants, nonetheless\nthe single matrix here appears to be respected by a number of tools.\n\nSigned-off-by: David Plowman <david.plowman@raspberrypi.com>\n---\n src/qcam/dng_writer.cpp | 93 +++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 93 insertions(+)","diff":"diff --git a/src/qcam/dng_writer.cpp b/src/qcam/dng_writer.cpp\nindex 61505d3..222df9f 100644\n--- a/src/qcam/dng_writer.cpp\n+++ b/src/qcam/dng_writer.cpp\n@@ -34,6 +34,63 @@ struct FormatInfo {\n \t\t\t      unsigned int stride);\n };\n \n+struct Matrix {\n+\tMatrix(float m0, float m1, float m2,\n+\t       float m3, float m4, float m5,\n+\t       float m6, float m7, float m8)\n+\t{\n+\t\tm[0] = m0, m[1] = m1, m[2] = m2;\n+\t\tm[3] = m3, m[4] = m4, m[5] = m5;\n+\t\tm[6] = m6, m[7] = m7, m[8] = m8;\n+\t}\n+\tMatrix(float diag0, float diag1, float diag2)\n+\t\t: Matrix(diag0, 0, 0, 0, diag1, 0, 0, 0, diag2) {}\n+\tMatrix() {}\n+\tfloat m[9];\n+\tMatrix transpose() const\n+\t{\n+\t\treturn Matrix(m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8]);\n+\t}\n+\tMatrix cofactors() const\n+\t{\n+\t\treturn Matrix(m[4] * m[8] - m[5] * m[7],\n+\t\t\t      -(m[3] * m[8] - m[5] * m[6]),\n+\t\t\t      m[3] * m[7] - m[4] * m[6],\n+\t\t\t      -(m[1] * m[8] - m[2] * m[7]),\n+\t\t\t      m[0] * m[8] - m[2] * m[6],\n+\t\t\t      -(m[0] * m[7] - m[1] * m[6]),\n+\t\t\t      m[1] * m[5] - m[2] * m[4],\n+\t\t\t      -(m[0] * m[5] - m[2] * m[3]),\n+\t\t\t      m[0] * m[4] - m[1] * m[3]);\n+\t}\n+\tMatrix adjugate() const { return cofactors().transpose(); }\n+\tfloat determinant() const\n+\t{\n+\t\treturn (m[0] * (m[4] * m[8] - m[5] * m[7]) -\n+\t\t\tm[1] * (m[3] * m[8] - m[5] * m[6]) +\n+\t\t\tm[2] * (m[3] * m[7] - m[4] * m[6]));\n+\t}\n+\tMatrix inverse() const { return adjugate() * (1.0 / determinant()); }\n+\tMatrix operator*(Matrix const &other) const\n+\t{\n+\t\tMatrix result;\n+\t\tfor (int i = 0; i < 3; i++)\n+\t\t\tfor (int j = 0; j < 3; j++)\n+\t\t\t\tresult.m[i * 3 + j] =\n+\t\t\t\t\tm[i * 3 + 0] * other.m[0 + j] +\n+\t\t\t\t\tm[i * 3 + 1] * other.m[3 + j] +\n+\t\t\t\t\tm[i * 3 + 2] * other.m[6 + j];\n+\t\treturn result;\n+\t}\n+\tMatrix operator*(float const &f) const\n+\t{\n+\t\tMatrix result;\n+\t\tfor (int i = 0; i < 9; i++)\n+\t\t\tresult.m[i] = m[i] * f;\n+\t\treturn result;\n+\t}\n+};\n+\n void packScanlineSBGGR10P(void *output, const void *input, unsigned int width)\n {\n \tconst uint8_t *in = static_cast<const uint8_t *>(input);\n@@ -315,6 +372,42 @@ int DNGWriter::write(const char *filename, const Camera *camera,\n \tTIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n \tTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);\n \n+\t/*\n+\t * Fill in some reasonable colour information in the DNG. We supply\n+\t * the \"neutral\" colour values which determine the white balance, and the\n+\t * \"ColorMatrix1\" which converts XYZ to (un-white-balanced) camera RGB.\n+\t * Note that this is not a \"proper\" colour calibration for the DNG,\n+\t * nonetheless, many tools should be able to render the colours better.\n+\t */\n+\tfloat neutral[3] = { 1, 1, 1 };\n+\tMatrix wbGain(1, 1, 1);\n+\t/* From http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html */\n+\tMatrix rgb2xyz(0.4124564, 0.3575761, 0.1804375,\n+\t\t       0.2126729, 0.7151522, 0.0721750,\n+\t\t       0.0193339, 0.1191920, 0.9503041);\n+\tMatrix ccm(1, 1, 1);\n+\tconst double eps = 1e-2;\n+\n+\tif (metadata.contains(controls::ColourGains)) {\n+\t\tSpan<const float> colour_gains = metadata.get(controls::ColourGains);\n+\t\tif (colour_gains[0] > eps && colour_gains[1] > eps) {\n+\t\t\twbGain = Matrix(colour_gains[0], 1, colour_gains[1]);\n+\t\t\tneutral[0] = 1.0 / colour_gains[0]; /* red */\n+\t\t\tneutral[2] = 1.0 / colour_gains[1]; /* blue */\n+\t\t}\n+\t}\n+\tif (metadata.contains(controls::ColourCorrectionMatrix)) {\n+\t\tSpan<const float> m = metadata.get(controls::ColourCorrectionMatrix);\n+\t\tMatrix tmp = Matrix(m[0], m[1], m[2], m[3], m[4], m[5], m[6], m[7], m[8]);\n+\t\tif (tmp.determinant() > eps)\n+\t\t\tccm = tmp;\n+\t}\n+\t/* This is guaranteed to be invertible because all the bits in it are. */\n+\tMatrix colorMatrix1 = (rgb2xyz * ccm * wbGain).inverse();\n+\n+\tTIFFSetField(tif, TIFFTAG_COLORMATRIX1, 9, colorMatrix1.m);\n+\tTIFFSetField(tif, TIFFTAG_ASSHOTNEUTRAL, 3, neutral);\n+\n \t/*\n \t * Reserve space for the SubIFD and ExifIFD tags, pointing to the IFD\n \t * for the raw image and EXIF data respectively. The real offsets will\n","prefixes":["libcamera-devel","3/3"]}