From d90351341fb5d00b29450e6ff2c3f2fa768fc225 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 29 Nov 2016 01:12:19 +1100 Subject: [PATCH] Add new To method --- src/ImageSharp/Image/Image.cs | 58 ++++++++++++++++++- src/ImageSharp/Image/ImageFrame.cs | 51 ++++++++++++++-- .../Formats/GeneralFormatTests.cs | 1 + 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/src/ImageSharp/Image/Image.cs b/src/ImageSharp/Image/Image.cs index 0bcf7c6eb..cfac0c7a0 100644 --- a/src/ImageSharp/Image/Image.cs +++ b/src/ImageSharp/Image/Image.cs @@ -11,6 +11,7 @@ namespace ImageSharp using System.IO; using System.Linq; using System.Text; + using System.Threading.Tasks; using Formats; @@ -245,7 +246,7 @@ namespace ImageSharp /// /// Returns a Base64 encoded string from the given image. /// - /// data:image/gif;base64,R0lGODlhAQABAIABAEdJRgAAACwAAAAAAQABAAACAkQBAA== + /// /// The public string ToBase64String() { @@ -257,6 +258,57 @@ namespace ImageSharp } } + /// + /// Returns a copy of the image in the given pixel format. + /// + /// The pixel format. + /// The packed format. uint, long, float. + /// The + public Image To() + where TColor2 : struct, IPackedPixel + where TPacked2 : struct + { + Image target = new Image(this.Width, this.Height) + { + Quality = this.Quality, + FrameDelay = this.FrameDelay, + HorizontalResolution = this.HorizontalResolution, + VerticalResolution = this.VerticalResolution, + CurrentImageFormat = this.CurrentImageFormat, + RepeatCount = this.RepeatCount + }; + + using (PixelAccessor pixels = this.Lock()) + using (PixelAccessor targetPixels = target.Lock()) + { + Parallel.For( + 0, + target.Height, + Bootstrapper.Instance.ParallelOptions, + y => + { + for (int x = 0; x < target.Width; x++) + { + TColor2 color = default(TColor2); + color.PackFromVector4(pixels[x, y].ToVector4()); + targetPixels[x, y] = color; + } + }); + } + + if (this.ExifProfile != null) + { + target.ExifProfile = new ExifProfile(this.ExifProfile); + } + + foreach (ImageFrame frame in this.Frames) + { + target.Frames.Add(frame.To()); + } + + return target; + } + /// /// Copies the properties from the other . /// @@ -278,6 +330,10 @@ namespace ImageSharp } } + /// + /// Creates a new from this instance + /// + /// The internal virtual ImageFrame ToFrame() { return new ImageFrame(this); diff --git a/src/ImageSharp/Image/ImageFrame.cs b/src/ImageSharp/Image/ImageFrame.cs index d63618766..2a49a3980 100644 --- a/src/ImageSharp/Image/ImageFrame.cs +++ b/src/ImageSharp/Image/ImageFrame.cs @@ -5,6 +5,8 @@ namespace ImageSharp { + using System.Threading.Tasks; + /// /// Represents a single frame in a animation. /// @@ -24,9 +26,7 @@ namespace ImageSharp /// /// Initializes a new instance of the class. /// - /// - /// The image to create the frame from. - /// + /// The image to create the frame from. public ImageFrame(ImageBase image) : base(image) { @@ -38,9 +38,52 @@ namespace ImageSharp return $"ImageFrame: {this.Width}x{this.Height}"; } + /// + /// Returns a copy of the image frame in the given pixel format. + /// + /// The pixel format. + /// The packed format. uint, long, float. + /// The + public ImageFrame To() + where TColor2 : struct, IPackedPixel + where TPacked2 : struct + { + ImageFrame target = new ImageFrame + { + Quality = this.Quality, + FrameDelay = this.FrameDelay + }; + + target.InitPixels(this.Width, this.Height); + + using (PixelAccessor pixels = this.Lock()) + using (PixelAccessor targetPixels = target.Lock()) + { + Parallel.For( + 0, + target.Height, + Bootstrapper.Instance.ParallelOptions, + y => + { + for (int x = 0; x < target.Width; x++) + { + TColor2 color = default(TColor2); + color.PackFromVector4(pixels[x, y].ToVector4()); + targetPixels[x, y] = color; + } + }); + } + + return target; + } + + /// + /// Clones the current instance. + /// + /// The internal virtual ImageFrame Clone() { return new ImageFrame(this); } } -} +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs index 7d88ca6e1..542a131d8 100644 --- a/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs +++ b/tests/ImageSharp.Tests/Formats/GeneralFormatTests.cs @@ -52,6 +52,7 @@ namespace ImageSharp.Tests foreach (TestFile file in Files) { Image image = file.CreateImage(); + // Image image = file.CreateImage().To(); using (FileStream output = File.OpenWrite($"{path}/{file.FileName}")) {