Exemple #1
0
        public byte[] UnSwizzle(byte[] data, int width, int height, int blockSize)
        {
            var unswizzled   = new byte[data.Length];
            var dataIndex    = 0;
            int heightTexels = height / 4;
            int widthTexels  = width / 4;
            var texelCount   = widthTexels * heightTexels;

            for (int texel = 0; texel < texelCount; ++texel)
            {
                int pixelIndex = SwizzleUtilities.Morton(texel, widthTexels, heightTexels);
                int destIndex  = blockSize * pixelIndex;
                Array.Copy(data, dataIndex, unswizzled, destIndex, blockSize);
                dataIndex += blockSize;
            }

            return(unswizzled);
        }
        private byte[] DoSwizzle(byte[] data, int width, int height, int blockSize, bool unswizzle)
        {
            var processed           = new byte[data.Length];
            var heightTexels        = height / 4;
            var heightTexelsAligned = (heightTexels + 7) / 8;
            int widthTexels         = width / 4;
            var widthTexelsAligned  = (widthTexels + 7) / 8;
            var dataIndex           = 0;

            for (int y = 0; y < heightTexelsAligned; ++y)
            {
                for (int x = 0; x < widthTexelsAligned; ++x)
                {
                    for (int t = 0; t < 64; ++t)
                    {
                        int pixelIndex = SwizzleUtilities.Morton(t, 8, 8);
                        int num8       = pixelIndex / 8;
                        int num9       = pixelIndex % 8;
                        var yOffset    = (y * 8) + num8;
                        var xOffset    = (x * 8) + num9;

                        if (xOffset < widthTexels && yOffset < heightTexels)
                        {
                            var destPixelIndex = yOffset * widthTexels + xOffset;
                            int destIndex      = blockSize * destPixelIndex;

                            if (unswizzle)
                            {
                                Array.Copy(data, dataIndex, processed, destIndex, blockSize);
                            }
                            else
                            {
                                Array.Copy(data, destIndex, processed, dataIndex, blockSize);
                            }
                        }

                        dataIndex += blockSize;
                    }
                }
            }

            return(processed);
        }