Show a patch.

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

{
    "id": 19686,
    "url": "https://patchwork.libcamera.org/api/1.1/patches/19686/?format=api",
    "web_url": "https://patchwork.libcamera.org/patch/19686/",
    "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": "<20240312154146.1135416-2-mzamazal@redhat.com>",
    "date": "2024-03-12T15:41:46",
    "name": "[v2,1/1] apps: cam: Add support for pnm output format",
    "commit_ref": null,
    "pull_url": null,
    "state": "superseded",
    "archived": false,
    "hash": "80ad5642e5fbc70dfffa908a69854e92da77e1a0",
    "submitter": {
        "id": 177,
        "url": "https://patchwork.libcamera.org/api/1.1/people/177/?format=api",
        "name": "Milan Zamazal",
        "email": "mzamazal@redhat.com"
    },
    "delegate": null,
    "mbox": "https://patchwork.libcamera.org/patch/19686/mbox/",
    "series": [
        {
            "id": 4217,
            "url": "https://patchwork.libcamera.org/api/1.1/series/4217/?format=api",
            "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4217",
            "date": "2024-03-12T15:41:45",
            "name": "cam: Add support for pnm output format",
            "version": 2,
            "mbox": "https://patchwork.libcamera.org/series/4217/mbox/"
        }
    ],
    "comments": "https://patchwork.libcamera.org/api/patches/19686/comments/",
    "check": "pending",
    "checks": "https://patchwork.libcamera.org/api/patches/19686/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 56FFDBD160\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 12 Mar 2024 15:42:46 +0000 (UTC)",
            "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 1CC0762C85;\n\tTue, 12 Mar 2024 16:42:46 +0100 (CET)",
            "from us-smtp-delivery-124.mimecast.com\n\t(us-smtp-delivery-124.mimecast.com [170.10.133.124])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 05B6A6295D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Mar 2024 16:42:43 +0100 (CET)",
            "from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73])\n\tby relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3,\n\tcipher=TLS_AES_256_GCM_SHA384) id us-mta-482-AFQtogffNE-Yj10Z9m_oxw-1;\n\tTue, 12 Mar 2024 11:42:40 -0400",
            "from smtp.corp.redhat.com\n\t(int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\tkey-exchange X25519 server-signature RSA-PSS (2048 bits)\n\tserver-digest SHA256) (No client certificate requested)\n\tby mimecast-mx02.redhat.com (Postfix) with ESMTPS id 94ED629AC027\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 12 Mar 2024 15:42:40 +0000 (UTC)",
            "from nuthatch.brq.redhat.com (unknown [10.43.17.39])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 20C44492BC9;\n\tTue, 12 Mar 2024 15:42:40 +0000 (UTC)"
        ],
        "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"XSGObq0f\"; dkim-atps=neutral",
        "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1710258162;\n\th=from:from:reply-to:subject:subject:date:date:message-id:message-id:\n\tto:to:cc:cc:mime-version:mime-version:content-type:content-type:\n\tcontent-transfer-encoding:content-transfer-encoding:\n\tin-reply-to:in-reply-to:references:references;\n\tbh=jPxuKtxgiTUk3e9BLIHt9REaktcdZcb9LvsUh/cW5cY=;\n\tb=XSGObq0f16+xNbgl//cnVgSA5xldNuh4QA8rHW6bF0rElrWtcA8Zx8fNM+ObkY00mfRYrZ\n\tJo45LomG2qaYMoVKoUum0P1P8gUyj+6rSlUDVCTQcDWT5/gItVKT9eaSlayrGjkl0Y4rVZ\n\tYNdZgZqnTeJtNnSW/vrMNcAhvHl6mUs=",
        "X-MC-Unique": "AFQtogffNE-Yj10Z9m_oxw-1",
        "From": "Milan Zamazal <mzamazal@redhat.com>",
        "To": "libcamera-devel@lists.libcamera.org",
        "Subject": "[PATCH v2 1/1] apps: cam: Add support for pnm output format",
        "Date": "Tue, 12 Mar 2024 16:41:46 +0100",
        "Message-ID": "<20240312154146.1135416-2-mzamazal@redhat.com>",
        "In-Reply-To": "<20240312154146.1135416-1-mzamazal@redhat.com>",
        "References": "<20240312154146.1135416-1-mzamazal@redhat.com>",
        "MIME-Version": "1.0",
        "X-Scanned-By": "MIMEDefang 3.4.1 on 10.11.54.9",
        "X-Mimecast-Spam-Score": "0",
        "X-Mimecast-Originator": "redhat.com",
        "Content-Transfer-Encoding": "8bit",
        "Content-Type": "text/plain; charset=\"US-ASCII\"; x-default=true",
        "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>",
        "Errors-To": "libcamera-devel-bounces@lists.libcamera.org",
        "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"
    },
    "content": "When file output is requested from cam app, it simply dumps the processed data\nand it must be converted to a readable image format manually.  Let's add support\nfor pnm output file format to make files produced by cam directly readable by\nimage display and processing software.\n\nFor now, only BGR888 output format, which is the simplest one to use, is\nsupported but nothing prevents adding support for other output formats if\nneeded.\n\nSigned-off-by: Milan Zamazal <mzamazal@redhat.com>\n---\n src/apps/cam/file_sink.cpp     | 11 +++++++\n src/apps/cam/main.cpp          |  2 ++\n src/apps/common/meson.build    |  1 +\n src/apps/common/pnm_writer.cpp | 54 ++++++++++++++++++++++++++++++++++\n src/apps/common/pnm_writer.h   | 18 ++++++++++++\n 5 files changed, 86 insertions(+)\n create mode 100644 src/apps/common/pnm_writer.cpp\n create mode 100644 src/apps/common/pnm_writer.h",
    "diff": "diff --git a/src/apps/cam/file_sink.cpp b/src/apps/cam/file_sink.cpp\nindex dca350c4..3db59925 100644\n--- a/src/apps/cam/file_sink.cpp\n+++ b/src/apps/cam/file_sink.cpp\n@@ -16,6 +16,7 @@\n #include <libcamera/camera.h>\n \n #include \"../common/dng_writer.h\"\n+#include \"../common/pnm_writer.h\"\n #include \"../common/image.h\"\n \n #include \"file_sink.h\"\n@@ -73,6 +74,7 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer,\n \tif (!pattern_.empty())\n \t\tfilename = pattern_;\n \n+\tbool pnm = filename.find(\".pnm\", filename.size() - 4) != std::string::npos;\n #ifdef HAVE_TIFF\n \tbool dng = filename.find(\".dng\", filename.size() - 4) != std::string::npos;\n #endif /* HAVE_TIFF */\n@@ -90,6 +92,15 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer,\n \n \tImage *image = mappedBuffers_[buffer].get();\n \n+\tif (pnm) {\n+\t\tret = PNMWriter::write(filename.c_str(), stream->configuration(),\n+\t\t\t\t       image->data(0).data());\n+\t\tif (ret < 0)\n+\t\t\tstd::cerr << \"failed to write PNM file `\" << filename\n+\t\t\t\t  << \"'\" << std::endl;\n+\n+\t\treturn;\n+\t}\n #ifdef HAVE_TIFF\n \tif (dng) {\n \t\tret = DNGWriter::write(filename.c_str(), camera_,\ndiff --git a/src/apps/cam/main.cpp b/src/apps/cam/main.cpp\nindex 179cc376..cdaa0baa 100644\n--- a/src/apps/cam/main.cpp\n+++ b/src/apps/cam/main.cpp\n@@ -150,6 +150,8 @@ int CamApp::parseOptions(int argc, char *argv[])\n \t\t\t \"to write files, using the default file name. Otherwise it sets the\\n\"\n \t\t\t \"full file path and name. The first '#' character in the file name\\n\"\n \t\t\t \"is expanded to the camera index, stream name and frame sequence number.\\n\"\n+\t\t\t \"If the file name ends with '.pnm', then the frame will be written to\\n\"\n+\t\t\t \"the output file(s) in PNM format.\\n\"\n #ifdef HAVE_TIFF\n \t\t\t \"If the file name ends with '.dng', then the frame will be written to\\n\"\n \t\t\t \"the output file(s) in DNG format.\\n\"\ndiff --git a/src/apps/common/meson.build b/src/apps/common/meson.build\nindex 479326cd..47b7e41a 100644\n--- a/src/apps/common/meson.build\n+++ b/src/apps/common/meson.build\n@@ -3,6 +3,7 @@\n apps_sources = files([\n     'image.cpp',\n     'options.cpp',\n+    'pnm_writer.cpp',\n     'stream_options.cpp',\n ])\n \ndiff --git a/src/apps/common/pnm_writer.cpp b/src/apps/common/pnm_writer.cpp\nnew file mode 100644\nindex 00000000..b505f97e\n--- /dev/null\n+++ b/src/apps/common/pnm_writer.cpp\n@@ -0,0 +1,54 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024 Red Hat, Inc.\n+ *\n+ * pnm_writer.cpp - PNM writer\n+ */\n+\n+#include \"pnm_writer.h\"\n+\n+#include <fstream>\n+#include <iostream>\n+\n+#include <libcamera/formats.h>\n+#include <libcamera/pixel_format.h>\n+\n+using namespace libcamera;\n+\n+int PNMWriter::write(const char *filename,\n+\t\t     const StreamConfiguration &config,\n+\t\t     const void *data)\n+{\n+\tif (config.pixelFormat != formats::BGR888) {\n+\t\tstd::cerr << \"Only BGR888 output pixel format is supported (\"\n+\t\t\t  << config.pixelFormat << \" requested)\" << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tstd::ofstream output(filename, std::ios::binary);\n+\tif (!output) {\n+\t\tstd::cerr << \"Failed to open pnm file: \" << filename << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\toutput << \"P6\" << std::endl\n+\t       << config.size.width << \" \" << config.size.height << std::endl\n+\t       << \"255\" << std::endl;\n+\tif (!output) {\n+\t\tstd::cerr << \"Failed to write the file header\" << std::endl;\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tconst unsigned int rowLength = config.size.width * 3;\n+\tconst unsigned int paddedRowLength = config.stride;\n+\tconst char *row = reinterpret_cast<const char *>(data);\n+\tfor (unsigned int y = 0; y < config.size.height; y++, row += paddedRowLength) {\n+\t\toutput.write(row, rowLength);\n+\t\tif (!output) {\n+\t\t\tstd::cerr << \"Failed to write image data at row \" << y << std::endl;\n+\t\t\treturn -EINVAL;\n+\t\t}\n+\t}\n+\n+\treturn 0;\n+}\ndiff --git a/src/apps/common/pnm_writer.h b/src/apps/common/pnm_writer.h\nnew file mode 100644\nindex 00000000..a0ae4587\n--- /dev/null\n+++ b/src/apps/common/pnm_writer.h\n@@ -0,0 +1,18 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2020, Raspberry Pi Ltd\n+ *\n+ * pnm_writer.h - PNM writer\n+ */\n+\n+#pragma once\n+\n+#include <libcamera/stream.h>\n+\n+class PNMWriter\n+{\n+public:\n+\tstatic int write(const char *filename,\n+\t\t\t const libcamera::StreamConfiguration &config,\n+\t\t\t const void *data);\n+};\n",
    "prefixes": [
        "v2",
        "1/1"
    ]
}