Skip to content

Commit

Permalink
Hue: Optimize and parallelize
Browse files Browse the repository at this point in the history
  • Loading branch information
ferdnyc committed Feb 6, 2020
1 parent 49972b2 commit 89d1667
Showing 1 changed file with 17 additions and 18 deletions.
35 changes: 17 additions & 18 deletions src/effects/Hue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,36 +66,35 @@ std::shared_ptr<Frame> Hue::GetFrame(std::shared_ptr<Frame> frame, int64_t frame
// Get the frame's image
std::shared_ptr<QImage> frame_image = frame->GetImage();

int pixel_count = frame_image->width() * frame_image->height();

// Get the current hue percentage shift amount, and convert to degrees
double degrees = 360.0 * hue.GetValue(frame_number);
float cosA = cos(degrees*3.14159265f/180);
float sinA = sin(degrees*3.14159265f/180);

// Calculate a rotation matrix for the RGB colorspace (based on the current hue shift keyframe value)
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},
{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},
{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)}};
float matrix[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
};

// Loop through pixels
unsigned char *pixels = (unsigned char *) frame_image->bits();
for (int pixel = 0, byte_index=0; pixel < frame_image->width() * frame_image->height(); pixel++, byte_index+=4)

#pragma omp parallel for shared (pixels)
for (int pixel = 0; pixel < pixel_count; ++pixel)
{
// Get the RGB values from the pixel
int R = pixels[byte_index];
int G = pixels[byte_index + 1];
int B = pixels[byte_index + 2];
int A = pixels[byte_index + 3];
// 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];

// Multiply each color by the hue rotation matrix
float rx = constrain(R * matrix[0][0] + G * matrix[0][1] + B * matrix[0][2]);
float gx = constrain(R * matrix[1][0] + G * matrix[1][1] + B * matrix[1][2]);
float bx = constrain(R * matrix[2][0] + G * matrix[2][1] + B * matrix[2][2]);

// Set all pixels to new value
pixels[byte_index] = rx;
pixels[byte_index + 1] = gx;
pixels[byte_index + 2] = bx;
pixels[byte_index + 3] = A; // leave the alpha value alone
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]);
}

// return the modified frame
Expand Down

0 comments on commit 89d1667

Please sign in to comment.