diff --git a/src/Greenshot.Base/Controls/SaveImageFileDialog.cs b/src/Greenshot.Base/Controls/SaveImageFileDialog.cs index 3c9b2780e..428c1581d 100644 --- a/src/Greenshot.Base/Controls/SaveImageFileDialog.cs +++ b/src/Greenshot.Base/Controls/SaveImageFileDialog.cs @@ -166,7 +166,7 @@ namespace Greenshot.Base.Controls // if the filename contains a valid extension, which is the same like the selected filter item's extension, the filename is okay if (fn.EndsWith(Extension, StringComparison.CurrentCultureIgnoreCase)) return fn; // otherwise we just add the selected filter item's extension - else return fn + "." + Extension; + return fn + "." + Extension; } set { diff --git a/src/Greenshot.Base/Core/ClipboardHelper.cs b/src/Greenshot.Base/Core/ClipboardHelper.cs index a87ac9071..ac830c0f2 100644 --- a/src/Greenshot.Base/Core/ClipboardHelper.cs +++ b/src/Greenshot.Base/Core/ClipboardHelper.cs @@ -65,7 +65,8 @@ namespace Greenshot.Base.Core //private static readonly string FORMAT_HTML = "HTML Format"; // Template for the HTML Text on the clipboard - // see: https://msdn.microsoft.com/en-us/library/ms649015%28v=vs.85%29.aspx + // see: https://msdn.microsoft.com/en-us/library/ms649015%28v=v + // s.85%29.aspx // or: https://msdn.microsoft.com/en-us/library/Aa767917.aspx private const string HtmlClipboardString = @"Version:0.9 StartHTML:<<<<<<<1 diff --git a/src/Greenshot.Base/Core/Enums/ExifOrientations.cs b/src/Greenshot.Base/Core/Enums/ExifOrientations.cs new file mode 100644 index 000000000..b566619da --- /dev/null +++ b/src/Greenshot.Base/Core/Enums/ExifOrientations.cs @@ -0,0 +1,36 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: https://getgreenshot.org/ + * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +namespace Greenshot.Base.Core.Enums +{ + internal enum ExifOrientations : byte + { + Unknown = 0, + TopLeft = 1, + TopRight = 2, + BottomRight = 3, + BottomLeft = 4, + LeftTop = 5, + RightTop = 6, + RightBottom = 7, + LeftBottom = 8, + } +} \ No newline at end of file diff --git a/src/Greenshot.Base/Core/FileFormatHandlers/AbstractFileFormatHandler.cs b/src/Greenshot.Base/Core/FileFormatHandlers/AbstractFileFormatHandler.cs deleted file mode 100644 index c8d842b45..000000000 --- a/src/Greenshot.Base/Core/FileFormatHandlers/AbstractFileFormatHandler.cs +++ /dev/null @@ -1,27 +0,0 @@ -using System.Collections.Generic; -using System.Drawing; -using System.IO; -using Greenshot.Base.Interfaces; -using Greenshot.Base.Interfaces.Drawing; - -namespace Greenshot.Base.Core.FileFormatHandlers -{ - public abstract class AbstractFileFormatHandler - { - /// - /// Make sure we handle the input extension always the same, by "normalizing" it - /// - /// string - /// string - protected string NormalizeExtension(string extension) - { - if (string.IsNullOrEmpty(extension)) - { - return null; - } - - extension = extension.ToLowerInvariant(); - return !extension.StartsWith(".") ? $".{extension}" : extension; - } - } -} diff --git a/src/Greenshot.Base/Core/ImageHelper.cs b/src/Greenshot.Base/Core/ImageHelper.cs index e9745dd9b..b16c0b514 100644 --- a/src/Greenshot.Base/Core/ImageHelper.cs +++ b/src/Greenshot.Base/Core/ImageHelper.cs @@ -26,28 +26,26 @@ using System.Drawing.Drawing2D; using System.Drawing.Imaging; using System.IO; using System.Linq; +using System.Windows; +using System.Windows.Media; +using System.Windows.Media.Imaging; +using Greenshot.Base.Core.Enums; using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Effects; using Greenshot.Base.IniFile; using Greenshot.Base.Interfaces; using Greenshot.Base.UnmanagedHelpers; using log4net; +using Brush = System.Drawing.Brush; +using Color = System.Drawing.Color; +using Matrix = System.Drawing.Drawing2D.Matrix; +using Pen = System.Drawing.Pen; +using PixelFormat = System.Drawing.Imaging.PixelFormat; +using Point = System.Drawing.Point; +using Size = System.Drawing.Size; namespace Greenshot.Base.Core { - internal enum ExifOrientations : byte - { - Unknown = 0, - TopLeft = 1, - TopRight = 2, - BottomRight = 3, - BottomLeft = 4, - LeftTop = 5, - RightTop = 6, - RightBottom = 7, - LeftBottom = 8, - } - /// /// Description of ImageHelper. /// @@ -1570,7 +1568,7 @@ namespace Greenshot.Base.Core nPercentW = nPercentH; if (canvasUseNewSize) { - destX = Math.Max(0, Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2)); + destX = Math.Max(0, System.Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2)); } } else if ((int) nPercentH == 1) @@ -1578,7 +1576,7 @@ namespace Greenshot.Base.Core nPercentH = nPercentW; if (canvasUseNewSize) { - destY = Math.Max(0, Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2)); + destY = Math.Max(0, System.Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2)); } } else if ((int) nPercentH != 0 && nPercentH < nPercentW) @@ -1586,7 +1584,7 @@ namespace Greenshot.Base.Core nPercentW = nPercentH; if (canvasUseNewSize) { - destX = Math.Max(0, Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2)); + destX = Math.Max(0, System.Convert.ToInt32((newWidth - sourceImage.Width * nPercentW) / 2)); } } else @@ -1594,7 +1592,7 @@ namespace Greenshot.Base.Core nPercentH = nPercentW; if (canvasUseNewSize) { - destY = Math.Max(0, Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2)); + destY = Math.Max(0, System.Convert.ToInt32((newHeight - sourceImage.Height * nPercentH) / 2)); } } } @@ -1739,16 +1737,97 @@ namespace Greenshot.Base.Core { var bitmap = CreateEmptyLike(image, Color.Transparent); - using var gfx = Graphics.FromImage(bitmap); - gfx.InterpolationMode = InterpolationMode.HighQualityBicubic; + using var graphics = Graphics.FromImage(bitmap); + graphics.InterpolationMode = InterpolationMode.HighQualityBicubic; - gfx.TranslateTransform((float)bitmap.Width / 2, (float)bitmap.Height / 2); - gfx.RotateTransform(rotationAngle); - gfx.TranslateTransform(-(float)bitmap.Width / 2, -(float)bitmap.Height / 2); + graphics.TranslateTransform((float)bitmap.Width / 2, (float)bitmap.Height / 2); + graphics.RotateTransform(rotationAngle); + graphics.TranslateTransform(-(float)bitmap.Width / 2, -(float)bitmap.Height / 2); - gfx.DrawImage(image, new Point(0, 0)); + graphics.DrawImage(image, new Point(0, 0)); return bitmap; } + + /// + /// Map a System.Drawing.Imaging.PixelFormat to a System.Windows.Media.PixelFormat + /// + /// System.Drawing.Imaging.PixelFormat + /// System.Windows.Media.PixelFormat + /// + public static System.Windows.Media.PixelFormat Map(this PixelFormat pixelFormat) => + pixelFormat switch + { + PixelFormat.Format32bppArgb => PixelFormats.Bgra32, + PixelFormat.Format24bppRgb => PixelFormats.Bgr24, + PixelFormat.Format32bppRgb => PixelFormats.Bgr32, + _ => throw new NotSupportedException($"Can't map {pixelFormat}.") + }; + + /// + /// Map a System.Windows.Media.PixelFormat to a System.Drawing.Imaging.PixelFormat + /// + /// System.Windows.Media.PixelFormat + /// System.Drawing.Imaging.PixelFormat + /// + public static PixelFormat Map(this System.Windows.Media.PixelFormat pixelFormat) + { + if (pixelFormat == PixelFormats.Bgra32) + { + return PixelFormat.Format32bppArgb; + } + if (pixelFormat == PixelFormats.Bgr24) + { + return PixelFormat.Format24bppRgb; + } + if (pixelFormat == PixelFormats.Bgr32) + { + return PixelFormat.Format32bppRgb; + } + + throw new NotSupportedException($"Can't map {pixelFormat}."); + } + + /// + /// Convert a Bitmap to a BitmapSource + /// + /// Bitmap + /// BitmapSource + public static BitmapSource ToBitmapSource(this Bitmap bitmap) + { + var bitmapData = bitmap.LockBits(new Rectangle(0, 0, bitmap.Width, bitmap.Height), ImageLockMode.ReadOnly, bitmap.PixelFormat); + + BitmapSource bitmapSource; + try + { + bitmapSource = BitmapSource.Create( + bitmapData.Width, bitmapData.Height, + bitmap.HorizontalResolution, bitmap.VerticalResolution, + bitmap.PixelFormat.Map(), null, + bitmapData.Scan0, bitmapData.Stride * bitmapData.Height, bitmapData.Stride); + } + finally + { + bitmap.UnlockBits(bitmapData); + } + + return bitmapSource; + } + + /// + /// Convert a BitmapSource to a Bitmap + /// + /// BitmapSource + /// Bitmap + public static Bitmap ToBitmap(this BitmapSource bitmapSource) + { + var pixelFormat = bitmapSource.Format.Map(); + + Bitmap bitmap = new Bitmap(bitmapSource.PixelWidth, bitmapSource.PixelHeight, pixelFormat); + BitmapData data = bitmap.LockBits(new Rectangle(Point.Empty, bitmap.Size), ImageLockMode.WriteOnly, pixelFormat); + bitmapSource.CopyPixels(Int32Rect.Empty, data.Scan0, data.Height * data.Stride, data.Stride); + bitmap.UnlockBits(data); + return bitmap; + } } } \ No newline at end of file diff --git a/src/Greenshot.Editor/Drawing/MetafileContainer.cs b/src/Greenshot.Editor/Drawing/MetafileContainer.cs index 79159b262..4372ce24d 100644 --- a/src/Greenshot.Editor/Drawing/MetafileContainer.cs +++ b/src/Greenshot.Editor/Drawing/MetafileContainer.cs @@ -64,6 +64,16 @@ namespace Greenshot.Editor.Drawing return newImage; } + protected override void Dispose(bool disposing) + { + if (disposing) + { + _metafile?.Dispose(); + } + + base.Dispose(disposing); + } + public override bool HasDefaultSize => true; public override Size DefaultSize => new Size(_metafile.Width, _metafile.Height); diff --git a/src/Greenshot.Editor/EditorInitialize.cs b/src/Greenshot.Editor/EditorInitialize.cs index 47a50e7ac..0fed52da7 100644 --- a/src/Greenshot.Editor/EditorInitialize.cs +++ b/src/Greenshot.Editor/EditorInitialize.cs @@ -28,12 +28,19 @@ namespace Greenshot.Editor { public static void Initialize() { - FileFormatHandlerRegistry.FileFormatHandlers.Add(new SvgFileFormatHandler()); + // All generic things, like gif, png, jpg etc. FileFormatHandlerRegistry.FileFormatHandlers.Add(new DefaultFileFormatHandler()); - FileFormatHandlerRegistry.FileFormatHandlers.Add(new DibFileFormatHandler()); FileFormatHandlerRegistry.FileFormatHandlers.Add(new GreenshotFileFormatHandler()); + // For .svg support + FileFormatHandlerRegistry.FileFormatHandlers.Add(new SvgFileFormatHandler()); + // For clipboard support + FileFormatHandlerRegistry.FileFormatHandlers.Add(new DibFileFormatHandler()); + // .ico FileFormatHandlerRegistry.FileFormatHandlers.Add(new IconFileFormatHandler()); + // EMF & WMF FileFormatHandlerRegistry.FileFormatHandlers.Add(new MetaFileFormatHandler()); + // JPG XR + FileFormatHandlerRegistry.FileFormatHandlers.Add(new WmpFileFormatHandler()); } } } diff --git a/src/Greenshot.Editor/FileFormatHandlers/AbstractFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/AbstractFileFormatHandler.cs new file mode 100644 index 000000000..84b8496ce --- /dev/null +++ b/src/Greenshot.Editor/FileFormatHandlers/AbstractFileFormatHandler.cs @@ -0,0 +1,76 @@ +using System.Collections.Generic; +using System.Drawing; +using System.IO; +using System.Linq; +using Greenshot.Base.Interfaces; +using Greenshot.Base.Interfaces.Drawing; +using Greenshot.Editor.Drawing; + +namespace Greenshot.Editor.FileFormatHandlers +{ + public abstract class AbstractFileFormatHandler : IFileFormatHandler + { + /// + /// Make sure we handle the input extension always the same, by "normalizing" it + /// + /// string + /// string + protected string NormalizeExtension(string extension) + { + if (string.IsNullOrEmpty(extension)) + { + return null; + } + + extension = extension.ToLowerInvariant(); + return !extension.StartsWith(".") ? $".{extension}" : extension; + } + + protected abstract string[] OurExtensions { get; } + + /// + public virtual IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) + { + return OurExtensions; + } + + /// + public virtual bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) + { + return OurExtensions.Contains(NormalizeExtension(extension)); + } + + /// + public virtual int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) + { + return int.MaxValue; + } + + public abstract bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension); + public abstract bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap); + + /// + /// Default implementation taking the TryLoadFromStream image and placing it in an ImageContainer + /// + /// Stream + /// string + /// IDrawableContainer out + /// ISurface + /// bool + public virtual bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parentSurface = null) + { + if (TryLoadFromStream(stream, extension, out var bitmap)) + { + var imageContainer = new ImageContainer(parentSurface) + { + Image = bitmap + }; + drawableContainer = imageContainer; + return true; + } + + drawableContainer = null; + return true; + } + } +} diff --git a/src/Greenshot.Editor/FileFormatHandlers/DefaultFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/DefaultFileFormatHandler.cs index 419e1898e..97ab042e6 100644 --- a/src/Greenshot.Editor/FileFormatHandlers/DefaultFileFormatHandler.cs +++ b/src/Greenshot.Editor/FileFormatHandlers/DefaultFileFormatHandler.cs @@ -19,16 +19,11 @@ * along with this program. If not, see . */ -using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; -using System.Linq; using Greenshot.Base.Core; -using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Interfaces; -using Greenshot.Base.Interfaces.Drawing; -using Greenshot.Editor.Drawing; namespace Greenshot.Editor.FileFormatHandlers { @@ -37,28 +32,10 @@ namespace Greenshot.Editor.FileFormatHandlers /// public class DefaultFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler { - private static readonly string [] OurExtensions = { ".png", ".bmp", ".gif", ".jpg", ".jpeg", ".tiff", ".tif" }; + protected override string[] OurExtensions { get; } = { ".png", ".bmp", ".gif", ".jpg", ".jpeg", ".tiff", ".tif" }; /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) - { - return OurExtensions; - } - - /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return OurExtensions.Contains(NormalizeExtension(extension)); - } - - /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return int.MaxValue; - } - - /// - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) { ImageFormat imageFormat = NormalizeExtension(extension) switch { @@ -81,28 +58,11 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) { using var tmpImage = Image.FromStream(stream, true, true); bitmap = ImageHelper.Clone(tmpImage, PixelFormat.Format32bppArgb); return true; } - - /// - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface surface = null) - { - if (TryLoadFromStream(stream, extension, out var bitmap)) - { - var imageContainer = new ImageContainer(surface) - { - Image = bitmap - }; - drawableContainer = imageContainer; - return true; - } - - drawableContainer = null; - return true; - } } } diff --git a/src/Greenshot.Editor/FileFormatHandlers/DibFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/DibFileFormatHandler.cs index 495f669b9..607374ac9 100644 --- a/src/Greenshot.Editor/FileFormatHandlers/DibFileFormatHandler.cs +++ b/src/Greenshot.Editor/FileFormatHandlers/DibFileFormatHandler.cs @@ -20,18 +20,13 @@ */ using System; -using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; -using System.Linq; using System.Runtime.InteropServices; using Greenshot.Base.Core; -using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Interfaces; -using Greenshot.Base.Interfaces.Drawing; using Greenshot.Base.UnmanagedHelpers; -using Greenshot.Editor.Drawing; using log4net; namespace Greenshot.Editor.FileFormatHandlers @@ -43,29 +38,10 @@ namespace Greenshot.Editor.FileFormatHandlers { private const double DpiToPelsPerMeter = 39.3701; private static readonly ILog Log = LogManager.GetLogger(typeof(DibFileFormatHandler)); - private static readonly string [] OurExtensions = { ".dib", ".format17" }; + protected override string[] OurExtensions { get; } = { ".dib", ".format17" }; /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) - { - return OurExtensions; - } - - /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - extension = NormalizeExtension(extension); - return OurExtensions.Contains(extension); - } - - /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return int.MaxValue; - } - - /// - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) { var dibBytes = ConvertToDib(bitmap); destination.Write(dibBytes, 0, dibBytes.Length); @@ -73,7 +49,7 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) { byte[] dibBuffer = new byte[stream.Length]; _ = stream.Read(dibBuffer, 0, dibBuffer.Length); @@ -133,24 +109,6 @@ namespace Greenshot.Editor.FileFormatHandlers return true; } - /// - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface surface = null) - { - if (TryLoadFromStream(stream, extension, out var bitmap)) - { - var imageContainer = new ImageContainer(surface) - { - Image = bitmap - }; - drawableContainer = imageContainer; - return true; - } - - drawableContainer = null; - return true; - } - - /// /// Converts the Bitmap to a Device Independent Bitmap format of type BITFIELDS. /// diff --git a/src/Greenshot.Editor/FileFormatHandlers/GreenshotFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/GreenshotFileFormatHandler.cs index 0a4483884..150a98327 100644 --- a/src/Greenshot.Editor/FileFormatHandlers/GreenshotFileFormatHandler.cs +++ b/src/Greenshot.Editor/FileFormatHandlers/GreenshotFileFormatHandler.cs @@ -25,18 +25,16 @@ using System.Drawing; using System.IO; using System.Linq; using Greenshot.Base.Core; -using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Interfaces; -using Greenshot.Base.Interfaces.Drawing; namespace Greenshot.Editor.FileFormatHandlers { public class GreenshotFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler { - private static readonly string[] OurExtensions = { ".greenshot" }; + protected override string[] OurExtensions { get; } = { ".greenshot" }; /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) + public override IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) { if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream) { @@ -47,12 +45,8 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) + public override bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) { - if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream) - { - return false; - } if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) { return false; @@ -61,29 +55,17 @@ namespace Greenshot.Editor.FileFormatHandlers return OurExtensions.Contains(NormalizeExtension(extension)); } - - /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) { - return int.MaxValue; + throw new NotImplementedException(); } - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) - { - // TODO: Implement this - return false; - } - - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) { var surface = SimpleServiceProvider.Current.GetInstance>().Invoke(); bitmap = (Bitmap)surface.GetImageForExport(); return true; } - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent = null) - { - throw new NotImplementedException(); - } } } diff --git a/src/Greenshot.Editor/FileFormatHandlers/IconFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/IconFileFormatHandler.cs index aa254e760..4a29545a4 100644 --- a/src/Greenshot.Editor/FileFormatHandlers/IconFileFormatHandler.cs +++ b/src/Greenshot.Editor/FileFormatHandlers/IconFileFormatHandler.cs @@ -20,16 +20,11 @@ */ using System; -using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; -using System.Linq; using Greenshot.Base.Core; -using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Interfaces; -using Greenshot.Base.Interfaces.Drawing; -using Greenshot.Editor.Drawing; using log4net; namespace Greenshot.Editor.FileFormatHandlers @@ -39,45 +34,18 @@ namespace Greenshot.Editor.FileFormatHandlers /// public class IconFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler { - private static readonly ILog Log = LogManager.GetLogger(typeof(ImageHelper)); + private static readonly ILog Log = LogManager.GetLogger(typeof(IconFileFormatHandler)); - private static readonly string[] OurExtensions = { ".ico" }; + protected override string[] OurExtensions { get; } = { ".ico" }; - /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) - { - if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) - { - return Enumerable.Empty(); - } - return OurExtensions; - } - - /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) - { - return false; - } - - return OurExtensions.Contains(NormalizeExtension(extension)); - } - - /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return int.MaxValue; - } - - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) { // TODO: Implement this return false; } - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) { _ = stream.Seek(0, SeekOrigin.Current); @@ -115,23 +83,6 @@ namespace Greenshot.Editor.FileFormatHandlers return false; } - - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface surface = null) - { - if (TryLoadFromStream(stream, extension, out var bitmap)) - { - var imageContainer = new ImageContainer(surface) - { - Image = bitmap - }; - drawableContainer = imageContainer; - return true; - } - - drawableContainer = null; - return true; - } - /// /// Based on: https://www.codeproject.com/KB/cs/IconExtractor.aspx /// And a hint from: https://www.codeproject.com/KB/cs/IconLib.aspx diff --git a/src/Greenshot.Editor/FileFormatHandlers/MetaFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/MetaFileFormatHandler.cs index 42280c0c2..1245c3b71 100644 --- a/src/Greenshot.Editor/FileFormatHandlers/MetaFileFormatHandler.cs +++ b/src/Greenshot.Editor/FileFormatHandlers/MetaFileFormatHandler.cs @@ -25,7 +25,6 @@ using System.Drawing.Imaging; using System.IO; using System.Linq; using Greenshot.Base.Core; -using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Interfaces; using Greenshot.Base.Interfaces.Drawing; using Greenshot.Editor.Drawing; @@ -37,10 +36,9 @@ namespace Greenshot.Editor.FileFormatHandlers /// public class MetaFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler { - private static readonly string [] OurExtensions = { ".wmf", ".emf" }; + protected override string[] OurExtensions { get; } = { ".wmf", ".emf" }; - /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) + public override IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) { if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) { @@ -51,7 +49,7 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) + public override bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) { if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) { @@ -62,19 +60,13 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return int.MaxValue; - } - - /// - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) { return false; } /// - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) { try { @@ -93,7 +85,7 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface surface = null) + public override bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface surface = null) { if (Image.FromStream(stream, true, true) is Metafile metaFile) { diff --git a/src/Greenshot.Editor/FileFormatHandlers/PngFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/PngFileFormatHandler.cs deleted file mode 100644 index a7a2820d5..000000000 --- a/src/Greenshot.Editor/FileFormatHandlers/PngFileFormatHandler.cs +++ /dev/null @@ -1,84 +0,0 @@ -/* - * Greenshot - a free and open source screenshot tool - * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom - * - * For more information see: https://getgreenshot.org/ - * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 1 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -using System; -using System.Collections.Generic; -using System.Drawing; -using System.IO; -using System.Linq; -using Greenshot.Base.Interfaces; -using Greenshot.Base.Interfaces.Drawing; - -namespace Greenshot.Editor.FileFormatHandlers -{ - /// - /// This can be an ImageSharp implementation - /// - public class PngFileFormatHandler : IFileFormatHandler - { - private static readonly string[] OurExtensions = { "png" }; - - /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) - { - if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream) - { - return Enumerable.Empty(); - } - - return OurExtensions; - } - - /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - if (fileFormatHandlerAction == FileFormatHandlerActions.LoadDrawableFromStream) - { - return false; - } - - return OurExtensions.Contains(extension?.ToLowerInvariant()); - } - /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return int.MaxValue; - } - - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) - { - // TODO: Implement this - throw new NotImplementedException(); - } - - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) - { - // TODO: Implement this - return false; - } - - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent) - { - // TODO: Implement this - throw new NotImplementedException(); - } - } -} diff --git a/src/Greenshot.Editor/FileFormatHandlers/SvgFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/SvgFileFormatHandler.cs index 3cc04d681..33011a17a 100644 --- a/src/Greenshot.Editor/FileFormatHandlers/SvgFileFormatHandler.cs +++ b/src/Greenshot.Editor/FileFormatHandlers/SvgFileFormatHandler.cs @@ -24,7 +24,6 @@ using System.Collections.Generic; using System.Drawing; using System.IO; using System.Linq; -using Greenshot.Base.Core.FileFormatHandlers; using Greenshot.Base.Interfaces; using Greenshot.Base.Interfaces.Drawing; using Greenshot.Editor.Drawing; @@ -39,10 +38,10 @@ namespace Greenshot.Editor.FileFormatHandlers public class SvgFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler { private static readonly ILog Log = LogManager.GetLogger(typeof(SvgFileFormatHandler)); - private static readonly string[] OurExtensions = { ".svg" }; + protected override string[] OurExtensions { get; } = { ".svg" }; /// - public IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) + public override IEnumerable SupportedExtensions(FileFormatHandlerActions fileFormatHandlerAction) { if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) { @@ -53,7 +52,7 @@ namespace Greenshot.Editor.FileFormatHandlers } /// - public bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) + public override bool Supports(FileFormatHandlerActions fileFormatHandlerAction, string extension) { if (fileFormatHandlerAction == FileFormatHandlerActions.SaveToStream) { @@ -62,13 +61,8 @@ namespace Greenshot.Editor.FileFormatHandlers return OurExtensions.Contains(NormalizeExtension(extension)); } - /// - public int PriorityFor(FileFormatHandlerActions fileFormatHandlerAction, string extension) - { - return int.MaxValue; - } - public bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) { var svgDocument = SvgDocument.Open(stream); @@ -85,13 +79,13 @@ namespace Greenshot.Editor.FileFormatHandlers return false; } - public bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) { // TODO: Implement this return false; } - public bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent = null) + public override bool TryLoadDrawableFromStream(Stream stream, string extension, out IDrawableContainer drawableContainer, ISurface parent = null) { var svgDocument = SvgDocument.Open(stream); if (svgDocument == null) diff --git a/src/Greenshot.Editor/FileFormatHandlers/WmpFileFormatHandler.cs b/src/Greenshot.Editor/FileFormatHandlers/WmpFileFormatHandler.cs new file mode 100644 index 000000000..6f0974dea --- /dev/null +++ b/src/Greenshot.Editor/FileFormatHandlers/WmpFileFormatHandler.cs @@ -0,0 +1,66 @@ +/* + * Greenshot - a free and open source screenshot tool + * Copyright (C) 2007-2021 Thomas Braun, Jens Klingen, Robin Krom + * + * For more information see: https://getgreenshot.org/ + * The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 1 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +using System.Drawing; +using System.IO; +using System.Windows.Media.Imaging; +using Greenshot.Base.Interfaces; +using Greenshot.Base.Core; + +namespace Greenshot.Editor.FileFormatHandlers +{ + /// + /// This is the default .NET bitmap file format handler + /// + public class WmpFileFormatHandler : AbstractFileFormatHandler, IFileFormatHandler + { + protected override string[] OurExtensions { get; } = { ".jxr", ".wdp", ".wmp" }; + + /// + public override bool TrySaveToStream(Bitmap bitmap, Stream destination, string extension) + { + try + { + var bitmapSource = bitmap.ToBitmapSource(); + var bitmapFrame = BitmapFrame.Create(bitmapSource); + var jpegXrEncoder = new WmpBitmapEncoder(); + jpegXrEncoder.Frames.Add(bitmapFrame); + // TODO: Support supplying a quality + //jpegXrEncoder.ImageQualityLevel = quality / 100f; + jpegXrEncoder.Save(destination); + return true; + } + catch + { + return false; + } + } + + /// + public override bool TryLoadFromStream(Stream stream, string extension, out Bitmap bitmap) + { + var decoder = new WmpBitmapDecoder(stream, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.None); + var bitmapSource = decoder.Frames[0]; + bitmap = bitmapSource.ToBitmap(); + return true; + } + } +}