Browse Source

#718: add pixel types Gray8 and Gray16

af/merge-core
Peter Amrehn 7 years ago
parent
commit
39b3cb708c
  1. 270
      src/ImageSharp/PixelFormats/Gray16.cs
  2. 265
      src/ImageSharp/PixelFormats/Gray8.cs
  3. 211
      tests/ImageSharp.Tests/PixelFormats/Gray8Tests.cs

270
src/ImageSharp/PixelFormats/Gray16.cs

@ -0,0 +1,270 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.PixelFormats
{
/// <summary>
/// Packed pixel type containing a single 16 bit normalized gray values.
/// <para>
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
/// </para>
/// </summary>
public struct Gray16 : IPixel<Gray16>, IPackedVector<ushort>
{
/// <summary>
/// RX as in ITU-R recommendation 709 to match libpng
/// </summary>
private const float Rx = .2126F;
/// <summary>
/// GX as in ITU-R recommendation 709 to match libpng
/// </summary>
private const float Gx = .7152F;
/// <summary>
/// BX as in ITU-R recommendation 709 to match libpng
/// </summary>
private const float Bx = .0722F;
/// <summary>
/// Initializes a new instance of the <see cref="Gray16"/> struct.
/// </summary>
/// <param name="gray">The gray component</param>
public Gray16(byte gray)
{
this.PackedValue = gray;
}
/// <inheritdoc />
public ushort PackedValue { get; set; }
/// <summary>
/// Compares two <see cref="Gray16"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="Gray16"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Gray16"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Gray16 left, Gray16 right)
{
return left.PackedValue == right.PackedValue;
}
/// <summary>
/// Compares two <see cref="Gray16"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Gray16"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Gray16"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Gray16 left, Gray16 right)
{
return left.PackedValue != right.PackedValue;
}
/// <inheritdoc />
public PixelOperations<Gray16> CreatePixelOperations() => new PixelOperations<Gray16>();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromScaledVector4(Vector4 vector)
{
this.PackFromVector4(vector);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToScaledVector4()
{
var scaledGray = this.PackedValue / 65535f; // ushort.Max as float
return new Vector4(scaledGray, scaledGray, scaledGray, 1.0f);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = Pack(vector.X, vector.Y, vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4()
{
return new Vector4(this.PackedValue, this.PackedValue, this.PackedValue, 1.0f);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromRgba32(Rgba32 source)
{
this.PackedValue = Pack(source.R, source.G, source.B);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackedValue = Pack(source.R, source.G, source.B);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromBgra32(Bgra32 source)
{
this.PackedValue = Pack(source.R, source.G, source.B);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
{
var scaledValue = this.PackedAsByte();
dest.R = scaledValue;
dest.G = scaledValue;
dest.B = scaledValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest)
{
var scaledValue = this.PackedAsByte();
dest.R = scaledValue;
dest.G = scaledValue;
dest.B = scaledValue;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest)
{
var scaledValue = this.PackedAsByte();
dest.R = scaledValue;
dest.G = scaledValue;
dest.B = scaledValue;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)
{
var scaledValue = this.PackedAsByte();
dest.R = scaledValue;
dest.G = scaledValue;
dest.B = scaledValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgra32(ref Bgra32 dest)
{
var scaledValue = this.PackedAsByte();
dest.R = scaledValue;
dest.G = scaledValue;
dest.B = scaledValue;
dest.A = 255;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromRgb48(Rgb48 source) =>
this.PackedValue = Pack(source.R, source.G, source.B);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb48(ref Rgb48 dest)
{
dest.R = this.PackedValue;
dest.G = this.PackedValue;
dest.B = this.PackedValue;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromRgba64(Rgba64 source) =>
this.PackFromScaledVector4(source.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba64(ref Rgba64 dest) => dest.PackFromScaledVector4(this.ToScaledVector4());
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return obj is Gray16 other && this.Equals(other);
}
/// <summary>
/// Compares another <see cref="Gray16" /> packed vector with the packed vector.
/// </summary>
/// <param name="other">The Gray8 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Gray16 other)
{
return this.PackedValue == other.PackedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return (this.PackedValue / 65535f).ToString();
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => this.PackedValue.GetHashCode();
/// <summary>
/// Packs a <see cref="float"/> into a byte.
/// </summary>
/// <param name="r">Red value of the color to pack.</param>
/// <param name="g">Green value of the color to pack.</param>
/// <param name="b">Blue value of the color to pack.</param>
/// <returns>The <see cref="ushort"/> containing the packed value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static ushort Pack(float r, float g, float b)
{
float sum = r + g + b;
float val = (r * Rx) + (g * Gx) + (b * Bx);
return (ushort)Math.Round(val * 65535f / sum); // TODO: if this is correct, Rx, Gx, Bx consts could be scaled by 65535f directly!
}
/// <summary>
/// Packs the <see cref="ushort" /> into a byte.
/// </summary>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private byte PackedAsByte()
{
return (byte)(this.PackedValue >> 8);
}
}
}

265
src/ImageSharp/PixelFormats/Gray8.cs

@ -0,0 +1,265 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
namespace SixLabors.ImageSharp.PixelFormats
{
/// <summary>
/// Packed pixel type containing a single 8 bit normalized gray values.
/// <para>
/// Ranges from [0, 0, 0, 1] to [1, 1, 1, 1] in vector form.
/// </para>
/// </summary>
public struct Gray8 : IPixel<Gray8>, IPackedVector<byte>
{
/// <summary>
/// RX as in ITU-R recommendation 709 to match libpng
/// </summary>
private const float Rx = .2126F;
/// <summary>
/// GX as in ITU-R recommendation 709 to match libpng
/// </summary>
private const float Gx = .7152F;
/// <summary>
/// BX as in ITU-R recommendation 709 to match libpng
/// </summary>
private const float Bx = .0722F;
/// <summary>
/// Initializes a new instance of the <see cref="Gray8"/> struct.
/// </summary>
/// <param name="gray">The gray component</param>
public Gray8(byte gray)
{
this.PackedValue = gray;
}
/// <inheritdoc />
public byte PackedValue { get; set; }
/// <summary>
/// Compares two <see cref="Gray8"/> objects for equality.
/// </summary>
/// <param name="left">
/// The <see cref="Gray8"/> on the left side of the operand.
/// </param>
/// <param name="right">
/// The <see cref="Gray8"/> on the right side of the operand.
/// </param>
/// <returns>
/// True if the <paramref name="left"/> parameter is equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator ==(Gray8 left, Gray8 right)
{
return left.PackedValue == right.PackedValue;
}
/// <summary>
/// Compares two <see cref="Gray8"/> objects for equality.
/// </summary>
/// <param name="left">The <see cref="Gray8"/> on the left side of the operand.</param>
/// <param name="right">The <see cref="Gray8"/> on the right side of the operand.</param>
/// <returns>
/// True if the <paramref name="left"/> parameter is not equal to the <paramref name="right"/> parameter; otherwise, false.
/// </returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool operator !=(Gray8 left, Gray8 right)
{
return left.PackedValue != right.PackedValue;
}
/// <inheritdoc />
public PixelOperations<Gray8> CreatePixelOperations() => new PixelOperations<Gray8>();
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromScaledVector4(Vector4 vector)
{
this.PackFromVector4(vector);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToScaledVector4()
{
var scaledGray = this.PackedValue / 255f;
return new Vector4(scaledGray, scaledGray, scaledGray, 1.0f);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromVector4(Vector4 vector)
{
this.PackedValue = Pack(vector.X, vector.Y, vector.Z);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Vector4 ToVector4()
{
return new Vector4(this.PackedValue, this.PackedValue, this.PackedValue, 1.0f);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromRgba32(Rgba32 source)
{
this.PackedValue = Pack(source.R, source.G, source.B);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromArgb32(Argb32 source)
{
this.PackedValue = Pack(source.R, source.G, source.B);
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromBgra32(Bgra32 source)
{
this.PackedValue = Pack(source.R, source.G, source.B);
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb24(ref Rgb24 dest)
{
dest.R = this.PackedValue;
dest.G = this.PackedValue;
dest.B = this.PackedValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba32(ref Rgba32 dest)
{
dest.R = this.PackedValue;
dest.G = this.PackedValue;
dest.B = this.PackedValue;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToArgb32(ref Argb32 dest)
{
dest.R = this.PackedValue;
dest.G = this.PackedValue;
dest.B = this.PackedValue;
dest.A = 255;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgr24(ref Bgr24 dest)
{
dest.R = this.PackedValue;
dest.G = this.PackedValue;
dest.B = this.PackedValue;
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToBgra32(ref Bgra32 dest)
{
dest.R = this.PackedValue;
dest.G = this.PackedValue;
dest.B = this.PackedValue;
dest.A = 255;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromRgb48(Rgb48 source) =>
this.PackedValue = Pack(source.R, source.G, source.B);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgb48(ref Rgb48 dest)
{
ushort gray = (ushort)(this.PackedValue * 255);
dest.R = gray;
dest.G = gray;
dest.B = gray;
}
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromRgba64(Rgba64 source) =>
this.PackFromScaledVector4(source.ToScaledVector4());
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromGray8(Gray8 source) => this.PackedValue = source.PackedValue;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void PackFromGray16(Gray16 source) => this.PackedValue = (byte)(source.PackedValue / 255);
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToRgba64(ref Rgba64 dest) => dest.PackFromScaledVector4(this.ToScaledVector4());
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToGray8(ref Gray8 dest) => dest.PackedValue = this.PackedValue;
/// <inheritdoc/>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ToGray16(ref Gray16 dest) => dest.PackedValue = (ushort)(this.PackedValue * 255);
/// <summary>
/// Compares an object with the packed vector.
/// </summary>
/// <param name="obj">The object to compare.</param>
/// <returns>True if the object is equal to the packed vector.</returns>
public override bool Equals(object obj)
{
return obj is Gray8 other && this.Equals(other);
}
/// <summary>
/// Compares another <see cref="Gray8" /> packed vector with the packed vector.
/// </summary>
/// <param name="other">The Gray8 packed vector to compare.</param>
/// <returns>True if the packed vectors are equal.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool Equals(Gray8 other)
{
return this.PackedValue == other.PackedValue;
}
/// <summary>
/// Gets a string representation of the packed vector.
/// </summary>
/// <returns>A string representation of the packed vector.</returns>
public override string ToString()
{
return (this.PackedValue / 255F).ToString();
}
/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public override int GetHashCode() => this.PackedValue.GetHashCode();
/// <summary>
/// Packs a <see cref="float"/> into a byte.
/// </summary>
/// <param name="r">Red value of the color to pack.</param>
/// <param name="g">Green value of the color to pack.</param>
/// <param name="b">Blue value of the color to pack.</param>
/// <returns>The <see cref="byte"/> containing the packed value.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static byte Pack(float r, float g, float b)
{
float sum = r + g + b;
float val = (r * Rx) + (g * Gx) + (b * Bx);
return (byte)Math.Round(val * 255 / sum); // TODO: if this is correct, Rx, Gx, Bx consts could be scaled by 255 directly!
}
}
}

211
tests/ImageSharp.Tests/PixelFormats/Gray8Tests.cs

@ -0,0 +1,211 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System.Numerics;
using SixLabors.ImageSharp.PixelFormats;
using Xunit;
namespace SixLabors.ImageSharp.Tests.PixelFormats
{
public class Gray8Tests
{
[Theory]
[InlineData(0)]
[InlineData(255)]
[InlineData(10)]
[InlineData(42)]
public void Gray8_PackedValue_EqualsInput(byte input)
{
Assert.Equal(input, new Gray8(input).PackedValue);
}
[Theory]
[InlineData(0)]
[InlineData(255)]
[InlineData(30)]
public void Gray8_ToVector4(byte input)
{
// arrange
var gray = new Gray8(input);
// act
var actual = gray.ToVector4();
// assert
Assert.Equal(input, actual.X);
Assert.Equal(input, actual.Y);
Assert.Equal(input, actual.Z);
Assert.Equal(1, actual.W);
}
[Theory]
[InlineData(0)]
[InlineData(255)]
[InlineData(30)]
public void Gray8_ToScaledVector4(byte input)
{
// arrange
var gray = new Gray8(input);
// act
var actual = gray.ToScaledVector4();
// assert
float scaledInput = input / 255f;
Assert.Equal(scaledInput, actual.X);
Assert.Equal(scaledInput, actual.Y);
Assert.Equal(scaledInput, actual.Z);
Assert.Equal(1, actual.W);
}
[Fact]
public void Gray8_PackFromScaledVector4()
{
// arrange
Gray8 gray = default;
int expected = 128;
Vector4 scaled = new Gray8(128).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
byte actual = gray.PackedValue;
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromScaledVector4_ToRgb24()
{
// arrange
Rgb24 actual = default;
Gray8 gray = default;
var expected = new Rgb24(0, 0, 0);
Vector4 scaled = new Gray8(128).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
gray.ToRgb24(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromScaledVector4_ToRgba32()
{
// arrange
Rgba32 actual = default;
Gray8 gray = default;
var expected = new Rgba32(0, 0, 0, 128);
Vector4 scaled = new Gray8(128).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
gray.ToRgba32(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromScaledVector4_ToBgr24()
{
// arrange
Bgr24 actual = default;
Gray8 gray = default;
var expected = new Bgr24(128, 128, 128);
Vector4 scaled = new Gray8(128).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
gray.ToBgr24(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromScaledVector4_ToBgra32()
{
// arrange
Bgra32 actual = default;
Gray8 gray = default;
var expected = new Bgra32(128,128,128);
Vector4 scaled = new Gray8(128).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
gray.ToBgra32(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromScaledVector4_ToArgb32()
{
// arrange
Gray8 gray = default;
Argb32 actual = default;
var expected = new Argb32(128, 128, 128);
Vector4 scaled = new Gray8(128).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
gray.ToArgb32(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromScaledVector4_ToRgba64()
{
// arrange
Gray8 gray = default;
Rgba64 actual = default;
var expected = new Rgba64(65535, 65535, 65535, 65535);
Vector4 scaled = new Gray8(255).ToScaledVector4();
// act
gray.PackFromScaledVector4(scaled);
gray.ToRgba64(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromRgb48_ToRgb48()
{
// arrange
var gray = default(Gray8);
var actual = default(Rgb48);
var expected = new Rgb48(0, 0, 0);
// act
gray.PackFromRgb48(expected);
gray.ToRgb48(ref actual);
// assert
Assert.Equal(expected, actual);
}
[Fact]
public void Gray8_PackFromRgba64_ToRgba64()
{
// arrange
var gray = default(Gray8);
var actual = default(Rgba64);
var expected = new Rgba64(0, 0, 0, 65535);
// act
gray.PackFromRgba64(expected);
gray.ToRgba64(ref actual);
// assert
Assert.Equal(expected, actual);
}
}
}
Loading…
Cancel
Save