Пример #1
0
        public static FtexFile ConvertToFtex(DdsFile file, FtexTextureType textureType)
        {
            FtexFile result = new FtexFile();
            if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfA8R8G8B8()))
                result.PixelFormatType = 0;
            else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsLuminance()))
                result.PixelFormatType = 1;
            else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt1()))
                result.PixelFormatType = 2;
            else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt3()))
                result.PixelFormatType = 3; // HACK: This is just a guess. The value isn't used in the Ground Zeroes files.
            else if (file.Header.PixelFormat.Equals(DdsPixelFormat.DdsPfDxt5()))
                result.PixelFormatType = 4;
            else
                throw new NotImplementedException(String.Format("Unknown PixelFormatType {0}", file.Header.PixelFormat));

            result.Height = Convert.ToInt16(file.Header.Height);
            result.Width = Convert.ToInt16(file.Header.Width);
            result.Depth = Convert.ToInt16(file.Header.Depth);

            var mipMapData = GetMipMapData(file);
            var mipMaps = GetMipMapInfos(mipMapData);
            var ftexsFiles = GetFtexsFiles(mipMaps, mipMapData);
            result.MipMapCount = Convert.ToByte(mipMaps.Count());
            result.NrtFlag = 2;
            result.AddMipMapInfos(mipMaps);
            result.AddFtexsFiles(ftexsFiles);
            result.FtexsFileCount = Convert.ToByte(ftexsFiles.Count());
            result.AdditionalFtexsFileCount = Convert.ToByte(ftexsFiles.Count() - 1);
            result.TextureType = textureType;

            // TODO: Handle the DDS depth flag.
            return result;
        }
Пример #2
0
        public static DdsFile ConvertToDds(FtexFile file, bool deswizzle)
        {
            DdsFile result = new DdsFile
            {
                Header = new DdsFileHeader
                {
                    Size = DdsFileHeader.DefaultHeaderSize,
                    Flags = DdsFileHeaderFlags.Texture | DdsFileHeaderFlags.MipMap,
                    Height = file.Height,
                    Width = file.Width,
                    Depth = file.Depth,
                    MipMapCount = file.MipMapCount,
                    Caps = DdsSurfaceFlags.Texture | DdsSurfaceFlags.MipMap
                }
            };

            result.Data = file.Data;

            switch (file.PixelFormatType)
            {
                case 0:
                    result.Header.PixelFormat = DdsPixelFormat.DdsPfA8R8G8B8();
                    result.Header.Flags = result.Header.Flags | DdsFileHeaderFlags.Volume;
                    if (deswizzle)
                        result.Data = ByteSwap16(Deswizzle(result.Data, result.Header.Width, result.Header.Height, result.Header.MipMapCount, "RGBA8"));

                    break;
                case 1:
                    result.Header.PixelFormat = DdsPixelFormat.DdsLuminance();
                    break;
                case 2:
                    result.Header.PixelFormat = DdsPixelFormat.DdsPfDxt1();
                    if (deswizzle)
                        result.Data = ByteSwap16(Deswizzle(result.Data, result.Header.Width, result.Header.Height, result.Header.MipMapCount, "DXT1"));

                    break;
                case 3:
                    result.Header.PixelFormat = DdsPixelFormat.DdsPfDxt3(); // HACK: This is just a guess. The value isn't used in the Ground Zeroes files.
                    if (deswizzle)
                        result.Data = ByteSwap16(Deswizzle(result.Data, result.Header.Width, result.Header.Height, result.Header.MipMapCount, "DXT3"));

                    break;
                case 4:
                    result.Header.PixelFormat = DdsPixelFormat.DdsPfDxt5();
                    if (deswizzle)
                        result.Data = ByteSwap16(Deswizzle(result.Data, result.Header.Width, result.Header.Height, result.Header.MipMapCount, "DXT5"));

                    break;
                default:
                    throw new NotImplementedException(String.Format("Unknown PixelFormatType {0}", file.PixelFormatType));
            }

            return result;
        }
Пример #3
0
        public static DdsFile Read(Stream inputStream)
        {
            DdsFile result = new DdsFile();
            BinaryReader reader = new BinaryReader(inputStream, Encoding.Default, true);
            if (reader.ReadInt32() != MagicNumber)
                return null;

            result.Header = DdsFileHeader.Read(inputStream);

            if (result.Header.IsDx10())
            {
                result.HeaderDx10 = DdsFileHeaderDx10.Read(inputStream);
            }
            MemoryStream dataStream = new MemoryStream();
            inputStream.CopyTo(dataStream);
            result.Data = dataStream.ToArray();
            return result;
        }
Пример #4
0
        public static DdsFile Read(Stream inputStream)
        {
            DdsFile      result = new DdsFile();
            BinaryReader reader = new BinaryReader(inputStream, Encoding.Default, true);

            reader.Assert(MagicNumber);
            result.Header = DdsFileHeader.Read(inputStream);

            if (result.Header.IsDx10())
            {
                result.HeaderDx10 = DdsFileHeaderDx10.Read(inputStream);
            }
            MemoryStream dataStream = new MemoryStream();

            inputStream.CopyTo(dataStream);
            result.Data = dataStream.ToArray();
            return(result);
        }
Пример #5
0
 private static List<byte[]> GetMipMapData(DdsFile file)
 {
     const int minimumWidth = 4;
     const int minimumHeight = 4;
     List<byte[]> mipMapDatas = new List<byte[]>();
     byte[] data = file.Data;
     int dataOffset = 0;
     var width = file.Header.Width;
     var height = file.Header.Height;
     int mipMapsCount = file.Header.Flags.HasFlag(DdsFileHeaderFlags.MipMap) ? file.Header.MipMapCount : 1;
     for (int i = 0; i < mipMapsCount; i++)
     {
         int size = DdsPixelFormat.CalculateImageSize(file.Header.PixelFormat, width, height);
         var buffer = new byte[size];
         Array.Copy(data, dataOffset, buffer, 0, size);
         mipMapDatas.Add(buffer);
         dataOffset += size;
         width = Math.Max(width / 2, minimumWidth);
         height = Math.Max(height / 2, minimumHeight);
     }
     return mipMapDatas;
 }