///<summary>
/// Pack a color value to memory
///</summary>
///<param name="r,g,b,a">
/// The four color components, range 0.0f to 1.0f
/// (an exception to this case exists for floating point pixel
/// formats, which don't clamp to 0.0f..1.0f)
///</param>
///<param name="format">Pixelformat in which to write the color</param>
///<param name="dest">Destination memory location</param>
unsafe public static void PackColor(float r, float g, float b, float a, PixelFormat format, byte *dest)
{
// Catch-it-all here
PixelFormatDescription des = PixelConverter.GetDescriptionFor(format);
if ((des.flags & PixelFormatFlags.NativeEndian) != 0)
{
// Do the packing
uint value = ((Bitwise.FloatToFixed(r, des.rbits) << des.rshift) & des.rmask) |
((Bitwise.FloatToFixed(g, des.gbits) << des.gshift) & des.gmask) |
((Bitwise.FloatToFixed(b, des.bbits) << des.bshift) & des.bmask) |
((Bitwise.FloatToFixed(a, des.abits) << des.ashift) & des.amask);
// And write to memory
Bitwise.IntWrite(dest, des.elemBytes, value);
}
else
{
switch (format)
{
case PixelFormat.FLOAT32_R:
((float *)dest)[0] = r;
break;
case PixelFormat.FLOAT32_RGB:
((float *)dest)[0] = r;
((float *)dest)[1] = g;
((float *)dest)[2] = b;
break;
case PixelFormat.FLOAT32_RGBA:
((float *)dest)[0] = r;
((float *)dest)[1] = g;
((float *)dest)[2] = b;
((float *)dest)[3] = a;
break;
case PixelFormat.FLOAT16_R:
((ushort *)dest)[0] = Bitwise.FloatToHalf(r);
break;
case PixelFormat.FLOAT16_RGB:
((ushort *)dest)[0] = Bitwise.FloatToHalf(r);
((ushort *)dest)[1] = Bitwise.FloatToHalf(g);
((ushort *)dest)[2] = Bitwise.FloatToHalf(b);
break;
case PixelFormat.FLOAT16_RGBA:
((ushort *)dest)[0] = Bitwise.FloatToHalf(r);
((ushort *)dest)[1] = Bitwise.FloatToHalf(g);
((ushort *)dest)[2] = Bitwise.FloatToHalf(b);
((ushort *)dest)[3] = Bitwise.FloatToHalf(a);
break;
// case PixelFormat.SHORT_RGBA:
// ((ushort*)dest)[0] = Bitwise.FloatToFixed(r, 16);
// ((ushort*)dest)[1] = Bitwise.FloatToFixed(g, 16);
// ((ushort*)dest)[2] = Bitwise.FloatToFixed(b, 16);
// ((ushort*)dest)[3] = Bitwise.FloatToFixed(a, 16);
// break;
// case PixelFormat.BYTE_LA:
// ((byte*)dest)[0] = Bitwise.FloatToFixed(r, 8);
// ((byte*)dest)[1] = Bitwise.FloatToFixed(a, 8);
// break;
default:
// Not yet supported
throw new Exception("Pack to " + format + " not implemented, in PixelUtil.PackColor");
}
}
}