mirror of https://github.com/SixLabors/ImageSharp
Browse Source
- Some predefined colors - Source image should not get overwritten - Begin EntropyCrop - unfinished. Former-commit-id: 15096078a8c4c81e5c38b07111496b9382e98583 Former-commit-id: 1525d80b8094e74ee0b1a64a291e9fdc606b0438 Former-commit-id: 7ca269955bd18bb8c1793b3038c95f8f976694a1af/merge-core
19 changed files with 534 additions and 70 deletions
@ -0,0 +1,121 @@ |
|||
// <copyright file="ColorDefinitions.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor |
|||
{ |
|||
public partial struct Color |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #F0F8FF.
|
|||
/// </summary>
|
|||
public static readonly Color AliceBlue = new Color(240 / 255f, 248 / 255f, 1); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #FAEBD7.
|
|||
/// </summary>
|
|||
public static readonly Color AntiqueWhite = new Color(250 / 255f, 235 / 255f, 215 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #00FFFF.
|
|||
/// </summary>
|
|||
public static readonly Color Aqua = new Color(0, 1, 1); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #7FFFD4.
|
|||
/// </summary>
|
|||
public static readonly Color AquaMarine = new Color(127 / 255f, 1, 212 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #F0FFFF.
|
|||
/// </summary>
|
|||
public static readonly Color Azure = new Color(240 / 255f, 1, 1); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #F5F5DC.
|
|||
/// </summary>
|
|||
public static readonly Color Beige = new Color(245 / 255f, 245 / 255f, 220 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #FFE4C4.
|
|||
/// </summary>
|
|||
public static readonly Color Bisque = new Color(1, 228 / 255f, 196 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #000000.
|
|||
/// </summary>
|
|||
public static readonly Color Black = new Color(0, 0, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #0000FF.
|
|||
/// </summary>
|
|||
public static readonly Color Blue = new Color(0, 0, 1); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #C0C0C0.
|
|||
/// </summary>
|
|||
public static readonly Color Silver = new Color(192 / 255f, 192 / 255f, 192 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #808080.
|
|||
/// </summary>
|
|||
public static readonly Color Gray = new Color(128 / 255f, 128 / 255f, 128 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #FFFFFF.
|
|||
/// </summary>
|
|||
public static readonly Color White = new Color(1, 1, 1); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #800000.
|
|||
/// </summary>
|
|||
public static readonly Color Maroon = new Color(128 / 255f, 0, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #FF0000.
|
|||
/// </summary>
|
|||
public static readonly Color Red = new Color(1, 0, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #800080.
|
|||
/// </summary>
|
|||
public static readonly Color Purple = new Color(128 / 255f, 0, 128 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #FF00FF.
|
|||
/// </summary>
|
|||
public static readonly Color Fuchsia = new Color(1, 0, 1); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #008000.
|
|||
/// </summary>
|
|||
public static readonly Color Green = new Color(0, 128 / 255f, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #00FF00.
|
|||
/// </summary>
|
|||
public static readonly Color Lime = new Color(0, 1, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #808000.
|
|||
/// </summary>
|
|||
public static readonly Color Olive = new Color(128 / 255f, 128 / 255f, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #FFFF00.
|
|||
/// </summary>
|
|||
public static readonly Color Yellow = new Color(1, 1, 0); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #000080.
|
|||
/// </summary>
|
|||
public static readonly Color Navy = new Color(0, 0, 128 / 255f); |
|||
|
|||
/// <summary>
|
|||
/// Represents a <see cref="Color"/> matching the W3C definition that has an hex value of #008080.
|
|||
/// </summary>
|
|||
public static readonly Color Teal = new Color(0, 128 / 255f, 128 / 255f); |
|||
|
|||
} |
|||
} |
|||
@ -0,0 +1,33 @@ |
|||
// <copyright file="RgbaComponent.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Enumerates the RGBA (red, green, blue, alpha) color components.
|
|||
/// </summary>
|
|||
public enum RgbaComponent |
|||
{ |
|||
/// <summary>
|
|||
/// The blue component.
|
|||
/// </summary>
|
|||
B = 0, |
|||
|
|||
/// <summary>
|
|||
/// The green component.
|
|||
/// </summary>
|
|||
G = 1, |
|||
|
|||
/// <summary>
|
|||
/// The red component.
|
|||
/// </summary>
|
|||
R = 2, |
|||
|
|||
/// <summary>
|
|||
/// The alpha component.
|
|||
/// </summary>
|
|||
A = 3 |
|||
} |
|||
} |
|||
@ -0,0 +1,69 @@ |
|||
// <copyright file="Threshold.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Filters |
|||
{ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
/// <summary>
|
|||
/// An <see cref="IImageProcessor"/> to perform binary threshold filtering against an
|
|||
/// <see cref="Image"/>. The mage will be converted to greyscale before thresholding
|
|||
/// occurrs.
|
|||
/// </summary>
|
|||
public class Threshold : ParallelImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="Threshold"/> class.
|
|||
/// </summary>
|
|||
/// <param name="threshold">The threshold to split the image. Must be between 0 and 1.</param>
|
|||
/// <exception cref="ArgumentException">
|
|||
/// <paramref name="threshold"/> is less than 0 or is greater than 1.
|
|||
/// </exception>
|
|||
public Threshold(float threshold) |
|||
{ |
|||
Guard.MustBeBetweenOrEqualTo(threshold, 0, 1, nameof(threshold)); |
|||
this.Value = threshold; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the threshold value.
|
|||
/// </summary>
|
|||
public float Value { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle) |
|||
{ |
|||
new GreyscaleBt709().Apply(source, source, sourceRectangle); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
|||
{ |
|||
float threshold = this.Value; |
|||
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++) |
|||
{ |
|||
Color color = source[x, y]; |
|||
|
|||
// Any channel will do since it's greyscale.
|
|||
target[x, y] = color.B >= threshold ? Color.White : Color.Black; |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,77 @@ |
|||
// <copyright file="EntropyCrop.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageProcessor.Samplers |
|||
{ |
|||
using System; |
|||
using System.Threading.Tasks; |
|||
|
|||
using ImageProcessor.Filters; |
|||
|
|||
/// <summary>
|
|||
/// Provides methods to allow the cropping of an image to preserve areas of highest
|
|||
/// entropy.
|
|||
/// </summary>
|
|||
public class EntropyCrop : ParallelImageProcessor |
|||
{ |
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="EntropyCrop"/> class.
|
|||
/// </summary>
|
|||
/// <param name="threshold">The threshold to split the image. Must be between 0 and 1.</param>
|
|||
/// <exception cref="ArgumentException">
|
|||
/// <paramref name="threshold"/> is less than 0 or is greater than 1.
|
|||
/// </exception>
|
|||
public EntropyCrop(float threshold) |
|||
{ |
|||
Guard.MustBeBetweenOrEqualTo(threshold, 0, 1, nameof(threshold)); |
|||
this.Value = threshold; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the threshold value.
|
|||
/// </summary>
|
|||
public float Value { get; } |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void OnApply(ImageBase source, ImageBase target, Rectangle targetRectangle, Rectangle sourceRectangle) |
|||
{ |
|||
ImageBase temp = new Image(source.Width, source.Height); |
|||
|
|||
// TODO: Should we detect edges on a grayscale image?
|
|||
new Sobel() { Greyscale = true }.Apply(temp, source, sourceRectangle); |
|||
|
|||
// Apply threshold binarization filter.
|
|||
new Threshold(.5f).Apply(temp, temp, sourceRectangle); |
|||
|
|||
// Search for the first white pixels
|
|||
Rectangle rectangle = ImageMaths.GetFilteredBoundingRectangle(temp, 0); |
|||
target.SetPixels(rectangle.Width, rectangle.Height, new float[rectangle.Width * rectangle.Height * 4]); |
|||
|
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
protected override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) |
|||
{ |
|||
int targetY = targetRectangle.Y; |
|||
int targetBottom = targetRectangle.Height; |
|||
int startX = targetRectangle.X; |
|||
int endX = targetRectangle.Width; |
|||
|
|||
Parallel.For( |
|||
startY, |
|||
endY, |
|||
y => |
|||
{ |
|||
if (y >= targetY && y < targetBottom) |
|||
{ |
|||
for (int x = startX; x < endX; x++) |
|||
{ |
|||
target[x, y] = source[x, y]; |
|||
} |
|||
} |
|||
}); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue