Skip to content

Commit e5f11e9

Browse files
authored
Merge pull request #428 from ferdnyc/optimize-sat
Saturation: streamline and parallelize
2 parents bc6c9fd + 4979028 commit e5f11e9

File tree

1 file changed

+18
-26
lines changed

1 file changed

+18
-26
lines changed

src/effects/Saturation.cpp

Lines changed: 18 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -69,44 +69,36 @@ std::shared_ptr<Frame> Saturation::GetFrame(std::shared_ptr<Frame> frame, int64_
6969
if (!frame_image)
7070
return frame;
7171

72+
int pixel_count = frame_image->width() * frame_image->height();
73+
7274
// Get keyframe values for this frame
7375
float saturation_value = saturation.GetValue(frame_number);
7476

7577
// Constants used for color saturation formula
76-
double pR = .299;
77-
double pG = .587;
78-
double pB = .114;
78+
const double pR = .299;
79+
const double pG = .587;
80+
const double pB = .114;
7981

8082
// Loop through pixels
8183
unsigned char *pixels = (unsigned char *) frame_image->bits();
82-
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
84+
85+
#pragma omp parallel for shared (pixels)
86+
for (int pixel = 0; pixel < pixel_count; ++pixel)
8387
{
8488
// Get the RGB values from the pixel
85-
int R = pixels[byte_index];
86-
int G = pixels[byte_index + 1];
87-
int B = pixels[byte_index + 2];
88-
int A = pixels[byte_index + 3];
89+
int R = pixels[pixel * 4];
90+
int G = pixels[pixel * 4 + 1];
91+
int B = pixels[pixel * 4 + 2];
8992

9093
// Calculate the saturation multiplier
9194
double p = sqrt( (R * R * pR) +
92-
(G * G * pG) +
93-
(B * B * pB) );
94-
95-
// Adjust the saturation
96-
R = p + (R - p) * saturation_value;
97-
G = p + (G - p) * saturation_value;
98-
B = p + (B - p) * saturation_value;
99-
100-
// Constrain the value from 0 to 255
101-
R = constrain(R);
102-
G = constrain(G);
103-
B = constrain(B);
104-
105-
// Set all pixels to new value
106-
pixels[byte_index] = R;
107-
pixels[byte_index + 1] = G;
108-
pixels[byte_index + 2] = B;
109-
pixels[byte_index + 3] = A; // leave the alpha value alone
95+
(G * G * pG) +
96+
(B * B * pB) );
97+
98+
// Apply adjusted and constrained saturation
99+
pixels[pixel * 4] = constrain(p + (R - p) * saturation_value);
100+
pixels[pixel * 4 + 1] = constrain(p + (G - p) * saturation_value);
101+
pixels[pixel * 4 + 2] = constrain(p + (B - p) * saturation_value);
110102
}
111103

112104
// return the modified frame

0 commit comments

Comments
 (0)