예제 #1
0
        /// <summary>
        /// Apply shadow to mask onto the canvas
        /// </summary>
        /// <param name="clr">is the shadow 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 ApplyShadowToMask(
            System.Drawing.Color clrShadow,
            System.Drawing.Bitmap mask,
            System.Drawing.Bitmap canvas,
            System.Drawing.Color maskColor)
        {
            if (mask == null || canvas == null)
            {
                return(false);
            }

            BitmapData bitmapDataMask   = new BitmapData();
            BitmapData bitmapDataCanvas = new BitmapData();
            Rectangle  rectCanvas       = new Rectangle(0, 0, canvas.Width, canvas.Height);
            Rectangle  rectMask         = new Rectangle(0, 0, mask.Width, mask.Height);

            mask.LockBits(
                rectMask,
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataMask);

            canvas.LockBits(
                rectCanvas,
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataCanvas);

            unsafe
            {
                uint *pixelsMask   = (uint *)bitmapDataMask.Scan0;
                uint *pixelsCanvas = (uint *)bitmapDataCanvas.Scan0;

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

                uint col    = 0;
                int  stride = bitmapDataCanvas.Stride >> 2;
                for (uint row = 0; row < bitmapDataCanvas.Height; ++row)
                {
                    uint total_row_len      = (uint)(row * stride);
                    uint total_row_mask_len = (uint)(row * (bitmapDataMask.Stride >> 2));
                    for (col = 0; col < bitmapDataCanvas.Width; ++col)
                    {
                        if (row >= bitmapDataMask.Height || col >= bitmapDataMask.Width)
                        {
                            continue;
                        }

                        uint index     = total_row_len + col;
                        uint indexMask = total_row_mask_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);
                        }

                        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.UnlockBits(bitmapDataCanvas);
            mask.UnlockBits(bitmapDataMask);

            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.Drawing.Bitmap mask,
            System.Drawing.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);
            }

            BitmapData bitmapDataMask = new BitmapData();
            Rectangle  rect           = new Rectangle(0, 0, mask.Width, mask.Height);

            mask.LockBits(
                rect,
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataMask);

            unsafe
            {
                uint *pixelsMask = (uint *)bitmapDataMask.Scan0;

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

                uint col    = 0;
                int  stride = bitmapDataMask.Stride >> 2;
                for (uint row = 0; row < bitmapDataMask.Height; ++row)
                {
                    uint total_row_len = (uint)(row * stride);
                    for (col = 0; col < bitmapDataMask.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.UnlockBits(bitmapDataMask);

            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.Drawing.Bitmap image,
            System.Drawing.Bitmap mask,
            System.Drawing.Bitmap canvas,
            System.Drawing.Color maskColor,
            bool NoAlphaAtBoundary)
        {
            if (image == null || mask == null || canvas == null)
            {
                return(false);
            }

            BitmapData bitmapDataImage  = new BitmapData();
            BitmapData bitmapDataMask   = new BitmapData();
            BitmapData bitmapDataCanvas = new BitmapData();
            Rectangle  rectCanvas       = new Rectangle(0, 0, canvas.Width, canvas.Height);
            Rectangle  rectMask         = new Rectangle(0, 0, mask.Width, mask.Height);
            Rectangle  rectImage        = new Rectangle(0, 0, image.Width, image.Height);

            image.LockBits(
                rectImage,
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataImage);

            mask.LockBits(
                rectMask,
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataMask);

            canvas.LockBits(
                rectCanvas,
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataCanvas);

            unsafe
            {
                uint *pixelsImage  = (uint *)bitmapDataImage.Scan0;
                uint *pixelsMask   = (uint *)bitmapDataMask.Scan0;
                uint *pixelsCanvas = (uint *)bitmapDataCanvas.Scan0;

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

                uint col    = 0;
                int  stride = bitmapDataCanvas.Stride >> 2;
                for (uint row = 0; row < bitmapDataCanvas.Height; ++row)
                {
                    uint total_row_len       = (uint)(row * stride);
                    uint total_row_mask_len  = (uint)(row * (bitmapDataMask.Stride >> 2));
                    uint total_row_image_len = (uint)(row * (bitmapDataImage.Stride >> 2));
                    for (col = 0; col < bitmapDataCanvas.Width; ++col)
                    {
                        if (row >= bitmapDataImage.Height || col >= bitmapDataImage.Width)
                        {
                            continue;
                        }
                        if (row >= bitmapDataMask.Height || col >= bitmapDataMask.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.UnlockBits(bitmapDataCanvas);
            mask.UnlockBits(bitmapDataMask);
            image.UnlockBits(bitmapDataImage);

            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>
        /// <param name="offset">determine how much to offset the mask</param>
        /// <returns>true if successful</returns>
        public static bool ApplyColorToMask(
            System.Drawing.Color clr,
            System.Drawing.Bitmap mask,
            System.Drawing.Bitmap canvas,
            System.Drawing.Color maskColor,
            System.Drawing.Point offset)
        {
            if (mask == null || canvas == null)
            {
                return(false);
            }

            BitmapData bitmapDataMask   = new BitmapData();
            BitmapData bitmapDataCanvas = new BitmapData();
            Rectangle  rectCanvas       = new Rectangle(0, 0, canvas.Width, canvas.Height);
            Rectangle  rectMask         = new Rectangle(0, 0, mask.Width, mask.Height);

            mask.LockBits(
                rectMask,
                ImageLockMode.ReadOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataMask);

            canvas.LockBits(
                rectCanvas,
                ImageLockMode.WriteOnly,
                PixelFormat.Format32bppArgb,
                bitmapDataCanvas);

            unsafe
            {
                uint *pixelsMask   = (uint *)bitmapDataMask.Scan0;
                uint *pixelsCanvas = (uint *)bitmapDataCanvas.Scan0;

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

                uint col    = 0;
                int  stride = bitmapDataCanvas.Stride >> 2;
                for (uint row = 0; row < bitmapDataCanvas.Height; ++row)
                {
                    for (col = 0; col < bitmapDataCanvas.Width; ++col)
                    {
                        if ((row - offset.Y) >= bitmapDataMask.Height || (col - offset.X) >= bitmapDataMask.Width ||
                            (row - offset.Y) < 0 || (col - offset.X) < 0)
                        {
                            continue;
                        }

                        uint index     = (uint)(row * stride + col);
                        uint indexMask = (uint)((row - offset.Y) * (bitmapDataMask.Stride >> 2) + (col - 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 | 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.UnlockBits(bitmapDataCanvas);
            mask.UnlockBits(bitmapDataMask);

            return(true);
        }