qcam: format_converter: Add configurable stride support

Make the stride configurable to support conversion of images with
padding at the end of lines.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
Reviewed-by: Paul Elder <paul.elder@ideasonboard.com>
This commit is contained in:
Laurent Pinchart 2021-09-06 22:15:35 +03:00
parent f3671b33d9
commit 4abbd832fe
3 changed files with 11 additions and 9 deletions

View file

@ -30,7 +30,7 @@
#endif #endif
int FormatConverter::configure(const libcamera::PixelFormat &format, int FormatConverter::configure(const libcamera::PixelFormat &format,
const QSize &size) const QSize &size, unsigned int stride)
{ {
switch (format) { switch (format) {
case libcamera::formats::NV12: case libcamera::formats::NV12:
@ -152,6 +152,7 @@ int FormatConverter::configure(const libcamera::PixelFormat &format,
format_ = format; format_ = format;
width_ = size.width(); width_ = size.width();
height_ = size.height(); height_ = size.height();
stride_ = stride;
return 0; return 0;
} }
@ -186,7 +187,7 @@ static void yuv_to_rgb(int y, int u, int v, int *r, int *g, int *b)
void FormatConverter::convertNV(const Image *srcImage, unsigned char *dst) void FormatConverter::convertNV(const Image *srcImage, unsigned char *dst)
{ {
unsigned int c_stride = width_ * (2 / horzSubSample_); unsigned int c_stride = stride_ * (2 / horzSubSample_);
unsigned int c_inc = horzSubSample_ == 1 ? 2 : 0; unsigned int c_inc = horzSubSample_ == 1 ? 2 : 0;
unsigned int cb_pos = nvSwap_ ? 1 : 0; unsigned int cb_pos = nvSwap_ ? 1 : 0;
unsigned int cr_pos = nvSwap_ ? 0 : 1; unsigned int cr_pos = nvSwap_ ? 0 : 1;
@ -195,7 +196,7 @@ void FormatConverter::convertNV(const Image *srcImage, unsigned char *dst)
int r, g, b; int r, g, b;
for (unsigned int y = 0; y < height_; y++) { for (unsigned int y = 0; y < height_; y++) {
const unsigned char *src_y = src + y * width_; const unsigned char *src_y = src + y * stride_;
const unsigned char *src_cb = src_c + (y / vertSubSample_) * const unsigned char *src_cb = src_c + (y / vertSubSample_) *
c_stride + cb_pos; c_stride + cb_pos;
const unsigned char *src_cr = src_c + (y / vertSubSample_) * const unsigned char *src_cr = src_c + (y / vertSubSample_) *
@ -243,7 +244,7 @@ void FormatConverter::convertRGB(const Image *srcImage, unsigned char *dst)
dst[4 * x + 3] = 0xff; dst[4 * x + 3] = 0xff;
} }
src += width_ * bpp_; src += stride_;
dst += width_ * 4; dst += width_ * 4;
} }
} }
@ -258,7 +259,7 @@ void FormatConverter::convertYUV(const Image *srcImage, unsigned char *dst)
int r, g, b, y, cr, cb; int r, g, b, y, cr, cb;
cr_pos = (cb_pos_ + 2) % 4; cr_pos = (cb_pos_ + 2) % 4;
src_stride = width_ * 2; src_stride = stride_;
dst_stride = width_ * 4; dst_stride = width_ * 4;
for (src_y = 0, dst_y = 0; dst_y < height_; src_y++, dst_y++) { for (src_y = 0, dst_y = 0; dst_y < height_; src_y++, dst_y++) {

View file

@ -19,7 +19,8 @@ class QImage;
class FormatConverter class FormatConverter
{ {
public: public:
int configure(const libcamera::PixelFormat &format, const QSize &size); int configure(const libcamera::PixelFormat &format, const QSize &size,
unsigned int stride);
void convert(const Image *src, size_t size, QImage *dst); void convert(const Image *src, size_t size, QImage *dst);
@ -38,6 +39,7 @@ private:
libcamera::PixelFormat format_; libcamera::PixelFormat format_;
unsigned int width_; unsigned int width_;
unsigned int height_; unsigned int height_;
unsigned int stride_;
enum FormatFamily formatFamily_; enum FormatFamily formatFamily_;

View file

@ -52,8 +52,7 @@ const QList<libcamera::PixelFormat> &ViewFinderQt::nativeFormats() const
} }
int ViewFinderQt::setFormat(const libcamera::PixelFormat &format, int ViewFinderQt::setFormat(const libcamera::PixelFormat &format,
const QSize &size, const QSize &size, unsigned int stride)
[[maybe_unused]] unsigned int stride)
{ {
image_ = QImage(); image_ = QImage();
@ -62,7 +61,7 @@ int ViewFinderQt::setFormat(const libcamera::PixelFormat &format,
* the destination image. * the destination image.
*/ */
if (!::nativeFormats.contains(format)) { if (!::nativeFormats.contains(format)) {
int ret = converter_.configure(format, size); int ret = converter_.configure(format, size, stride);
if (ret < 0) if (ret < 0)
return ret; return ret;