From 666f17affcad23118e110909331b499411b57562 Mon Sep 17 00:00:00 2001 From: Milan Zamazal Date: Fri, 22 Mar 2024 20:50:32 +0100 Subject: apps: cam: Add support for PPM output format 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 Signed-off-by: Laurent Pinchart --- src/apps/common/meson.build | 1 + src/apps/common/ppm_writer.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++ src/apps/common/ppm_writer.h | 20 ++++++++++++++++ 3 files changed, 74 insertions(+) create mode 100644 src/apps/common/ppm_writer.cpp create mode 100644 src/apps/common/ppm_writer.h (limited to 'src/apps/common') 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..a8ccf67a --- /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 "ppm_writer.h" + +#include +#include + +#include +#include + +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); +}; -- cgit v1.2.1