CSharpImageLibrary.DDS.DDSGeneral.WriteCompressedMipMap C# (CSharp) Method

WriteCompressedMipMap() static private method

static private WriteCompressedMipMap ( byte destination, int mipOffset, MipMap mipmap, int blockSize, Action compressor, AlphaSettings alphaSetting ) : int
destination byte
mipOffset int
mipmap MipMap
blockSize int
compressor Action
alphaSetting AlphaSettings
return int
        static int WriteCompressedMipMap(byte[] destination, int mipOffset, MipMap mipmap, int blockSize, Action<byte[], int, int, byte[], int, AlphaSettings> compressor, AlphaSettings alphaSetting)
        {
            int destinationTexelCount = mipmap.Width * mipmap.Height / 16;
            int sourceLineLength = mipmap.Width * 4;
            int numTexelsInLine = mipmap.Width / 4;

            var mipWriter = new Action<int>(texelIndex =>
            {
                // Since this is the top corner of the first texel in a line, skip 4 pixel rows (texel = 4x4 pixels) and the number of rows down the bitmap we are already.
                int sourceLineOffset = sourceLineLength * 4 * (texelIndex / numTexelsInLine);  // Length in bytes x 3 lines x texel line index (how many texel sized lines down the image are we). Index / width will truncate, so for the first texel line, it'll be < 0. For the second texel line, it'll be < 1 and > 0.

                int sourceTopLeftCorner = ((texelIndex % numTexelsInLine) * 16) + sourceLineOffset; // *16 since its 4 pixels with 4 channels each. Index % numTexels will effectively reset each line.
                compressor(mipmap.Pixels, sourceTopLeftCorner, sourceLineLength, destination, mipOffset + texelIndex * blockSize, alphaSetting);
            });

            // Choose an acceleration method.
            if (ImageEngine.EnableGPUAcceleration)
                Debugger.Break();
            else if (ImageEngine.EnableThreading)
                Parallel.For(0, destinationTexelCount, new ParallelOptions { MaxDegreeOfParallelism = ImageEngine.NumThreads }, mipWriter);
            else
                for (int i = 0; i < destinationTexelCount; i++)
                    mipWriter(i);

            return mipOffset + destinationTexelCount * blockSize;
        }