diff --git a/src/ImageSharp/Image/Image.cs b/src/ImageSharp/Image/Image.cs index cfac0c7a0..fcb72ced8 100644 --- a/src/ImageSharp/Image/Image.cs +++ b/src/ImageSharp/Image/Image.cs @@ -10,6 +10,7 @@ namespace ImageSharp using System.Diagnostics; using System.IO; using System.Linq; + using System.Numerics; using System.Text; using System.Threading.Tasks; @@ -260,14 +261,25 @@ namespace ImageSharp /// /// Returns a copy of the image in the given pixel format. + /// + /// Most color formats when converted to vectors have a range of 0 to 1. Some however, , for example scale between + /// -1 to 1. This requires additional computation to convert between the formats. + /// For example, if I wanted to convert from to the following function would be required. v => (2F * v) - Vector4.One + /// /// + /// A function that allows for the correction of vector scaling between color formats. /// The pixel format. /// The packed format. uint, long, float. /// The - public Image To() + public Image To(Func scaleFunc = null) where TColor2 : struct, IPackedPixel where TPacked2 : struct { + if (scaleFunc == null) + { + scaleFunc = v => v; + } + Image target = new Image(this.Width, this.Height) { Quality = this.Quality, @@ -290,7 +302,7 @@ namespace ImageSharp for (int x = 0; x < target.Width; x++) { TColor2 color = default(TColor2); - color.PackFromVector4(pixels[x, y].ToVector4()); + color.PackFromVector4(scaleFunc(pixels[x, y].ToVector4())); targetPixels[x, y] = color; } }); diff --git a/src/ImageSharp/Image/ImageFrame.cs b/src/ImageSharp/Image/ImageFrame.cs index 2a49a3980..fd4d7fa52 100644 --- a/src/ImageSharp/Image/ImageFrame.cs +++ b/src/ImageSharp/Image/ImageFrame.cs @@ -5,6 +5,8 @@ namespace ImageSharp { + using System; + using System.Numerics; using System.Threading.Tasks; /// @@ -40,14 +42,25 @@ namespace ImageSharp /// /// Returns a copy of the image frame in the given pixel format. + /// + /// Most color formats when converted to vectors have a range of 0 to 1. Some however, , for example scale between + /// -1 to 1. This requires additional computation to convert between the formats. + /// For example, if I wanted to convert from to the following function would be required. v => (2F * v) - Vector4.One + /// /// + /// A function that allows for the correction of vector scaling between color formats. /// The pixel format. /// The packed format. uint, long, float. /// The - public ImageFrame To() + public ImageFrame To(Func scaleFunc = null) where TColor2 : struct, IPackedPixel where TPacked2 : struct { + if (scaleFunc == null) + { + scaleFunc = v => v; + } + ImageFrame target = new ImageFrame { Quality = this.Quality, @@ -68,7 +81,7 @@ namespace ImageSharp for (int x = 0; x < target.Width; x++) { TColor2 color = default(TColor2); - color.PackFromVector4(pixels[x, y].ToVector4()); + color.PackFromVector4(scaleFunc(pixels[x, y].ToVector4())); targetPixels[x, y] = color; } }); diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index dc175e390..4639d7bee 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -7,6 +7,7 @@ namespace ImageSharp.Tests { using System; using System.IO; + using System.Numerics; using Xunit; @@ -62,8 +63,8 @@ namespace ImageSharp.Tests // Image image = file.CreateImage().To(); // Image image = file.CreateImage().To(); - // TODO: Conversion between types who's vector ranges are different are not possible. - // Image image = file.CreateImage().To(); + // TODO: Conversion between types who's vector ranges are different are not possible without scaling function, Make static version of known ones. + // Image image = file.CreateImage().To(v => (2F * v) - Vector4.One); using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) {