public byte[] Compress(Color[] colors) { var colorSpace = new ColorSpace(colors); var colorTable = Helpers.CreateFor1BitAlpha(colorSpace.MinColor, colorSpace.MaxColor); var block = new BC1BlockData { Color0 = colorTable[0], Color1 = colorTable[1] }; for (int i = 0; i < colors.Length; ++i) { var color32 = colors[i]; // If color has alpha, use a specific index // to identify the color when decompressed block.ColorIndexes[i] = Helpers.GetIndexOfClosestColor(colorTable, Helpers.To16Bit(color32)); } var b = block.ToBytes(); return(b); }
/// <summary> /// Instantiates a <see cref="BC1BlockData"/> from compressed BC1 block data. /// </summary> public static BC1BlockData FromBytes(byte[] bytes) { var color0Low = bytes[0]; var color0High = bytes[1]; var color1Low = bytes[2]; var color1Hight = bytes[3]; var indexes = new byte[4]; indexes[0] = bytes[4]; indexes[1] = bytes[5]; indexes[2] = bytes[6]; indexes[3] = bytes[7]; var block = new BC1BlockData { Color0 = Helpers.FromValue((ushort)((color0High << 8) | color0Low)), Color1 = Helpers.FromValue((ushort)((color1Hight << 8) | color1Low)) }; for (int p = 0, row = 0; p < Constants.TexelCount; p += Constants.BlockDimension, ++row) { block.ColorIndexes[p] = indexes[row] & 0x03; block.ColorIndexes[p + 1] = (indexes[row] >> 2) & 0x03; block.ColorIndexes[p + 2] = (indexes[row] >> 4) & 0x03; block.ColorIndexes[p + 3] = (indexes[row] >> 6) & 0x03; } return(block); }
public Color[] Decompress(byte[] blockData) { var block = BC1BlockData.FromBytes(blockData); var colorTable = Helpers.CreateFor1BitAlpha(block.Color0, block.Color1); var colors = new Color[Constants.TexelCount]; for (int i = 0; i < colors.Length; ++i) { int index = block.ColorIndexes[i]; int alpha = 255; var color16 = colorTable[index]; var color32 = Color.FromArgb(alpha, Helpers.To32Bit(color16)); colors[i] = color32; } return(colors); }