diff --git a/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs b/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs index 45fbcb3..83c8834 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs @@ -29,11 +29,10 @@ namespace FileConverter.ConversionJobs return new ConversionJob_ImageMagick(conversionPreset); } - //if (conversionPreset.OutputType == OutputType.Pdf || - // Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document) - //{ - // return new ConversionJob_Pdf(conversionPreset); - //} + if (conversionPreset.OutputType == OutputType.Pdf) + { + return new ConversionJob_ImageMagick(conversionPreset); + } return new ConversionJob_FFMPEG(conversionPreset); } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs b/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs index 9a6155c..4de3cde 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs @@ -119,7 +119,7 @@ namespace FileConverter.ConversionJobs { image.Progress += this.Image_Progress; - if (!ignoreScale) + if (!ignoreScale && this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageScale)) { float scaleFactor = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageScale); if (Math.Abs(scaleFactor - 1f) >= 0.005f) @@ -130,38 +130,47 @@ namespace FileConverter.ConversionJobs } } - float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageRotation); - if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f) + if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageRotation)) { - Debug.Log("Apply rotation: {0}°.", rotateAngleInDegrees); - - image.Rotate(rotateAngleInDegrees); - } - - bool clampSizeToPowerOf2 = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2); - if (clampSizeToPowerOf2) - { - int referenceSize = System.Math.Min(image.Width, image.Height); - int size = 2; - while (size*2 <= referenceSize) + float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageRotation); + if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f) { - size *= 2; + Debug.Log("Apply rotation: {0}°.", rotateAngleInDegrees); + + image.Rotate(rotateAngleInDegrees); } - - Debug.Log("Clamp size to the nearest power of 2 size (from {0}x{1} to {2}x{2}).", image.Width, image.Height, size); - - image.Scale(size, size); } - int maximumSize = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageMaximumSize); - if (maximumSize > 0) + if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2)) { - int width = System.Math.Min(image.Width, maximumSize); - int height = System.Math.Min(image.Height, maximumSize); + bool clampSizeToPowerOf2 = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2); + if (clampSizeToPowerOf2) + { + int referenceSize = System.Math.Min(image.Width, image.Height); + int size = 2; + while (size*2 <= referenceSize) + { + size *= 2; + } - Debug.Log("Clamp size to maximum size of {2}x{2} (from {0}x{1} to {2}x{3}).", image.Width, image.Height, width, height); + Debug.Log("Clamp size to the nearest power of 2 size (from {0}x{1} to {2}x{2}).", image.Width, image.Height, size); - image.Scale(width, height); + image.Scale(size, size); + } + } + + if (this.ConversionPreset.IsRelevantSetting(ConversionPreset.ConversionSettingKeys.ImageMaximumSize)) + { + int maximumSize = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageMaximumSize); + if (maximumSize > 0) + { + int width = System.Math.Min(image.Width, maximumSize); + int height = System.Math.Min(image.Height, maximumSize); + + Debug.Log("Clamp size to maximum size of {2}x{2} (from {0}x{1} to {2}x{3}).", image.Width, image.Height, width, height); + + image.Scale(width, height); + } } Debug.Log("Convert image (output: {0}).", this.OutputFilePath); @@ -175,6 +184,9 @@ namespace FileConverter.ConversionJobs image.Quality = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageQuality); break; + case OutputType.Pdf: + break; + default: this.ConversionFailed($"Unsupported output format {this.ConversionPreset.OutputType}."); image.Progress -= this.Image_Progress; diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Pdf.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Pdf.cs deleted file mode 100644 index 8360e9a..0000000 --- a/Application/FileConverter/ConversionJobs/ConversionJob_Pdf.cs +++ /dev/null @@ -1,93 +0,0 @@ -// License: http://www.gnu.org/licenses/gpl.html GPL version 3. - -namespace FileConverter.ConversionJobs -{ - using System; - - using FileConverter.Diagnostics; - using ImageMagick; - - public class ConversionJob_Pdf : ConversionJob - { - private int pageCount = 0; - - public ConversionJob_Pdf() : base() - { - } - - public ConversionJob_Pdf(ConversionPreset conversionPreset) : base(conversionPreset) - { - } - - protected override void Initialize() - { - base.Initialize(); - - string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); - MagickNET.SetGhostscriptDirectory(applicationDirectory); - - if (this.ConversionPreset == null) - { - throw new Exception("The conversion preset must be valid."); - } - } - - protected override int GetOuputFilesCount() - { - using (MagickImageCollection images = new MagickImageCollection()) - { - MagickReadSettings settings = new MagickReadSettings(); - settings.Density = new Density(1, 1); - images.Read(this.InputFilePath); - - return images.Count; - } - } - - protected override void Convert() - { - if (this.ConversionPreset == null) - { - throw new Exception("The conversion preset must be valid."); - } - - this.UserState = Properties.Resources.ConversionStateReadPdf; - - MagickReadSettings settings = new MagickReadSettings(); - // Settings the density to 300 dpi will create an image with a better quality - settings.Density = new Density(300, 300); - - using (MagickImageCollection images = new MagickImageCollection()) - { - // Add all the pages of the pdf file to the collection - images.Read(this.InputFilePath, settings); - Debug.Log("Load pdf {0} succeed.", this.InputFilePath); - - this.pageCount = images.Count; - - this.CurrentOuputFilePathIndex = 0; - - foreach (MagickImage image in images) - { - this.UserState = Properties.Resources.ConversionStateConversion; - - Debug.Log("Write page {0}/{1}.", this.CurrentOuputFilePathIndex + 1, this.pageCount); - - image.Progress += this.Image_Progress; - - // Write page to file that contains the page number - image.Write(this.OutputFilePath); - this.CurrentOuputFilePathIndex++; - - image.Progress -= this.Image_Progress; - } - } - } - - private void Image_Progress(object sender, ProgressEventArgs eventArgs) - { - float alreadyCompletedPages = this.CurrentOuputFilePathIndex / (float)this.pageCount; - this.Progress = alreadyCompletedPages + (float)eventArgs.Progress.ToDouble() / (100f * this.pageCount); - } - } -} diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index 92a9c6c..06e243c 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -376,6 +376,11 @@ namespace FileConverter return (T)System.Convert.ChangeType(settingsValue, type); } + public bool IsRelevantSetting(string settingsKey) + { + return this.Settings.ContainsKey(settingsKey); + } + [NotifyPropertyChangedInvocator] protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) { @@ -401,11 +406,6 @@ namespace FileConverter } } - private bool IsRelevantSetting(string settingsKey) - { - return this.Settings.ContainsKey(settingsKey); - } - private void InitializeDefaultSettings(OutputType outputType) { this.settings.Clear(); @@ -496,11 +496,14 @@ namespace FileConverter this.InitializeSettingsValue(ConversionPreset.ConversionSettingKeys.VideoRotation, "0"); break; + case OutputType.Pdf: + break; + default: throw new System.Exception("Missing default settings for type " + outputType); } - this.OnPropertyChanged("Settings"); + this.OnPropertyChanged(nameof(this.Settings)); } private void InitializeSettingsValue(string settingsKey, string value, bool force = false) diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 9f227db..7e9a8de 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -101,7 +101,6 @@ - diff --git a/Application/FileConverter/Helpers.cs b/Application/FileConverter/Helpers.cs index 2704317..3a412b4 100644 --- a/Application/FileConverter/Helpers.cs +++ b/Application/FileConverter/Helpers.cs @@ -111,7 +111,7 @@ namespace FileConverter return category == InputCategoryNames.Image || category == InputCategoryNames.Video || category == InputCategoryNames.AnimatedImage; case OutputType.Pdf: - return category == InputCategoryNames.Image || category == InputCategoryNames.Document; + return category == InputCategoryNames.Image; default: return false; diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml.cs b/Application/FileConverter/Windows/SettingsWindow.xaml.cs index d355ede..80e1587 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml.cs +++ b/Application/FileConverter/Windows/SettingsWindow.xaml.cs @@ -59,6 +59,7 @@ namespace FileConverter OutputType.Jpg, OutputType.Png, OutputType.Gif, + OutputType.Pdf, }; this.OutputFormats.ItemsSource = outputTypes; @@ -452,7 +453,7 @@ namespace FileConverter } this.inputCategories = categories.ToArray(); - this.OnPropertyChanged("InputCategories"); + this.OnPropertyChanged(nameof(this.InputCategories)); } } }