Skip to content

Commit

Permalink
Fix alpha and mask effects, so they correctly multiply the alpha to a…
Browse files Browse the repository at this point in the history
…ll colors (since we have switched to a premulitplied alpha format)
  • Loading branch information
jonoomph committed Oct 14, 2020
1 parent 6bd7fb7 commit 1eecda3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
6 changes: 5 additions & 1 deletion src/Clip.cpp
Expand Up @@ -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;
}

Expand Down
16 changes: 12 additions & 4 deletions src/effects/Mask.cpp
Expand Up @@ -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);
Expand All @@ -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;
}

}
Expand Down

0 comments on commit 1eecda3

Please sign in to comment.