mirror of https://github.com/SixLabors/ImageSharp
17 changed files with 650 additions and 1 deletions
@ -0,0 +1,156 @@ |
|||
// <copyright file="ColorSpaceConverter.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.ColorSpaces.Conversion |
|||
{ |
|||
using ImageSharp.ColorSpaces; |
|||
using ImageSharp.ColorSpaces.Conversion.Implementation.YCbCr; |
|||
|
|||
/// <summary>
|
|||
/// Converts between color spaces ensuring that the color is adapted using chromatic adaptation.
|
|||
/// </summary>
|
|||
public partial class ColorSpaceConverter |
|||
{ |
|||
private static readonly YCbCrAndRgbConverter YCbCrAndRgbConverter = new YCbCrAndRgbConverter(); |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="CieLab"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(CieLab color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
CieXyz xyzColor = this.ToCieXyz(color); |
|||
|
|||
return this.ToYCbCr(xyzColor); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="CieLch"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(CieLch color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
CieXyz xyzColor = this.ToCieXyz(color); |
|||
|
|||
return this.ToYCbCr(xyzColor); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="CieXyy"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(CieXyy color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
CieXyz xyzColor = this.ToCieXyz(color); |
|||
|
|||
return this.ToYCbCr(xyzColor); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="CieXyz"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(CieXyz color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
Rgb rgb = this.ToRgb(color); |
|||
|
|||
return YCbCrAndRgbConverter.Convert(rgb); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="Cmyk"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(Cmyk color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
Rgb rgb = this.ToRgb(color); |
|||
|
|||
return YCbCrAndRgbConverter.Convert(rgb); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="Hsl"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(Hsl color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
Rgb rgb = this.ToRgb(color); |
|||
|
|||
return YCbCrAndRgbConverter.Convert(rgb); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="HunterLab"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(HunterLab color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
CieXyz xyzColor = this.ToCieXyz(color); |
|||
|
|||
return this.ToYCbCr(xyzColor); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="LinearRgb"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(LinearRgb color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
Rgb rgb = this.ToRgb(color); |
|||
|
|||
return YCbCrAndRgbConverter.Convert(rgb); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="Lms"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(Lms color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
CieXyz xyzColor = this.ToCieXyz(color); |
|||
|
|||
return this.ToYCbCr(xyzColor); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Converts a <see cref="Rgb"/> into a <see cref="YCbCr"/>
|
|||
/// </summary>
|
|||
/// <param name="color">The color to convert.</param>
|
|||
/// <returns>The <see cref="YCbCr"/></returns>
|
|||
public YCbCr ToYCbCr(Rgb color) |
|||
{ |
|||
Guard.NotNull(color, nameof(color)); |
|||
|
|||
return YCbCrAndRgbConverter.Convert(color); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,52 @@ |
|||
// <copyright file="YCbCrAndRgbConverter .cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.ColorSpaces.Conversion.Implementation.YCbCr |
|||
{ |
|||
using System; |
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
using ImageSharp.ColorSpaces; |
|||
|
|||
/// <summary>
|
|||
/// Color converter between YCbCr and Rgb
|
|||
/// See <see href="https://en.wikipedia.org/wiki/YCbCr#JPEG_conversion"/> for formulas.
|
|||
/// </summary>
|
|||
internal class YCbCrAndRgbConverter : IColorConversion<YCbCr, Rgb>, IColorConversion<Rgb, YCbCr> |
|||
{ |
|||
private static readonly Vector3 MaxBytes = new Vector3(255F); |
|||
|
|||
/// <inheritdoc/>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public Rgb Convert(YCbCr input) |
|||
{ |
|||
DebugGuard.NotNull(input, nameof(input)); |
|||
|
|||
float y = input.Y; |
|||
float cb = input.Cb - 128F; |
|||
float cr = input.Cr - 128F; |
|||
|
|||
|
|||
return new Rgb(new Vector3(r, g, b) / MaxBytes); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public YCbCr Convert(Rgb input) |
|||
{ |
|||
DebugGuard.NotNull(input, nameof(input)); |
|||
|
|||
Vector3 rgb = input.Vector * MaxBytes; |
|||
float r = rgb.X; |
|||
float g = rgb.Y; |
|||
float b = rgb.Z; |
|||
|
|||
float y = (0.299F * r) + (0.587F * g) + (0.114F * b); |
|||
|
|||
return new YCbCr(y, cb, cr); |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,183 @@ |
|||
// <copyright file="YCbCr.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.ColorSpaces |
|||
{ |
|||
using System; |
|||
using System.ComponentModel; |
|||
using System.Numerics; |
|||
using System.Runtime.CompilerServices; |
|||
|
|||
/// <summary>
|
|||
/// Represents an YCbCr (luminance, blue chroma, red chroma) color as defined in the ITU-T T.871 specification for the JFIF use with Jpeg.
|
|||
/// <see href="http://en.wikipedia.org/wiki/YCbCr"/>
|
|||
/// <see href="http://www.ijg.org/files/T-REC-T.871-201105-I!!PDF-E.pdf"/>
|
|||
/// </summary>
|
|||
public struct YCbCr : IColorVector, IEquatable<YCbCr>, IAlmostEquatable<YCbCr, float> |
|||
{ |
|||
/// <summary>
|
|||
/// Represents a <see cref="YCbCr"/> that has Y, Cb, and Cr values set to zero.
|
|||
/// </summary>
|
|||
public static readonly YCbCr Empty = default(YCbCr); |
|||
|
|||
/// <summary>
|
|||
/// Vector which is used in clamping to the max value
|
|||
/// </summary>
|
|||
private static readonly Vector3 VectorMax = new Vector3(255F); |
|||
|
|||
/// <summary>
|
|||
/// The backing vector for SIMD support.
|
|||
/// </summary>
|
|||
private readonly Vector3 backingVector; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="YCbCr"/> struct.
|
|||
/// </summary>
|
|||
/// <param name="y">The y luminance component.</param>
|
|||
/// <param name="cb">The cb chroma component.</param>
|
|||
/// <param name="cr">The cr chroma component.</param>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public YCbCr(float y, float cb, float cr) |
|||
: this(new Vector3(y, cb, cr)) |
|||
{ |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="YCbCr"/> struct.
|
|||
/// </summary>
|
|||
/// <param name="vector">The vector representing the y, cb, cr components.</param>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public YCbCr(Vector3 vector) |
|||
{ |
|||
this.backingVector = Vector3.Clamp(vector, Vector3.Zero, VectorMax); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the Y luminance component.
|
|||
/// <remarks>A value ranging between 0 and 255.</remarks>
|
|||
/// </summary>
|
|||
public float Y |
|||
{ |
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
get => this.backingVector.X; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the Cb chroma component.
|
|||
/// <remarks>A value ranging between 0 and 255.</remarks>
|
|||
/// </summary>
|
|||
public float Cb |
|||
{ |
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
get => this.backingVector.Y; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets the Cr chroma component.
|
|||
/// <remarks>A value ranging between 0 and 255.</remarks>
|
|||
/// </summary>
|
|||
public float Cr |
|||
{ |
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
get => this.backingVector.Z; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Gets a value indicating whether this <see cref="YCbCr"/> is empty.
|
|||
/// </summary>
|
|||
[EditorBrowsable(EditorBrowsableState.Never)] |
|||
public bool IsEmpty => this.Equals(Empty); |
|||
|
|||
/// <inheritdoc/>
|
|||
public Vector3 Vector |
|||
{ |
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
get => this.backingVector; |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Compares two <see cref="YCbCr"/> objects for equality.
|
|||
/// </summary>
|
|||
/// <param name="left">
|
|||
/// The <see cref="YCbCr"/> on the left side of the operand.
|
|||
/// </param>
|
|||
/// <param name="right">
|
|||
/// The <see cref="YCbCr"/> on the right side of the operand.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static bool operator ==(YCbCr left, YCbCr right) |
|||
{ |
|||
return left.Equals(right); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Compares two <see cref="YCbCr"/> objects for inequality.
|
|||
/// </summary>
|
|||
/// <param name="left">
|
|||
/// The <see cref="YCbCr"/> on the left side of the operand.
|
|||
/// </param>
|
|||
/// <param name="right">
|
|||
/// The <see cref="YCbCr"/> on the right side of the operand.
|
|||
/// </param>
|
|||
/// <returns>
|
|||
/// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false.
|
|||
/// </returns>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public static bool operator !=(YCbCr left, YCbCr right) |
|||
{ |
|||
return !left.Equals(right); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public override int GetHashCode() |
|||
{ |
|||
return this.backingVector.GetHashCode(); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
public override string ToString() |
|||
{ |
|||
if (this.IsEmpty) |
|||
{ |
|||
return "YCbCr [ Empty ]"; |
|||
} |
|||
|
|||
return $"YCbCr [ Y={this.Y}, Cb={this.Cb}, Cr={this.Cr} ]"; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public override bool Equals(object obj) |
|||
{ |
|||
if (obj is YCbCr) |
|||
{ |
|||
return this.Equals((YCbCr)obj); |
|||
} |
|||
|
|||
return false; |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public bool Equals(YCbCr other) |
|||
{ |
|||
return this.backingVector.Equals(other.backingVector); |
|||
} |
|||
|
|||
/// <inheritdoc/>
|
|||
[MethodImpl(MethodImplOptions.AggressiveInlining)] |
|||
public bool AlmostEquals(YCbCr other, float precision) |
|||
{ |
|||
Vector3 result = Vector3.Abs(this.backingVector - other.backingVector); |
|||
|
|||
return result.X <= precision |
|||
&& result.Y <= precision |
|||
&& result.Z <= precision; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,66 @@ |
|||
namespace ImageSharp.Tests.Colors.Colorspaces |
|||
{ |
|||
using System.Collections.Generic; |
|||
|
|||
using ImageSharp.ColorSpaces; |
|||
using ImageSharp.ColorSpaces.Conversion; |
|||
|
|||
using Xunit; |
|||
|
|||
/// <summary>
|
|||
/// Tests <see cref="Rgb"/>-<see cref="YCbCr"/> conversions.
|
|||
/// </summary>
|
|||
/// <remarks>
|
|||
/// Test data generated mathematically
|
|||
/// </remarks>
|
|||
public class RgbAndYCbCrConversionTest |
|||
{ |
|||
private static readonly IEqualityComparer<float> FloatRoundingComparer = new FloatRoundingComparer(3); |
|||
|
|||
private static readonly ColorSpaceConverter Converter = new ColorSpaceConverter(); |
|||
|
|||
/// <summary>
|
|||
/// Tests conversion from <see cref="YCbCr"/> to <see cref="Rgb"/>.
|
|||
/// </summary>
|
|||
[Theory] |
|||
[InlineData(255, 128, 128, 1, 1, 1)] |
|||
[InlineData(0, 128, 128, 0, 0, 0)] |
|||
[InlineData(128, 128, 128, 0.502, 0.502, 0.502)] |
|||
public void Convert_YCbCr_To_Rgb(float y, float cb, float cr, float r, float g, float b) |
|||
{ |
|||
// Arrange
|
|||
YCbCr input = new YCbCr(y, cb, cr); |
|||
|
|||
// Act
|
|||
Rgb output = Converter.ToRgb(input); |
|||
|
|||
// Assert
|
|||
Assert.Equal(Rgb.DefaultWorkingSpace, output.WorkingSpace); |
|||
Assert.Equal(r, output.R, FloatRoundingComparer); |
|||
Assert.Equal(g, output.G, FloatRoundingComparer); |
|||
Assert.Equal(b, output.B, FloatRoundingComparer); |
|||
} |
|||
|
|||
/// <summary>
|
|||
/// Tests conversion from <see cref="Rgb"/> to <see cref="YCbCr"/>.
|
|||
/// </summary>
|
|||
[Theory] |
|||
[InlineData(0, 0, 0, 0, 128, 128)] |
|||
[InlineData(1, 1, 1, 255, 128, 128)] |
|||
[InlineData(0.5, 0.5, 0.5, 127.5, 128, 128)] |
|||
[InlineData(1, 0, 0, 76.245, 84.972, 255)] |
|||
public void Convert_Rgb_To_YCbCr(float r, float g, float b, float y, float cb, float cr) |
|||
{ |
|||
// Arrange
|
|||
Rgb input = new Rgb(r, g, b); |
|||
|
|||
// Act
|
|||
YCbCr output = Converter.ToYCbCr(input); |
|||
|
|||
// Assert
|
|||
Assert.Equal(y, output.Y, FloatRoundingComparer); |
|||
Assert.Equal(cb, output.Cb, FloatRoundingComparer); |
|||
Assert.Equal(cr, output.Cr, FloatRoundingComparer); |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue