[libcamera-devel] [PATCH v2 4/6] libcamera: PixelFormatInfo: Add functions bytesPerLine and imageSize

Paul Elder paul.elder at ideasonboard.com
Tue Jun 30 16:58:06 CEST 2020


Add member functions to PixelFormatInfo for calculating bytes-per-line
and image size. This will simplify existing code that calculates these
things.

Signed-off-by: Paul Elder <paul.elder at ideasonboard.com>

---
Changes in v2:
- make these functions const
- add documentation
- inline DIV_ROUND_UP
---
 include/libcamera/internal/formats.h |  3 +++
 src/libcamera/formats.cpp            | 27 +++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/include/libcamera/internal/formats.h b/include/libcamera/internal/formats.h
index 3c7440a..af0f1ac 100644
--- a/include/libcamera/internal/formats.h
+++ b/include/libcamera/internal/formats.h
@@ -46,6 +46,9 @@ public:
 	static const PixelFormatInfo &info(const PixelFormat &format);
 	static const PixelFormatInfo &info(const V4L2PixelFormat &format);
 
+	unsigned int bytesPerLine(unsigned int width) const;
+	unsigned int imageSize(const Size &size) const;
+
 	const char *name;
 	PixelFormat format;
 	V4L2PixelFormat v4l2Format;
diff --git a/src/libcamera/formats.cpp b/src/libcamera/formats.cpp
index 4d18825..698c905 100644
--- a/src/libcamera/formats.cpp
+++ b/src/libcamera/formats.cpp
@@ -693,4 +693,31 @@ const PixelFormatInfo &PixelFormatInfo::info(const V4L2PixelFormat &format)
 	return info->second;
 }
 
+/**
+ * \brief Compute the bytes per line
+ * \param[in] width The width of the line, in pixels
+ * \return The number of bytes necessary to store the line, or 0 if the
+ * PixelFormatInfo instance not valid
+ */
+unsigned int PixelFormatInfo::bytesPerLine(unsigned int width) const
+{
+	if (!isValid())
+		return 0;
+
+	/* ceil(width / pixelsPerGroup) * bytesPerGroup */
+	return ((width + pixelsPerGroup - 1) / pixelsPerGroup) * bytesPerGroup;
+
+}
+
+/**
+ * \brief Compute the bytes necessary to store the frame
+ * \param[in] size The size of the frame, in pixels
+ * \return The number of bytes necessary to store the frame, or 0 if the
+ * PixelFormatInfo instance is not valid
+ */
+unsigned int PixelFormatInfo::imageSize(const Size &size) const
+{
+	return size.height * bytesPerLine(size.width);
+}
+
 } /* namespace libcamera */
-- 
2.27.0



More information about the libcamera-devel mailing list