Browse Source

Added new interface to convert an IPackedPixel to and from a set of bytes.

Former-commit-id: 63481b92335c358cfeb10da5df3c4fb79abf7786
Former-commit-id: 2e9a02d7896340fbd9610c358ba4cd472ac124a1
Former-commit-id: bb5030a386fb749f779e5acddd462832d6412ee2
af/merge-core
dirk 9 years ago
parent
commit
36cf049c29
  1. 55
      src/ImageProcessorCore/Colors/Color.cs
  2. 33
      src/ImageProcessorCore/Colors/ComponentOrder.cs
  3. 31
      src/ImageProcessorCore/Colors/PackedPixel/IPackedBytes.cs
  4. 4
      src/ImageProcessorCore/Colors/PackedPixel/IPackedPixel.cs
  5. 0
      src/ImageProcessorCore/Colors/PackedPixel/IPackedVector.cs

55
src/ImageProcessorCore/Colors/Color.cs

@ -43,7 +43,7 @@ namespace ImageProcessorCore
/// <param name="a">The alpha component.</param>
public Color(byte r, byte g, byte b, byte a = 255)
{
this.packedValue = (uint)(r << 24 | g << 16 | b << 8 | a);
this.packedValue = Pack(r, g, b, a);
}
/// <summary>
@ -219,6 +219,44 @@ namespace ImageProcessorCore
return new Color(hex);
}
/// <inheritdoc/>
public void PackFromBytes(byte r, byte g, byte b, byte a)
{
this.packedValue = Pack(r, g, b, a);
}
/// <inheritdoc/>
public void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder)
{
switch (componentOrder)
{
case ComponentOrder.BGR:
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
break;
case ComponentOrder.BGRA:
bytes[startIndex] = this.B;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.R;
bytes[startIndex + 3] = this.A;
break;
case ComponentOrder.RGB:
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
break;
case ComponentOrder.RGBA:
bytes[startIndex] = this.R;
bytes[startIndex + 1] = this.G;
bytes[startIndex + 2] = this.B;
bytes[startIndex + 3] = this.A;
break;
default:
throw new NotSupportedException();
}
}
/// <summary>
/// Converts the value of this instance to a hexadecimal string.
/// </summary>
@ -295,7 +333,7 @@ namespace ImageProcessorCore
}
/// <summary>
/// Packs the four floats into a uint.
/// Packs the four floats into a <see cref="uint"/>.
/// </summary>
/// <param name="x">The x-component</param>
/// <param name="y">The y-component</param>
@ -308,6 +346,19 @@ namespace ImageProcessorCore
return Pack(ref value);
}
/// <summary>
/// Packs the four floats into a <see cref="uint"/>.
/// </summary>
/// <param name="x">The x-component</param>
/// <param name="y">The y-component</param>
/// <param name="z">The z-component</param>
/// <param name="w">The w-component</param>
/// <returns>The <see cref="uint"/></returns>
private static uint Pack(byte x, byte y, byte z, byte w)
{
return (uint)(x << 24 | y << 16 | z << 8 | w);
}
/// <summary>
/// Converts the specified hex value to an rrggbbaa hex value.
/// </summary>

33
src/ImageProcessorCore/Colors/ComponentOrder.cs

@ -0,0 +1,33 @@
// <copyright file="ComponentOrder.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessorCore
{
/// <summary>
/// Enumerates the various component orders.
/// </summary>
public enum ComponentOrder
{
/// <summary>
/// Blue-> Green-> Red order.
/// </summary>
BGR,
/// <summary>
/// Blue-> Green-> Red-> Alpha order.
/// </summary>
BGRA,
/// <summary>
/// Red-> Green-> Blue order.
/// </summary>
RGB,
/// <summary>
/// Red-> Green-> Blue-> Alpha order.
/// </summary>
RGBA,
}
}

31
src/ImageProcessorCore/Colors/PackedPixel/IPackedBytes.cs

@ -0,0 +1,31 @@
// <copyright file="IPackedBytes.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageProcessorCore
{
/// <summary>
/// An interface that converts packed vector types to and from <see cref="byte[]"/> values,
/// allowing multiple encodings to be manipulated in a generic manner.
/// </summary>
public interface IPackedBytes
{
/// <summary>
/// Gets the packed representation from the gives bytes.
/// </summary>
/// <param name="x">The x-component.</param>
/// <param name="y">The y-component.</param>
/// <param name="z">The z-omponent.</param>
/// <param name="w">The w-component.</param>
void PackFromBytes(byte x, byte y, byte z, byte w);
/// <summary>
/// Sets the packed representation into the gives bytes.
/// </summary>
/// <param name="bytes">The bytes to set the color in.</param>
/// <param name="startIndex">The starting index of the <paramref name="bytes"/>.</param>
/// <param name="componentOrder">The order of the components.</param>
void ToBytes(byte[] bytes, int startIndex, ComponentOrder componentOrder);
}
}

4
src/ImageProcessorCore/Colors/PackedPixel/IPackedPixel.cs

@ -1,4 +1,4 @@
// <copyright file="IPackedVector.cs" company="James Jackson-South">
// <copyright file="IPackedPixel.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -9,7 +9,7 @@ namespace ImageProcessorCore
/// An interface that represents a packed pixel type.
/// </summary>
/// <typeparam name="TPacked">The packed format. <example>uint, long, float.</example></typeparam>
public interface IPackedPixel<TPacked> : IPackedVector<TPacked>
public interface IPackedPixel<TPacked> : IPackedVector<TPacked>, IPackedBytes
where TPacked : struct
{
}

0
src/ImageProcessorCore/Colors/PackedVector/IPackedVector.cs → src/ImageProcessorCore/Colors/PackedPixel/IPackedVector.cs

Loading…
Cancel
Save