Exemplo n.º 1
0
        public void Deserialize(UnityBinaryReader reader)
        {
            dwSize = reader.ReadLEUInt32();
            if (dwSize != 124)
            {
                throw new FileFormatException("Invalid DDS file header size: " + dwSize.ToString() + '.');
            }

            dwFlags = reader.ReadLEUInt32();
            if (!Utils.ContainsBitFlags(dwFlags, (uint)DDSFlags.Height, (uint)DDSFlags.Width))
            {
                throw new FileFormatException("Invalid DDS file flags: " + dwFlags.ToString() + '.');
            }

            dwHeight            = reader.ReadLEUInt32();
            dwWidth             = reader.ReadLEUInt32();
            dwPitchOrLinearSize = reader.ReadLEUInt32();
            dwDepth             = reader.ReadLEUInt32();
            dwMipMapCount       = reader.ReadLEUInt32();

            dwReserved1 = new uint[11];
            for (int i = 0; i < dwReserved1.Length; i++)
            {
                dwReserved1[i] = reader.ReadLEUInt32();
            }

            pixelFormat = new DDSPixelFormat();
            pixelFormat.Deserialize(reader);

            dwCaps = reader.ReadLEUInt32();
            if (!Utils.ContainsBitFlags(dwCaps, (uint)DDSCaps.Texture))
            {
                throw new FileFormatException("Invalid DDS file caps: " + dwCaps.ToString() + '.');
            }

            dwCaps2     = reader.ReadLEUInt32();
            dwCaps3     = reader.ReadLEUInt32();
            dwCaps4     = reader.ReadLEUInt32();
            dwReserved2 = reader.ReadLEUInt32();
        }
Exemplo n.º 2
0
 private static byte[] DecodeDXT5ToARGB(byte[] compressedData, uint width, uint height, DDSPixelFormat pixelFormat, uint mipmapCount)
 {
     return(DecodeDXTToARGB(5, compressedData, width, height, pixelFormat, mipmapCount));
 }
Exemplo n.º 3
0
        /// <summary>
        /// Decodes DXT data to ARGB.
        /// </summary>
        private static byte[] DecodeDXTToARGB(int DXTVersion, byte[] compressedData, uint width, uint height, DDSPixelFormat pixelFormat, uint mipmapCount)
        {
            bool alphaFlag     = Utils.ContainsBitFlags(pixelFormat.flags, (uint)DDSPixelFormatFlags.AlphaPixels);
            bool containsAlpha = alphaFlag || ((pixelFormat.RGBBitCount == 32) && (pixelFormat.ABitMask != 0));

            var reader = new UnityBinaryReader(new MemoryStream(compressedData));
            var argb   = new byte[TextureUtils.CalculateMipMappedTextureDataSize((int)width, (int)height, 4)];

            int mipMapWidth   = (int)width;
            int mipMapHeight  = (int)height;
            int baseARGBIndex = 0;

            for (int mipMapIndex = 0; mipMapIndex < mipmapCount; mipMapIndex++)
            {
                for (int rowIndex = 0; rowIndex < mipMapHeight; rowIndex += 4)
                {
                    for (int columnIndex = 0; columnIndex < mipMapWidth; columnIndex += 4)
                    {
                        Color32[] colors = null;

                        // Doing a switch instead of using a delegate for speed.
                        switch (DXTVersion)
                        {
                        case 1:
                            colors = DecodeDXT1TexelBlock(reader, containsAlpha);
                            break;

                        case 3:
                            colors = DecodeDXT3TexelBlock(reader);
                            break;

                        case 5:
                            colors = DecodeDXT5TexelBlock(reader);
                            break;

                        default:
                            throw new NotImplementedException("Tried decoding a DDS file using an unsupported DXT format: DXT" + DXTVersion.ToString());
                        }

                        CopyDecodedTexelBlock(colors, argb, baseARGBIndex, rowIndex, columnIndex, mipMapWidth, mipMapHeight);
                    }
                }

                baseARGBIndex += (mipMapWidth * mipMapHeight * 4);

                mipMapWidth  /= 2;
                mipMapHeight /= 2;
            }

            return(argb);
        }
Exemplo n.º 4
0
		public void Deserialize(UnityBinaryReader reader)
		{
			dwSize = reader.ReadLEUInt32();
			if(dwSize != 124)
			{
				throw new FileFormatException("Invalid DDS file header size: " + dwSize.ToString() + '.');
			}

			dwFlags = reader.ReadLEUInt32();
			if(!Utils.ContainsBitFlags(dwFlags, (uint)DDSFlags.Height, (uint)DDSFlags.Width))
			{
				throw new FileFormatException("Invalid DDS file flags: " + dwFlags.ToString() + '.');
			}

			dwHeight = reader.ReadLEUInt32();
			dwWidth = reader.ReadLEUInt32();
			dwPitchOrLinearSize = reader.ReadLEUInt32();
			dwDepth = reader.ReadLEUInt32();
			dwMipMapCount = reader.ReadLEUInt32();

			dwReserved1 = new uint[11];
			for(int i = 0; i < dwReserved1.Length; i++)
			{
				dwReserved1[i] = reader.ReadLEUInt32();
			}

			pixelFormat = new DDSPixelFormat();
			pixelFormat.Deserialize(reader);

			dwCaps = reader.ReadLEUInt32();
			if(!Utils.ContainsBitFlags(dwCaps, (uint)DDSCaps.Texture))
			{
				throw new FileFormatException("Invalid DDS file caps: " + dwCaps.ToString() + '.');
			}

			dwCaps2 = reader.ReadLEUInt32();
			dwCaps3 = reader.ReadLEUInt32();
			dwCaps4 = reader.ReadLEUInt32();
			dwReserved2 = reader.ReadLEUInt32();
		}
Exemplo n.º 5
0
		private static byte[] DecodeDXT5ToARGB(byte[] compressedData, uint width, uint height, DDSPixelFormat pixelFormat, uint mipmapCount)
		{
			return DecodeDXTToARGB(5, compressedData, width, height, pixelFormat, mipmapCount);
		}
Exemplo n.º 6
0
		/// <summary>
		/// Decodes DXT data to ARGB.
		/// </summary>
		private static byte[] DecodeDXTToARGB(int DXTVersion, byte[] compressedData, uint width, uint height, DDSPixelFormat pixelFormat, uint mipmapCount)
		{
			bool alphaFlag = Utils.ContainsBitFlags(pixelFormat.flags, (uint)DDSPixelFormatFlags.AlphaPixels);
			bool containsAlpha = alphaFlag || ((pixelFormat.RGBBitCount == 32) && (pixelFormat.ABitMask != 0));

			var reader = new UnityBinaryReader(new MemoryStream(compressedData));
			var argb = new byte[TextureUtils.CalculateMipMappedTextureDataSize((int)width, (int)height, 4)];

			int mipMapWidth = (int)width;
			int mipMapHeight = (int)height;
			int baseARGBIndex = 0;

			for(int mipMapIndex = 0; mipMapIndex < mipmapCount; mipMapIndex++)
			{
				for(int rowIndex = 0; rowIndex < mipMapHeight; rowIndex += 4)
				{
					for(int columnIndex = 0; columnIndex < mipMapWidth; columnIndex += 4)
					{
						Color32[] colors = null;

						// Doing a switch instead of using a delegate for speed.
						switch(DXTVersion)
						{
							case 1:
								colors = DecodeDXT1TexelBlock(reader, containsAlpha);
								break;
							case 3:
								colors = DecodeDXT3TexelBlock(reader);
								break;
							case 5:
								colors = DecodeDXT5TexelBlock(reader);
								break;
							default:
								throw new NotImplementedException("Tried decoding a DDS file using an unsupported DXT format: DXT" + DXTVersion.ToString());
						}

						CopyDecodedTexelBlock(colors, argb, baseARGBIndex, rowIndex, columnIndex, mipMapWidth, mipMapHeight);
					}
				}

				baseARGBIndex += (mipMapWidth * mipMapHeight * 4);

				mipMapWidth /= 2;
				mipMapHeight /= 2;
			}

			return argb;
		}