{"id":19546,"url":"https://patchwork.libcamera.org/api/patches/19546/?format=json","web_url":"https://patchwork.libcamera.org/patch/19546/","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":"<20240227114815.565807-1-mzamazal@redhat.com>","date":"2024-02-27T11:48:15","name":"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/people/177/?format=json","name":"Milan Zamazal","email":"mzamazal@redhat.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/19546/mbox/","series":[{"id":4186,"url":"https://patchwork.libcamera.org/api/series/4186/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4186","date":"2024-02-27T11:48:15","name":"apps: cam: Add support for pnm output format","version":1,"mbox":"https://patchwork.libcamera.org/series/4186/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/19546/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/19546/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 A9B41BD80A\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 27 Feb 2024 11:50:10 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 7AC8B62865;\n\tTue, 27 Feb 2024 12:50:09 +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 06912627FC\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Feb 2024 12:50:07 +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-526-dPwuH2_qM7SICeh28e-4kA-1; Tue, 27 Feb 2024 06:50:05 -0500","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 EB864811E81\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 27 Feb 2024 11:50:04 +0000 (UTC)","from nuthatch.brq.redhat.com (unknown [10.43.17.39])\n\tby smtp.corp.redhat.com (Postfix) with ESMTP id 76F25492BC7;\n\tTue, 27 Feb 2024 11:50:04 +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=\"TZ4fL0AM\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;\n\ts=mimecast20190719; t=1709034606;\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\tbh=jPxuKtxgiTUk3e9BLIHt9REaktcdZcb9LvsUh/cW5cY=;\n\tb=TZ4fL0AMjg+tgrF3yMcYiblAG0rCzQgzC+PHp2Ywn5eMxbuE9yR3Hmu9E4pq1rPzQ8BKJy\n\t9u9JKlPL+I1WDm3ITdsHiu6D6d+fmlMrNmeXuNZRwlX43+vxiLUqFuJtqfNw+ZGLEP0SB/\n\tugtSY9sxmYumbiWg3sT6rQzCUZ5ZauU=","X-MC-Unique":"dPwuH2_qM7SICeh28e-4kA-1","From":"Milan Zamazal <mzamazal@redhat.com>","To":"libcamera-devel@lists.libcamera.org","Subject":"[PATCH] apps: cam: Add support for pnm output format","Date":"Tue, 27 Feb 2024 12:48:15 +0100","Message-ID":"<20240227114815.565807-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":[]}