Skip to content

Commit 1e8e2a2

Browse files
authored
Merge pull request #427 from ferdnyc/optimized-hue
Hue: Optimize and parallelize
2 parents e5f11e9 + 89d1667 commit 1e8e2a2

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

src/effects/Hue.cpp

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -66,36 +66,35 @@ std::shared_ptr<Frame> Hue::GetFrame(std::shared_ptr<Frame> frame, int64_t frame
6666
// Get the frame's image
6767
std::shared_ptr<QImage> frame_image = frame->GetImage();
6868

69+
int pixel_count = frame_image->width() * frame_image->height();
70+
6971
// Get the current hue percentage shift amount, and convert to degrees
7072
double degrees = 360.0 * hue.GetValue(frame_number);
7173
float cosA = cos(degrees*3.14159265f/180);
7274
float sinA = sin(degrees*3.14159265f/180);
7375

7476
// Calculate a rotation matrix for the RGB colorspace (based on the current hue shift keyframe value)
75-
float matrix[3][3] = {{cosA + (1.0f - cosA) / 3.0f, 1.0f/3.0f * (1.0f - cosA) - sqrtf(1.0f/3.0f) * sinA, 1.0f/3.0f * (1.0f - cosA) + sqrtf(1.0f/3.0f) * sinA},
76-
{1.0f/3.0f * (1.0f - cosA) + sqrtf(1.0f/3.0f) * sinA, cosA + 1.0f/3.0f*(1.0f - cosA), 1.0f/3.0f * (1.0f - cosA) - sqrtf(1.0f/3.0f) * sinA},
77-
{1.0f/3.0f * (1.0f - cosA) - sqrtf(1.0f/3.0f) * sinA, 1.0f/3.0f * (1.0f - cosA) + sqrtf(1.0f/3.0f) * sinA, cosA + 1.0f/3.0f * (1.0f - cosA)}};
77+
float matrix[3] = {
78+
cosA + (1.0f - cosA) / 3.0f,
79+
1.0f/3.0f * (1.0f - cosA) - sqrtf(1.0f/3.0f) * sinA,
80+
1.0f/3.0f * (1.0f - cosA) + sqrtf(1.0f/3.0f) * sinA
81+
};
7882

7983
// Loop through pixels
8084
unsigned char *pixels = (unsigned char *) frame_image->bits();
81-
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)
85+
86+
#pragma omp parallel for shared (pixels)
87+
for (int pixel = 0; pixel < pixel_count; ++pixel)
8288
{
83-
// Get the RGB values from the pixel
84-
int R = pixels[byte_index];
85-
int G = pixels[byte_index + 1];
86-
int B = pixels[byte_index + 2];
87-
int A = pixels[byte_index + 3];
89+
// Get the RGB values from the pixel (ignore the alpha channel)
90+
int R = pixels[pixel * 4];
91+
int G = pixels[pixel * 4 + 1];
92+
int B = pixels[pixel * 4 + 2];
8893

8994
// Multiply each color by the hue rotation matrix
90-
float rx = constrain(R * matrix[0][0] + G * matrix[0][1] + B * matrix[0][2]);
91-
float gx = constrain(R * matrix[1][0] + G * matrix[1][1] + B * matrix[1][2]);
92-
float bx = constrain(R * matrix[2][0] + G * matrix[2][1] + B * matrix[2][2]);
93-
94-
// Set all pixels to new value
95-
pixels[byte_index] = rx;
96-
pixels[byte_index + 1] = gx;
97-
pixels[byte_index + 2] = bx;
98-
pixels[byte_index + 3] = A; // leave the alpha value alone
95+
pixels[pixel * 4] = constrain(R * matrix[0] + G * matrix[1] + B * matrix[2]);
96+
pixels[pixel * 4 + 1] = constrain(R * matrix[2] + G * matrix[0] + B * matrix[1]);
97+
pixels[pixel * 4 + 2] = constrain(R * matrix[1] + G * matrix[2] + B * matrix[0]);
9998
}
10099

101100
// return the modified frame

0 commit comments

Comments
 (0)