From 3451e72b33cf794dc71bd3b5d08f46cc3cf423f0 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Dec 2016 16:33:37 +1100 Subject: [PATCH] Add Rg32 --- src/ImageSharp/Colors/PackedPixel/Rg32.cs | 173 ++++++++++++++++++ .../Colors/PackedPixelTests.cs | 42 +++++ .../Formats/GeneralFormatTests.cs | 1 + 3 files changed, 216 insertions(+) create mode 100644 src/ImageSharp/Colors/PackedPixel/Rg32.cs diff --git a/src/ImageSharp/Colors/PackedPixel/Rg32.cs b/src/ImageSharp/Colors/PackedPixel/Rg32.cs new file mode 100644 index 000000000..8ca53a829 --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/Rg32.cs @@ -0,0 +1,173 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Numerics; + + /// + /// Packed pixel type containing two 16-bit unsigned normalized values ranging from 0 to 1. + /// + public struct Rg32 : IPackedPixel, IEquatable, IPackedVector + { + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + public Rg32(float x, float y) + { + this.PackedValue = Pack(x, y); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The vector containing the component values. + public Rg32(Vector2 vector) + { + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + public uint PackedValue { get; set; } + + /// + /// Compares two objects for equality. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the parameter is equal to the parameter; otherwise, false. + /// + public static bool operator ==(Rg32 left, Rg32 right) + { + return left.PackedValue == right.PackedValue; + } + + /// + /// Compares two objects for equality. + /// + /// + /// The on the left side of the operand. + /// + /// + /// The on the right side of the operand. + /// + /// + /// True if the parameter is not equal to the parameter; otherwise, false. + /// + public static bool operator !=(Rg32 left, Rg32 right) + { + return left.PackedValue != right.PackedValue; + } + + /// + /// Expands the packed representation into a . + /// The vector components are typically expanded in least to greatest significance order. + /// + /// The . + public Vector2 ToVector2() + { + return new Vector2( + (this.PackedValue & 0xFFFF) / 65535F, + ((this.PackedValue >> 16) & 0xFFFF) / 65535F); + } + + /// + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y); + } + + /// + public Vector4 ToVector4() + { + return new Vector4(this.ToVector2(), 0F, 1F); + } + + /// + public void PackFromBytes(byte x, byte y, byte z, byte w) + { + this.PackFromVector4(new Vector4(x, y, z, w) / 255F); + } + + /// + public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder) + { + Vector4 vector = this.ToVector4() * 255F; + + switch (componentOrder) + { + case ComponentOrder.ZYX: + bytes[startIndex] = (byte)vector.Z; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.X; + break; + case ComponentOrder.ZYXW: + bytes[startIndex] = (byte)vector.Z; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.X; + bytes[startIndex + 3] = (byte)vector.W; + break; + case ComponentOrder.XYZ: + bytes[startIndex] = (byte)vector.X; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.Z; + break; + case ComponentOrder.XYZW: + bytes[startIndex] = (byte)vector.X; + bytes[startIndex + 1] = (byte)vector.Y; + bytes[startIndex + 2] = (byte)vector.Z; + bytes[startIndex + 3] = (byte)vector.W; + break; + default: + throw new NotSupportedException(); + } + } + + /// + public override bool Equals(object obj) + { + return (obj is Rg32) && this.Equals((Rg32)obj); + } + + /// + public bool Equals(Rg32 other) + { + return this.PackedValue == other.PackedValue; + } + + /// + public override string ToString() + { + return this.ToVector2().ToString(); + } + + /// + public override int GetHashCode() + { + return this.PackedValue.GetHashCode(); + } + + /// + /// Packs the components into a . + /// + /// The x-component + /// The y-component + /// The containing the packed values. + private static uint Pack(float x, float y) + { + return (uint)( + ((int)Math.Round(x.Clamp(0, 1) * 65535F) & 0xFFFF) | + (((int)Math.Round(y.Clamp(0, 1) * 65535F) & 0xFFFF) << 16)); + } + } +} diff --git a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs index d25979e1e..75700337f 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -564,6 +564,48 @@ namespace ImageSharp.Tests.Colors Assert.Equal(rgba, new byte[] { 9, 115, 202, 127 }); } + [Fact] + public void Rg32() + { + // Test the limits. + Assert.Equal((uint)0x0, new Rg32(Vector2.Zero).PackedValue); + Assert.Equal(0xFFFFFFFF, new Rg32(Vector2.One).PackedValue); + + // Test ToVector2 + Assert.True(Equal(Vector2.Zero, new Rg32(Vector2.Zero).ToVector2())); + Assert.True(Equal(Vector2.One, new Rg32(Vector2.One).ToVector2())); + + // Test clamping. + Assert.True(Equal(Vector2.Zero, new Rg32(Vector2.One * -1234.0f).ToVector2())); + Assert.True(Equal(Vector2.One, new Rg32(Vector2.One * 1234.0f).ToVector2())); + + // Test Ordering + float x = 0xb6dc; + float y = 0xA59f; + Assert.Equal(0xa59fb6dc, new Rg32(x / 0xffff, y / 0xffff).PackedValue); + x = 0.1f; + y = -0.3f; + Assert.Equal((uint)6554, new Rg32(x, y).PackedValue); + + // Test ordering + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new Rg32(x, y).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 25, 0, 0 }); + + new Rg32(x, y).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 25, 0, 0, 255 }); + + new Rg32(x, y).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 0, 0, 25 }); + + new Rg32(x, y).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 0, 0, 25, 255 }); + } + // Comparison helpers with small tolerance to allow for floating point rounding during computations. public static bool Equal(float a, float b) { diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index f094253b8..b2191d909 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -61,6 +61,7 @@ namespace ImageSharp.Tests // Image image = file.CreateImage().To(); // Image image = file.CreateImage().To(); // Image image = file.CreateImage().To(); + // Image image = file.CreateImage().To(); // TODO: Conversion between types who's vector ranges are different are not possible without scaling function, Make static version of known ones. // Image image = file.CreateImage().To(v => (2F * v) - Vector4.One);