Fix alpha and mask effects, so they correctly multiply the alpha to all colors (since we have switched to a premulitplied alpha format)

This commit is contained in:
Jonathan Thomas
2020-10-14 14:19:26 -05:00
parent 6bd7fb7235
commit 1eecda3d4e
2 changed files with 17 additions and 5 deletions

View File

@@ -1155,7 +1155,11 @@ void Clip::apply_keyframes(std::shared_ptr<Frame> frame, int width, int height)
// Loop through pixels
for (int pixel = 0, byte_index=0; pixel < source_image->width() * source_image->height(); pixel++, byte_index+=4)
{
// Apply alpha to pixel
// Apply alpha to pixel values (since we use a premultiplied value, we must
// multiply the alpha with all colors).
pixels[byte_index + 0] *= alpha_value;
pixels[byte_index + 1] *= alpha_value;
pixels[byte_index + 2] *= alpha_value;
pixels[byte_index + 3] *= alpha_value;
}

View File

@@ -117,6 +117,7 @@ std::shared_ptr<Frame> Mask::GetFrame(std::shared_ptr<Frame> frame, int64_t fram
R = mask_pixels[byte_index];
G = mask_pixels[byte_index + 1];
B = mask_pixels[byte_index + 2];
A = mask_pixels[byte_index + 3];
// Get the average luminosity
gray_value = qGray(R, G, B);
@@ -131,16 +132,23 @@ std::shared_ptr<Frame> Mask::GetFrame(std::shared_ptr<Frame> frame, int64_t fram
// Constrain the value from 0 to 255
gray_value = constrain(gray_value);
// Calculate the % change in alpha
float alpha_percent = float(constrain(A - gray_value)) / 255.0;
// Set the alpha channel to the gray value
if (replace_image) {
// Replace frame pixels with gray value
// Replace frame pixels with gray value (including alpha channel)
pixels[byte_index + 0] = gray_value;
pixels[byte_index + 1] = gray_value;
pixels[byte_index + 2] = gray_value;
pixels[byte_index + 3] = gray_value;
} else {
// Set alpha channel
A = pixels[byte_index + 3];
pixels[byte_index + 3] = constrain(A - gray_value);
// Mulitply new alpha value with all the colors (since we are using a premultiplied
// alpha format)
pixels[byte_index + 0] *= alpha_percent;
pixels[byte_index + 1] *= alpha_percent;
pixels[byte_index + 2] *= alpha_percent;
pixels[byte_index + 3] *= alpha_percent;
}
}