Exemplo n.º 1
0
        public bool WriteToFile(string fileName)
        {
            EndianWriter writer = null;

            try
            {
                // Open the output file in an EndianWriter.
                writer = new EndianWriter(Endianness.Little, new FileStream(fileName, FileMode.Create, FileAccess.Write, FileShare.None));
            }
            catch (Exception e)
            {
                // Failed to open the output file for writing.
                return(false);
            }

            // Write the normal DDS header.
            writer.Write(this.header.dwMagic);
            writer.Write(this.header.dwSize);
            writer.Write((int)this.header.dwFlags);
            writer.Write(this.header.dwHeight);
            writer.Write(this.header.dwWidth);
            writer.Write(this.header.dwPitchOrLinearSize);
            writer.Write(this.header.dwDepth);
            writer.Write(this.header.dwMipMapCount);
            writer.WritePadding(11 * 4);
            writer.Write(this.header.ddspf.dwSize);
            writer.Write((int)this.header.ddspf.dwFlags);
            writer.Write(this.header.ddspf.dwFourCC);
            writer.Write(this.header.ddspf.dwRGBBitCount);
            writer.Write(this.header.ddspf.dwRBitMask);
            writer.Write(this.header.ddspf.dwGBitMask);
            writer.Write(this.header.ddspf.dwBBitMask);
            writer.Write(this.header.ddspf.dwABitMask);
            writer.Write((int)this.header.dwCaps);
            writer.Write((int)this.header.dwCaps2);
            writer.Write(this.header.dwCaps3);
            writer.Write(this.header.dwCaps4);
            writer.WritePadding(4);

            // Check if we are using a DX10 header and if so write it to file.
            if (this.header.ddspf.dwFourCC == MakeFourCC("DX10"))
            {
                // Write the dx10 header.
                writer.Write((int)this.dxt10header.dxgiFormat);
                writer.Write((int)this.dxt10header.resourceDimension);
                writer.Write((int)this.dxt10header.miscFlag);
                writer.Write(this.dxt10header.arraySize);
                writer.Write(this.dxt10header.miscFlags2);
            }

            // Write the pixel data to file.
            writer.Write(this.PixelBuffer);

            // Close the file and return.
            writer.Close();
            return(true);
        }
Exemplo n.º 2
0
        public override byte[] ToBuffer()
        {
            // Create a new memory stream to back our file with.
            MemoryStream ms     = new MemoryStream();
            EndianWriter writer = new EndianWriter(this.IsBigEndian == true ? Endianness.Big : Endianness.Little, ms);

            // Write the header fields.
            writer.Write(rTextureHeader.kMagic);
            writer.Write((byte)rTextureHeader.kVersion);
            writer.Write((byte)this.header.TextureType);
            writer.Write((byte)this.header.Flags);
            writer.Write(this.header.MipMapCount);
            writer.Write(this.header.Width);
            writer.Write(this.header.Height);
            writer.Write(this.header.Depth);
            writer.Write(FourCCFromTextureFormat(this.header.Format, this.XboxFormat));

            // Check if we need to write the background color.
            if (this.header.Flags.HasFlag(TextureFlags.HasD3DClearColor) == true)
            {
                // Write the background color.
                writer.Write(this.BackgroundColor[0]);
                writer.Write(this.BackgroundColor[1]);
                writer.Write(this.BackgroundColor[2]);
                writer.Write(this.BackgroundColor[3]);
            }

            // If this is a cubemap write the cubemap data.
            if (this.header.TextureType == TextureType.Type_CubeMap)
            {
                // Write the cubemap data.
                writer.Write(this.cubemapData);
            }

            // If this is a depth map we need to write a DDS header for it.
            if (this.header.TextureType == TextureType.Type_DepthMap)
            {
                // Write the DDS header for the depth map.
                writer.Write(this.depthMapHeader.dwMagic);
                writer.Write(this.depthMapHeader.dwSize);
                writer.Write((int)this.depthMapHeader.dwFlags);
                writer.Write(this.depthMapHeader.dwHeight);
                writer.Write(this.depthMapHeader.dwWidth);
                writer.Write(this.depthMapHeader.dwPitchOrLinearSize);
                writer.Write(this.depthMapHeader.dwDepth);
                writer.Write(this.depthMapHeader.dwMipMapCount);
                writer.WritePadding(11 * 4);
                writer.Write(this.depthMapHeader.ddspf.dwSize);
                writer.Write((int)this.depthMapHeader.ddspf.dwFlags);
                writer.Write(this.depthMapHeader.ddspf.dwFourCC);
                writer.Write(this.depthMapHeader.ddspf.dwRGBBitCount);
                writer.Write(this.depthMapHeader.ddspf.dwRBitMask);
                writer.Write(this.depthMapHeader.ddspf.dwGBitMask);
                writer.Write(this.depthMapHeader.ddspf.dwBBitMask);
                writer.Write(this.depthMapHeader.ddspf.dwABitMask);
                writer.Write((int)this.depthMapHeader.dwCaps);
                writer.Write((int)this.depthMapHeader.dwCaps2);
                writer.Write(this.depthMapHeader.dwCaps3);
                writer.Write(this.depthMapHeader.dwCaps4);
                writer.WritePadding(4);
            }

            // Read the pixel buffer from the data stream.
            byte[] pixelData = new byte[this.PixelDataStream.Length];
            this.PixelDataStream.Seek(0, SeekOrigin.Begin);
            this.PixelDataStream.Read(pixelData, 0, pixelData.Length);

            // TODO: For xbox textures we need to tile the pixel buffer.

            // Write the pixel data.
            writer.Write(pixelData);

            // Close the binary writer and return the memory stream as a byte array.
            writer.Close();
            return(ms.ToArray());
        }