[libcamera-devel,03/21] qcam: Use QSize through the code base

Message ID 20200323142205.28342-4-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • qcam: Bypass format conversion when not required
Related show

Commit Message

Laurent Pinchart March 23, 2020, 2:21 p.m. UTC
Qt has a QSize class to store sizes. Use it to replace width and height
where applicable.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/qcam/format_converter.cpp |  6 +++---
 src/qcam/format_converter.h   |  5 +++--
 src/qcam/main_window.cpp      |  4 ++--
 src/qcam/viewfinder.cpp       | 13 ++++++-------
 src/qcam/viewfinder.h         | 11 +++++------
 5 files changed, 19 insertions(+), 20 deletions(-)

Comments

Kieran Bingham March 23, 2020, 2:44 p.m. UTC | #1
Hi Laurent,

On 23/03/2020 14:21, Laurent Pinchart wrote:
> Qt has a QSize class to store sizes. Use it to replace width and height
> where applicable.
> 

Nice :-)

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/qcam/format_converter.cpp |  6 +++---
>  src/qcam/format_converter.h   |  5 +++--
>  src/qcam/main_window.cpp      |  4 ++--
>  src/qcam/viewfinder.cpp       | 13 ++++++-------
>  src/qcam/viewfinder.h         | 11 +++++------
>  5 files changed, 19 insertions(+), 20 deletions(-)
> 
> diff --git a/src/qcam/format_converter.cpp b/src/qcam/format_converter.cpp
> index d8962a28c06c..bf887ad446eb 100644
> --- a/src/qcam/format_converter.cpp
> +++ b/src/qcam/format_converter.cpp
> @@ -26,7 +26,7 @@
>  #endif
>  
>  int FormatConverter::configure(const libcamera::PixelFormat &format,
> -			       unsigned int width, unsigned int height)
> +			       const QSize &size)
>  {
>  	switch (format) {
>  	case DRM_FORMAT_NV12:
> @@ -139,8 +139,8 @@ int FormatConverter::configure(const libcamera::PixelFormat &format,
>  	};
>  
>  	format_ = format;
> -	width_ = width;
> -	height_ = height;
> +	width_ = size.width();
> +	height_ = size.height();
>  
>  	return 0;
>  }
> diff --git a/src/qcam/format_converter.h b/src/qcam/format_converter.h
> index 96bde2384ddf..5e28adf0ef63 100644
> --- a/src/qcam/format_converter.h
> +++ b/src/qcam/format_converter.h
> @@ -9,6 +9,8 @@
>  
>  #include <stddef.h>
>  
> +#include <QSize>
> +
>  #include <libcamera/pixelformats.h>
>  
>  class QImage;
> @@ -16,8 +18,7 @@ class QImage;
>  class FormatConverter
>  {
>  public:
> -	int configure(const libcamera::PixelFormat &format, unsigned int width,
> -		      unsigned int height);
> +	int configure(const libcamera::PixelFormat &format, const QSize &size);
>  
>  	void convert(const unsigned char *src, size_t size, QImage *dst);
>  
> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
> index 66aaf40c5d45..e0668176e427 100644
> --- a/src/qcam/main_window.cpp
> +++ b/src/qcam/main_window.cpp
> @@ -232,8 +232,8 @@ int MainWindow::startCapture()
>  	}
>  
>  	Stream *stream = cfg.stream();
> -	ret = viewfinder_->setFormat(cfg.pixelFormat, cfg.size.width,
> -				     cfg.size.height);
> +	ret = viewfinder_->setFormat(cfg.pixelFormat,
> +				     QSize(cfg.size.width, cfg.size.height));
>  	if (ret < 0) {
>  		std::cout << "Failed to set viewfinder format" << std::endl;
>  		return ret;
> diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp
> index f4602f07c5d2..066ac605e7b4 100644
> --- a/src/qcam/viewfinder.cpp
> +++ b/src/qcam/viewfinder.cpp
> @@ -15,7 +15,7 @@
>  #include "format_converter.h"
>  
>  ViewFinder::ViewFinder(QWidget *parent)
> -	: QWidget(parent), format_(0), width_(0), height_(0), image_(nullptr)
> +	: QWidget(parent), format_(0), image_(nullptr)
>  {
>  }
>  
> @@ -46,20 +46,19 @@ QImage ViewFinder::getCurrentImage()
>  }
>  
>  int ViewFinder::setFormat(const libcamera::PixelFormat &format,
> -			  unsigned int width, unsigned int height)
> +			  const QSize &size)
>  {
>  	int ret;
>  
> -	ret = converter_.configure(format, width, height);
> +	ret = converter_.configure(format, size);
>  	if (ret < 0)
>  		return ret;
>  
>  	format_ = format;
> -	width_ = width;
> -	height_ = height;
> +	size_ = size;
>  
>  	delete image_;
> -	image_ = new QImage(width, height, QImage::Format_RGB32);
> +	image_ = new QImage(size_, QImage::Format_RGB32);
>  
>  	updateGeometry();
>  	return 0;
> @@ -73,5 +72,5 @@ void ViewFinder::paintEvent(QPaintEvent *)
>  
>  QSize ViewFinder::sizeHint() const
>  {
> -	return image_ ? image_->size() : QSize(640, 480);
> +	return size_.isValid() ? size_ : QSize(640, 480);
>  }
> diff --git a/src/qcam/viewfinder.h b/src/qcam/viewfinder.h
> index 0549f038edd6..a019c3a470ea 100644
> --- a/src/qcam/viewfinder.h
> +++ b/src/qcam/viewfinder.h
> @@ -8,6 +8,7 @@
>  #define __QCAM_VIEWFINDER_H__
>  
>  #include <QMutex>
> +#include <QSize>
>  #include <QWidget>
>  
>  #include <libcamera/pixelformats.h>
> @@ -22,8 +23,7 @@ public:
>  	ViewFinder(QWidget *parent);
>  	~ViewFinder();
>  
> -	int setFormat(const libcamera::PixelFormat &format, unsigned int width,
> -		      unsigned int height);
> +	int setFormat(const libcamera::PixelFormat &format, const QSize &size);
>  	void display(const unsigned char *rgb, size_t size);
>  
>  	QImage getCurrentImage();
> @@ -33,12 +33,11 @@ protected:
>  	QSize sizeHint() const override;
>  
>  private:
> -	libcamera::PixelFormat format_;
> -	unsigned int width_;
> -	unsigned int height_;
> -
>  	FormatConverter converter_;
>  
> +	libcamera::PixelFormat format_;
> +	QSize size_;
> +
>  	QImage *image_;
>  	QMutex mutex_; /* Prevent concurrent access to image_ */
>  };
>

Patch

diff --git a/src/qcam/format_converter.cpp b/src/qcam/format_converter.cpp
index d8962a28c06c..bf887ad446eb 100644
--- a/src/qcam/format_converter.cpp
+++ b/src/qcam/format_converter.cpp
@@ -26,7 +26,7 @@ 
 #endif
 
 int FormatConverter::configure(const libcamera::PixelFormat &format,
-			       unsigned int width, unsigned int height)
+			       const QSize &size)
 {
 	switch (format) {
 	case DRM_FORMAT_NV12:
@@ -139,8 +139,8 @@  int FormatConverter::configure(const libcamera::PixelFormat &format,
 	};
 
 	format_ = format;
-	width_ = width;
-	height_ = height;
+	width_ = size.width();
+	height_ = size.height();
 
 	return 0;
 }
diff --git a/src/qcam/format_converter.h b/src/qcam/format_converter.h
index 96bde2384ddf..5e28adf0ef63 100644
--- a/src/qcam/format_converter.h
+++ b/src/qcam/format_converter.h
@@ -9,6 +9,8 @@ 
 
 #include <stddef.h>
 
+#include <QSize>
+
 #include <libcamera/pixelformats.h>
 
 class QImage;
@@ -16,8 +18,7 @@  class QImage;
 class FormatConverter
 {
 public:
-	int configure(const libcamera::PixelFormat &format, unsigned int width,
-		      unsigned int height);
+	int configure(const libcamera::PixelFormat &format, const QSize &size);
 
 	void convert(const unsigned char *src, size_t size, QImage *dst);
 
diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp
index 66aaf40c5d45..e0668176e427 100644
--- a/src/qcam/main_window.cpp
+++ b/src/qcam/main_window.cpp
@@ -232,8 +232,8 @@  int MainWindow::startCapture()
 	}
 
 	Stream *stream = cfg.stream();
-	ret = viewfinder_->setFormat(cfg.pixelFormat, cfg.size.width,
-				     cfg.size.height);
+	ret = viewfinder_->setFormat(cfg.pixelFormat,
+				     QSize(cfg.size.width, cfg.size.height));
 	if (ret < 0) {
 		std::cout << "Failed to set viewfinder format" << std::endl;
 		return ret;
diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp
index f4602f07c5d2..066ac605e7b4 100644
--- a/src/qcam/viewfinder.cpp
+++ b/src/qcam/viewfinder.cpp
@@ -15,7 +15,7 @@ 
 #include "format_converter.h"
 
 ViewFinder::ViewFinder(QWidget *parent)
-	: QWidget(parent), format_(0), width_(0), height_(0), image_(nullptr)
+	: QWidget(parent), format_(0), image_(nullptr)
 {
 }
 
@@ -46,20 +46,19 @@  QImage ViewFinder::getCurrentImage()
 }
 
 int ViewFinder::setFormat(const libcamera::PixelFormat &format,
-			  unsigned int width, unsigned int height)
+			  const QSize &size)
 {
 	int ret;
 
-	ret = converter_.configure(format, width, height);
+	ret = converter_.configure(format, size);
 	if (ret < 0)
 		return ret;
 
 	format_ = format;
-	width_ = width;
-	height_ = height;
+	size_ = size;
 
 	delete image_;
-	image_ = new QImage(width, height, QImage::Format_RGB32);
+	image_ = new QImage(size_, QImage::Format_RGB32);
 
 	updateGeometry();
 	return 0;
@@ -73,5 +72,5 @@  void ViewFinder::paintEvent(QPaintEvent *)
 
 QSize ViewFinder::sizeHint() const
 {
-	return image_ ? image_->size() : QSize(640, 480);
+	return size_.isValid() ? size_ : QSize(640, 480);
 }
diff --git a/src/qcam/viewfinder.h b/src/qcam/viewfinder.h
index 0549f038edd6..a019c3a470ea 100644
--- a/src/qcam/viewfinder.h
+++ b/src/qcam/viewfinder.h
@@ -8,6 +8,7 @@ 
 #define __QCAM_VIEWFINDER_H__
 
 #include <QMutex>
+#include <QSize>
 #include <QWidget>
 
 #include <libcamera/pixelformats.h>
@@ -22,8 +23,7 @@  public:
 	ViewFinder(QWidget *parent);
 	~ViewFinder();
 
-	int setFormat(const libcamera::PixelFormat &format, unsigned int width,
-		      unsigned int height);
+	int setFormat(const libcamera::PixelFormat &format, const QSize &size);
 	void display(const unsigned char *rgb, size_t size);
 
 	QImage getCurrentImage();
@@ -33,12 +33,11 @@  protected:
 	QSize sizeHint() const override;
 
 private:
-	libcamera::PixelFormat format_;
-	unsigned int width_;
-	unsigned int height_;
-
 	FormatConverter converter_;
 
+	libcamera::PixelFormat format_;
+	QSize size_;
+
 	QImage *image_;
 	QMutex mutex_; /* Prevent concurrent access to image_ */
 };