From 969fa9ae505eea0dccf7ed83420172a7d844fe59 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 1 Dec 2016 19:04:17 +1100 Subject: [PATCH] Add Rgba1010102, Make Bgr565 conversion more accurate. --- src/ImageSharp/Colors/PackedPixel/Bgr565.cs | 28 +-- .../Colors/PackedPixel/Rgba1010102.cs | 172 ++++++++++++++++++ .../Colors/PackedPixelTests.cs | 59 +++++- .../Formats/GeneralFormatTests.cs | 1 + 4 files changed, 242 insertions(+), 18 deletions(-) create mode 100644 src/ImageSharp/Colors/PackedPixel/Rgba1010102.cs diff --git a/src/ImageSharp/Colors/PackedPixel/Bgr565.cs b/src/ImageSharp/Colors/PackedPixel/Bgr565.cs index 72a1668b8..214e03c5f 100644 --- a/src/ImageSharp/Colors/PackedPixel/Bgr565.cs +++ b/src/ImageSharp/Colors/PackedPixel/Bgr565.cs @@ -103,26 +103,26 @@ namespace ImageSharp switch (componentOrder) { case ComponentOrder.ZYX: - bytes[startIndex] = (byte)vector.Z; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.X; + bytes[startIndex] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(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; + bytes[startIndex] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 3] = (byte)(float)Math.Round(vector.W); break; case ComponentOrder.XYZ: - bytes[startIndex] = (byte)vector.X; - bytes[startIndex + 1] = (byte)vector.Y; - bytes[startIndex + 2] = (byte)vector.Z; + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(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; + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 3] = (byte)(float)Math.Round(vector.W); break; default: throw new NotSupportedException(); diff --git a/src/ImageSharp/Colors/PackedPixel/Rgba1010102.cs b/src/ImageSharp/Colors/PackedPixel/Rgba1010102.cs new file mode 100644 index 000000000..54ac6279f --- /dev/null +++ b/src/ImageSharp/Colors/PackedPixel/Rgba1010102.cs @@ -0,0 +1,172 @@ +// +// Copyright (c) James Jackson-South and contributors. +// Licensed under the Apache License, Version 2.0. +// + +namespace ImageSharp +{ + using System; + using System.Numerics; + + /// + /// Packed vector type containing unsigned normalized values ranging from 0 to 1. + /// The x, y and z components use 10 bits, and the w component uses 2 bits. + /// + public struct Rgba1010102 : IPackedPixel, IEquatable, IPackedVector + { + /// + /// Initializes a new instance of the struct. + /// + /// The x-component + /// The y-component + /// The z-component + /// The w-component + public Rgba1010102(float x, float y, float z, float w) + { + this.PackedValue = Pack(x, y, z, w); + } + + /// + /// Initializes a new instance of the struct. + /// + /// The vector containing the component values. + public Rgba1010102(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W); + } + + /// + 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 ==(Rgba1010102 left, Rgba1010102 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 !=(Rgba1010102 left, Rgba1010102 right) + { + return left.PackedValue != right.PackedValue; + } + + /// + public Vector4 ToVector4() + { + return new Vector4( + ((this.PackedValue >> 0) & 0x03FF) / 1023F, + ((this.PackedValue >> 10) & 0x03FF) / 1023F, + ((this.PackedValue >> 20) & 0x03FF) / 1023F, + ((this.PackedValue >> 30) & 0x03) / 3F); + } + + /// + public void PackFromVector4(Vector4 vector) + { + this.PackedValue = Pack(vector.X, vector.Y, vector.Z, vector.W); + } + + /// + 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)(float)Math.Round(vector.Z); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + break; + case ComponentOrder.ZYXW: + bytes[startIndex] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 3] = (byte)(float)Math.Round(vector.W); + break; + case ComponentOrder.XYZ: + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.Z); + break; + case ComponentOrder.XYZW: + bytes[startIndex] = (byte)(float)Math.Round(vector.X); + bytes[startIndex + 1] = (byte)(float)Math.Round(vector.Y); + bytes[startIndex + 2] = (byte)(float)Math.Round(vector.Z); + bytes[startIndex + 3] = (byte)(float)Math.Round(vector.W); + break; + default: + throw new NotSupportedException(); + } + } + + /// + public override bool Equals(object obj) + { + return (obj is Rgba1010102) && this.Equals((Rgba1010102)obj); + } + + /// + public bool Equals(Rgba1010102 other) + { + return this.PackedValue == other.PackedValue; + } + + /// + public override string ToString() + { + return this.ToVector4().ToString(); + } + + /// + public override int GetHashCode() + { + return this.PackedValue.GetHashCode(); + } + + /// + /// Packs the components into a . + /// + /// The x-component + /// The y-component + /// The z-component + /// The w-component + /// The containing the packed values. + private static uint Pack(float x, float y, float z, float w) + { + return (uint)( + (((int)Math.Round(x.Clamp(0, 1) * 1023F) & 0x03FF) << 0) | + (((int)Math.Round(y.Clamp(0, 1) * 1023F) & 0x03FF) << 10) | + (((int)Math.Round(z.Clamp(0, 1) * 1023F) & 0x03FF) << 20) | + (((int)Math.Round(w.Clamp(0, 1) * 3F) & 0x03) << 30)); + } + } +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs index 75700337f..1c6f90970 100644 --- a/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs +++ b/tests/ImageSharp.Tests/Colors/PackedPixelTests.cs @@ -92,16 +92,16 @@ namespace ImageSharp.Tests.Colors byte[] bgra = new byte[4]; new Bgr565(x, y, z).ToBytes(rgb, 0, ComponentOrder.XYZ); - Assert.Equal(rgb, new byte[] { 24, 0, 131 }); + Assert.Equal(rgb, new byte[] { 25, 0, 132 }); new Bgr565(x, y, z).ToBytes(rgba, 0, ComponentOrder.XYZW); - Assert.Equal(rgba, new byte[] { 24, 0, 131, 255 }); + Assert.Equal(rgba, new byte[] { 25, 0, 132, 255 }); new Bgr565(x, y, z).ToBytes(bgr, 0, ComponentOrder.ZYX); - Assert.Equal(bgr, new byte[] { 131, 0, 24 }); + Assert.Equal(bgr, new byte[] { 132, 0, 25 }); new Bgr565(x, y, z).ToBytes(bgra, 0, ComponentOrder.ZYXW); - Assert.Equal(bgra, new byte[] { 131, 0, 24, 255 }); + Assert.Equal(bgra, new byte[] { 132, 0, 25, 255 }); } [Fact] @@ -606,6 +606,57 @@ namespace ImageSharp.Tests.Colors Assert.Equal(bgra, new byte[] { 0, 0, 25, 255 }); } + [Fact] + public void Rgba1010102() + { + // Test the limits. + Assert.Equal((uint)0x0, new Rgba1010102(Vector4.Zero).PackedValue); + Assert.Equal(0xFFFFFFFF, new Rgba1010102(Vector4.One).PackedValue); + + // Test ToVector4 + Assert.True(Equal(Vector4.Zero, new Rgba1010102(Vector4.Zero).ToVector4())); + Assert.True(Equal(Vector4.One, new Rgba1010102(Vector4.One).ToVector4())); + + // Test clamping. + Assert.True(Equal(Vector4.Zero, new Rgba1010102(Vector4.One * -1234.0f).ToVector4())); + Assert.True(Equal(Vector4.One, new Rgba1010102(Vector4.One * 1234.0f).ToVector4())); + + // Test Ordering + float x = 0x2db; + float y = 0x36d; + float z = 0x3b7; + float w = 0x1; + Assert.Equal((uint)0x7B7DB6DB, new Rgba1010102(x / 0x3ff, y / 0x3ff, z / 0x3ff, w / 3).PackedValue); + x = 0.1f; + y = -0.3f; + z = 0.5f; + w = -0.7f; + Assert.Equal((uint)536871014, new Rgba1010102(x, y, z, w).PackedValue); + + byte[] rgb = new byte[3]; + byte[] rgba = new byte[4]; + byte[] bgr = new byte[3]; + byte[] bgra = new byte[4]; + + new Rgba1010102(x, y, z, w).ToBytes(rgb, 0, ComponentOrder.XYZ); + Assert.Equal(rgb, new byte[] { 25, 0, 128 }); + + new Rgba1010102(x, y, z, w).ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 25, 0, 128, 0 }); + + new Rgba1010102(x, y, z, w).ToBytes(bgr, 0, ComponentOrder.ZYX); + Assert.Equal(bgr, new byte[] { 128, 0, 25 }); + + new Rgba1010102(x, y, z, w).ToBytes(bgra, 0, ComponentOrder.ZYXW); + Assert.Equal(bgra, new byte[] { 128, 0, 25, 0 }); + + // Alpha component accuracy will be awful. + Rgba1010102 r = new Rgba1010102(); + r.PackFromBytes(25, 0, 128, 0); + r.ToBytes(rgba, 0, ComponentOrder.XYZW); + Assert.Equal(rgba, new byte[] { 25, 0, 128, 0 }); + } + // 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 b2191d909..6c95951d8 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -62,6 +62,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);