static void RunTest(IEnumerable <string> files, bool verbose, int threading) { SimisProvider provider; try { provider = new SimisProvider(Path.GetDirectoryName(Application.ExecutablePath) + @"\Resources"); } catch (FileException ex) { Console.WriteLine(ex.ToString()); return; } var totalCount = new TestFormatCount(); var supportedCount = new TestFormatCount(); var formatCounts = new Dictionary <string, TestFormatCount>(); var timeStart = DateTime.Now; Func <SimisJinxFormat, TestFormatCount> GetFormatFor = (simisFormat) => { var formatName = simisFormat.Name; if (!formatCounts.ContainsKey(formatName)) { formatCounts[formatName] = new TestFormatCount() { FormatName = formatName, SortKey = formatName }; } return(formatCounts[formatName]); }; Func <string, ProcessFileResults> ProcessFile = (file) => { if (verbose && (threading > 1)) { lock (formatCounts) { Console.WriteLine(String.Format("[Thread {0}] {1}", Thread.CurrentThread.ManagedThreadId, file)); } } var result = new ProcessFileResults(); var formatCount = new TestFormatCount(); var fileProvider = provider.GetForPath(file); SimisFile newFile = null; Stream readStream = new UnclosableStream(new BufferedInMemoryStream(File.OpenRead(file))); Stream saveStream = new UnclosableStream(new MemoryStream()); { result.Total = true; try { using (var reader = SimisReader.FromStream(readStream, fileProvider)) { var readerJinx = reader as SimisJinxReader; var readerAce = reader as SimisAceReader; if (readerJinx != null) { readerJinx.ReadToken(); if (readerJinx.JinxStreamFormat == null) { return(result); } result.JinxStreamFormat = readerJinx.JinxStreamFormat; } else if (readerAce != null) { if (fileProvider.Formats.FirstOrDefault() == null) { return(result); } result.JinxStreamFormat = fileProvider.Formats.First(); } else { return(result); } } } catch (ReaderException) { return(result); } readStream.Position = 0; } // First, read the file in. try { try { newFile = new SimisFile(readStream, fileProvider); } catch (Exception e) { throw new FileException(file, e); } result.ReadSuccess = true; } catch (FileException ex) { if (verbose) { lock (formatCounts) { Console.WriteLine("Read: " + ex + "\n"); } } return(result); } // Second, write the file out into memory. try { try { newFile.Write(saveStream); } catch (Exception e) { throw new FileException(file, e); } // WriteSuccess is delayed until after the comparison. We won't claim write support without comparison support. } catch (FileException ex) { if (verbose) { lock (formatCounts) { Console.WriteLine("Write: " + ex + "\n"); } } return(result); } // Third, verify that the output is the same as the input. readStream.Seek(0, SeekOrigin.Begin); saveStream.Seek(0, SeekOrigin.Begin); var readReader = new BinaryReader(new SimisTestableStream(readStream), newFile.StreamIsBinary ? ByteEncoding.Encoding : Encoding.Unicode); var saveReader = new BinaryReader(new SimisTestableStream(saveStream), newFile.StreamIsBinary ? ByteEncoding.Encoding : Encoding.Unicode); var isDXTACE = (result.JinxStreamFormat.Extension == "ace") && ((newFile.Ace.Format & 0x10) != 0); var readChars = readReader.ReadChars((int)readReader.BaseStream.Length); var saveChars = saveReader.ReadChars((int)saveReader.BaseStream.Length); var charBytes = newFile.StreamIsBinary ? 1 : 2; var charMin = Math.Min(readChars.Length, saveChars.Length); for (var i = 0; i < charMin; i++) { if (isDXTACE && (i > 168)) { break; } if (readChars[i] != saveChars[i]) { readReader.BaseStream.Position = charBytes * (i + 1); saveReader.BaseStream.Position = charBytes * (i + 1); var readEx = new ReaderException(readReader, newFile.StreamIsBinary, charBytes, ""); var saveEx = new ReaderException(saveReader, newFile.StreamIsBinary, charBytes, ""); if (verbose) { lock (formatCounts) { Console.WriteLine("Compare: " + String.Format(CultureInfo.CurrentCulture, "{0}\n\nFile character {1:N0} does not match: {2:X4} vs {3:X4}.\n\n{4}{5}\n", file, charBytes * i, readChars[i], saveChars[i], readEx.ToString(), saveEx.ToString())); } } return(result); } } if ((result.JinxStreamFormat.Extension == "ace") && ((newFile.Ace.Format & 0x10) != 0)) { // DXT images are a massive pain because it is a lossy compression. saveStream.Seek(0, SeekOrigin.Begin); var saveOutput = new SimisFile(saveStream, fileProvider); Debug.Assert(saveOutput.Ace != null); Debug.Assert(saveOutput.Ace.Format == newFile.Ace.Format); try { if (newFile.Ace.Width != saveOutput.Ace.Width) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE width expected {0}; got {1}.", newFile.Ace.Width, saveOutput.Ace.Width)); } if (newFile.Ace.Height != saveOutput.Ace.Height) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE height expected {0}; got {1}.", newFile.Ace.Height, saveOutput.Ace.Height)); } if (newFile.Ace.Unknown7 != saveOutput.Ace.Unknown7) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE unknown7 expected {0}; got {1}.", newFile.Ace.Unknown7, saveOutput.Ace.Unknown7)); } if (newFile.Ace.Creator != saveOutput.Ace.Creator) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE creator expected {0}; got {1}.", newFile.Ace.Creator, saveOutput.Ace.Creator)); } var newFileChannels = String.Join(",", newFile.Ace.Channel.Select(c => c.Type.ToString() + ":" + c.Size).ToArray()); var saveFileChannels = String.Join(",", saveOutput.Ace.Channel.Select(c => c.Type.ToString() + ":" + c.Size).ToArray()); if (newFileChannels != saveFileChannels) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE channels expected {0}; got {1}.", newFileChannels, saveFileChannels)); } if (newFile.Ace.Image.Count != saveOutput.Ace.Image.Count) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE image count expected {0}; got {1}.", newFile.Ace.Image.Count, saveOutput.Ace.Image.Count)); } var errors = new List <double>(); for (var i = 0; i < newFile.Ace.Image.Count; i++) { if (newFile.Ace.Image[i].Width != saveOutput.Ace.Image[i].Width) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE image {2} width expected {0}; got {1}.", newFile.Ace.Image[i].Width, saveOutput.Ace.Image[i].Width, i)); } if (newFile.Ace.Image[i].Height != saveOutput.Ace.Image[i].Height) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "ACE image {2} height expected {0}; got {1}.", newFile.Ace.Image[i].Height, saveOutput.Ace.Image[i].Height, i)); } errors.Add(ImageComparison.GetRootMeanSquareError(newFile.Ace.Image[i].ImageColor, saveOutput.Ace.Image[i].ImageColor, newFile.Ace.Width, newFile.Ace.Height)); errors.Add(ImageComparison.GetRootMeanSquareError(newFile.Ace.Image[i].ImageMask, saveOutput.Ace.Image[i].ImageMask, newFile.Ace.Width, newFile.Ace.Height)); } // Any error over 10.0 is considered a fail. var maxError = 10.0; if (errors.Max() > maxError) { throw new InvalidDataException(String.Format(CultureInfo.CurrentCulture, "Image RMS (root mean square) errors are too high; highest: {2,5:F1} > {0,5:F1}; all: {1}.", maxError, String.Join(", ", errors.Select(e => e.ToString("F1").PadLeft(5)).ToArray()), errors.Max())); } } catch (InvalidDataException ex) { if (verbose) { lock (formatCounts) { Console.WriteLine("Compare: " + String.Format(CultureInfo.CurrentCulture, "{0}\n\n{1}\n", file, ex.Message)); } } return(result); } } else { if (readChars.Length != saveChars.Length) { readReader.BaseStream.Position = charBytes * charMin; saveReader.BaseStream.Position = charBytes * charMin; var readEx = new ReaderException(readReader, newFile.StreamIsBinary, 0, ""); var saveEx = new ReaderException(saveReader, newFile.StreamIsBinary, 0, ""); if (verbose) { lock (formatCounts) { Console.WriteLine("Compare: " + String.Format(CultureInfo.CurrentCulture, "{0}\n\nFile and stream length do not match: {1:N0} vs {2:N0}.\n\n{3}{4}\n", file, readReader.BaseStream.Length, saveReader.BaseStream.Length, readEx.ToString(), saveEx.ToString())); } } return(result); } } // It all worked! result.WriteSuccess = true; return(result); }; if (threading > 1) { var filesEnumerator = files.GetEnumerator(); var filesFinished = false; var threads = new List <Thread>(threading); for (var i = 0; i < threading; i++) { threads.Add(new Thread(() => { var file = ""; var results = new List <ProcessFileResults>(); while (true) { lock (filesEnumerator) { if (filesFinished || !filesEnumerator.MoveNext()) { filesFinished = true; break; } file = filesEnumerator.Current; } results.Add(ProcessFile(file)); } lock (totalCount) { foreach (var result in results) { if (result.Total) { totalCount.Total++; } if (result.ReadSuccess) { totalCount.ReadSuccess++; } if (result.WriteSuccess) { totalCount.WriteSuccess++; } if (result.JinxStreamFormat != null) { var formatCount = GetFormatFor(result.JinxStreamFormat); if (result.Total) { supportedCount.Total++; } if (result.ReadSuccess) { supportedCount.ReadSuccess++; } if (result.WriteSuccess) { supportedCount.WriteSuccess++; } if (result.Total) { formatCount.Total++; } if (result.ReadSuccess) { formatCount.ReadSuccess++; } if (result.WriteSuccess) { formatCount.WriteSuccess++; } } } } })); } foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } } else { foreach (var file in files) { var result = ProcessFile(file); if (result.Total) { totalCount.Total++; } if (result.ReadSuccess) { totalCount.ReadSuccess++; } if (result.WriteSuccess) { totalCount.WriteSuccess++; } if (result.JinxStreamFormat != null) { var formatCount = GetFormatFor(result.JinxStreamFormat); if (result.Total) { supportedCount.Total++; } if (result.ReadSuccess) { supportedCount.ReadSuccess++; } if (result.WriteSuccess) { supportedCount.WriteSuccess++; } if (result.Total) { formatCount.Total++; } if (result.ReadSuccess) { formatCount.ReadSuccess++; } if (result.WriteSuccess) { formatCount.WriteSuccess++; } } } } supportedCount.FormatName = "(Total supported files of " + totalCount.Total + ")"; supportedCount.SortKey = "ZZZ"; formatCounts[""] = supportedCount; var outFormat = "{0,-40:S} {1,1:S}{2,-7:D} {3,1:S}{4,-7:D} {5,1:S}{6,-7:D}"; Console.WriteLine(String.Format(CultureInfo.CurrentCulture, outFormat, "Format Name", "", "Total", "", "Read", "", "Write")); Console.WriteLine(String.Empty.PadLeft(69, '=')); foreach (var formatCount in formatCounts.OrderBy(kvp => kvp.Value.SortKey).Select(kvp => kvp.Value)) { Console.WriteLine(String.Format(CultureInfo.CurrentCulture, outFormat, formatCount.FormatName, "", formatCount.Total, formatCount.Total == formatCount.ReadSuccess ? "*" : "", formatCount.ReadSuccess, formatCount.Total == formatCount.WriteSuccess ? "*" : formatCount.ReadSuccess == formatCount.WriteSuccess ? "+" : "", formatCount.WriteSuccess)); } }
static void DoConversion(IEnumerable <ConversionFile> files, bool verbose, int threading, bool convertRoundtrip, bool convertTexture, bool convertDXT1, bool convertZLIB) { if (!files.Any()) { throw new OperationCanceledException("Must specify files for conversion."); } SimisProvider provider; try { provider = new SimisProvider(Path.GetDirectoryName(Application.ExecutablePath) + @"\Resources"); } catch (FileException ex) { Console.WriteLine(ex.ToString()); return; } Action <ConversionFile> ConvertFile = (file) => { if (verbose) { lock (files) { if (threading > 1) { Console.WriteLine("[Thread {0}] {1} -> {2}", Thread.CurrentThread.ManagedThreadId, file.Input, file.Output); } else { Console.WriteLine("{0} -> {1}", file.Input, file.Output); } } } try { var inputExt = Path.GetExtension(file.Input).ToUpperInvariant(); var outputExt = Path.GetExtension(file.Output).ToUpperInvariant(); if ((inputExt == ".ACE") && (outputExt == ".ACE")) { // ACE -> ACE var inputAce = new SimisFile(file.Input, provider); var outputAce = new SimisFile(file.Output, true, convertZLIB, inputAce.Ace); outputAce.Write(); } else if (inputExt == ".ACE") { // ACE -> *** var inputAce = new SimisFile(file.Input, provider); var width = convertRoundtrip ? inputAce.Ace.Image.Max(i => i.Width) : inputAce.Ace.Image[0].Width; var height = convertRoundtrip ? inputAce.Ace.Image.Sum(i => i.Height) : inputAce.Ace.Image[0].Height; var outputImage = new Bitmap(width * (convertRoundtrip ? 2 : 1), height, PixelFormat.Format32bppArgb); using (var g = Graphics.FromImage(outputImage)) { g.FillRectangle(Brushes.Transparent, 0, 0, outputImage.Width, outputImage.Height); if (convertRoundtrip) { var y = 0; foreach (var image in inputAce.Ace.Image) { g.DrawImageUnscaled(image.ImageColor, 0, y); g.DrawImageUnscaled(image.ImageMask, width, y); y += image.Height; } } else { g.DrawImageUnscaled(inputAce.Ace.Image[0].GetImage(inputAce.Ace.HasAlpha ? SimisAceImageType.ColorAndAlpha : inputAce.Ace.HasMask ? SimisAceImageType.ColorAndMask : SimisAceImageType.ColorOnly), 0, 0); } } outputImage.Save(file.Output); } else if (outputExt == ".ACE") { // *** -> ACE var inputImage = Image.FromFile(file.Input); var width = inputImage.Width; var height = inputImage.Height; // Roundtripping or not, textures have special requirements of 2^n width and height. if (convertTexture) { if (convertRoundtrip) { var expectedHeight = (int)Math.Pow(2, (int)(Math.Log(height + 1) / Math.Log(2))) - 1; if (height != expectedHeight) { throw new InvalidOperationException(String.Format("Image height {0} is not correct for round-tripping a texture. It must be 2^n-1.", height, expectedHeight)); } height = (height + 1) / 2; // Roundtripping always has two columns: color and mask. width /= 2; } else { var expectedHeight = (int)Math.Pow(2, (int)(Math.Log(height) / Math.Log(2))); if (height != expectedHeight) { throw new InvalidOperationException(String.Format("Image height {0} is not correct for a texture. It must be 2^n.", height, expectedHeight)); } } var expectedWidth = (int)Math.Pow(2, (int)(Math.Log(width) / Math.Log(2))); if (width != expectedWidth) { throw new InvalidOperationException(String.Format("Image width {0} is not correct for a texture. It must be 2^n.", width, expectedWidth)); } if (width != height) { throw new InvalidOperationException(String.Format("Image width {0} and height {1} must be equal for a texture.", width, height)); } } if (convertRoundtrip || convertTexture) { var imageCount = 1 + (int)(convertTexture ? Math.Log(height) / Math.Log(2) : 0); var aceChannels = new[] { new SimisAceChannel(8, SimisAceChannelId.Red), new SimisAceChannel(8, SimisAceChannelId.Green), new SimisAceChannel(8, SimisAceChannelId.Blue), new SimisAceChannel(8, SimisAceChannelId.Alpha), new SimisAceChannel(1, SimisAceChannelId.Mask), }; // Remove the alpha channel for DXT1. if (convertDXT1) { aceChannels = new[] { aceChannels[0], aceChannels[1], aceChannels[2], aceChannels[4] }; } var aceImages = new SimisAceImage[imageCount]; var y = 0; for (var i = 0; i < imageCount; i++) { var scale = (int)Math.Pow(2, i); var colorImage = new Bitmap(width / scale, height / scale, PixelFormat.Format32bppArgb); var maskImage = new Bitmap(width / scale, height / scale, PixelFormat.Format32bppRgb); var sourceRect = convertRoundtrip ? new Rectangle(0, y, width / scale, height / scale) : new Rectangle(0, 0, width, height); using (var g = Graphics.FromImage(colorImage)) { g.DrawImage(inputImage, new Rectangle(Point.Empty, colorImage.Size), sourceRect, GraphicsUnit.Pixel); } sourceRect.X = width; using (var g = Graphics.FromImage(maskImage)) { if (width < inputImage.Width) { g.DrawImage(inputImage, new Rectangle(Point.Empty, maskImage.Size), sourceRect, GraphicsUnit.Pixel); } else { g.FillRectangle(Brushes.White, new Rectangle(Point.Empty, maskImage.Size)); } } aceImages[i] = new SimisAceImage(colorImage, maskImage); y += colorImage.Height; } var ace = new SimisAce((convertDXT1 ? 0x10 : 0x00) + (convertTexture ? 0x05 : 0x00), width, height, convertDXT1 ? 0x12 : 0x00, 0, "Unknown", "JGR Image File", new byte[44], aceChannels, aceImages, new byte[0], new byte[0]); var aceFile = new SimisFile(file.Output, true, convertZLIB, ace); aceFile.Write(); } else { // TODO: Handle the various alpha/mask fun here. var aceChannels = new[] { new SimisAceChannel(8, SimisAceChannelId.Red), new SimisAceChannel(8, SimisAceChannelId.Green), new SimisAceChannel(8, SimisAceChannelId.Blue), new SimisAceChannel(8, SimisAceChannelId.Alpha), }; // Replace the alpha channel with mask channel for DXT1. if (convertDXT1) { aceChannels[3] = new SimisAceChannel(1, SimisAceChannelId.Mask); } var maskImage = new Bitmap(width, height, PixelFormat.Format32bppRgb); using (var g = Graphics.FromImage(maskImage)) { g.FillRectangle(Brushes.White, 0, 0, maskImage.Width, maskImage.Height); } var aceImages = new[] { new SimisAceImage(new Bitmap(inputImage), maskImage), }; var ace = new SimisAce(convertDXT1 ? 0x10 : 0x00, width, height, convertDXT1 ? 0x12 : 0x00, 0, "Unknown", "JGR Image File", new byte[44], aceChannels, aceImages, new byte[0], new byte[0]); var aceFile = new SimisFile(file.Output, true, convertZLIB, ace); aceFile.Write(); } } else { // *** -> *** Image.FromFile(file.Input).Save(file.Output); } } catch (Exception ex) { Console.Error.WriteLine(ex.ToString()); } }; if (threading > 1) { var filesEnumerator = files.GetEnumerator(); var filesFinished = false; var threads = new List <Thread>(threading); for (var i = 0; i < threading; i++) { threads.Add(new Thread(() => { ConversionFile file; while (true) { lock (filesEnumerator) { if (filesFinished || !filesEnumerator.MoveNext()) { filesFinished = true; break; } file = filesEnumerator.Current; } ConvertFile(file); } })); } foreach (var thread in threads) { thread.Start(); } foreach (var thread in threads) { thread.Join(); } } else { foreach (var file in files) { ConvertFile(file); } } }