Ejemplo n.º 1
0
        public DdsImage ToDds(int index)
        {
            if (index < 0 || index >= Bitmaps.Count)
            {
                throw new ArgumentOutOfRangeException(nameof(index));
            }

            var submap = Bitmaps[index];

            var resource = InterleavedResources.Any()
                ? InterleavedResources[submap.InterleavedIndex].ResourcePointer
                : Resources[index].ResourcePointer;

            int virtualWidth, virtualHeight;

            if (cache.Metadata.IsMcc)
            {
                virtualWidth  = submap.Width;
                virtualHeight = submap.Height * submap.FaceCount;
            }
            else
            {
                TextureUtils.GetVirtualSize(submap.BitmapFormat, submap.Width, submap.Height, submap.FaceCount, out virtualWidth, out virtualHeight);
            }

            var lod0Size = virtualWidth * virtualHeight * submap.BitmapFormat.Bpp() / 8;
            var data     = resource.ReadData(PageType.Auto, lod0Size);

            if (cache.ByteOrder == ByteOrder.BigEndian)
            {
                var unitSize = submap.BitmapFormat.LinearUnitSize();
                for (int i = 0; i < data.Length - 1; i += unitSize)
                {
                    Array.Reverse(data, i, unitSize);
                }
            }

            if (submap.Flags.HasFlag(BitmapFlags.Swizzled))
            {
                data = TextureUtils.XTextureScramble(data, virtualWidth, virtualHeight, submap.BitmapFormat, false);
            }

            if (virtualWidth > submap.Width || virtualHeight > submap.Height)
            {
                data = TextureUtils.ApplyCrop(data, submap.BitmapFormat, submap.FaceCount, virtualWidth, virtualHeight, submap.Width, submap.Height * submap.FaceCount);
            }

            return(TextureUtils.GetDds(submap.Height, submap.Width, submap.BitmapFormat, submap.BitmapType == TextureType.CubeMap, data, cache.Metadata.IsMcc));
        }