OpenBve.ImageExtensions.CombineBitmapAlpha C# (CSharp) Méthode

CombineBitmapAlpha() public static méthode

Combines a RGB bitmap and a greyscale mask into a RGBA bitmap
public static CombineBitmapAlpha ( Bitmap b, Bitmap s, Bitmap a ) : bool
b System.Drawing.Bitmap The output bitmap
s System.Drawing.Bitmap The RGB source bitmap
a System.Drawing.Bitmap The greyscale mask
Résultat bool
        public static bool CombineBitmapAlpha(Bitmap b, Bitmap s, Bitmap a)
        {
            BitmapData bmData = b.LockBits(new Rectangle(0, 0, b.Width, b.Height), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
            BitmapData smData = s.LockBits(new Rectangle(0, 0, s.Width, s.Height), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
            BitmapData amData = a.LockBits(new Rectangle(0, 0, a.Width, a.Height), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);

            unsafe
            {
                byte* pDestData = (byte*)(void*)bmData.Scan0;
                byte* pSourceData = (byte*)(void*)smData.Scan0;
                byte* pMaskData = (byte*)(void*)amData.Scan0;

                int nDestOffset = bmData.Stride - b.Width * 4;
                int nSourceOffset = smData.Stride - s.Width * 3;
                //If a BW mask, then this should be amData.Stride - a.Width
                //According to Loksim devs, these are both handled identically (Presume as grayscale)
                int nMaskOffset = amData.Stride - a.Width * 4;

                for (int y = 0; y < s.Height; ++y)
                {
                    for (int x = 0; x < s.Width; ++x)
                    {
                        pDestData[0] = pSourceData[0];
                        pDestData[1] = pSourceData[1];
                        pDestData[2] = pSourceData[2];
                        pDestData[3] = pMaskData[0];

                        pSourceData += 3;
                        pDestData += 4;
                        pMaskData += 4;
                    }
                    pSourceData += nSourceOffset;
                    pDestData += nDestOffset;
                    pMaskData += nMaskOffset;
                }
            }

            b.UnlockBits(bmData);
            s.UnlockBits(smData);
            a.UnlockBits(amData);

            return true;
        }
    }