Skip to content

Commit f9b4fe5

Browse files
authored
Merge pull request #424 from ferdnyc/optimized-brightness
Parallelize and streamline Brightness effect
2 parents 4fc9e9d + 1a42b45 commit f9b4fe5

File tree

1 file changed

+16
-27
lines changed

1 file changed

+16
-27
lines changed

src/effects/Brightness.cpp

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,24 @@ std::shared_ptr<Frame> Brightness::GetFrame(std::shared_ptr<Frame> frame, int64_
7272

7373
// Loop through pixels
7474
unsigned char *pixels = (unsigned char *) frame_image->bits();
75-
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
76-
{
77-
// Get the RGB values from the pixel
78-
int R = pixels[byte_index];
79-
int G = pixels[byte_index + 1];
80-
int B = pixels[byte_index + 2];
81-
int A = pixels[byte_index + 3];
75+
int pixel_count = frame_image->width() * frame_image->height();
8276

83-
// Adjust the contrast
77+
#pragma omp parallel for
78+
for (int pixel = 0; pixel < pixel_count; ++pixel)
79+
{
80+
// Compute contrast adjustment factor
8481
float factor = (259 * (contrast_value + 255)) / (255 * (259 - contrast_value));
85-
R = constrain((factor * (R - 128)) + 128);
86-
G = constrain((factor * (G - 128)) + 128);
87-
B = constrain((factor * (B - 128)) + 128);
88-
89-
// Adjust the brightness
90-
R += (255 * brightness_value);
91-
G += (255 * brightness_value);
92-
B += (255 * brightness_value);
93-
94-
// Constrain the value from 0 to 255
95-
R = constrain(R);
96-
G = constrain(G);
97-
B = constrain(B);
98-
99-
// Set all pixels to new value
100-
pixels[byte_index] = R;
101-
pixels[byte_index + 1] = G;
102-
pixels[byte_index + 2] = B;
103-
pixels[byte_index + 3] = A; // leave the alpha value alone
82+
83+
// Get RGB pixels from image and apply constrained contrast adjustment
84+
int R = constrain((factor * (pixels[pixel * 4] - 128)) + 128);
85+
int G = constrain((factor * (pixels[pixel * 4 + 1] - 128)) + 128);
86+
int B = constrain((factor * (pixels[pixel * 4 + 2] - 128)) + 128);
87+
// (Don't modify Alpha value)
88+
89+
// Adjust brightness and write constrained values back to image
90+
pixels[pixel * 4] = constrain(R + (255 * brightness_value));
91+
pixels[pixel * 4 + 1] = constrain(G + (255 * brightness_value));
92+
pixels[pixel * 4 + 2] = constrain(B + (255 * brightness_value));
10493
}
10594

10695
// return the modified frame

0 commit comments

Comments
 (0)