From 6594d95120067f6aa310728cc3fc2232a2ea2820 Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 2 Feb 2016 08:51:23 +0100 Subject: [PATCH] Add possibility to convert all images to ico (no restriction on size anymore) --- .../ConversionJobs/ConversionJob_Ico.cs | 76 +++++++++++++++++++ .../ConversionPreset/ConversionPreset.cs | 4 + 2 files changed, 80 insertions(+) create mode 100644 Application/FileConverter/ConversionJobs/ConversionJob_Ico.cs diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_Ico.cs b/Application/FileConverter/ConversionJobs/ConversionJob_Ico.cs new file mode 100644 index 0000000..e8d0ff5 --- /dev/null +++ b/Application/FileConverter/ConversionJobs/ConversionJob_Ico.cs @@ -0,0 +1,76 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter.ConversionJobs +{ + using System; + using System.IO; + + public class ConversionJob_Ico : ConversionJob + { + private string intermediateFilePath; + private ConversionJob pngConversionJob; + private ConversionJob icoConversionJob; + + public ConversionJob_Ico(ConversionPreset conversionPreset) : base(conversionPreset) + { + } + + protected override void Initialize() + { + base.Initialize(); + + if (this.ConversionPreset == null) + { + throw new Exception("The conversion preset must be valid."); + } + + // Generate intermediate file path. + string fileName = Path.GetFileName(this.OutputFilePath); + string tempPath = Path.GetTempPath(); + this.intermediateFilePath = PathHelpers.GenerateUniquePath(tempPath + fileName + ".png"); + + // Convert input in png file to send it to ffmpeg for the ico conversion. + ConversionPreset intermediatePreset = new ConversionPreset("To compatible image", OutputType.Png, this.ConversionPreset.InputTypes.ToArray()); + intermediatePreset.SetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2, "True"); + intermediatePreset.SetSettingsValue(ConversionPreset.ConversionSettingKeys.ImageMaximumSize, "256"); + this.pngConversionJob = ConversionJobFactory.Create(intermediatePreset, this.InputFilePath); + this.pngConversionJob.PrepareConversion(this.InputFilePath, this.intermediateFilePath); + + // Convert png file into ico. + this.icoConversionJob = new ConversionJob_FFMPEG(this.ConversionPreset); + this.icoConversionJob.PrepareConversion(this.intermediateFilePath, this.OutputFilePath); + } + + protected override void Convert() + { + if (this.ConversionPreset == null) + { + throw new Exception("The conversion preset must be valid."); + } + + Diagnostics.Debug.Log(string.Empty); + Diagnostics.Debug.Log("Convert image to PNG (intermediate format)."); + this.pngConversionJob.StartConvertion(); + + if (this.pngConversionJob.State != ConversionState.Done) + { + this.ConversionFailed(this.pngConversionJob.ErrorMessage); + return; + } + + Diagnostics.Debug.Log(string.Empty); + Diagnostics.Debug.Log("Convert png intermediate image to ICO."); + this.icoConversionJob.StartConvertion(); + + if (this.icoConversionJob.State != ConversionState.Done) + { + this.ConversionFailed(this.icoConversionJob.ErrorMessage); + return; + } + + Diagnostics.Debug.Log("Delete intermediate file {0}.", this.intermediateFilePath); + + File.Delete(this.intermediateFilePath); + } + } +} diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index c30ab94..a5aa236 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -463,6 +463,8 @@ namespace FileConverter switch (settingsKey) { case ConversionPreset.ConversionSettingKeys.ImageScale: + case ConversionPreset.ConversionSettingKeys.ImageClampSizePowerOf2: + case ConversionPreset.ConversionSettingKeys.ImageMaximumSize: return true; } @@ -686,6 +688,8 @@ namespace FileConverter public const string AudioBitrate = "AudioBitrate"; public const string ImageQuality = "ImageQuality"; public const string ImageScale = "ImageScale"; + public const string ImageClampSizePowerOf2 = "ImageClampSizePowerOf2"; + public const string ImageMaximumSize = "ImageMaximumSize"; public const string VideoQuality = "VideoQuality"; public const string VideoEncodingSpeed = "VideoEncodingSpeed"; public const string VideoScale = "VideoScale";