// BC5 internal static void DecompressATI2Block(byte[] source, int sourceStart, byte[] destination, int decompressedStart, int decompressedLineLength, bool unused) { // Green = +1 DDS_BlockHelpers.Decompress8BitBlock(source, sourceStart, destination, decompressedStart + 1, decompressedLineLength, false); // Red = +2, source + 8 to skip first compressed block. DDS_BlockHelpers.Decompress8BitBlock(source, sourceStart + 8, destination, decompressedStart + 2, decompressedLineLength, false); // KFreon: Alpha is 255, and blue needs to be calculated for (int i = 0; i < 16; i++) { int offset = GetDecompressedOffset(decompressedStart, decompressedLineLength, i); // Get Red and Green on the range -1 - 1 // *2-1 moves the range from 0 - 1, to -1 - 1 double green = (destination[offset + 1] / 127.5d) - 1d; double red = (destination[offset + 2] / 127.5d) - 1d; // Z solution for: x2 + y2 + z2 = 1, unit normal vectors. Only consider +ve root as ATI2 is a tangent space mapping and Z must be +ve. // Also when 1 - x2 - y2 < 0, Z = NaN, but is compensated for in ExpandTo255. double Z = Math.Sqrt(1d - (red * red + green * green)); destination[offset] = ExpandTo255(Z); // Blue destination[offset + 3] = 255; // Alpha } }
internal static void DecompressBC3Block(byte[] source, int sourceStart, byte[] destination, int decompressedStart, int decompressedLineLength, bool isPremultiplied) { // Alpha, +3 to select that channel. DDS_BlockHelpers.Decompress8BitBlock(source, sourceStart, destination, decompressedStart + 3, decompressedLineLength, false); // RGB DDS_BlockHelpers.DecompressRGBBlock(source, sourceStart + 8, destination, decompressedStart, decompressedLineLength, false, isPremultiplied); }
// BC4 internal static void DecompressATI1Block(byte[] source, int sourceStart, byte[] destination, int decompressedStart, int decompressedLineLength, bool unused) { DDS_BlockHelpers.Decompress8BitBlock(source, sourceStart, destination, decompressedStart, decompressedLineLength, false); // KFreon: All channels are the same to make grayscale, and alpha needs to be 255. for (int i = 0; i < 16; i++) { int offset = GetDecompressedOffset(decompressedStart, decompressedLineLength, i); // Since one channel (blue) was set by the decompression above, just need to set the remaining channels destination[offset + 1] = destination[offset]; destination[offset + 2] = destination[offset]; destination[offset + 3] = 255; // Alpha } }
internal static void DecompressBC2Block(byte[] source, int sourceStart, byte[] destination, int decompressedStart, int decompressedLineLength, bool isPremultiplied) { // KFreon: Decompress alpha (only half of the texel count though, since each byte is 2 texels of alpha) for (int i = 0; i < 8; i++) { // Start + alphaOffset + lineOffset. // DecompressedStart = Top Left corner of texel in full image in bytes. // alphaOffset = effectively column offset in a row of bitmap. Since a compressed byte has 2 pixels worth of alpha, i % 2 * 8 skips 2 pixels of BGRA each byte read, +3 selects alpha channel. // lineOffset = texels aren't contiguous i.e. each row in texel isn't next to each other when decompressed. Need to skip to next line in entire bitmap. i / 2 is truncated by int cast, // so every 2 cycles (4 pixels, a full texel row) a bitmap line is skipped to the next line in texel. int offset = decompressedStart + ((i % 2) * 8 + 3) + (decompressedLineLength * (i / 2)); destination[offset] = (byte)(source[sourceStart + i] & 0xF0); // Decompress doesn't need formatDetails as it's just reading raw. destination[offset + 4] = (byte)(source[sourceStart + i] & 0x0F << 4); } // +8 skips the above alpha, otherwise it's just a BC1 RGB block DDS_BlockHelpers.DecompressRGBBlock(source, sourceStart + 8, destination, decompressedStart, decompressedLineLength, false, isPremultiplied); }
// TODO: Virtual/physical size. Less than 4x4 texels #region Compressed Readers internal static void DecompressBC1Block(byte[] source, int sourceStart, byte[] destination, int decompressedStart, int decompressedLineLength, bool isPremultiplied) { DDS_BlockHelpers.DecompressRGBBlock(source, sourceStart, destination, decompressedStart, decompressedLineLength, true, isPremultiplied); }