From 99aab0a12be3d83bede45ad8cd5e04ae9c1da1f2 Mon Sep 17 00:00:00 2001 From: Jonathan Thomas Date: Wed, 21 Dec 2022 22:18:42 -0600 Subject: [PATCH] Fix Brightness, Hue, and Saturation effects (#889) * Fix Brightness, Hue, and Saturation effects to fully support pre-multiplied colors (when alpha channel found) * Fixing whitespace on effects --- src/effects/Brightness.cpp | 27 ++++++++++++++++++++------- src/effects/Hue.cpp | 17 +++++++++++++---- src/effects/Saturation.cpp | 30 +++++++++++++++++------------- 3 files changed, 50 insertions(+), 24 deletions(-) diff --git a/src/effects/Brightness.cpp b/src/effects/Brightness.cpp index 8b2ddc87..58340fff 100644 --- a/src/effects/Brightness.cpp +++ b/src/effects/Brightness.cpp @@ -63,16 +63,29 @@ std::shared_ptr Brightness::GetFrame(std::shared_ptr Hue::GetFrame(std::shared_ptr #pragma omp parallel for shared (pixels) for (int pixel = 0; pixel < pixel_count; ++pixel) { - // Get the RGB values from the pixel (ignore the alpha channel) - int R = pixels[pixel * 4]; - int G = pixels[pixel * 4 + 1]; - int B = pixels[pixel * 4 + 2]; + // Calculate alpha % (to be used for removing pre-multiplied alpha value) + int A = pixels[pixel * 4 + 3]; + float alpha_percent = A / 255.0; + + // Get RGB values, and remove pre-multiplied alpha + int R = pixels[pixel * 4 + 0] / alpha_percent; + int G = pixels[pixel * 4 + 1] / alpha_percent; + int B = pixels[pixel * 4 + 2] / alpha_percent; // Multiply each color by the hue rotation matrix pixels[pixel * 4] = constrain(R * matrix[0] + G * matrix[1] + B * matrix[2]); pixels[pixel * 4 + 1] = constrain(R * matrix[2] + G * matrix[0] + B * matrix[1]); pixels[pixel * 4 + 2] = constrain(R * matrix[1] + G * matrix[2] + B * matrix[0]); + + // Pre-multiply the alpha back into the color channels + pixels[pixel * 4 + 0] *= alpha_percent; + pixels[pixel * 4 + 1] *= alpha_percent; + pixels[pixel * 4 + 2] *= alpha_percent; } // return the modified frame diff --git a/src/effects/Saturation.cpp b/src/effects/Saturation.cpp index f242ee14..6733deec 100644 --- a/src/effects/Saturation.cpp +++ b/src/effects/Saturation.cpp @@ -72,10 +72,14 @@ std::shared_ptr Saturation::GetFrame(std::shared_ptr Saturation::GetFrame(std::shared_ptr Saturation::GetFrame(std::shared_ptr