Esempio n. 1
0
        public override Bitmap Apply(Bitmap bmp)
        {
            Bitmap bmpResult = bmp.CreateEmptyBitmap();

            using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly))
                {
                    int right  = source.Width - 1;
                    int bottom = source.Height - 1;

                    for (int y = 0; y < source.Height; y++)
                    {
                        for (int x = 0; x < source.Width; x++)
                        {
                            ColorBgra colorR       = source.GetPixel(MathHelpers.Clamp(x - OffsetRed.X, 0, right), MathHelpers.Clamp(y - OffsetRed.Y, 0, bottom));
                            ColorBgra colorG       = source.GetPixel(MathHelpers.Clamp(x - OffsetGreen.X, 0, right), MathHelpers.Clamp(y - OffsetGreen.Y, 0, bottom));
                            ColorBgra colorB       = source.GetPixel(MathHelpers.Clamp(x - OffsetBlue.X, 0, right), MathHelpers.Clamp(y - OffsetBlue.Y, 0, bottom));
                            ColorBgra shiftedColor = new ColorBgra((byte)(colorB.Blue * colorB.Alpha / 255), (byte)(colorG.Green * colorG.Alpha / 255),
                                                                   (byte)(colorR.Red * colorR.Alpha / 255), (byte)((colorR.Alpha + colorG.Alpha + colorB.Alpha) / 3));
                            dest.SetPixel(x, y, shiftedColor);
                        }
                    }
                }

            return(bmpResult);
        }
Esempio n. 2
0
        public override Bitmap Apply(Bitmap bmp)
        {
            Bitmap bmpResult = bmp.CreateEmptyBitmap();

            using (UnsafeBitmap source = new UnsafeBitmap(bmp, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap dest = new UnsafeBitmap(bmpResult, true, ImageLockMode.WriteOnly))
                {
                    for (int y = 0; y < dest.Height; y++)
                    {
                        for (int x = 0; x < dest.Width; x++)
                        {
                            ColorBgra colorR = source.GetPixel(MathHelpers.Clamp(x + OffsetRed.X, 0, dest.Width - 1), MathHelpers.Clamp(y + OffsetRed.Y, 0, dest.Height - 1));
                            ColorBgra colorG = source.GetPixel(MathHelpers.Clamp(x + OffsetGreen.X, 0, dest.Width - 1), MathHelpers.Clamp(y + OffsetGreen.Y, 0, dest.Height - 1));
                            ColorBgra colorB = source.GetPixel(MathHelpers.Clamp(x + OffsetBlue.X, 0, dest.Width - 1), MathHelpers.Clamp(y + OffsetBlue.Y, 0, dest.Height - 1));

                            byte colorR_alpha = colorR.Alpha;
                            byte colorG_alpha = colorG.Alpha;
                            byte colorB_alpha = colorB.Alpha;
                            byte colorA       = (byte)(colorR_alpha / 3 + colorG_alpha / 3 + colorB_alpha / 3);

                            ColorBgra shiftedColor = new ColorBgra((byte)(colorB.Blue * colorB_alpha / 255), (byte)(colorG.Green * colorG_alpha / 255),
                                                                   (byte)(colorR.Red * colorR_alpha / 255), colorA);
                            dest.SetPixel(x, y, shiftedColor);
                        }
                    }
                }

            return(bmpResult);
        }
Esempio n. 3
0
        private Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                    using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                        using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                        {
                            int pixelCount = blackBitmap.PixelCount;

                            for (int i = 0; i < pixelCount; i++)
                            {
                                ColorBgra white = whiteBitmap.GetPixel(i);
                                ColorBgra black = blackBitmap.GetPixel(i);

                                double alpha = (black.Red - white.Red + 255) / 255.0;

                                if (alpha == 1)
                                {
                                    resultBitmap.SetPixel(i, white);
                                }
                                else if (alpha > 0)
                                {
                                    white.Blue  = (byte)(black.Blue / alpha);
                                    white.Green = (byte)(black.Green / alpha);
                                    white.Red   = (byte)(black.Red / alpha);
                                    white.Alpha = (byte)(255 * alpha);

                                    resultBitmap.SetPixel(i, white);
                                }
                            }
                        }

                return(result);
            }

            return(whiteBackground);
        }
        private static Bitmap CreateTransparentImage(Bitmap whiteBackground, Bitmap blackBackground)
        {
            if (whiteBackground != null && blackBackground != null && whiteBackground.Size == blackBackground.Size)
            {
                Bitmap result = new Bitmap(whiteBackground.Width, whiteBackground.Height, PixelFormat.Format32bppArgb);

                using (UnsafeBitmap whiteBitmap = new UnsafeBitmap(whiteBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap blackBitmap = new UnsafeBitmap(blackBackground, true, ImageLockMode.ReadOnly))
                using (UnsafeBitmap resultBitmap = new UnsafeBitmap(result, true, ImageLockMode.WriteOnly))
                {
                    int pixelCount = blackBitmap.PixelCount;

                    for (int i = 0; i < pixelCount; i++)
                    {
                        ColorBgra white = whiteBitmap.GetPixel(i);
                        ColorBgra black = blackBitmap.GetPixel(i);

                        double alpha = (black.Red - white.Red + 255) / 255.0;

                        if (alpha == 1)
                        {
                            resultBitmap.SetPixel(i, white);
                        }
                        else if (alpha > 0)
                        {
                            white.Blue = (byte)(black.Blue / alpha);
                            white.Green = (byte)(black.Green / alpha);
                            white.Red = (byte)(black.Red / alpha);
                            white.Alpha = (byte)(255 * alpha);

                            resultBitmap.SetPixel(i, white);
                        }
                    }
                }

                return result;
            }

            return whiteBackground;
        }