This project is read-only.
1

Resolved

Bug in Blit function for WPF

description

Hello,
there is bug in Blit function, when using BlendMode.Alpha.

instead of code
if (blendMode == BlendMode.Alpha)
                                                {
                                                    var isa = 255 - sa;
#if NETFX_CORE
                                        // Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
                                        destPixel =                          ((da & 0xff) << 24) |
                                                    ((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
                                                    ((((sg * sa + isa * dg) >> 8) & 0xff) <<  8) |
                                                     (((sb * sa + isa * db) >> 8) & 0xff);
#else
                                                    destPixel =
                                                                 ((((sa << 8) + isa * da) >> 8) << 24) |
                                                                (((((sr << 8) + isa * dr) >> 8) & 0xff) << 16) |
                                                                (((((sg << 8) + isa * dg) >> 8) & 0xff) << 8) |
                                                                 ((((sb << 8) + isa * db) >> 8) & 0xff);
#endif
should be
                                    if (BlendMode == BlendMode.Alpha)
                                    {                                    
                                        var isa = 255 - sa;
#if NETFX_CORE
                                        // Special case for WinRT since it does not use pARGB (pre-multiplied alpha)
                                        destPixel =                          ((da & 0xff) << 24) |
                                                    ((((sr * sa + isa * dr) >> 8) & 0xff) << 16) |
                                                    ((((sg * sa + isa * dg) >> 8) & 0xff) <<  8) |
                                                     (((sb * sa + isa * db) >> 8) & 0xff);
#else
                                        destPixel =                            ((da & 0xff) << 24) |
                                                    (((((sr * sa) + isa * dr) >> 8) & 0xff) << 16) |
                                                    (((((sg * sa) + isa * dg) >> 8) & 0xff) << 8) |
                                                     ((((sb * sa) + isa * db) >> 8) & 0xff);
#endif
                                    }
(instead of (((sr << 8) + isa * dr) >> 8) should be (((sr * sa + isa * dr) >> 8), difference in A component is not subject of this issue
Tested with some icons, fixed version renders nice blended icons

comments

teichgraf wrote Mar 23, 2015 at 10:27 AM

I assume you were using it on WPF?
Keep in mind that the WB uses pre-multiplied so the RGB components are already multiplied with their alpha values on most platforms, so performing sr * sa actually means (sr*sa) * sa. I added an additional check for the WPF build if the source.Format is pRGBA. This should work for all cases now. Please get latest source code from the repository and try it.
Thanks for raising this issue.

dbshell wrote Mar 29, 2015 at 11:59 AM

Hello,
thank you for explanation.
I have used version from official downloads, with some fixes and extension. When upgrading to latest source code version, it works perfectly.
Only when upgrading, FillRectangle function draws rectangles one pixel smaller than previous rectangle... I fixed it in my calling code.
Jan

dbshell wrote Mar 29, 2015 at 12:01 PM

Sorry for typing error is mean:
FillRectangle function draws rectangles one pixel smaller than previous version