gpt4 book ai didi

c# - 更快的算法来改变位图中的色相/饱和度/亮度

转载 作者:太空宇宙 更新时间:2023-11-03 20:19:48 28 4
gpt4 key购买 nike

我正在尝试过滤位图图像以增加或减少色相、饱和度和亮度值。

我的代码运行良好,但速度很慢

我在内存中锁定了两个位图,原始源和当前目标。用户可以移动各种 trackbar 控件来修改每个值,然后将其转换为 HSL 值。例如,轨迹栏上的值对应于 -1.0 到 1.0 的范围。

每次引发跟踪栏值更改的事件时,我都会运行一个函数来锁定目标位图并将 HSL 值应用于源位图,然后将结果存储在目标位图中。完成后,我解锁目标位图并在屏幕上绘制图像。

以前我对我的其他过滤器使用了一个查找表,因为我是按字节操作的。但是我不知道如何使用 HSL 来应用它。这是我正在使用的代码:

byte red, green, blue;

for (int i = 0; i < sourceBytes.Length; i += 3)
{
blue = sourceBytes[i];
green = sourceBytes[i + 1];
red = sourceBytes[i + 2];

Color newColor = Color.FromArgb(red, green, blue);

if (ModifyHue)
newColor = HSL.ModifyHue(newColor, Hue);

if (ModifySaturation)
newColor = HSL.ModifySaturation(newColor, Saturation);

if (ModifyLightness)
newColor = HSL.ModifyBrightness(newColor, Lightness);

destBytes[i] = newColor.B;
destBytes[i + 1] = newColor.G;
destBytes[i + 2] = newColor.R;
}

这是我的 ModifyBrightness 函数:

public static Color ModifyBrightness(Color color, double brightness)
{
HSL hsl = FromRGB(color);
hsl.L *= brightness;
return hsl.ToRGB();
}

所以基本上,如果他们的亮度 slider 位于最中间,它的值将为 0,当我将它传递给函数时,我会将其转换为“1.0”,因此它会将亮度乘以 1.0,这意味着它不会改变。如果他们将 slider 一直向右拖动,它的值为 100,这将导致修饰符为 2.0,因此我将亮度值乘以 2.0 使其翻倍。

最佳答案

我最终研究了 ImageAttributes 和 ColorMatrix,发现性能非常好。

下面是我如何为饱和度和亮度滤镜实现它:

// Luminance vector for linear RGB
const float rwgt = 0.3086f;
const float gwgt = 0.6094f;
const float bwgt = 0.0820f;

private ImageAttributes imageAttributes = new ImageAttributes();
private ColorMatrix colorMatrix = new ColorMatrix();
private float saturation = 1.0f;
private float brightness = 1.0f;

protected override void OnPaint(object sender, PaintEventArgs e)
{
base.OnPaint(sender, e);

e.Graphics.DrawImage(_bitmap, BitmapRect, BitmapRect.X, BitmapRect.Y, BitmapRect.Width, BitmapRect.Height, GraphicsUnit.Pixel, imageAttributes);
}

private void saturationTrackBar_ValueChanged(object sender, EventArgs e)
{
saturation = 1f - (saturationTrackBar.Value / 100f);

float baseSat = 1.0f - saturation;

colorMatrix[0, 0] = baseSat * rwgt + saturation;
colorMatrix[0, 1] = baseSat * rwgt;
colorMatrix[0, 2] = baseSat * rwgt;
colorMatrix[1, 0] = baseSat * gwgt;
colorMatrix[1, 1] = baseSat * gwgt + saturation;
colorMatrix[1, 2] = baseSat * gwgt;
colorMatrix[2, 0] = baseSat * bwgt;
colorMatrix[2, 1] = baseSat * bwgt;
colorMatrix[2, 2] = baseSat * bwgt + saturation;

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

Invalidate();
}

private void brightnessTrackBar_ValueChanged(object sender, EventArgs e)
{
brightness = 1f + (brightnessTrackBar.Value / 100f);

float adjustedBrightness = brightness - 1f;

colorMatrix[4, 0] = adjustedBrightness;
colorMatrix[4, 1] = adjustedBrightness;
colorMatrix[4, 2] = adjustedBrightness;

imageAttributes.SetColorMatrix(colorMatrix, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);

Invalidate();
}

关于c# - 更快的算法来改变位图中的色相/饱和度/亮度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14364716/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com