private async Task readNameTable(Action <DomainLoadProgress> progress) { DomainLoadProgress message = new DomainLoadProgress { Text = "Reading Name Table...", Current = 0, Total = NameTableCount }; reader.Seek(NameTableOffset); for (int i = 0; i < NameTableCount; ++i) { DomainNameTableEntry name = new DomainNameTableEntry { TableIndex = i }; await name.ReadNameTableEntry(reader); NameTable.Add(name); message.IncrementCurrent(); if (NameTableCount > 100) { progress?.Invoke(message); } } }
public override void SetPropertyValue(object value) { DomainNameTableIndex index = value as DomainNameTableIndex; if (index == null) { DomainNameTableEntry entry = value as DomainNameTableEntry; if (entry != null) { index = new DomainNameTableIndex(); index.SetNameTableIndex(entry); } } if (index != null) { NameIndexValue = index; return; } if (value is bool && byteValue.HasValue) { byteValue = Convert.ToByte(value); } }
public static DomainNameTableEntry AddDomainNameTableEntry(this List <DomainNameTableEntry> nameTable, string value) { DomainString valueString = new DomainString(); valueString.SetString(value); int index = nameTable.Max(nt => nt.TableIndex) + 1; DomainNameTableEntry entry = new DomainNameTableEntry(); entry.SetNameTableEntry(valueString, 0x0007001000000000, index); nameTable.Add(entry); return(entry); }
public override async Task SetObject(string filename, List <DomainNameTableEntry> nameTable) { ImageEngineImage image = await Task.Run(() => new ImageEngineImage(filename)); int width = image.Width; int height = image.Height; DomainPropertyIntValue sizeX = PropertyHeader.GetProperty("SizeX").FirstOrDefault()?.Value as DomainPropertyIntValue; DomainPropertyIntValue sizeY = PropertyHeader.GetProperty("SizeY").FirstOrDefault()?.Value as DomainPropertyIntValue; sizeX?.SetPropertyValue(width); sizeY?.SetPropertyValue(height); DomainPropertyIntValue mipTailBaseIdx = PropertyHeader.GetProperty("MipTailBaseIdx").FirstOrDefault()?.Value as DomainPropertyIntValue; mipTailBaseIdx?.SetPropertyValue((int)Math.Log(width > height ? width : height, 2)); DomainPropertyStringValue filePath = PropertyHeader.GetProperty("SourceFilePath").FirstOrDefault()?.Value as DomainPropertyStringValue; DomainPropertyStringValue fileTime = PropertyHeader.GetProperty("SourceFileTimestamp").FirstOrDefault()?.Value as DomainPropertyStringValue; filePath?.SetPropertyValue(filename); fileTime?.SetPropertyValue(File.GetLastWriteTime(filename).ToString("yyyy-MM-dd hh:mm:ss")); DomainPropertyByteValue pfFormat = PropertyHeader.GetProperty("Format").FirstOrDefault()?.Value as DomainPropertyByteValue; ImageEngineFormat imageFormat = image.Format.InternalFormat; if (!imageFormat.ToString().Contains("DDS")) { throw new Exception($"Image is not in a DDS format. It is actually {imageFormat}."); } if (pfFormat != null) { string formatStr = imageFormat.ToString().Replace("DDS", "PF"); if (formatStr.Contains("ARGB")) { formatStr = "PF_A8R8G8B8"; } else if (formatStr.Contains("G8")) { formatStr = "PF_G8"; } DomainNameTableEntry formatTableEntry = nameTable.SingleOrDefault(nt => nt.Name.String == formatStr) ?? nameTable.AddDomainNameTableEntry(formatStr); pfFormat.SetPropertyValue(formatTableEntry); } MipMaps.Clear(); while (true) { MemoryStream stream = new MemoryStream(); image.Save(stream, imageFormat, MipHandling.KeepTopOnly); await stream.FlushAsync(); MipMaps.Add(new DomainMipMap { ImageData = (await ByteArrayReader.CreateNew(stream.ToArray(), 0x80).Splice()).GetBytes(), // Strip off 128 bytes for the DDS header Width = image.Width, Height = image.Height }); if (width == 1 && height == 1) { break; } if (width > 1) { width /= 2; } if (height > 1) { height /= 2; } if (image.Width > 4 && image.Height > 4) { image.Resize(0.5, false); } } }
public override async Task SetObject(string filename, List <DomainNameTableEntry> nameTable, object configuration) { DdsSaveConfig config = configuration as DdsSaveConfig ?? new DdsSaveConfig(FileFormat.Unknown, 0, 0, false, false); DdsFile image = await Task.Run(() => new DdsFile(filename)); bool skipFirstMip = false; int width = image.Width; int height = image.Height; if (MipMaps[0].ImageData == null || MipMaps[0].ImageData.Length == 0) { width *= 2; height *= 2; skipFirstMip = true; } DomainPropertyIntValue sizeX = PropertyHeader.GetProperty("SizeX").FirstOrDefault()?.Value as DomainPropertyIntValue; DomainPropertyIntValue sizeY = PropertyHeader.GetProperty("SizeY").FirstOrDefault()?.Value as DomainPropertyIntValue; sizeX?.SetPropertyValue(skipFirstMip ? width * 2 : width); sizeY?.SetPropertyValue(skipFirstMip ? height * 2 : height); DomainPropertyIntValue mipTailBaseIdx = PropertyHeader.GetProperty("MipTailBaseIdx").FirstOrDefault()?.Value as DomainPropertyIntValue; int indexSize = width > height ? width : height; mipTailBaseIdx?.SetPropertyValue((int)Math.Log(skipFirstMip ? indexSize * 2 : indexSize, 2)); DomainPropertyStringValue filePath = PropertyHeader.GetProperty("SourceFilePath").FirstOrDefault()?.Value as DomainPropertyStringValue; DomainPropertyStringValue fileTime = PropertyHeader.GetProperty("SourceFileTimestamp").FirstOrDefault()?.Value as DomainPropertyStringValue; filePath?.SetPropertyValue(filename); fileTime?.SetPropertyValue(File.GetLastWriteTime(filename).ToString("yyyy-MM-dd hh:mm:ss")); DomainPropertyByteValue pfFormat = PropertyHeader.GetProperty("Format").FirstOrDefault()?.Value as DomainPropertyByteValue; FileFormat imageFormat = FileFormat.Unknown; if (pfFormat != null) { imageFormat = DdsPixelFormat.ParseFileFormat(pfFormat.PropertyString); } if (imageFormat == FileFormat.Unknown) { throw new Exception($"Unknown DDS File Format ({pfFormat?.PropertyString ?? "Unknown"})."); } if (config.FileFormat == FileFormat.Unknown) { config.FileFormat = imageFormat; } else { string formatStr = DdsPixelFormat.BuildFileFormat(config.FileFormat); DomainNameTableEntry formatTableEntry = nameTable.SingleOrDefault(nt => nt.Name.String == formatStr) ?? nameTable.AddDomainNameTableEntry(formatStr); pfFormat?.SetPropertyValue(formatTableEntry); } MipMaps.Clear(); if (skipFirstMip) { MipMaps.Add(new DomainMipMap { ImageData = null, Width = width, Height = height }); } image.GenerateMipMaps(4, 4); foreach (DdsMipMap mipMap in image.MipMaps.OrderByDescending(mip => mip.Width)) { MipMaps.Add(new DomainMipMap { ImageData = image.WriteMipMap(mipMap, config), Width = mipMap.Width, Height = mipMap.Height }); } }