Ejemplo n.º 1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        /// <remarks>
        /// Credits to enohka for this code.
        /// See more at: http://github.com/enohka/moddingSuite
        /// </remarks>
        public virtual TgvImage Read(String filePath)
        {
            var file = new TgvImage();

            byte[] rawDDSData = File.ReadAllBytes(filePath);
            using (var ms = new MemoryStream(rawDDSData))
            {
                var buffer = new byte[4];
                ms.Read(buffer, 0, buffer.Length);

                if (BitConverter.ToUInt32(buffer, 0) != DDSFormat.MagicHeader)
                {
                    throw new ArgumentException("Wrong DDS magic");
                }

                buffer = new byte[Marshal.SizeOf(typeof(DDSFormat.Header))];
                ms.Read(buffer, 0, buffer.Length);

                var header = MiscUtilities.ByteArrayToStructure <DDSFormat.Header>(buffer);
                header.MipMapCount = 1;

                DDSHelper.ConversionFlags conversionFlags;
                var format = DDSHelper.GetDXGIFormat(ref header.PixelFormat, out conversionFlags);

                //read only the main content mipmap
                uint minMipByteLength = DDSMipMapUilities.GetMinimumMipMapSizeForFormat(header.PixelFormat);
                uint mipByteLength    = (uint)DDSMipMapUilities.GetMipMapBytesCount((int)header.Width, (int)header.Height, format);
                mipByteLength = Math.Max(minMipByteLength, mipByteLength);

                buffer = new byte[mipByteLength];
                ms.Read(buffer, 0, buffer.Length);

                var mip = new TgvMipMap();
                mip.Content   = buffer;
                mip.Length    = mipByteLength;
                mip.MipSize   = header.Width * header.Height;
                mip.MipWidth  = header.Width;
                mip.MipHeight = header.Height;

                file.MipMapCount = (ushort)header.MipMapCount;
                file.MipMaps.Add(mip);
                file.Height            = header.Height;
                file.ImageHeight       = header.Height;
                file.Width             = header.Width;
                file.ImageWidth        = header.Width;
                file.Format            = format;
                file.PixelFormatString = TgvUtilities.GetTgvFromPixelFormat(format);
            }

            return(file);
        }
        public virtual TgvImage Read(String filePath)
        {
            var file = new TgvImage();

            byte[] rawDDSData = File.ReadAllBytes(filePath);
            using (var ms = new MemoryStream(rawDDSData))
            {
                var buffer = new byte[4];
                ms.Read(buffer, 0, buffer.Length);

                if (BitConverter.ToUInt32(buffer, 0) != DDSFormat.MagicHeader)
                {
                    throw new ArgumentException("Wrong DDS magic");
                }

                buffer = new byte[Marshal.SizeOf(typeof(DDSFormat.Header))];
                ms.Read(buffer, 0, buffer.Length);

                var header = MiscUtilities.ByteArrayToStructure <DDSFormat.Header>(buffer);

                DDSHelper.ConversionFlags conversionFlags;
                var format = DDSHelper.GetDXGIFormat(ref header.PixelFormat, out conversionFlags);

                if (header.MipMapCount == 0)
                {
                    header.MipMapCount = 1;
                }

                for (int i = 0; i < header.MipMapCount; i++)
                {
                    uint mipScale  = (uint)Math.Max(1, 2 << (i - 1));
                    uint mipWidth  = Math.Max(1, header.Width / mipScale);
                    uint mipHeight = Math.Max(1, header.Height / mipScale);

                    if (mipWidth < MinMipMapWidth || mipHeight < MinMipMapHeight)
                    {
                        break;
                    }

                    uint minMipByteLength = DDSMipMapUilities.GetMinimumMipMapSizeForFormat(header.PixelFormat);
                    uint mipByteLength    = (uint)DDSMipMapUilities.GetMipMapBytesCount((int)mipWidth, (int)mipHeight, format);
                    mipByteLength = Math.Max(minMipByteLength, mipByteLength);

                    buffer = new byte[mipByteLength];
                    ms.Read(buffer, 0, buffer.Length);

                    var mip = new TgvMipMap();
                    mip.Content   = buffer;
                    mip.Length    = mipByteLength;
                    mip.MipSize   = mipWidth * mipHeight;//(int)mipSize; //spr. czy to jest równe size czy width * height;
                    mip.MipWidth  = mipWidth;
                    mip.MipHeight = mipHeight;

                    file.MipMaps.Add(mip);
                }

                file.Height            = header.Height;
                file.ImageHeight       = header.Height;
                file.Width             = header.Width;
                file.ImageWidth        = header.Width;
                file.MipMapCount       = (ushort)file.MipMaps.Count;
                file.Format            = format;
                file.PixelFormatString = TgvUtilities.GetTgvFromPixelFormat(format);
            }

            return(file);
        }