From c01e618c551f025bb0a76501a7e0f1ca5afe315e Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 5 Jul 2016 08:15:09 +0200 Subject: [PATCH] Integrate full pdf support in base ImageMagick converter --- .../ConversionJobs/ConversionJobFactory.cs | 13 +- .../ConversionJob_ImageMagick.cs | 205 ++++++++++++------ 2 files changed, 151 insertions(+), 67 deletions(-) diff --git a/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs b/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs index 0e8e660..45fbcb3 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJobFactory.cs @@ -23,16 +23,17 @@ namespace FileConverter.ConversionJobs return new ConversionJob_Gif(conversionPreset); } - if (Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Image) + if (Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Image || + Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document) { 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 || + // Helpers.GetExtensionCategory(inputFileExtension) == Helpers.InputCategoryNames.Document) + //{ + // return new ConversionJob_Pdf(conversionPreset); + //} return new ConversionJob_FFMPEG(conversionPreset); } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs b/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs index 204dd5e..9a6155c 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_ImageMagick.cs @@ -9,6 +9,9 @@ namespace FileConverter.ConversionJobs public class ConversionJob_ImageMagick : ConversionJob { + private bool isInputFilePdf; + private int pageCount; + public ConversionJob_ImageMagick() : base() { } @@ -21,12 +24,34 @@ namespace FileConverter.ConversionJobs { base.Initialize(); + string applicationDirectory = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); + MagickNET.SetGhostscriptDirectory(applicationDirectory); + + this.isInputFilePdf = System.IO.Path.GetExtension(this.InputFilePath).ToLowerInvariant() == ".pdf"; + if (this.ConversionPreset == null) { throw new Exception("The conversion preset must be valid."); } } + protected override int GetOuputFilesCount() + { + if (System.IO.Path.GetExtension(this.InputFilePath).ToLowerInvariant() == ".pdf") + { + using (MagickImageCollection images = new MagickImageCollection()) + { + MagickReadSettings settings = new MagickReadSettings(); + settings.Density = new Density(1, 1); + images.Read(this.InputFilePath); + + return images.Count; + } + } + + return 1; + } + protected override void Convert() { if (this.ConversionPreset == null) @@ -34,78 +59,136 @@ namespace FileConverter.ConversionJobs throw new Exception("The conversion preset must be valid."); } - using (MagickImage image = new MagickImage(this.InputFilePath)) - { - image.Progress += this.Image_Progress; - Debug.Log("Load image {0} succeed.", this.InputFilePath); + this.CurrentOuputFilePathIndex = 0; + if (this.isInputFilePdf) + { + this.ConvertPdf(); + } + else + { + this.pageCount = 1; + using (MagickImage image = new MagickImage(this.InputFilePath)) + { + Debug.Log("Load image {0} succeed.", this.InputFilePath); + + this.ConvertImage(image); + } + } + } + + private void ConvertPdf() + { + MagickReadSettings settings = new MagickReadSettings(); + + float dpi = 200f; + float scaleFactor = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageScale); + if (Math.Abs(scaleFactor - 1f) >= 0.005f) + { + Debug.Log("Apply scale factor: {0}%.", scaleFactor*100); + + dpi *= scaleFactor; + } + + Debug.Log("Density: {0}dpi.", dpi); + settings.Density = new Density(dpi, dpi); + + this.UserState = Properties.Resources.ConversionStateReadPdf; + + 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.UserState = Properties.Resources.ConversionStateConversion; + + foreach (MagickImage image in images) + { + Debug.Log("Write page {0}/{1}.", this.CurrentOuputFilePathIndex + 1, this.pageCount); + this.ConvertImage(image, true); + + this.CurrentOuputFilePathIndex++; + } + } + } + + private void ConvertImage(MagickImage image, bool ignoreScale = false) + { + image.Progress += this.Image_Progress; + + if (!ignoreScale) + { float scaleFactor = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageScale); if (Math.Abs(scaleFactor - 1f) >= 0.005f) { - Debug.Log("Apply scale factor: {0}%.", scaleFactor * 100); + Debug.Log("Apply scale factor: {0}%.", scaleFactor*100); - image.Scale(new Percentage(scaleFactor * 100f)); + image.Scale(new Percentage(scaleFactor*100f)); } - - float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageRotation); - if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f) - { - 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) - { - size *= 2; - } - - 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) - { - 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); - switch (this.ConversionPreset.OutputType) - { - case OutputType.Png: - image.Quality = 100; - break; - - case OutputType.Jpg: - image.Quality = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageQuality); - break; - - default: - this.ConversionFailed(string.Format("Unsupported output format {0}.", this.ConversionPreset.OutputType)); - image.Progress -= this.Image_Progress; - return; - } - - image.Write(this.OutputFilePath); - image.Progress -= this.Image_Progress; } + + float rotateAngleInDegrees = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageRotation); + if (Math.Abs(rotateAngleInDegrees - 0f) >= 0.05f) + { + 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) + { + size *= 2; + } + + 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) + { + 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); + switch (this.ConversionPreset.OutputType) + { + case OutputType.Png: + image.Quality = 100; + break; + + case OutputType.Jpg: + image.Quality = this.ConversionPreset.GetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageQuality); + break; + + default: + this.ConversionFailed($"Unsupported output format {this.ConversionPreset.OutputType}."); + image.Progress -= this.Image_Progress; + return; + } + + image.Write(this.OutputFilePath); + image.Progress -= this.Image_Progress; } private void Image_Progress(object sender, ProgressEventArgs eventArgs) { - this.Progress = (float)eventArgs.Progress.ToDouble() / 100f; + float alreadyCompletedPages = this.CurrentOuputFilePathIndex / (float)this.pageCount; + this.Progress = alreadyCompletedPages + (float)eventArgs.Progress.ToDouble() / (100f * this.pageCount); } } }