public static DDSFile ReadFile(string fileName) { DDSFile file = new DDSFile(); using (FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read)) { byte[] buf = new byte[Marshal.SizeOf(typeof(DDSHEAD))]; int length = fs.Read(buf, 0, Marshal.SizeOf(typeof(DDSHEAD))); if (length != Marshal.SizeOf(typeof(DDSHEAD))) { throw new Exception("Could not read the whole header."); } file.header = (DDSHEAD)RawDeserialize(buf, typeof(DDSHEAD)); file.SetSizeAndMipmapCount(file.header.dwWidth, file.header.dwHeight, file.header.dwMipMapCount); file.mipmapData = new byte[file.RequiredMipSizeAll]; length = fs.Read(file.mipmapData, 0, Convert.ToInt32(file.RequiredMipSizeAll)); if (length != file.RequiredMipSizeAll) { throw new Exception("Could not read the whole mipmaps."); } if (fs.Length != fs.Position) { throw new Exception("There are rest bytes"); } } return(file); }
internal void Save() { string ddsFileName = path.Substring(0, path.Length - ".Texture2D".Length) + ".dds"; DDSFile file = new DDSFile(); int[] mips = GetExportableBulkDataIndices(); string format = (string)property["Format"]; file.SetPixelFormat(format); file.SetSizeAndMipmapCount(Width, Height, (uint)mips.Length); for (int i = 0; i < mips.Length; i++) { file.SetData(GetExportableBulkData(mips[i]), i); } file.WriteFile(ddsFileName); }