{"id":8978,"url":"https://patchwork.libcamera.org/api/patches/8978/?format=json","web_url":"https://patchwork.libcamera.org/patch/8978/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20200724145618.26304-4-david.plowman@raspberrypi.com>","date":"2020-07-24T14:56:18","name":"[libcamera-devel,v2,3/3] libcamera: qcam: Improve colour information in DNG files","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"654fe9f581b25278cad50c862bcd9964ddf69199","submitter":{"id":42,"url":"https://patchwork.libcamera.org/api/people/42/?format=json","name":"David Plowman","email":"david.plowman@raspberrypi.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/8978/mbox/","series":[{"id":1138,"url":"https://patchwork.libcamera.org/api/series/1138/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=1138","date":"2020-07-24T14:56:15","name":"ColourCorrectionMatrix control","version":2,"mbox":"https://patchwork.libcamera.org/series/1138/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/8978/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/8978/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 6E367BD86F\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 24 Jul 2020 14:56:42 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 3A5B261220;\n\tFri, 24 Jul 2020 16:56:42 +0200 (CEST)","from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com\n\t[IPv6:2a00:1450:4864:20::42c])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id DE1746123B\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Jul 2020 16:56:40 +0200 (CEST)","by mail-wr1-x42c.google.com with SMTP id r2so3503109wrs.8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Jul 2020 07:56:40 -0700 (PDT)","from pi4-davidp.lan (plowpeople3.plus.com. [80.229.223.72])\n\tby smtp.gmail.com with ESMTPSA id\n\th5sm2073156wrc.97.2020.07.24.07.56.39\n\t(version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n\tFri, 24 Jul 2020 07:56:39 -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=\"obYuQlm7\"; 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=9Qul+YVzNIOpRkBcJtc0uvzNviGiy+CJSxvosrLYLoU=;\n\tb=obYuQlm7xUY9SDaN87VRbdT17itdjBkJRtZdg9wR1o87qUa1grN7afY7EpwaDe4NLo\n\tEvbVp7WQQj4YawIbM3icRzkQH8i+vGl9VsCA1X7g/vATYpK2f2bgFVy6DmOReHhzJnPY\n\tOSU/QLkwAhOo45FmIAlZoZBYPIBUDJT9+QinhNTANLQ9Z96y5M7WPInRkl2Gdu/+0TEc\n\t9NIxhnHRtcdDxhWWNrza8XtohM/jhE6mEkQStgt+I+SFsyr6HS8+VleQuX/Pgwvum64Q\n\tj/dzdfYHM2ut4573d3N2erafNHmqvtSTGNuYR+Dvpcaad1JyvBkSKOubMRay4cN7DUfT\n\t7tJg==","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=9Qul+YVzNIOpRkBcJtc0uvzNviGiy+CJSxvosrLYLoU=;\n\tb=BMFoZOOUIFRJcct5bhtKE7ZiJhiJYCu/JV+w4K71HOcon52yGox4msf51npibxmY4Q\n\tzzH/LmEzfjHk0liTiB1ss6DwCkjV08EvJYsr08Lps/TMk3OR3tjEPeXwjoVU/03sbEEM\n\tdkSjlZs6UACkT9d1riMsCJyZj9OKjmtBiZowNq2wv4Gfdby1gPKgMAaSyx7dSW3w6I2t\n\tTFrgilC0oUzKlAgyEI3b2gNOGeE80Xc+UuP6yx29/Lg7fpMgTUauukzG86jHD428JL1Q\n\tIZsTJTc5gXdGOyH8iJyKqo0/boxbbVc08eIBGLj1OT23wq+jnGcxBrdKmNvzpp0AN9LM\n\t2yTA==","X-Gm-Message-State":"AOAM533Rk2AYP/X8fAEBQ+fjjQX7QMze5fc3YEbSNDmTtayEjZWtK7ap\n\tUj4RR/j+VVjW0YX7MEV6SRgFJ0W8j0IWkg==","X-Google-Smtp-Source":"ABdhPJzXHGLyODKv1dKbmBP3XAFG8nITlbQfouZg9HolBq8RxqvInUfuSE9LpH7IDtdVJq3Z6afA6g==","X-Received":"by 2002:adf:c386:: with SMTP id p6mr8926727wrf.344.1595602600146;\n\tFri, 24 Jul 2020 07:56:40 -0700 (PDT)","From":"David Plowman <david.plowman@raspberrypi.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Fri, 24 Jul 2020 15:56:18 +0100","Message-Id":"<20200724145618.26304-4-david.plowman@raspberrypi.com>","X-Mailer":"git-send-email 2.20.1","In-Reply-To":"<20200724145618.26304-1-david.plowman@raspberrypi.com>","References":"<20200724145618.26304-1-david.plowman@raspberrypi.com>","MIME-Version":"1.0","Subject":"[libcamera-devel] [PATCH v2 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 | 138 ++++++++++++++++++++++++++++++++++++++++\n 1 file changed, 138 insertions(+)","diff":"diff --git a/src/qcam/dng_writer.cpp b/src/qcam/dng_writer.cpp\nindex 61505d3..4f638ec 100644\n--- a/src/qcam/dng_writer.cpp\n+++ b/src/qcam/dng_writer.cpp\n@@ -34,6 +34,97 @@ struct FormatInfo {\n \t\t\t      unsigned int stride);\n };\n \n+struct Matrix3d {\n+\tMatrix3d()\n+\t{\n+\t}\n+\n+\tMatrix3d(float m0, float m1, float m2,\n+\t\t float m3, float m4, float m5,\n+\t\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+\n+\tMatrix3d(Span<const float> const &span)\n+\t\t: Matrix3d(span[0], span[1], span[2],\n+\t\t\t   span[3], span[4], span[5],\n+\t\t\t   span[6], span[7], span[8])\n+\t{\n+\t}\n+\n+\tstatic Matrix3d diag(float diag0, float diag1, float diag2)\n+\t{\n+\t\treturn Matrix3d(diag0, 0, 0, 0, diag1, 0, 0, 0, diag2);\n+\t}\n+\n+\tstatic Matrix3d identity()\n+\t{\n+\t\treturn Matrix3d(1, 0, 0, 0, 1, 0, 0, 0, 1);\n+\t}\n+\n+\tMatrix3d transpose() const\n+\t{\n+\t\treturn { m[0], m[3], m[6], m[1], m[4], m[7], m[2], m[5], m[8] };\n+\t}\n+\n+\tMatrix3d cofactors() const\n+\t{\n+\t\treturn { 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+\n+\tMatrix3d adjugate() const\n+\t{\n+\t\treturn cofactors().transpose();\n+\t}\n+\n+\tfloat determinant() const\n+\t{\n+\t\treturn m[0] * (m[4] * m[8] - m[5] * m[7]) -\n+\t\t       m[1] * (m[3] * m[8] - m[5] * m[6]) +\n+\t\t       m[2] * (m[3] * m[7] - m[4] * m[6]);\n+\t}\n+\n+\tMatrix3d inverse() const\n+\t{\n+\t\treturn adjugate() * (1.0 / determinant());\n+\t}\n+\n+\tMatrix3d operator*(Matrix3d const &other) const\n+\t{\n+\t\tMatrix3d result;\n+\t\tfor (unsigned int i = 0; i < 3; i++) {\n+\t\t\tfor (unsigned 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\t\t}\n+\t\t}\n+\t\treturn result;\n+\t}\n+\n+\tMatrix3d operator*(float f) const\n+\t{\n+\t\tMatrix3d result;\n+\t\tfor (unsigned int i = 0; i < 9; i++)\n+\t\t\tresult.m[i] = m[i] * f;\n+\t\treturn result;\n+\t}\n+\n+\tfloat m[9];\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 +406,53 @@ 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+\tMatrix3d wbGain = Matrix3d::identity();\n+\t/* From http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html */\n+\tconst Matrix3d rgb2xyz(0.4124564, 0.3575761, 0.1804375,\n+\t\t\t       0.2126729, 0.7151522, 0.0721750,\n+\t\t\t       0.0193339, 0.1191920, 0.9503041);\n+\tMatrix3d ccm = Matrix3d::identity();\n+\t/*\n+\t * Pick a reasonable number eps to protect against singularities. It\n+\t * should be comfortably larger than the point at which we run into\n+\t * numerical trouble, yet smaller than any plausible gain that we might\n+\t * apply to a colour, either explicitly or as part of the colour matrix.\n+\t */\n+\tconst double eps = 1e-2;\n+\n+\tif (metadata.contains(controls::ColourGains)) {\n+\t\tSpan<const float> const &colourGains = metadata.get(controls::ColourGains);\n+\t\tif (colourGains[0] > eps && colourGains[1] > eps) {\n+\t\t\twbGain = Matrix3d::diag(colourGains[0], 1, colourGains[1]);\n+\t\t\tneutral[0] = 1.0 / colourGains[0]; /* red */\n+\t\t\tneutral[2] = 1.0 / colourGains[1]; /* blue */\n+\t\t}\n+\t}\n+\tif (metadata.contains(controls::ColourCorrectionMatrix)) {\n+\t\tSpan<const float> const &coeffs = metadata.get(controls::ColourCorrectionMatrix);\n+\t\tMatrix3d ccm_supplied(coeffs);\n+\t\tif (ccm_supplied.determinant() > eps)\n+\t\t\tccm = ccm_supplied;\n+\t}\n+\n+\t/*\n+\t * rgb2xyz is known to be invertible, and we've ensured above that both\n+\t * the ccm and wbGain matrices are non-singular, so the product of all\n+\t * three is guaranteed to be invertible too.\n+\t */\n+\tMatrix3d 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","v2","3/3"]}