Patch Detail
Show a patch.
GET /api/patches/19736/?format=api
{ "id": 19736, "url": "https://patchwork.libcamera.org/api/patches/19736/?format=api", "web_url": "https://patchwork.libcamera.org/patch/19736/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/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": "<20240318104357.441724-2-mzamazal@redhat.com>", "date": "2024-03-18T10:43:57", "name": "[v3,1/1] apps: cam: Add support for pnm output format", "commit_ref": null, "pull_url": null, "state": "superseded", "archived": false, "hash": "9d4544593e52ce82426b8475b77e2eaca60c1c31", "submitter": { "id": 177, "url": "https://patchwork.libcamera.org/api/people/177/?format=api", "name": "Milan Zamazal", "email": "mzamazal@redhat.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/19736/mbox/", "series": [ { "id": 4228, "url": "https://patchwork.libcamera.org/api/series/4228/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=4228", "date": "2024-03-18T10:43:56", "name": "cam: Add support for pnm output format", "version": 3, "mbox": "https://patchwork.libcamera.org/series/4228/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/19736/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/19736/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 1EDBCC3272\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 18 Mar 2024 10:44:34 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id D689662C92;\n\tMon, 18 Mar 2024 11:44:33 +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 DBACD616EE\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 18 Mar 2024 11:44:31 +0100 (CET)", "from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com\n\t[66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS\n\t(version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id\n\tus-mta-658-4xjdWXwFP6KUrP9E9Qb59g-1; Mon, 18 Mar 2024 06:44:29 -0400", "from smtp.corp.redhat.com\n\t(int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2])\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 0304B185A781;\n\tMon, 18 Mar 2024 10:44:29 +0000 (UTC)", "from nuthatch.redhat.com (unknown [10.45.226.18])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 4238C40C6CB3;\n\tMon, 18 Mar 2024 10:44:28 +0000 (UTC)" ], "Authentication-Results": "lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=redhat.com header.i=@redhat.com\n\theader.b=\"B377PgJQ\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1710758670;\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=8AOZ9GtAlwJDM2dY5sQlgOjR1m4pq6nKl2BGLPBBrMc=;\n\tb=B377PgJQrdjQhZQ+zAfFm3L0cvEEIRbeOrhFjQ+yxQ3jg3AEFHvquTEOgFSpHQ9K/omGih\n\tVPbqKmmnPxqS2Myx8Bcx/PjLyKAvPjeF16DpzhuwGoZeiG344VZcm4GyWv7vWKWge68eKj\n\tq134OFBYVa/ERMtmN2EvrQ3f+4RXIro=", "X-MC-Unique": "4xjdWXwFP6KUrP9E9Qb59g-1", "From": "Milan Zamazal <mzamazal@redhat.com>", "To": "libcamera-devel@lists.libcamera.org", "Cc": "Milan Zamazal <mzamazal@redhat.com>,\n\tKieran Bingham <kieran.bingham@ideasonboard.com>", "Subject": "[PATCH v3 1/1] apps: cam: Add support for pnm output format", "Date": "Mon, 18 Mar 2024 11:43:57 +0100", "Message-ID": "<20240318104357.441724-2-mzamazal@redhat.com>", "In-Reply-To": "<20240318104357.441724-1-mzamazal@redhat.com>", "References": "<20240318104357.441724-1-mzamazal@redhat.com>", "MIME-Version": "1.0", "X-Scanned-By": "MIMEDefang 3.4.1 on 10.11.54.2", "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>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.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 | 20 +++++++++++++\n 5 files changed, 88 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..ac53719d 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));\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..9a63ec05\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 Span<uint8_t> &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.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..6c1e7967\n--- /dev/null\n+++ b/src/apps/common/pnm_writer.h\n@@ -0,0 +1,20 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2024, Red Hat, Inc.\n+ *\n+ * pnm_writer.h - PNM writer\n+ */\n+\n+#pragma once\n+\n+#include <libcamera/base/span.h>\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 libcamera::Span<uint8_t> &data);\n+};\n", "prefixes": [ "v3", "1/1" ] }