{"id":3675,"url":"https://patchwork.libcamera.org/api/patches/3675/?format=json","web_url":"https://patchwork.libcamera.org/patch/3675/","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":"<20200502225704.2911-5-laurent.pinchart@ideasonboard.com>","date":"2020-05-02T22:57:03","name":"[libcamera-devel,4/5] qcam: dng_writer: Populate DNG tags from metadata","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"4e3109a570d7fd6215230fe13ca356d48d423c22","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3675/mbox/","series":[{"id":874,"url":"https://patchwork.libcamera.org/api/series/874/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=874","date":"2020-05-02T22:56:59","name":"qcam: Improve DNG writer","version":1,"mbox":"https://patchwork.libcamera.org/series/874/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3675/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3675/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 0D18D61618\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  3 May 2020 00:57:12 +0200 (CEST)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id A92D3521\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  3 May 2020 00:57:11 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"mbWhJPVY\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1588460231;\n\tbh=UuBQR8QUttSEpos4JV/OxaP/5df2hGZjORzutizPPaI=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=mbWhJPVY6zAmaHsT3BBvVzJrvOJ/MppNslea68MwJTPMXNgQ67DrH7IecjuJ1ZJN8\n\tVID4RWGq3j11AI6xR6DLDCEvQ/93RhHNisEak3DKR3DyNs1rXk3iHRtMC8Fr1T9WUW\n\tLy4mi9Cn+2DNoBSDksd2OuxmyPkL/XdZv8DbM2N0=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sun,  3 May 2020 01:57:03 +0300","Message-Id":"<20200502225704.2911-5-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.25.3","In-Reply-To":"<20200502225704.2911-1-laurent.pinchart@ideasonboard.com>","References":"<20200502225704.2911-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 4/5] qcam: dng_writer: Populate DNG tags\n\tfrom metadata","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>","X-List-Received-Date":"Sat, 02 May 2020 22:57:12 -0000"},"content":"Populate the DNG black level, ISO speed rating and exposure time from\nmetadata. The ISO speed rating and exposure time are standardized as\nEXIF tags, not TIFF tags, and require a separate EXIF IFD.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n src/qcam/dng_writer.cpp | 71 +++++++++++++++++++++++++++++++++++++++--\n 1 file changed, 68 insertions(+), 3 deletions(-)","diff":"diff --git a/src/qcam/dng_writer.cpp b/src/qcam/dng_writer.cpp\nindex e55985ba86e7..d04a8e161218 100644\n--- a/src/qcam/dng_writer.cpp\n+++ b/src/qcam/dng_writer.cpp\n@@ -7,11 +7,14 @@\n \n #include \"dng_writer.h\"\n \n+#include <algorithm>\n #include <iostream>\n #include <map>\n \n #include <tiffio.h>\n \n+#include <libcamera/control_ids.h>\n+\n using namespace libcamera;\n \n enum CFAPatternColour : uint8_t {\n@@ -153,6 +156,7 @@ int DNGWriter::write(const char *filename, const Camera *camera,\n \tuint8_t scanline[(config.size.width * info->bitsPerSample + 7) / 8];\n \n \ttoff_t rawIFDOffset = 0;\n+\ttoff_t exifIFDOffset = 0;\n \n \t/*\n \t * Start with a thumbnail in IFD 0 for compatibility with TIFF baseline\n@@ -185,10 +189,12 @@ int DNGWriter::write(const char *filename, const Camera *camera,\n \tTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);\n \n \t/*\n-\t * Reserve space for the SubIFD tag, pointing to the IFD for the raw\n-\t * image. The real offset will be set later.\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+\t * be set later.\n \t */\n \tTIFFSetField(tif, TIFFTAG_SUBIFD, 1, &rawIFDOffset);\n+\tTIFFSetField(tif, TIFFTAG_EXIFIFD, exifIFDOffset);\n \n \t/* Write the thumbnail. */\n \tconst uint8_t *row = static_cast<const uint8_t *>(data);\n@@ -230,7 +236,47 @@ int DNGWriter::write(const char *filename, const Camera *camera,\n \tTIFFSetField(tif, TIFFTAG_CFAPLANECOLOR, 3, cfaPlaneColor);\n \tTIFFSetField(tif, TIFFTAG_CFALAYOUT, 1);\n \n-\t/* \\todo Add more EXIF fields to output. */\n+\tconst uint16_t blackLevelRepeatDim[] = { 2, 2 };\n+\tfloat blackLevel[] = { 0.0f, 0.0f, 0.0f, 0.0f };\n+\tuint32_t whiteLevel = (1 << info->bitsPerSample) - 1;\n+\n+\tif (metadata.contains(controls::SensorBlackLevels)) {\n+\t\tSpan<const int32_t> levels = metadata.get(controls::SensorBlackLevels);\n+\n+\t\t/*\n+\t\t * The black levels control is specified in R, Gr, Gb, B order.\n+\t\t * Map it to the TIFF tag that is specified in CFA pattern\n+\t\t * order.\n+\t\t */\n+\t\tunsigned int green = (info->pattern[0] == CFAPatternRed ||\n+\t\t\t\t      info->pattern[1] == CFAPatternRed)\n+\t\t\t\t   ? 0 : 1;\n+\n+\t\tfor (unsigned int i = 0; i < 4; ++i) {\n+\t\t\tunsigned int level;\n+\n+\t\t\tswitch (info->pattern[i]) {\n+\t\t\tcase CFAPatternRed:\n+\t\t\t\tlevel = levels[0];\n+\t\t\t\tbreak;\n+\t\t\tcase CFAPatternGreen:\n+\t\t\t\tlevel = levels[green + 1];\n+\t\t\t\tgreen = (green + 1) % 2;\n+\t\t\t\tbreak;\n+\t\t\tcase CFAPatternBlue:\n+\t\t\tdefault:\n+\t\t\t\tlevel = levels[3];\n+\t\t\t\tbreak;\n+\t\t\t}\n+\n+\t\t\t/* Map the 16-bit value to the bits per sample range. */\n+\t\t\tblackLevel[i] = level >> (16 - info->bitsPerSample);\n+\t\t}\n+\t}\n+\n+\tTIFFSetField(tif, TIFFTAG_BLACKLEVELREPEATDIM, &blackLevelRepeatDim);\n+\tTIFFSetField(tif, TIFFTAG_BLACKLEVEL, 4, &blackLevel);\n+\tTIFFSetField(tif, TIFFTAG_WHITELEVEL, 1, &whiteLevel);\n \n \t/* Write RAW content. */\n \trow = static_cast<const uint8_t *>(data);\n@@ -252,9 +298,28 @@ int DNGWriter::write(const char *filename, const Camera *camera,\n \trawIFDOffset = TIFFCurrentDirOffset(tif);\n \tTIFFWriteDirectory(tif);\n \n+\t/* Create a new IFD for the EXIF data and fill it. */\n+\tTIFFCreateEXIFDirectory(tif);\n+\n+\tif (metadata.contains(controls::AnalogueGain)) {\n+\t\tfloat gain = metadata.get(controls::AnalogueGain);\n+\t\tuint16_t iso = std::min(std::max(gain * 100, 0.0f), 65535.0f);\n+\t\tTIFFSetField(tif, EXIFTAG_ISOSPEEDRATINGS, 1, &iso);\n+\t}\n+\n+\tif (metadata.contains(controls::ExposureTime)) {\n+\t\tfloat exposureTime = metadata.get(controls::ExposureTime) / 1e6;\n+\t\tTIFFSetField(tif, EXIFTAG_EXPOSURETIME, exposureTime);\n+\t}\n+\n+\tTIFFCheckpointDirectory(tif);\n+\texifIFDOffset = TIFFCurrentDirOffset(tif);\n+\tTIFFWriteDirectory(tif);\n+\n \t/* Update the IFD offsets and close the file. */\n \tTIFFSetDirectory(tif, 0);\n \tTIFFSetField(tif, TIFFTAG_SUBIFD, 1, &rawIFDOffset);\n+\tTIFFSetField(tif, TIFFTAG_EXIFIFD, exifIFDOffset);\n \tTIFFWriteDirectory(tif);\n \n \tTIFFClose(tif);\n","prefixes":["libcamera-devel","4/5"]}