【WPF学习手记】图像处理:亮度、饱和度、对比度、Gamma调节

    WPF图像处理程序,包括亮度、饱和度、对比度、Gamma调节。

 

// Lightness Adjust
        public static void BitmapLightnessAdjust(Bitmap curBitmap, int width, int height, int delta)
        {
            if (delta == 0)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = width * height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            if (delta > 0)
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    rgbValues[i] = (byte)Math.Min(255, rgbValues[i] + delta);
                    rgbValues[i + 1] = (byte)Math.Min(255, rgbValues[i + 1] + delta);
                    rgbValues[i + 2] = (byte)Math.Min(255, rgbValues[i + 2] + delta);
                }
            }
            else
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    rgbValues[i] = (byte)Math.Max(0, rgbValues[i] + delta);
                    rgbValues[i + 1] = (byte)Math.Max(0, rgbValues[i + 1] + delta);
                    rgbValues[i + 2] = (byte)Math.Max(0, rgbValues[i + 2] + delta);
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

        // Saturation Adjust
        public static void BitmapSaturationAdjust(Bitmap curBitmap, int width, int height, int delta)
        {
            if (delta == 0)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = width * height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int i = 0; i < rgbValues.Length; i = i + 4)
            {
                if (rgbValues[i] > 128)
                {
                    rgbValues[i] = (byte)Math.Min(255, rgbValues[i] + delta);
                }
                else
                {
                    rgbValues[i] = (byte)Math.Max(0, rgbValues[i] - delta);
                }
                if (rgbValues[i + 1] > 128)
                {
                    rgbValues[i + 1] = (byte)Math.Min(255, rgbValues[i + 1] + delta);
                }
                else
                {
                    rgbValues[i + 1] = (byte)Math.Max(0, rgbValues[i + 1] - delta);
                }
                if (rgbValues[i + 2] > 128)
                {
                    rgbValues[i + 2] = (byte)Math.Min(255, rgbValues[i + 2] + delta);
                }
                else
                {
                    rgbValues[i + 2] = (byte)Math.Max(0, rgbValues[i + 2] - delta);
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

        // Contrast Adjust
        public static void BitmapContrastAdjust(Bitmap curBitmap, int width, int height, int delta)
        {
            if (delta == 0)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = width * height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            if (delta > 0)
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    if (rgbValues[i] > 128 && rgbValues[i + 1] > 128 && rgbValues[i + 2] > 128)
                    {
                        rgbValues[i] = (byte)Math.Min(255, rgbValues[i] + delta);
                        rgbValues[i + 1] = (byte)Math.Min(255, rgbValues[i + 1] + delta);
                        rgbValues[i + 2] = (byte)Math.Min(255, rgbValues[i + 2] + delta);
                    }
                    if (rgbValues[i] < 128 && rgbValues[i + 1] < 128 && rgbValues[i + 2] < 128)
                    {
                        rgbValues[i] = (byte)Math.Max(0, rgbValues[i] - delta);
                        rgbValues[i + 1] = (byte)Math.Max(0, rgbValues[i + 1] - delta);
                        rgbValues[i + 2] = (byte)Math.Max(0, rgbValues[i + 2] - delta);
                    }
                }
            }
            else
            {
                for (int i = 0; i < rgbValues.Length; i = i + 4)
                {
                    if (rgbValues[i] > 128 && rgbValues[i + 1] > 128 && rgbValues[i + 2] > 128)
                    {
                        rgbValues[i] = (byte)Math.Max(128, rgbValues[i] + delta);
                        rgbValues[i + 1] = (byte)Math.Max(128, rgbValues[i + 1] + delta);
                        rgbValues[i + 2] = (byte)Math.Max(128, rgbValues[i + 2] + delta);
                    }
                    if (rgbValues[i] < 128 && rgbValues[i + 1] < 128 && rgbValues[i + 2] < 128)
                    {
                        rgbValues[i] = (byte)Math.Min(128, rgbValues[i] - delta);
                        rgbValues[i + 1] = (byte)Math.Min(128, rgbValues[i + 1] - delta);
                        rgbValues[i + 2] = (byte)Math.Min(128, rgbValues[i + 2] - delta);
                    }
                }
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

        // Gamma Adjust
        public static void BitmapGammaAdjust(Bitmap curBitmap, int width, int height, double delta)
        {
            if (delta == 1)
            {
                return;
            }
            Rectangle rect = new Rectangle(0, 0, width, height);
            BitmapData bmpData = curBitmap.LockBits(rect, ImageLockMode.ReadWrite, curBitmap.PixelFormat);
            IntPtr ptr = bmpData.Scan0;
            int bytes = width * height * 4;
            byte[] rgbValues = new byte[bytes];
            Marshal.Copy(ptr, rgbValues, 0, bytes);
            for (int i = 0; i < rgbValues.Length; i = i + 4)
            {
                rgbValues[i] = (byte)Math.Min(255, (int)Math.Round(255 * Math.Pow(rgbValues[i] / 255.0, delta)));
                rgbValues[i + 1] = (byte)Math.Min(255, (int)Math.Round(255 * Math.Pow(rgbValues[i + 1] / 255.0, delta)));
                rgbValues[i + 2] = (byte)Math.Min(255, (int)Math.Round(255 * Math.Pow(rgbValues[i + 1] / 255.0, delta)));
            }
            Marshal.Copy(rgbValues, 0, ptr, bytes);
            curBitmap.UnlockBits(bmpData);
        }

 

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值