From a5b42e3024590f4fa6bb0787ba1643617312666b Mon Sep 17 00:00:00 2001 From: LordGrey Date: Fri, 31 May 2024 18:56:49 +0200 Subject: [PATCH] RGB24 and BGR24 cleanup --- include/utils/PixelFormat.h | 9 +++++++++ .../video/mediafoundation/MFSourceReaderCB.h | 15 ++++++++------- libsrc/grabber/video/v4l2/V4L2Grabber.cpp | 18 +++++++++++++++--- libsrc/utils/ImageResampler.cpp | 16 ++++++++++++++++ 4 files changed, 48 insertions(+), 10 deletions(-) diff --git a/include/utils/PixelFormat.h b/include/utils/PixelFormat.h index 584ffc20..375ddc4e 100644 --- a/include/utils/PixelFormat.h +++ b/include/utils/PixelFormat.h @@ -10,6 +10,7 @@ enum class PixelFormat { YUYV, UYVY, BGR16, + RGB24, BGR24, RGB32, BGR32, @@ -36,6 +37,10 @@ inline PixelFormat parsePixelFormat(const QString& pixelFormat) { return PixelFormat::BGR16; } + else if (format.compare("rgb24") == 0) + { + return PixelFormat::RGB24; + } else if (format.compare("bgr24") == 0) { return PixelFormat::BGR24; @@ -80,6 +85,10 @@ inline QString pixelFormatToString(const PixelFormat& pixelFormat) { return "BGR16"; } + else if (pixelFormat == PixelFormat::RGB24) + { + return "RGB24"; + } else if (pixelFormat == PixelFormat::BGR24) { return "BGR24"; diff --git a/libsrc/grabber/video/mediafoundation/MFSourceReaderCB.h b/libsrc/grabber/video/mediafoundation/MFSourceReaderCB.h index 2bcef437..3fc296e5 100644 --- a/libsrc/grabber/video/mediafoundation/MFSourceReaderCB.h +++ b/libsrc/grabber/video/mediafoundation/MFSourceReaderCB.h @@ -27,7 +27,8 @@ static PixelFormat GetPixelFormatForGuid(const GUID guid) { if (IsEqualGUID(guid, MFVideoFormat_RGB32)) return PixelFormat::RGB32; - if (IsEqualGUID(guid, MFVideoFormat_RGB24)) return PixelFormat::BGR24; + if (IsEqualGUID(guid, MFVideoFormat_RGB24)) return PixelFormat::RGB24; + if (IsEqualGUID(guid, MFVideoFormat_BGR24)) return PixelFormat::BGR24; if (IsEqualGUID(guid, MFVideoFormat_YUY2)) return PixelFormat::YUYV; if (IsEqualGUID(guid, MFVideoFormat_UYVY)) return PixelFormat::UYVY; #ifdef HAVE_TURBO_JPEG @@ -145,9 +146,9 @@ public: } #ifdef HAVE_TURBO_JPEG - if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) + if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) #else - if (_pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) + if (_pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) #endif pSample = TransformSample(_transform, pSample); @@ -181,9 +182,9 @@ public: _bEOS = TRUE; // Reached the end of the stream. #ifdef HAVE_TURBO_JPEG - if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) + if (_pixelformat != PixelFormat::MJPEG && _pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) #else - if (_pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) + if (_pixelformat != PixelFormat::RGB24 && _pixelformat != PixelFormat::BGR24 && _pixelformat != PixelFormat::NO_CHANGE) #endif SAFE_RELEASE(pSample); @@ -196,9 +197,9 @@ public: { _pixelformat = format; #ifdef HAVE_TURBO_JPEG - if (format == PixelFormat::MJPEG || format == PixelFormat::BGR24 || format == PixelFormat::NO_CHANGE) + if (format == PixelFormat::MJPEG || format == PixelFormat::RGB24 || format == PixelFormat::BGR24 || format == PixelFormat::NO_CHANGE) #else - if (format == PixelFormat::BGR24 || format == PixelFormat::NO_CHANGE) + if (format == PixelFormat::RGB24 || format == PixelFormat::BGR24 || format == PixelFormat::NO_CHANGE) #endif return S_OK; diff --git a/libsrc/grabber/video/v4l2/V4L2Grabber.cpp b/libsrc/grabber/video/v4l2/V4L2Grabber.cpp index b0c404e5..c5b1fd12 100644 --- a/libsrc/grabber/video/v4l2/V4L2Grabber.cpp +++ b/libsrc/grabber/video/v4l2/V4L2Grabber.cpp @@ -54,6 +54,7 @@ Q_GLOBAL_STATIC_WITH_ARGS(ControlIDPropertyMap, _controlIDPropertyMap, (initCont static PixelFormat GetPixelFormat(const unsigned int format) { if (format == V4L2_PIX_FMT_RGB32) return PixelFormat::RGB32; + if (format == V4L2_PIX_FMT_RGB24) return PixelFormat::RGB24; if (format == V4L2_PIX_FMT_BGR24) return PixelFormat::BGR24; if (format == V4L2_PIX_FMT_YUYV) return PixelFormat::YUYV; if (format == V4L2_PIX_FMT_UYVY) return PixelFormat::UYVY; @@ -557,6 +558,10 @@ void V4L2Grabber::init_device(VideoStandard videoStandard) fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB32; break; + case PixelFormat::RGB24: + fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_RGB24; + break; + case PixelFormat::BGR24: fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_BGR24; break; @@ -691,6 +696,14 @@ void V4L2Grabber::init_device(VideoStandard videoStandard) } break; + case V4L2_PIX_FMT_RGB24: + { + _pixelFormat = PixelFormat::RGB24; + _frameByteSize = _width * _height * 3; + Debug(_log, "Pixel format=RGB24"); + } + break; + case V4L2_PIX_FMT_BGR24: { _pixelFormat = PixelFormat::BGR24; @@ -699,7 +712,6 @@ void V4L2Grabber::init_device(VideoStandard videoStandard) } break; - case V4L2_PIX_FMT_YUYV: { _pixelFormat = PixelFormat::YUYV; @@ -743,9 +755,9 @@ void V4L2Grabber::init_device(VideoStandard videoStandard) default: #ifdef HAVE_TURBO_JPEG - throw_exception("Only pixel formats RGB32, BGR24, YUYV, UYVY, NV12, I420 and MJPEG are supported"); + throw_exception("Only pixel formats RGB32, RGB24, BGR24, YUYV, UYVY, NV12, I420 and MJPEG are supported"); #else - throw_exception("Only pixel formats RGB32, BGR24, YUYV, UYVY, NV12 and I420 are supported"); + throw_exception("Only pixel formats RGB32, RGB24, BGR24, YUYV, UYVY, NV12 and I420 are supported"); #endif return; } diff --git a/libsrc/utils/ImageResampler.cpp b/libsrc/utils/ImageResampler.cpp index 483e35a9..b205e0d3 100644 --- a/libsrc/utils/ImageResampler.cpp +++ b/libsrc/utils/ImageResampler.cpp @@ -133,6 +133,22 @@ void ImageResampler::processImage(const uint8_t * data, int width, int height, i break; } + case PixelFormat::RGB24: + { + for (int yDest = yDestStart, ySource = cropTop + (_verticalDecimation >> 1); yDest <= yDestEnd; ySource += _verticalDecimation, ++yDest) + { + for (int xDest = xDestStart, xSource = cropLeft + (_horizontalDecimation >> 1); xDest <= xDestEnd; xSource += _horizontalDecimation, ++xDest) + { + ColorRgb & rgb = outputImage(abs(xDest), abs(yDest)); + int index = lineLength * ySource + (xSource << 1) + xSource; + rgb.red = data[index ]; + rgb.green = data[index+1]; + rgb.blue = data[index+2]; + } + } + break; + } + case PixelFormat::BGR24: { for (int yDest = yDestStart, ySource = cropTop + (_verticalDecimation >> 1); yDest <= yDestEnd; ySource += _verticalDecimation, ++yDest)