/// <summary> /// Write a universal compression header /// </summary> /// <param name="bitstream"></param> /// <param name="data"></param> /// <param name="headerFlags"></param> public static void WriteUniversalHeader(Bitstream bitstream, Datastream data, HeaderFlags headerFlags) { if (headerFlags.HasFlag(HeaderFlags.SymbolCount)) { UniversalCodec.Lomont.EncodeLomont1(bitstream, (uint)data.Count, 6, 0); } if (headerFlags.HasFlag(HeaderFlags.BitsPerSymbol)) { var max = data.Any() ? data.Max() : 0; var bitsPerSymbol = CodecBase.BitsRequired(max); UniversalCodec.Lomont.EncodeLomont1(bitstream, bitsPerSymbol - 1, 3, 0); } }
/// <summary> /// Read a universal compression header /// Returns (symbol count, bits per symbol) /// </summary> /// <param name="bitstream"></param> /// <param name="headerFlags"></param> public static Tuple <uint, uint> ReadUniversalHeader(Bitstream bitstream, HeaderFlags headerFlags) { uint decompressedSymbolCount = 0; uint bitsPerSymbol = 0; if (headerFlags.HasFlag(HeaderFlags.SymbolCount)) { decompressedSymbolCount = UniversalCodec.Lomont.DecodeLomont1(bitstream, 6, 0); } if (headerFlags.HasFlag(HeaderFlags.BitsPerSymbol)) { bitsPerSymbol = UniversalCodec.Lomont.DecodeLomont1(bitstream, 3, 0) + 1; } return(new Tuple <uint, uint>(decompressedSymbolCount, bitsPerSymbol)); }
public static void Save(BinaryWriter bw, DDS dds) { HeaderFlags flags = HeaderFlags.Caps | HeaderFlags.Height | HeaderFlags.Width | HeaderFlags.PixelFormat | HeaderFlags.MipMapCount; flags |= dds.Format == D3DFormat.A8R8G8B8 ? HeaderFlags.Pitch : HeaderFlags.LinearSize; bw.Write(new byte[] { 0x44, 0x44, 0x53, 0x20 }); // 'DDS ' bw.Write(124); bw.Write((int)flags); bw.Write(dds.Height); bw.Write(dds.Width); bw.Write(flags.HasFlag(HeaderFlags.Pitch) ? dds.Width * 4 : dds.MipMaps[0].Data.Length); bw.Write(dds.Depth); bw.Write(dds.MipMaps.Count); for (int i = 0; i < 11; i++) { bw.Write(0); } // PixelFormat bw.Write(32); switch (dds.Format) { case D3DFormat.DXT1: case D3DFormat.DXT3: case D3DFormat.DXT5: bw.Write(4); // fourCC length bw.Write(dds.Format.ToString().ToCharArray()); bw.Write(0); bw.Write(0); bw.Write(0); bw.Write(0); bw.Write(0); break; default: bw.Write(0); // fourCC length bw.Write(0); bw.Write(32); // RGB bit count bw.Write(255 << 16); // R mask bw.Write(255 << 8); // G mask bw.Write(255 << 0); // B mask bw.Write(255 << 24); // A mask break; } bw.Write((int)DDSCaps.DDSCAPS_TEXTURE); bw.Write(0); // Caps 2 bw.Write(0); // Caps 3 bw.Write(0); // Caps 4 bw.Write(0); // Reserved for (int i = 0; i < dds.MipMaps.Count; i++) { bw.Write(dds.MipMaps[i].Data); } }
private static bool ValidateTexture(HeaderDXT10 header, HeaderFlags flags, out int depth, out PixelFormat format, out ResourceDimension resDim, out int arraySize, out bool isCubeMap) { depth = 0; format = Renderer.PixelFormat.Unknown; resDim = ResourceDimension.Unknown; arraySize = 0x1; isCubeMap = false; arraySize = header.ArraySize; if (arraySize == 0) { return(false); } if (header.MiscFlag2 != FlagsDX10.AlphaModeUnknown) { return(false); } if (DDSPixelFormat.BitsPerPixel(header.DXGIFormat) == 0) { return(false); } format = header.DXGIFormat; switch (header.Dimension) { case ResourceDimension.Texture1D: depth = 1; break; case ResourceDimension.Texture2D: if (header.MiscFlag.HasFlag(ResourceOptionFlags.TextureCube)) { arraySize *= 6; isCubeMap = true; } depth = 1; break; case ResourceDimension.Texture3D: if (!flags.HasFlag(HeaderFlags.Depth)) { return(false); } if (arraySize > 1) { return(false); } break; default: return(false); } resDim = header.Dimension; return(true); }