You've already forked libopenshot
mirror of
https://github.com/OpenShot/libopenshot.git
synced 2026-03-02 08:53:52 -08:00
Merge pull request #427 from ferdnyc/optimized-hue
Hue: Optimize and parallelize
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user