From ccacad9b3fcf648e3776378dca4d7cc95e9db794 Mon Sep 17 00:00:00 2001 From: LordGrey <48840279+Lord-Grey@users.noreply.github.com> Date: Sat, 30 Dec 2023 16:22:59 +0100 Subject: [PATCH] Remove extra pixel consistently --- include/utils/Image.h | 20 +++++----- include/utils/ImageData.h | 51 ++++++++++---------------- libsrc/grabber/audio/AudioGrabber.cpp | 2 +- libsrc/grabber/video/EncoderThread.cpp | 2 +- libsrc/grabber/x11/X11Grabber.cpp | 1 - libsrc/hyperion/GrabberWrapper.cpp | 1 - 6 files changed, 31 insertions(+), 46 deletions(-) diff --git a/include/utils/Image.h b/include/utils/Image.h index a0722040..674b431a 100644 --- a/include/utils/Image.h +++ b/include/utils/Image.h @@ -15,9 +15,8 @@ public: { } - Image(unsigned width, unsigned height) : + Image(int width, int height) : Image(width, height, Pixel_T()) - { } @@ -28,7 +27,7 @@ public: /// @param height The height of the image /// @param background The color of the image /// - Image(unsigned width, unsigned height, const Pixel_T background) : + Image(int width, int height, const Pixel_T background) : _d_ptr(new ImageData(width, height, background)) { } @@ -78,7 +77,7 @@ public: /// /// @return The width of the image /// - inline unsigned width() const + inline int width() const { return _d_ptr->width(); } @@ -88,7 +87,7 @@ public: /// /// @return The height of the image /// - inline unsigned height() const + inline int height() const { return _d_ptr->height(); } @@ -111,7 +110,7 @@ public: /// /// @return const reference to specified pixel /// - uint8_t blue(unsigned pixel) const + uint8_t blue(int pixel) const { return _d_ptr->blue(pixel); } @@ -121,7 +120,7 @@ public: /// /// @param x The x index /// @param y The y index - const Pixel_T& operator()(unsigned x, unsigned y) const + const Pixel_T& operator()(int x, int y) const { return _d_ptr->operator()(x, y); } @@ -129,7 +128,7 @@ public: /// /// @return reference to specified pixel /// - Pixel_T& operator()(unsigned x, unsigned y) + Pixel_T& operator()(int x, int y) { return _d_ptr->operator()(x, y); } @@ -137,7 +136,7 @@ public: /// Resize the image /// @param width The width of the image /// @param height The height of the image - void resize(unsigned width, unsigned height) + void resize(int width, int height) { _d_ptr->resize(width, height); } @@ -198,12 +197,11 @@ private: /// /// @return The index into the underlying data-vector /// - inline unsigned toIndex(unsigned x, unsigned y) const + inline int toIndex(int x, int y) const { return _d_ptr->toIndex(x, y); } -private: QSharedDataPointer> _d_ptr; }; diff --git a/include/utils/ImageData.h b/include/utils/ImageData.h index 7a7a1808..c46c0d46 100644 --- a/include/utils/ImageData.h +++ b/include/utils/ImageData.h @@ -21,10 +21,10 @@ class ImageData : public QSharedData public: typedef Pixel_T pixel_type; - ImageData(unsigned width, unsigned height, const Pixel_T background) : + ImageData(int width, int height, const Pixel_T background) : _width(width), _height(height), - _pixels(new Pixel_T[width * height + 1]) + _pixels(new Pixel_T[static_cast(width * height)]) { std::fill(_pixels, _pixels + width * height, background); } @@ -33,9 +33,9 @@ public: QSharedData(other), _width(other._width), _height(other._height), - _pixels(new Pixel_T[other._width * other._height + 1]) + _pixels(new Pixel_T[other._width * other._height]) { - memcpy(_pixels, other._pixels, static_cast(other._width) * static_cast(other._height) * sizeof(Pixel_T)); + memcpy(_pixels, other._pixels, other._width * other._height * sizeof(Pixel_T)); } ImageData& operator=(ImageData rhs) @@ -71,42 +71,42 @@ public: delete[] _pixels; } - inline unsigned width() const + inline int width() const { return _width; } - inline unsigned height() const + inline int height() const { return _height; } - uint8_t red(unsigned pixel) const + uint8_t red(int pixel) const { return (_pixels + pixel)->red; } - uint8_t green(unsigned pixel) const + uint8_t green(int pixel) const { return (_pixels + pixel)->green; } - uint8_t blue(unsigned pixel) const + uint8_t blue(int pixel) const { return (_pixels + pixel)->blue; } - const Pixel_T& operator()(unsigned x, unsigned y) const + const Pixel_T& operator()(int x, int y) const { return _pixels[toIndex(x,y)]; } - Pixel_T& operator()(unsigned x, unsigned y) + Pixel_T& operator()(int x, int y) { return _pixels[toIndex(x,y)]; } - void resize(unsigned width, unsigned height) + void resize(int width, int height) { if (width == _width && height == _height) { @@ -114,7 +114,7 @@ public: } // Allocate a new buffer without initializing the content - Pixel_T* newPixels = new Pixel_T[static_cast(width) * static_cast(height)]; + Pixel_T* newPixels = new Pixel_T[static_cast(width * height)]; // Release the old buffer without copying data delete[] _pixels; @@ -143,9 +143,9 @@ public: image.resize(_width, _height); } - const unsigned imageSize = _width * _height; + const int imageSize = _width * _height; - for (unsigned idx = 0; idx < imageSize; idx++) + for (int idx = 0; idx < imageSize; idx++) { const Pixel_T & color = _pixels[idx]; image.memptr()[idx] = ColorRgb{color.red, color.green, color.blue}; @@ -154,40 +154,29 @@ public: ssize_t size() const { - return static_cast(_width) * static_cast(_height) * sizeof(Pixel_T); + return static_cast(_width * _height) * sizeof(Pixel_T); } void clear() { if (_width != 1 || _height != 1) { - _width = 1; - _height = 1; - - // Allocate a new buffer without initializing the content - Pixel_T* newPixels = new Pixel_T[static_cast(_width) * static_cast(_height)]; - - // Release the old buffer without copying data - delete[] _pixels; - - // Update the pointer to the new buffer - _pixels = newPixels; + resize(1,1); } - // Set the single pixel to the default background _pixels[0] = Pixel_T(); } private: - inline unsigned toIndex(unsigned x, unsigned y) const + inline int toIndex(int x, int y) const { return y * _width + x; } /// The width of the image - unsigned _width; + int _width; /// The height of the image - unsigned _height; + int _height; /// The pixels of the image Pixel_T* _pixels; }; diff --git a/libsrc/grabber/audio/AudioGrabber.cpp b/libsrc/grabber/audio/AudioGrabber.cpp index 995a1b7c..1e2be84d 100644 --- a/libsrc/grabber/audio/AudioGrabber.cpp +++ b/libsrc/grabber/audio/AudioGrabber.cpp @@ -178,7 +178,7 @@ void AudioGrabber::processAudioFrame(int16_t* buffer, int length) } // Convert to Image - Image finalImage (static_cast(image.width()), static_cast(image.height())); + Image finalImage (image.width(),image.height()); for (int y = 0; y < image.height(); y++) { memcpy((unsigned char*)finalImage.memptr() + y * image.width() * 3, static_cast(image.scanLine(y)), image.width() * 3); diff --git a/libsrc/grabber/video/EncoderThread.cpp b/libsrc/grabber/video/EncoderThread.cpp index 3555bb9f..254472c0 100644 --- a/libsrc/grabber/video/EncoderThread.cpp +++ b/libsrc/grabber/video/EncoderThread.cpp @@ -318,7 +318,7 @@ void EncoderThread::processImageMjpeg() } } - Image srcImage(static_cast(_width), static_cast(_height)); + Image srcImage(_width, _height); if (tjDecompress2(_tjInstance, _localData , _size, reinterpret_cast(srcImage.memptr()), _width, 0, _height, diff --git a/libsrc/grabber/x11/X11Grabber.cpp b/libsrc/grabber/x11/X11Grabber.cpp index cf24eea2..2295e725 100644 --- a/libsrc/grabber/x11/X11Grabber.cpp +++ b/libsrc/grabber/x11/X11Grabber.cpp @@ -27,7 +27,6 @@ X11Grabber::X11Grabber(int cropLeft, int cropRight, int cropTop, int cropBottom) , _XRandRAvailable(false) , _isWayland (false) , _logger{} - , _image(0,0) { _logger = Logger::getInstance("X11"); diff --git a/libsrc/hyperion/GrabberWrapper.cpp b/libsrc/hyperion/GrabberWrapper.cpp index da17f07c..d7a1e533 100644 --- a/libsrc/hyperion/GrabberWrapper.cpp +++ b/libsrc/hyperion/GrabberWrapper.cpp @@ -30,7 +30,6 @@ GrabberWrapper::GrabberWrapper(const QString& grabberName, Grabber * ggrabber, i , _timer(new QTimer(this)) , _updateInterval_ms(1000/updateRate_Hz) , _ggrabber(ggrabber) - , _image(0,0) { GrabberWrapper::instance = this;