From patchwork Fri Mar 22 19:50:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Milan Zamazal X-Patchwork-Id: 19798 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id 2AA70BD160 for ; Fri, 22 Mar 2024 19:51:09 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id D6E1263075; Fri, 22 Mar 2024 20:51:08 +0100 (CET) Authentication-Results: lancelot.ideasonboard.com; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="f+22tYOy"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id C47DC61C45 for ; Fri, 22 Mar 2024 20:51:06 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1711137065; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=d07stczum5vCHDq24XkUO+35VPXkSpsiBsWhPKPBfec=; b=f+22tYOymmyCdUEayO1LhNHPMLYG71xpihNc9E2zsDUJZAgpsCyh7D36SzGRaxnGlZCiIY 6VLy2wBObq0RddzP290DCbuQRZI7CM4sbV16j5DdYCdlcEL2n7N9udQt6jItEWeU3ppFIX a2oqW2ns6gvrG0A1b+Wq1N5U4O8z8kY= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-15-EasMW90qOoGwXm2iE0_tyA-1; Fri, 22 Mar 2024 15:51:02 -0400 X-MC-Unique: EasMW90qOoGwXm2iE0_tyA-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id D25CA3C02455; Fri, 22 Mar 2024 19:51:01 +0000 (UTC) Received: from nuthatch.redhat.com (unknown [10.45.224.17]) by smtp.corp.redhat.com (Postfix) with ESMTP id AF7842022C1D; Fri, 22 Mar 2024 19:51:00 +0000 (UTC) From: Milan Zamazal To: libcamera-devel@lists.libcamera.org Cc: Milan Zamazal , Kieran Bingham , Laurent Pinchart Subject: [PATCH v4 1/1] apps: cam: Add support for PPM output format Date: Fri, 22 Mar 2024 20:50:32 +0100 Message-ID: <20240322195032.1803017-2-mzamazal@redhat.com> In-Reply-To: <20240322195032.1803017-1-mzamazal@redhat.com> References: <20240322195032.1803017-1-mzamazal@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" When file output is requested from cam app, it simply dumps the processed data and it must be converted to a readable image format manually. Let's add support for PPM output file format to make files produced by cam directly readable by image display and processing software. For now, only BGR888 output format, which is the simplest one to use, is supported but nothing prevents adding support for other output formats if needed. Nevertheless, they would typically need byte reordering or other conversions for PPM file format. It may be better to find a way to dump the image data in other output formats directly using some of the already existing file formats or raw file format converters. Signed-off-by: Milan Zamazal Reviewed-by: Kieran Bingham Reviewed-by: Laurent Pinchart --- src/apps/cam/file_sink.cpp | 11 +++++++ src/apps/cam/main.cpp | 2 ++ src/apps/common/meson.build | 1 + src/apps/common/ppm_writer.cpp | 53 ++++++++++++++++++++++++++++++++++ src/apps/common/ppm_writer.h | 20 +++++++++++++ 5 files changed, 87 insertions(+) create mode 100644 src/apps/common/ppm_writer.cpp create mode 100644 src/apps/common/ppm_writer.h diff --git a/src/apps/cam/file_sink.cpp b/src/apps/cam/file_sink.cpp index dca350c4..906b50e6 100644 --- a/src/apps/cam/file_sink.cpp +++ b/src/apps/cam/file_sink.cpp @@ -17,6 +17,7 @@ #include "../common/dng_writer.h" #include "../common/image.h" +#include "../common/ppm_writer.h" #include "file_sink.h" @@ -76,6 +77,7 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer, #ifdef HAVE_TIFF bool dng = filename.find(".dng", filename.size() - 4) != std::string::npos; #endif /* HAVE_TIFF */ + bool ppm = filename.find(".ppm", filename.size() - 4) != std::string::npos; if (filename.empty() || filename.back() == '/') filename += "frame-#.bin"; @@ -102,6 +104,15 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer, return; } #endif /* HAVE_TIFF */ + if (ppm) { + ret = PPMWriter::write(filename.c_str(), stream->configuration(), + image->data(0)); + if (ret < 0) + std::cerr << "failed to write PPM file `" << filename + << "'" << std::endl; + + return; + } fd = open(filename.c_str(), O_CREAT | O_WRONLY | (pos == std::string::npos ? O_APPEND : O_TRUNC), diff --git a/src/apps/cam/main.cpp b/src/apps/cam/main.cpp index 179cc376..1aabee01 100644 --- a/src/apps/cam/main.cpp +++ b/src/apps/cam/main.cpp @@ -154,6 +154,8 @@ int CamApp::parseOptions(int argc, char *argv[]) "If the file name ends with '.dng', then the frame will be written to\n" "the output file(s) in DNG format.\n" #endif + "If the file name ends with '.ppm', then the frame will be written to\n" + "the output file(s) in PPM format.\n" "The default file name is 'frame-#.bin'.", "file", ArgumentOptional, "filename", false, OptCamera); diff --git a/src/apps/common/meson.build b/src/apps/common/meson.build index 479326cd..5b683390 100644 --- a/src/apps/common/meson.build +++ b/src/apps/common/meson.build @@ -3,6 +3,7 @@ apps_sources = files([ 'image.cpp', 'options.cpp', + 'ppm_writer.cpp', 'stream_options.cpp', ]) diff --git a/src/apps/common/ppm_writer.cpp b/src/apps/common/ppm_writer.cpp new file mode 100644 index 00000000..04a49499 --- /dev/null +++ b/src/apps/common/ppm_writer.cpp @@ -0,0 +1,53 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024 Red Hat, Inc. + * + * ppm_writer.cpp - PPM writer + */ + +#include +#include + +#include +#include + +#include "ppm_writer.h" + +using namespace libcamera; + +int PPMWriter::write(const char *filename, + const StreamConfiguration &config, + const Span &data) +{ + if (config.pixelFormat != formats::BGR888) { + std::cerr << "Only BGR888 output pixel format is supported (" + << config.pixelFormat << " requested)" << std::endl; + return -EINVAL; + } + + std::ofstream output(filename, std::ios::binary); + if (!output) { + std::cerr << "Failed to open ppm file: " << filename << std::endl; + return -EINVAL; + } + + output << "P6" << std::endl + << config.size.width << " " << config.size.height << std::endl + << "255" << std::endl; + if (!output) { + std::cerr << "Failed to write the file header" << std::endl; + return -EINVAL; + } + + const unsigned int rowLength = config.size.width * 3; + const char *row = reinterpret_cast(data.data()); + for (unsigned int y = 0; y < config.size.height; y++, row += config.stride) { + output.write(row, rowLength); + if (!output) { + std::cerr << "Failed to write image data at row " << y << std::endl; + return -EINVAL; + } + } + + return 0; +} diff --git a/src/apps/common/ppm_writer.h b/src/apps/common/ppm_writer.h new file mode 100644 index 00000000..4c38f5ce --- /dev/null +++ b/src/apps/common/ppm_writer.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +/* + * Copyright (C) 2024, Red Hat, Inc. + * + * ppm_writer.h - PPM writer + */ + +#pragma once + +#include + +#include + +class PPMWriter +{ +public: + static int write(const char *filename, + const libcamera::StreamConfiguration &config, + const libcamera::Span &data); +};