From a9633988b440f7a52d747d8ccf5450f7ef03e79c Mon Sep 17 00:00:00 2001 From: poddav Date: Fri, 29 Apr 2022 13:16:54 +0400 Subject: [PATCH] (GUI): copy images when no conversion required. --- GUI/GarConvert.cs | 36 +++++++++++++++++++++++++++++------- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/GUI/GarConvert.cs b/GUI/GarConvert.cs index 1531710a..3a8be873 100644 --- a/GUI/GarConvert.cs +++ b/GUI/GarConvert.cs @@ -228,24 +228,39 @@ namespace GARbro.GUI var src_format = ImageFormat.FindFormat (file); if (null == src_format) return; - if (src_format.Item1 == m_image_format && m_image_format.Extensions.Any (ext => ext == source_ext)) - return; - file.Position = 0; - var image = src_format.Item1.Read (file, src_format.Item2); - var output = CreateNewFile (target_name); + Stream output = null; try { - m_image_format.Write (output, image); + if (src_format.Item1 == m_image_format && m_image_format.Extensions.Any (ext => ext == source_ext)) + { + if (AreSamePaths (filename, target_name)) + return; + output = CreateNewFile (target_name); + file.Position = 0; + file.AsStream.CopyTo (output); + } + else + { + file.Position = 0; + var image = src_format.Item1.Read (file, src_format.Item2); + output = CreateNewFile (target_name); + m_image_format.Write (output, image); + } } catch // delete destination file on conversion failure { // FIXME if user chooses to overwrite file, and conversion results in error, // then original file will be lost. output.Dispose(); + output = null; File.Delete (target_name); throw; } - output.Dispose(); + finally + { + if (output != null) + output.Dispose(); + } } } @@ -264,5 +279,12 @@ namespace GARbro.GUI m_main.ListViewFocus(); m_main.RefreshView(); } + + static internal bool AreSamePaths (string filename1, string filename2) + { + filename1 = Path.GetFullPath (filename1); + filename2 = Path.GetFullPath (filename2); + return string.Equals (filename1, filename2, StringComparison.OrdinalIgnoreCase); + } } }