From e75b83bd2a46d85a53dd063fab0a36f89231c60e Mon Sep 17 00:00:00 2001 From: Cvolton Date: Thu, 17 Oct 2024 13:23:17 +0200 Subject: [PATCH] ensure even dimensions on saved screenshots --- src/images/CompositeScreenshot.cpp | 44 ++++++++++++++++++++++++++++++ src/images/CompositeScreenshot.h | 1 + 2 files changed, 45 insertions(+) diff --git a/src/images/CompositeScreenshot.cpp b/src/images/CompositeScreenshot.cpp index a693f7d..b1cc585 100644 --- a/src/images/CompositeScreenshot.cpp +++ b/src/images/CompositeScreenshot.cpp @@ -2,6 +2,7 @@ #include "Utils.h" #include +#include inline BYTE toByte(int value){ return value > 255 ? 255 : value; @@ -136,4 +137,47 @@ void CompositeScreenshot::cropImage() { Gdiplus::Bitmap* croppedBitmap = m_image->Clone(crop, PixelFormatDontCare); delete m_image; m_image = croppedBitmap; + + ensureEvenDimensions(); +} + +void CompositeScreenshot::ensureEvenDimensions(){ + auto oldBitmap = m_image; + + auto oldHeight = oldBitmap->GetHeight(); + auto oldWidth = oldBitmap->GetWidth(); + + if(oldWidth % 2 == 0 && oldHeight % 2 == 0) return; + + auto newWidth = m_image->GetWidth(); + auto newHeight = m_image->GetHeight(); + + if(newWidth % 2 != 0) newWidth++; + if(newHeight % 2 != 0) newHeight++; + + Gdiplus::Bitmap* newBitmap = new Gdiplus::Bitmap(newWidth, newHeight, PixelFormat32bppARGB); + + Gdiplus::Rect oldRect(0, 0, m_image->GetWidth(), m_image->GetHeight()); + Gdiplus::Rect newRect(0, 0, newBitmap->GetWidth(), newBitmap->GetHeight()); + + Gdiplus::BitmapData newBitmapData; + newBitmap->LockBits(&newRect, Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &newBitmapData); + BYTE* newPixels = (BYTE*) (void*) newBitmapData.Scan0; + + Gdiplus::BitmapData oldBitmapData; + oldBitmap->LockBits(&oldRect, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &oldBitmapData); + BYTE* oldPixels = (BYTE*) (void*) oldBitmapData.Scan0; + + for(size_t x = 0; x < oldHeight; x++){ + size_t rowStart = x*oldWidth; + size_t newRowStart = x*newWidth; + + std::memcpy(newPixels + newRowStart*4, oldPixels + rowStart*4, oldWidth*4); + } + + newBitmap->UnlockBits(&newBitmapData); + oldBitmap->UnlockBits(&oldBitmapData); + + delete m_image; + m_image = newBitmap; } \ No newline at end of file diff --git a/src/images/CompositeScreenshot.h b/src/images/CompositeScreenshot.h index 0047bf5..4308795 100644 --- a/src/images/CompositeScreenshot.h +++ b/src/images/CompositeScreenshot.h @@ -13,6 +13,7 @@ class CompositeScreenshot : public Screenshot { void init(const Screenshot& white, const Screenshot& black); void differentiateAlpha(Gdiplus::Bitmap* whiteShot, Gdiplus::Bitmap* blackShot); void cropImage(); + void ensureEvenDimensions(); Gdiplus::Rect calculateCrop(); public: Gdiplus::Rect getCrop();