Skip to content

Commit 1eecda3

Browse files
committed
Fix alpha and mask effects, so they correctly multiply the alpha to all colors (since we have switched to a premulitplied alpha format)
1 parent 6bd7fb7 commit 1eecda3

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Clip.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1155,7 +1155,11 @@ void Clip::apply_keyframes(std::shared_ptr<Frame> frame, int width, int height)
11551155
// Loop through pixels
11561156
for (int pixel = 0, byte_index=0; pixel < source_image->width() * source_image->height(); pixel++, byte_index+=4)
11571157
{
1158-
// Apply alpha to pixel
1158+
// Apply alpha to pixel values (since we use a premultiplied value, we must
1159+
// multiply the alpha with all colors).
1160+
pixels[byte_index + 0] *= alpha_value;
1161+
pixels[byte_index + 1] *= alpha_value;
1162+
pixels[byte_index + 2] *= alpha_value;
11591163
pixels[byte_index + 3] *= alpha_value;
11601164
}
11611165

src/effects/Mask.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ std::shared_ptr<Frame> Mask::GetFrame(std::shared_ptr<Frame> frame, int64_t fram
117117
R = mask_pixels[byte_index];
118118
G = mask_pixels[byte_index + 1];
119119
B = mask_pixels[byte_index + 2];
120+
A = mask_pixels[byte_index + 3];
120121

121122
// Get the average luminosity
122123
gray_value = qGray(R, G, B);
@@ -131,16 +132,23 @@ std::shared_ptr<Frame> Mask::GetFrame(std::shared_ptr<Frame> frame, int64_t fram
131132
// Constrain the value from 0 to 255
132133
gray_value = constrain(gray_value);
133134

135+
// Calculate the % change in alpha
136+
float alpha_percent = float(constrain(A - gray_value)) / 255.0;
137+
134138
// Set the alpha channel to the gray value
135139
if (replace_image) {
136-
// Replace frame pixels with gray value
140+
// Replace frame pixels with gray value (including alpha channel)
137141
pixels[byte_index + 0] = gray_value;
138142
pixels[byte_index + 1] = gray_value;
139143
pixels[byte_index + 2] = gray_value;
144+
pixels[byte_index + 3] = gray_value;
140145
} else {
141-
// Set alpha channel
142-
A = pixels[byte_index + 3];
143-
pixels[byte_index + 3] = constrain(A - gray_value);
146+
// Mulitply new alpha value with all the colors (since we are using a premultiplied
147+
// alpha format)
148+
pixels[byte_index + 0] *= alpha_percent;
149+
pixels[byte_index + 1] *= alpha_percent;
150+
pixels[byte_index + 2] *= alpha_percent;
151+
pixels[byte_index + 3] *= alpha_percent;
144152
}
145153

146154
}

0 commit comments

Comments
 (0)