示例#1
0
        /// <summary>
        /// Apply shadow to mask onto the canvas
        /// </summary>
        /// <param name="clrShadow">is the color to be used</param>
        /// <param name="mask">is the mask image to be read</param>
        /// <param name="canvas">is the destination image to be draw upon</param>
        /// <param name="maskColor">is mask color used in mask image</param>
        /// <param name="offset">determine how much to offset the mask</param>
        /// <returns>true if successful</returns>
        public static bool ApplyShadowToMask(
            System.Windows.Media.Color clrShadow,
            System.Windows.Media.Imaging.WriteableBitmap mask,
            System.Windows.Media.Imaging.WriteableBitmap canvas,
            System.Windows.Media.Color maskColor,
            System.Windows.Point offset)
        {
            if (mask == null || canvas == null)
            {
                return(false);
            }


            mask.Lock();

            canvas.Lock();

            unsafe
            {
                uint *pixelsMask   = (uint *)mask.BackBuffer;
                uint *pixelsCanvas = (uint *)canvas.BackBuffer;

                if (pixelsMask == null || pixelsCanvas == null)
                {
                    return(false);
                }

                uint col    = 0;
                int  stride = canvas.BackBufferStride >> 2;
                for (int row = 0; row < canvas.Height; ++row)
                {
                    uint total_row_len      = (uint)(row * stride);
                    uint total_row_mask_len = (uint)((row - (int)offset.Y) * (mask.BackBufferStride >> 2));
                    for (col = 0; col < canvas.Width; ++col)
                    {
                        if (row - (int)offset.Y >= mask.Height || col - (int)offset.X >= mask.Width ||
                            row - (int)offset.Y < 0 || col - (int)offset.X < 0)
                        {
                            continue;
                        }

                        uint index     = total_row_len + col;
                        uint indexMask = (uint)(total_row_mask_len + (col - (int)offset.X));

                        byte maskByte = 0;

                        if (MaskColor.IsEqual(maskColor, MaskColor.Red))
                        {
                            maskByte = (Byte)((pixelsMask[indexMask] & 0xff0000) >> 16);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Green))
                        {
                            maskByte = (Byte)((pixelsMask[indexMask] & 0xff00) >> 8);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Blue))
                        {
                            maskByte = (Byte)(pixelsMask[indexMask] & 0xff);
                        }

                        uint color = (uint)(0xff << 24 | clrShadow.R << 16 | clrShadow.G << 8 | clrShadow.B);

                        if (maskByte > 0)
                        {
                            uint maskAlpha = (pixelsMask[indexMask] >> 24);
                            pixelsCanvas[index] = Alphablend(pixelsCanvas[index], color, (Byte)(maskAlpha), (Byte)(maskAlpha * clrShadow.A / 255));
                        }
                    }
                }
            }
            canvas.Unlock();
            mask.Unlock();

            return(true);
        }
示例#2
0
        /// <summary>
        /// Measure the mask image based on the mask color.
        /// </summary>
        /// <param name="mask">is the mask image to be measured</param>
        /// <param name="maskColor">is mask color used in mask image</param>
        /// <param name="top">returns the topmost Y </param>
        /// <param name="left">returns the leftmost X</param>
        /// <param name="bottom">returns the bottommost Y</param>
        /// <param name="right">returns the rightmost X</param>
        /// <returns>true if successful</returns>
        public static bool MeasureMaskLength(
            System.Windows.Media.Imaging.WriteableBitmap mask,
            System.Windows.Media.Color maskColor,
            ref uint top,
            ref uint left,
            ref uint bottom,
            ref uint right)
        {
            top    = 30000;
            left   = 30000;
            bottom = 0;
            right  = 0;

            if (mask == null)
            {
                return(false);
            }

            mask.Lock();

            unsafe
            {
                uint *pixelsMask = (uint *)mask.BackBuffer;

                if (pixelsMask == null)
                {
                    return(false);
                }

                uint col    = 0;
                int  stride = mask.BackBufferStride >> 2;
                for (uint row = 0; row < mask.Height; ++row)
                {
                    uint total_row_len = (uint)(row * stride);
                    for (col = 0; col < mask.Width; ++col)
                    {
                        uint index  = total_row_len + col;
                        byte nAlpha = 0;

                        if (MaskColor.IsEqual(maskColor, MaskColor.Red))
                        {
                            nAlpha = (Byte)((pixelsMask[index] & 0xff0000) >> 16);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Green))
                        {
                            nAlpha = (Byte)((pixelsMask[index] & 0xff00) >> 8);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Blue))
                        {
                            nAlpha = (Byte)(pixelsMask[index] & 0xff);
                        }

                        if (nAlpha > 0)
                        {
                            if (col < left)
                            {
                                left = col;
                            }
                            if (row < top)
                            {
                                top = row;
                            }
                            if (col > right)
                            {
                                right = col;
                            }
                            if (row > bottom)
                            {
                                bottom = row;
                            }
                        }
                    }
                }
            }
            mask.Unlock();

            return(true);
        }
示例#3
0
        /// <summary>
        /// Apply image to mask onto the canvas
        /// </summary>
        /// <param name="image">is the image to be used</param>
        /// <param name="mask">is the mask image to be read</param>
        /// <param name="canvas">is the destination image to be draw upon</param>
        /// <param name="maskColor">is mask color used in mask image</param>
        /// <returns>true if successful</returns>
        public static bool ApplyImageToMask(
            System.Windows.Media.Imaging.WriteableBitmap image,
            System.Windows.Media.Imaging.WriteableBitmap mask,
            System.Windows.Media.Imaging.WriteableBitmap canvas,
            System.Windows.Media.Color maskColor,
            bool NoAlphaAtBoundary)
        {
            if (image == null || mask == null || canvas == null)
            {
                return(false);
            }

            image.Lock();

            mask.Lock();

            canvas.Lock();

            unsafe
            {
                uint *pixelsImage  = (uint *)image.BackBuffer;
                uint *pixelsMask   = (uint *)mask.BackBuffer;
                uint *pixelsCanvas = (uint *)canvas.BackBuffer;

                if (pixelsImage == null || pixelsMask == null || pixelsCanvas == null)
                {
                    return(false);
                }

                uint col    = 0;
                int  stride = canvas.BackBufferStride >> 2;
                for (uint row = 0; row < canvas.Height; ++row)
                {
                    uint total_row_len       = (uint)(row * stride);
                    uint total_row_mask_len  = (uint)(row * (mask.BackBufferStride >> 2));
                    uint total_row_image_len = (uint)(row * (image.BackBufferStride >> 2));
                    for (col = 0; col < canvas.Width; ++col)
                    {
                        if (row >= image.Height || col >= image.Width)
                        {
                            continue;
                        }
                        if (row >= mask.Height || col >= mask.Width)
                        {
                            continue;
                        }

                        uint index      = total_row_len + col;
                        uint indexMask  = total_row_mask_len + col;
                        uint indexImage = total_row_image_len + col;

                        byte maskByte = 0;

                        if (MaskColor.IsEqual(maskColor, MaskColor.Red))
                        {
                            maskByte = (Byte)((pixelsMask[indexMask] & 0xff0000) >> 16);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Green))
                        {
                            maskByte = (Byte)((pixelsMask[indexMask] & 0xff00) >> 8);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Blue))
                        {
                            maskByte = (Byte)(pixelsMask[indexMask] & 0xff);
                        }

                        if (maskByte > 0)
                        {
                            if (NoAlphaAtBoundary)
                            {
                                pixelsCanvas[index] = AlphablendNoAlphaAtBoundary(pixelsCanvas[index], pixelsImage[indexImage], (Byte)(pixelsMask[indexMask] >> 24), (Byte)(pixelsMask[indexMask] >> 24));
                            }
                            else
                            {
                                pixelsCanvas[index] = Alphablend(pixelsCanvas[index], pixelsImage[indexImage], (Byte)(pixelsMask[indexMask] >> 24), (Byte)(pixelsMask[indexMask] >> 24));
                            }
                        }
                    }
                }
            }
            canvas.Unlock();
            mask.Unlock();
            image.Unlock();

            return(true);
        }
示例#4
0
        /// <summary>
        /// Apply color to mask onto the canvas
        /// </summary>
        /// <param name="clr">is the color to be used</param>
        /// <param name="mask">is the mask image to be read</param>
        /// <param name="canvas">is the destination image to be draw upon</param>
        /// <param name="maskColor">is mask color used in mask image</param>
        /// <returns>true if successful</returns>
        public static bool ApplyColorToMask(
            System.Windows.Media.Color clr,
            System.Windows.Media.Imaging.WriteableBitmap mask,
            System.Windows.Media.Imaging.WriteableBitmap canvas,
            System.Windows.Media.Color maskColor)
        {
            if (mask == null || canvas == null)
            {
                return(false);
            }


            mask.Lock();

            canvas.Lock();

            unsafe
            {
                uint *pixelsMask   = (uint *)mask.BackBuffer;
                uint *pixelsCanvas = (uint *)canvas.BackBuffer;

                if (pixelsMask == null || pixelsCanvas == null)
                {
                    return(false);
                }

                uint col    = 0;
                int  stride = canvas.BackBufferStride >> 2;
                for (uint row = 0; row < canvas.Height; ++row)
                {
                    for (col = 0; col < canvas.Width; ++col)
                    {
                        if (row >= mask.Height || col >= mask.Width)
                        {
                            continue;
                        }

                        uint index     = (uint)(row * stride + col);
                        uint indexMask = (uint)(row * (mask.BackBufferStride >> 2) + col);

                        byte maskByte = 0;

                        if (MaskColor.IsEqual(maskColor, MaskColor.Red))
                        {
                            maskByte = (Byte)((pixelsMask[indexMask] & 0xff0000) >> 16);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Green))
                        {
                            maskByte = (Byte)((pixelsMask[indexMask] & 0xff00) >> 8);
                        }
                        else if (MaskColor.IsEqual(maskColor, MaskColor.Blue))
                        {
                            maskByte = (Byte)(pixelsMask[indexMask] & 0xff);
                        }

                        uint color = (uint)(0xff << 24 | clr.R << 16 | clr.G << 8 | clr.B);

                        if (maskByte > 0)
                        {
                            pixelsCanvas[index] = Alphablend(pixelsCanvas[index], color, (Byte)(pixelsMask[indexMask] >> 24), (Byte)(pixelsMask[indexMask] >> 24));
                        }
                    }
                }
            }
            canvas.Unlock();
            mask.Unlock();

            return(true);
        }