@@ -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