コード例 #1
0
ファイル: Utils.cs プロジェクト: sahwar/Brush-Factory
        /// <summary>
        /// Multiplies alpha by an amount.
        /// </summary>
        /// <param name="img">
        /// The affected image.
        /// </param>
        /// <param name="alpha">A value from 0 to 1 to multiply with.</param>
        public static unsafe void ColorImage(Bitmap img, float alpha)
        {
            BitmapData bmpData = img.LockBits(
                new Rectangle(0, 0,
                              img.Width,
                              img.Height),
                ImageLockMode.ReadOnly,
                img.PixelFormat);

            byte *row = (byte *)bmpData.Scan0;

            for (int y = 0; y < img.Height; y++)
            {
                for (int x = 0; x < img.Width; x++)
                {
                    int       ptr = y * bmpData.Stride + x * 4;
                    ColorBgra unmultipliedPixel = ColorBgra.FromBgra(row[ptr], row[ptr + 1], row[ptr + 2], row[ptr + 3]).ConvertFromPremultipliedAlpha();

                    unmultipliedPixel.A = (byte)(unmultipliedPixel.A * alpha);

                    ColorBgra premultiplied = unmultipliedPixel.ConvertToPremultipliedAlpha();

                    row[ptr + 3] = premultiplied.A;
                    row[ptr + 2] = premultiplied.R;
                    row[ptr + 1] = premultiplied.G;
                    row[ptr]     = premultiplied.B;
                }
            }

            img.UnlockBits(bmpData);
        }
コード例 #2
0
        /// <summary>
        /// Overwrites RGB channels and multiplies alpha.
        /// </summary>
        /// <param name="img">
        /// The affected image.
        /// </param>
        /// <param name="color">The color to overwrite the image with.</param>
        /// <param name="alpha">A value from 0 to 1 to multiply with.</param>
        public static unsafe void ColorImage(Bitmap img, Color?col, float alpha)
        {
            BitmapData bmpData = img.LockBits(
                new Rectangle(0, 0,
                              img.Width,
                              img.Height),
                ImageLockMode.ReadOnly,
                img.PixelFormat);

            byte *    row   = (byte *)bmpData.Scan0;
            Color     color = col ?? default;
            ColorBgra pixel;

            for (int y = 0; y < img.Height; y++)
            {
                for (int x = 0; x < img.Width; x++)
                {
                    int ptr = y * bmpData.Stride + x * 4;

                    if (col != null)
                    {
                        pixel = ColorBgra.FromBgra(color.B, color.G, color.R, (byte)(row[ptr + 3] * alpha)).ConvertToPremultipliedAlpha();
                    }
                    else
                    {
                        ColorBgra unmultipliedPixel = ColorBgra.FromBgra(row[ptr], row[ptr + 1], row[ptr + 2], row[ptr + 3]).ConvertFromPremultipliedAlpha();
                        unmultipliedPixel.A = (byte)(unmultipliedPixel.A * alpha);
                        pixel = unmultipliedPixel.ConvertToPremultipliedAlpha();
                    }

                    row[ptr + 3] = pixel.A;
                    row[ptr + 2] = pixel.R;
                    row[ptr + 1] = pixel.G;
                    row[ptr]     = pixel.B;
                }
            }

            img.UnlockBits(bmpData);
        }