Skip to content

Commit

Permalink
optimize differentiate alpha performance (#16)
Browse files Browse the repository at this point in the history
* try direct array access again

* fix width height swap

* revert to copied vars for testing

* remove restrict

* try memcpy optimization
  • Loading branch information
Cvolton authored Oct 17, 2024
1 parent 1c512aa commit 992c310
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions src/images/CompositeScreenshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ void CompositeScreenshot::differentiateAlpha(Gdiplus::Bitmap* whiteShot, Gdiplus
Gdiplus::BitmapData transparentBitmapData;
Gdiplus::Rect rect1(0, 0, m_image->GetWidth(), m_image->GetHeight());
m_image->LockBits(&rect1, Gdiplus::ImageLockModeWrite, PixelFormat32bppARGB, &transparentBitmapData);
BYTE* transparentPixels = (BYTE*) (void*) transparentBitmapData.Scan0;
BYTE* transparentPixels = (BYTE*) transparentBitmapData.Scan0;

Gdiplus::BitmapData whiteBitmapData;
whiteShot->LockBits(&rect1, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &whiteBitmapData);
BYTE* whitePixels = (BYTE*) (void*) whiteBitmapData.Scan0;
const BYTE* whitePixels = (BYTE*) whiteBitmapData.Scan0;

Gdiplus::BitmapData blackBitmapData;
blackShot->LockBits(&rect1, Gdiplus::ImageLockModeRead, PixelFormat32bppARGB, &blackBitmapData);
BYTE* blackPixels = (BYTE*) (void*) blackBitmapData.Scan0;
const BYTE* blackPixels = (BYTE*) blackBitmapData.Scan0;

bool isOnlyOneMonitorConnected = monitorRects.size() == 1;

Expand All @@ -53,8 +53,11 @@ void CompositeScreenshot::differentiateAlpha(Gdiplus::Bitmap* whiteShot, Gdiplus

auto beforeStamp = CppShot::currentTimestamp();

BYTE* transparentFullBegin = nullptr;
BYTE* whiteFullBegin = nullptr;

for(int y = 0; y < height; y++){
for(int x = 0; x < height; x++){
for(int x = 0; x < width; x++){
int currentPixel = (y*width + x)*4;

bool isInsideMonitor = isOnlyOneMonitorConnected;
Expand All @@ -81,12 +84,22 @@ void CompositeScreenshot::differentiateAlpha(Gdiplus::Bitmap* whiteShot, Gdiplus
? toByte((blackR - whiteR + 255 + blackG - whiteG + 255 + blackB - whiteB + 255) / 3)
: 0;

transparentPixels[currentPixel + 3] = alpha;
if(alpha == 255) {
if(transparentFullBegin == nullptr) transparentFullBegin = transparentPixels + currentPixel;
if(whiteFullBegin == nullptr) whiteFullBegin = (BYTE*) whitePixels + currentPixel;
} else {
if(transparentFullBegin != nullptr) {
std::memcpy(transparentFullBegin, whiteFullBegin, (transparentPixels + currentPixel) - transparentFullBegin);
transparentFullBegin = nullptr;
whiteFullBegin = nullptr;
}

if (alpha > 0) {
transparentPixels[currentPixel + 2] = toByte(255 * blackR / alpha); // RED
transparentPixels[currentPixel + 1] = toByte(255 * blackG / alpha); // GREEN
transparentPixels[currentPixel] = toByte(255 * blackB / alpha); // BLUE
if (alpha > 0) {
transparentPixels[currentPixel + 3] = alpha;
transparentPixels[currentPixel + 2] = toByte(255 * blackR / alpha); // RED
transparentPixels[currentPixel + 1] = toByte(255 * blackG / alpha); // GREEN
transparentPixels[currentPixel] = toByte(255 * blackB / alpha); // BLUE
}
}
}
}
Expand Down Expand Up @@ -158,7 +171,7 @@ void CompositeScreenshot::cropImage() {
delete m_image;
m_image = croppedBitmap;

ensureEvenDimensions();
//ensureEvenDimensions();
}

void CompositeScreenshot::ensureEvenDimensions(){
Expand Down

0 comments on commit 992c310

Please sign in to comment.