mirror of https://github.com/SixLabors/ImageSharp
Browse Source
Former-commit-id: 0ccb55b3b1d634cbdf289c4496006174d8e382dc Former-commit-id: ea1ba7bbf746e2cf0833e591e451b6ca4df78458 Former-commit-id: 1a6addaf532b4a547bd84d37d74db7198d940d30af/merge-core
6 changed files with 171 additions and 8 deletions
@ -0,0 +1,77 @@ |
|||
// <copyright file="Brightness.cs" company="James South">
|
|||
// Copyright (c) James South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Filters |
|||
{ |
|||
using System; |
|||
using System.Numerics; |
|||
using System.Threading.Tasks; |
|||
|
|||
/// <summary>
|
|||
/// An <see cref="IImageProcessor"/> to change the brightness of an <see cref="Image"/>.
|
|||
/// </summary>
|
|||
public class Brightness : ParallelImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Brightness"/> class.
|
|||
/// </summary>
|
|||
/// <param name="brightness">The new brightness of the image. Must be between -100 and 100.</param>
|
|||
/// <exception cref="ArgumentException">
|
|||
/// <paramref name="brightness"/> is less than -100 or is greater than 100.
|
|||
/// </exception>
|
|||
public Brightness(int brightness) |
|||
{ |
|||
Guard.MustBeBetweenOrEqualTo(brightness, -100, 100, nameof(brightness)); |
|||
this.Value = brightness; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the brightness value.
|
|||
/// </summary>
|
|||
public int Value { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
|||
{ |
|||
float brightness = this.Value / 100f; |
|||
int sourceY = sourceRectangle.Y; |
|||
int sourceBottom = sourceRectangle.Bottom; |
|||
int startX = sourceRectangle.X; |
|||
int endX = sourceRectangle.Right; |
|||
|
|||
Parallel.For( |
|||
startY, |
|||
endY, |
|||
y => |
|||
{ |
|||
if (y >= sourceY && y < sourceBottom) |
|||
{ |
|||
for (int x = startX; x < endX; x++) |
|||
{ |
|||
target[x, y] = AdjustBrightness(source[x, y], brightness); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a <see cref="Color"/> with the brightness adjusted.
|
|||
/// </summary>
|
|||
/// <param name="color">The source color.</param>
|
|||
/// <param name="brightness">The brightness adjustment factor.</param>
|
|||
/// <returns>
|
|||
/// The <see cref="Color"/>.
|
|||
/// </returns>
|
|||
private static Color AdjustBrightness(Color color, float brightness) |
|||
{ |
|||
color = PixelOperations.ToLinear(color); |
|||
|
|||
Vector3 vector3 = color.ToVector3(); |
|||
vector3 += new Vector3(brightness, brightness, brightness); |
|||
|
|||
return PixelOperations.ToSrgb(new Color(vector3, color.A)); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,78 @@ |
|||
// <copyright file="Saturation.cs" company="James South">
|
|||
// Copyright (c) James South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Filters |
|||
{ |
|||
using System; |
|||
using System.Numerics; |
|||
using System.Threading.Tasks; |
|||
|
|||
/// <summary>
|
|||
/// An <see cref="IImageProcessor"/> to change the saturation of an <see cref="Image"/>.
|
|||
/// </summary>
|
|||
public class Saturation : ParallelImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Saturation"/> class.
|
|||
/// </summary>
|
|||
/// <param name="saturation">The new saturation of the image. Must be between -100 and 100.</param>
|
|||
/// <exception cref="ArgumentException">
|
|||
/// <paramref name="saturation"/> is less than -100 or is greater than 100.
|
|||
/// </exception>
|
|||
public Saturation(int saturation) |
|||
{ |
|||
Guard.MustBeBetweenOrEqualTo(saturation, -100, 100, nameof(saturation)); |
|||
this.Value = saturation; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the saturation value.
|
|||
/// </summary>
|
|||
public int Value { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
|||
{ |
|||
float saturation = this.Value / 100f; |
|||
int sourceY = sourceRectangle.Y; |
|||
int sourceBottom = sourceRectangle.Bottom; |
|||
int startX = sourceRectangle.X; |
|||
int endX = sourceRectangle.Right; |
|||
|
|||
Parallel.For( |
|||
startY, |
|||
endY, |
|||
y => |
|||
{ |
|||
if (y >= sourceY && y < sourceBottom) |
|||
{ |
|||
for (int x = startX; x < endX; x++) |
|||
{ |
|||
target[x, y] = AdjustSaturation(source[x, y], saturation); |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Returns a <see cref="Color"/> with the saturation adjusted.
|
|||
/// </summary>
|
|||
/// <param name="color">The source color.</param>
|
|||
/// <param name="saturation">The saturation adjustment factor.</param>
|
|||
/// <returns>
|
|||
/// The <see cref="Color"/>.
|
|||
/// </returns>
|
|||
private static Color AdjustSaturation(Color color, float saturation) |
|||
{ |
|||
//color = PixelOperations.ToLinear(color);
|
|||
|
|||
// TODO: This can be done with a matrix. But why can I not get conversion to work?
|
|||
Hsv hsv = color; |
|||
return new Hsv(hsv.H, saturation, hsv.V); |
|||
|
|||
//return PixelOperations.ToSrgb(newHsv);
|
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue