Show a patch.

GET /api/1.1/patches/3663/?format=api
HTTP 200 OK
Allow: GET, PUT, PATCH, HEAD, OPTIONS
Content-Type: application/json
Vary: Accept

{
    "id": 3663,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/3663/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/3663/",
    "project": {
        "id": 1,
        "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api",
        "name": "libcamera",
        "link_name": "libcamera",
        "list_id": "libcamera_core",
        "list_email": "libcamera-devel@lists.libcamera.org",
        "web_url": "",
        "scm_url": "",
        "webscm_url": ""
    },
    "msgid": "<20200502121356.1045727-3-niklas.soderlund@ragnatech.se>",
    "date": "2020-05-02T12:13:55",
    "name": "[libcamera-devel,v4,2/3] qcam: Add DNGWriter",
    "commit_ref": null,
    "pull_url": null,
    "state": "accepted",
    "archived": false,
    "hash": "259b2e1a76166022dbec445df06944234f0efb97",
    "submitter": {
        "id": 5,
        "url": "https://patchwork.libcamera.org/api/1.1/people/5/?format=api",
        "name": "Niklas Söderlund",
        "email": "niklas.soderlund@ragnatech.se"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/3663/mbox/",
    "series": [
        {
            "id": 870,
            "url": "https://patchwork.libcamera.org/api/1.1/series/870/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=870",
            "date": "2020-05-02T12:13:53",
            "name": "qcam: Add RAW capture support",
            "version": 4,
            "mbox": "https://patchwork.libcamera.org/series/870/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/3663/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/3663/checks/",
    "tags": {},
    "headers": {
        "Return-Path": "<niklas.soderlund@ragnatech.se>",
        "Received": [
            "from vsp-unauthed02.binero.net (vsp-unauthed02.binero.net\n\t[195.74.38.227])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 35D5B6162D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat,  2 May 2020 14:14:06 +0200 (CEST)",
            "from bismarck.berto.se (p4fca2392.dip0.t-ipconnect.de\n\t[79.202.35.146]) by bin-vsp-out-03.atm.binero.net (Halon) with ESMTPA\n\tid 689a42a4-8c6e-11ea-89d0-0050569116f7;\n\tSat, 02 May 2020 14:14:01 +0200 (CEST)"
        ],
        "X-Halon-ID": "689a42a4-8c6e-11ea-89d0-0050569116f7",
        "Authorized-sender": "niklas@soderlund.pp.se",
        "From": "=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Date": "Sat,  2 May 2020 14:13:55 +0200",
        "Message-Id": "<20200502121356.1045727-3-niklas.soderlund@ragnatech.se>",
        "X-Mailer": "git-send-email 2.26.2",
        "In-Reply-To": "<20200502121356.1045727-1-niklas.soderlund@ragnatech.se>",
        "References": "<20200502121356.1045727-1-niklas.soderlund@ragnatech.se>",
        "MIME-Version": "1.0",
        "Content-Type": "text/plain; charset=UTF-8",
        "Content-Transfer-Encoding": "8bit",
        "Subject": "[libcamera-devel] [PATCH v4 2/3] qcam: Add DNGWriter",
        "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 12:14:07 -0000"
    },
    "content": "Add an initial DNG file writer. The writer can only deal with a small\nset of pixel formats. The generated file is consumable by standard\ntools. The writer needs to be extended to write more metadata to the\ngenerated file.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n* Changes since v2\n- Mark input arg as const\n- Fold getFormatInfo() into only caller\n- Male DNGWriter::write() static and take filename as argument\n- Make DNGWriter::write() take StreamConfiguraiton instead of Stream\n- Optimize scanline loop\n- Make libtiff dependecy optional\n- Add 12 byte formats from Laurent\n- Reformat table from Laurent\n\n* Changes since v3\n- Make data and buffer arguments const for DNGWriter::write()\n- Fix memory size of uint8_t scanline[].\n---\n src/qcam/dng_writer.cpp | 165 ++++++++++++++++++++++++++++++++++++++++\n src/qcam/dng_writer.h   |  28 +++++++\n src/qcam/meson.build    |  20 ++++-\n 3 files changed, 210 insertions(+), 3 deletions(-)\n create mode 100644 src/qcam/dng_writer.cpp\n create mode 100644 src/qcam/dng_writer.h",
    "diff": "diff --git a/src/qcam/dng_writer.cpp b/src/qcam/dng_writer.cpp\nnew file mode 100644\nindex 0000000000000000..08078369529bb780\n--- /dev/null\n+++ b/src/qcam/dng_writer.cpp\n@@ -0,0 +1,165 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.\n+ *\n+ * dng_writer.cpp - DNG writer\n+ */\n+\n+#include \"dng_writer.h\"\n+\n+#include <iostream>\n+#include <map>\n+\n+#include <tiffio.h>\n+\n+using namespace libcamera;\n+\n+enum CFAPatternColour : uint8_t {\n+\tCFAPatternRed = 0,\n+\tCFAPatternGreen = 1,\n+\tCFAPatternBlue = 2,\n+};\n+\n+struct FormatInfo {\n+\tuint8_t bitsPerSample;\n+\tCFAPatternColour pattern[4];\n+\tvoid (*packScanline)(void *, const void *, unsigned int);\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+\tuint8_t *out = static_cast<uint8_t *>(output);\n+\n+\t/* \\todo Can this be made more efficient? */\n+\tfor (unsigned int x = 0; x < width; x += 4) {\n+\t\t*out++ = in[0];\n+\t\t*out++ = (in[4] & 0x03) << 6 | in[1] >> 2;\n+\t\t*out++ = (in[1] & 0x03) << 6 | (in[4] & 0x0c) << 2 | in[2] >> 4;\n+\t\t*out++ = (in[2] & 0x0f) << 4 | (in[4] & 0x30) >> 2 | in[3] >> 6;\n+\t\t*out++ = (in[3] & 0x3f) << 2 | (in[4] & 0xc0) >> 6;\n+\t\tin += 5;\n+\t}\n+}\n+\n+void packScanlineSBGGR12P(void *output, const void *input, unsigned int width)\n+{\n+\tconst uint8_t *in = static_cast<const uint8_t *>(input);\n+\tuint8_t *out = static_cast<uint8_t *>(output);\n+\n+\t/* \\todo: Can this be made more efficient? */\n+\tfor (unsigned int i = 0; i < width; i += 2) {\n+\t\t*out++ = in[0];\n+\t\t*out++ = (in[2] & 0x0f) << 4 | in[1] >> 4;\n+\t\t*out++ = (in[1] & 0x0f) << 4 | in[2] >> 4;\n+\t\tin += 3;\n+\t}\n+}\n+\n+static const std::map<PixelFormat, FormatInfo> formatInfo = {\n+\t{ PixelFormat(DRM_FORMAT_SBGGR10, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 10,\n+\t\t.pattern = { CFAPatternBlue, CFAPatternGreen, CFAPatternGreen, CFAPatternRed },\n+\t\t.packScanline = packScanlineSBGGR10P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SGBRG10, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 10,\n+\t\t.pattern = { CFAPatternGreen, CFAPatternBlue, CFAPatternRed, CFAPatternGreen },\n+\t\tpackScanlineSBGGR10P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SGRBG10, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 10,\n+\t\t.pattern = { CFAPatternGreen, CFAPatternRed, CFAPatternBlue, CFAPatternGreen },\n+\t\t.packScanline = packScanlineSBGGR10P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SRGGB10, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 10,\n+\t\t.pattern = { CFAPatternRed, CFAPatternGreen, CFAPatternGreen, CFAPatternBlue },\n+\t\t.packScanline = packScanlineSBGGR10P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SBGGR12, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 12,\n+\t\t.pattern = { CFAPatternBlue, CFAPatternGreen, CFAPatternGreen, CFAPatternRed },\n+\t\t.packScanline = packScanlineSBGGR12P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SGBRG12, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 12,\n+\t\t.pattern = { CFAPatternGreen, CFAPatternBlue, CFAPatternRed, CFAPatternGreen },\n+\t\t.packScanline = packScanlineSBGGR12P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SGRBG12, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 12,\n+\t\t.pattern = { CFAPatternGreen, CFAPatternRed, CFAPatternBlue, CFAPatternGreen },\n+\t\t.packScanline = packScanlineSBGGR12P,\n+\t} },\n+\t{ PixelFormat(DRM_FORMAT_SRGGB12, MIPI_FORMAT_MOD_CSI2_PACKED), {\n+\t\t.bitsPerSample = 12,\n+\t\t.pattern = { CFAPatternRed, CFAPatternGreen, CFAPatternGreen, CFAPatternBlue },\n+\t\t.packScanline = packScanlineSBGGR12P,\n+\t} },\n+};\n+\n+int DNGWriter::write(const char *filename, const Camera *camera,\n+\t\t     const StreamConfiguration &config,\n+\t\t     const FrameBuffer *buffer, const void *data)\n+{\n+\tconst auto it = formatInfo.find(config.pixelFormat);\n+\tif (it == formatInfo.cend()) {\n+\t\tstd::cerr << \"Unsupported pixel format\" << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\tconst FormatInfo *info = &it->second;\n+\n+\tTIFF *tif = TIFFOpen(filename, \"w\");\n+\tif (!tif) {\n+\t\tstd::cerr << \"Failed to open tiff file\" << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tconst uint8_t version[] = { 1, 2, 0, 0 };\n+\tconst uint16_t cfa_repeatpatterndim[] = { 2, 2 };\n+\tTIFFSetField(tif, TIFFTAG_DNGVERSION, version);\n+\tTIFFSetField(tif, TIFFTAG_DNGBACKWARDVERSION, version);\n+\tTIFFSetField(tif, TIFFTAG_SUBFILETYPE, 0);\n+\tTIFFSetField(tif, TIFFTAG_IMAGEWIDTH, config.size.width);\n+\tTIFFSetField(tif, TIFFTAG_IMAGELENGTH, config.size.height);\n+\tTIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, info->bitsPerSample);\n+\tTIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);\n+\tTIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_CFA);\n+\tTIFFSetField(tif, TIFFTAG_FILLORDER, FILLORDER_MSB2LSB);\n+\tTIFFSetField(tif, TIFFTAG_MAKE, \"libcamera\");\n+\tTIFFSetField(tif, TIFFTAG_MODEL, camera->name().c_str());\n+\tTIFFSetField(tif, TIFFTAG_UNIQUECAMERAMODEL, camera->name().c_str());\n+\tTIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);\n+\tTIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);\n+\tTIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);\n+\tTIFFSetField(tif, TIFFTAG_SOFTWARE, \"qcam\");\n+\tTIFFSetField(tif, TIFFTAG_SAMPLEFORMAT, SAMPLEFORMAT_UINT);\n+\tTIFFSetField(tif, TIFFTAG_CFAREPEATPATTERNDIM, cfa_repeatpatterndim);\n+\tTIFFSetField(tif, TIFFTAG_CFAPATTERN, info->pattern);\n+\tTIFFSetField(tif, TIFFTAG_CFAPLANECOLOR, 3, \"\\00\\01\\02\");\n+\tTIFFSetField(tif, TIFFTAG_CFALAYOUT, 1);\n+\n+\t/* \\todo Add more EXIF fields to output. */\n+\n+\t/* Write RAW content. */\n+\tconst uint8_t *row = static_cast<const uint8_t *>(data);\n+\tuint8_t scanline[(config.size.width * info->bitsPerSample + 7) / 8];\n+\tfor (unsigned int y = 0; y < config.size.height; y++) {\n+\t\tinfo->packScanline(&scanline, row, config.size.width);\n+\n+\t\tif (TIFFWriteScanline(tif, &scanline, y, 0) != 1) {\n+\t\t\tstd::cerr << \"Failed to write scanline\" << std::endl;\n+\t\t\tTIFFClose(tif);\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\n+\t\trow += config.stride;\n+\t}\n+\n+\tTIFFWriteDirectory(tif);\n+\n+\tTIFFClose(tif);\n+\n+\treturn 0;\n+}\ndiff --git a/src/qcam/dng_writer.h b/src/qcam/dng_writer.h\nnew file mode 100644\nindex 0000000000000000..4dd904db4ce68c22\n--- /dev/null\n+++ b/src/qcam/dng_writer.h\n@@ -0,0 +1,28 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi (Trading) Ltd.\n+ *\n+ * dng_writer.h - DNG writer\n+ */\n+#ifndef __LIBCAMERA_DNG_WRITER_H__\n+#define __LIBCAMERA_DNG_WRITER_H__\n+\n+#ifdef HAVE_TIFF\n+#define HAVE_DNG\n+\n+#include <libcamera/buffer.h>\n+#include <libcamera/camera.h>\n+#include <libcamera/stream.h>\n+\n+using namespace libcamera;\n+\n+class DNGWriter\n+{\n+public:\n+\tstatic int write(const char *filename, const Camera *camera,\n+\t\t\t const StreamConfiguration &config,\n+\t\t\t const FrameBuffer *buffer, const void *data);\n+};\n+#endif /* HAVE_DNG */\n+\n+#endif /* __LIBCAMERA_DNG_WRITER_H__ */\ndiff --git a/src/qcam/meson.build b/src/qcam/meson.build\nindex 895264be4a3388f4..949ef6149c8a187e 100644\n--- a/src/qcam/meson.build\n+++ b/src/qcam/meson.build\n@@ -1,9 +1,9 @@\n qcam_sources = files([\n+    '../cam/options.cpp',\n+    '../cam/stream_options.cpp',\n     'format_converter.cpp',\n     'main.cpp',\n     'main_window.cpp',\n-    '../cam/options.cpp',\n-    '../cam/stream_options.cpp',\n     'viewfinder.cpp',\n ])\n \n@@ -23,8 +23,22 @@ qt5_dep = dependency('qt5',\n                      required : false)\n \n if qt5_dep.found()\n+    qcam_deps = [\n+        libcamera_dep,\n+        qt5_dep,\n+    ]\n+\n     qt5_cpp_args = [ '-DQT_NO_KEYWORDS' ]\n \n+    tiff_dep = dependency('libtiff-4', required : false)\n+    if tiff_dep.found()\n+        qt5_cpp_args += [ '-DHAVE_TIFF' ]\n+        qcam_deps += [ tiff_dep ]\n+        qcam_sources += files([\n+            'dng_writer.cpp',\n+        ])\n+    endif\n+\n     # gcc 9 introduced a deprecated-copy warning that is triggered by Qt until\n     # Qt 5.13. clang 10 introduced the same warning, but detects more issues\n     # that are not fixed in Qt yet. Disable the warning manually in both cases.\n@@ -40,6 +54,6 @@ if qt5_dep.found()\n \n     qcam  = executable('qcam', qcam_sources, resources,\n                        install : true,\n-                       dependencies : [libcamera_dep, qt5_dep],\n+                       dependencies : qcam_deps,\n                        cpp_args : qt5_cpp_args)\n endif\n",
    "prefixes": [
        "libcamera-devel",
        "v4",
        "2/3"
    ]
}