|
| 1 | +// Copyright (c) Six Labors. |
| 2 | +// Licensed under the Six Labors Split License. |
| 3 | + |
| 4 | +using System.Numerics; |
| 5 | +using System.Runtime.CompilerServices; |
| 6 | + |
| 7 | +namespace SixLabors.ImageSharp.ColorProfiles; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Represents an YCbCr (luminance, blue chroma, red chroma) color as defined in the ITU-T T.871 specification for the JFIF use with Jpeg. |
| 11 | +/// <see href="http://en.wikipedia.org/wiki/YCbCr"/> |
| 12 | +/// <see href="http://www.ijg.org/files/T-REC-T.871-201105-I!!PDF-E.pdf"/> |
| 13 | +/// </summary> |
| 14 | +public readonly struct YCbCr : IColorProfile<YCbCr, Rgb> |
| 15 | +{ |
| 16 | + private static readonly Vector3 Min = Vector3.Zero; |
| 17 | + private static readonly Vector3 Max = new(255); |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Initializes a new instance of the <see cref="YCbCr"/> struct. |
| 21 | + /// </summary> |
| 22 | + /// <param name="y">The y luminance component.</param> |
| 23 | + /// <param name="cb">The cb chroma component.</param> |
| 24 | + /// <param name="cr">The cr chroma component.</param> |
| 25 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 26 | + public YCbCr(float y, float cb, float cr) |
| 27 | + : this(new Vector3(y, cb, cr)) |
| 28 | + { |
| 29 | + } |
| 30 | + |
| 31 | + /// <summary> |
| 32 | + /// Initializes a new instance of the <see cref="YCbCr"/> struct. |
| 33 | + /// </summary> |
| 34 | + /// <param name="vector">The vector representing the y, cb, cr components.</param> |
| 35 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 36 | + public YCbCr(Vector3 vector) |
| 37 | + { |
| 38 | + vector = Vector3.Clamp(vector, Min, Max); |
| 39 | + this.Y = vector.X; |
| 40 | + this.Cb = vector.Y; |
| 41 | + this.Cr = vector.Z; |
| 42 | + } |
| 43 | + |
| 44 | + /// <summary> |
| 45 | + /// Gets the Y luminance component. |
| 46 | + /// <remarks>A value ranging between 0 and 255.</remarks> |
| 47 | + /// </summary> |
| 48 | + public readonly float Y { get; } |
| 49 | + |
| 50 | + /// <summary> |
| 51 | + /// Gets the Cb chroma component. |
| 52 | + /// <remarks>A value ranging between 0 and 255.</remarks> |
| 53 | + /// </summary> |
| 54 | + public readonly float Cb { get; } |
| 55 | + |
| 56 | + /// <summary> |
| 57 | + /// Gets the Cr chroma component. |
| 58 | + /// <remarks>A value ranging between 0 and 255.</remarks> |
| 59 | + /// </summary> |
| 60 | + public readonly float Cr { get; } |
| 61 | + |
| 62 | + /// <summary> |
| 63 | + /// Compares two <see cref="YCbCr"/> objects for equality. |
| 64 | + /// </summary> |
| 65 | + /// <param name="left">The <see cref="YCbCr"/> on the left side of the operand.</param> |
| 66 | + /// <param name="right">The <see cref="YCbCr"/> on the right side of the operand.</param> |
| 67 | + /// <returns> |
| 68 | + /// True if the current left is equal to the <paramref name="right"/> parameter; otherwise, false. |
| 69 | + /// </returns> |
| 70 | + public static bool operator ==(YCbCr left, YCbCr right) => left.Equals(right); |
| 71 | + |
| 72 | + /// <summary> |
| 73 | + /// Compares two <see cref="YCbCr"/> objects for inequality. |
| 74 | + /// </summary> |
| 75 | + /// <param name="left">The <see cref="YCbCr"/> on the left side of the operand.</param> |
| 76 | + /// <param name="right">The <see cref="YCbCr"/> on the right side of the operand.</param> |
| 77 | + /// <returns> |
| 78 | + /// True if the current left is unequal to the <paramref name="right"/> parameter; otherwise, false. |
| 79 | + /// </returns> |
| 80 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 81 | + public static bool operator !=(YCbCr left, YCbCr right) => !left.Equals(right); |
| 82 | + |
| 83 | + /// <inheritdoc/> |
| 84 | + public static YCbCr FromProfileConnectingSpace(ColorConversionOptions options, in Rgb source) |
| 85 | + { |
| 86 | + Vector3 rgb = source.ToScaledVector3() * Max; |
| 87 | + float r = rgb.X; |
| 88 | + float g = rgb.Y; |
| 89 | + float b = rgb.Z; |
| 90 | + |
| 91 | + float y = (0.299F * r) + (0.587F * g) + (0.114F * b); |
| 92 | + float cb = 128F + ((-0.168736F * r) - (0.331264F * g) + (0.5F * b)); |
| 93 | + float cr = 128F + ((0.5F * r) - (0.418688F * g) - (0.081312F * b)); |
| 94 | + |
| 95 | + return new YCbCr(y, cb, cr); |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc/> |
| 99 | + public static void FromProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<Rgb> source, Span<YCbCr> destination) |
| 100 | + { |
| 101 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 102 | + |
| 103 | + // TODO: We can optimize this by using SIMD |
| 104 | + for (int i = 0; i < source.Length; i++) |
| 105 | + { |
| 106 | + Rgb rgb = source[i]; |
| 107 | + destination[i] = FromProfileConnectingSpace(options, in rgb); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// <inheritdoc/> |
| 112 | + public Rgb ToProfileConnectingSpace(ColorConversionOptions options) |
| 113 | + { |
| 114 | + float y = this.Y; |
| 115 | + float cb = this.Cb - 128F; |
| 116 | + float cr = this.Cr - 128F; |
| 117 | + |
| 118 | + float r = MathF.Round(y + (1.402F * cr), MidpointRounding.AwayFromZero); |
| 119 | + float g = MathF.Round(y - (0.344136F * cb) - (0.714136F * cr), MidpointRounding.AwayFromZero); |
| 120 | + float b = MathF.Round(y + (1.772F * cb), MidpointRounding.AwayFromZero); |
| 121 | + |
| 122 | + return Rgb.FromScaledVector3(new Vector3(r, g, b) / Max); |
| 123 | + } |
| 124 | + |
| 125 | + /// <inheritdoc/> |
| 126 | + public static void ToProfileConnectionSpace(ColorConversionOptions options, ReadOnlySpan<YCbCr> source, Span<Rgb> destination) |
| 127 | + { |
| 128 | + Guard.DestinationShouldNotBeTooShort(source, destination, nameof(destination)); |
| 129 | + |
| 130 | + // TODO: We can optimize this by using SIMD |
| 131 | + for (int i = 0; i < source.Length; i++) |
| 132 | + { |
| 133 | + YCbCr ycbcr = source[i]; |
| 134 | + destination[i] = ycbcr.ToProfileConnectingSpace(options); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + /// <inheritdoc/> |
| 139 | + public static ChromaticAdaptionWhitePointSource GetChromaticAdaptionWhitePointSource() |
| 140 | + => ChromaticAdaptionWhitePointSource.RgbWorkingSpace; |
| 141 | + |
| 142 | + /// <inheritdoc/> |
| 143 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 144 | + public override int GetHashCode() => HashCode.Combine(this.Y, this.Cb, this.Cr); |
| 145 | + |
| 146 | + /// <inheritdoc/> |
| 147 | + public override string ToString() => FormattableString.Invariant($"YCbCr({this.Y}, {this.Cb}, {this.Cr})"); |
| 148 | + |
| 149 | + /// <inheritdoc/> |
| 150 | + public override bool Equals(object? obj) => obj is YCbCr other && this.Equals(other); |
| 151 | + |
| 152 | + /// <inheritdoc/> |
| 153 | + [MethodImpl(InliningOptions.ShortMethod)] |
| 154 | + public bool Equals(YCbCr other) |
| 155 | + => this.Y.Equals(other.Y) |
| 156 | + && this.Cb.Equals(other.Cb) |
| 157 | + && this.Cr.Equals(other.Cr); |
| 158 | +} |
0 commit comments