From 69100f4f0c65eb8facd5f21d2f3616a61554dcdf Mon Sep 17 00:00:00 2001 From: Scott Williams Date: Sun, 26 Mar 2017 09:31:45 +0100 Subject: [PATCH] Move metadata loading into constructor/creator. --- src/ImageSharp/Formats/Gif/GifDecoderCore.cs | 3 +- .../Formats/Jpeg/JpegDecoderCore.cs | 4 +- src/ImageSharp/Formats/Png/PngDecoderCore.cs | 3 +- src/ImageSharp/Image.Create.cs | 26 ++++++++-- src/ImageSharp/Image.cs | 15 ++++++ src/ImageSharp/Image/Image{TColor}.cs | 34 ++++++++++---- src/ImageSharp/MetaData/ImageMetaData.cs | 47 ++++++++----------- 7 files changed, 85 insertions(+), 47 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs index 79348a7ab..4c119ca73 100644 --- a/src/ImageSharp/Formats/Gif/GifDecoderCore.cs +++ b/src/ImageSharp/Formats/Gif/GifDecoderCore.cs @@ -362,8 +362,7 @@ namespace ImageSharp.Formats this.metaData.Quality = colorTableLength / 3; // This initializes the image to become fully transparent because the alpha channel is zero. - this.image = Image.Create(imageWidth, imageHeight, this.configuration); - this.image.MetaData.LoadFrom(this.metaData); + this.image = Image.Create(imageWidth, imageHeight, this.metaData, this.configuration); this.SetFrameDelay(this.metaData); diff --git a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs index fc9506b54..33533aa12 100644 --- a/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs +++ b/src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs @@ -505,9 +505,7 @@ namespace ImageSharp.Formats private Image ConvertJpegPixelsToImagePixels(ImageMetaData metadata) where TColor : struct, IPixel { - Image image = Image.Create(this.ImageWidth, this.ImageHeight, this.configuration); - - image.MetaData.LoadFrom(metadata); + Image image = Image.Create(this.ImageWidth, this.ImageHeight, metadata, this.configuration); if (this.grayImage.IsInitialized) { diff --git a/src/ImageSharp/Formats/Png/PngDecoderCore.cs b/src/ImageSharp/Formats/Png/PngDecoderCore.cs index 7705e9549..d6529940e 100644 --- a/src/ImageSharp/Formats/Png/PngDecoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngDecoderCore.cs @@ -220,8 +220,7 @@ namespace ImageSharp.Formats throw new ArgumentOutOfRangeException($"The input png '{this.header.Width}x{this.header.Height}' is bigger than the max allowed size '{Image.MaxWidth}x{Image.MaxHeight}'"); } - Image image = Image.Create(this.header.Width, this.header.Height, this.configuration); - image.MetaData.LoadFrom(metadata); + Image image = Image.Create(this.header.Width, this.header.Height, metadata, this.configuration); using (PixelAccessor pixels = image.Lock()) { diff --git a/src/ImageSharp/Image.Create.cs b/src/ImageSharp/Image.Create.cs index bc430724a..fcecefd7b 100644 --- a/src/ImageSharp/Image.Create.cs +++ b/src/ImageSharp/Image.Create.cs @@ -24,23 +24,43 @@ namespace ImageSharp /// The pixel format. /// The width of the image in pixels. /// The height of the image in pixels. + /// The images matadata to preload. /// /// The configuration providing initialization code which allows extending the library. /// /// /// A new unless is in which case it returns /// - internal static Image Create(int width, int height, Configuration configuration) + internal static Image Create(int width, int height, ImageMetaData metadata, Configuration configuration) where TColor : struct, IPixel { if (typeof(TColor) == typeof(Color)) { - return new Image(width, height, configuration) as Image; + return new Image(width, height, metadata, configuration) as Image; } else { - return new Image(width, height, configuration); + return new Image(width, height, metadata, configuration); } } + + /// + /// Create a new instance of the class + /// with the height and the width of the image. + /// + /// The pixel format. + /// The width of the image in pixels. + /// The height of the image in pixels. + /// + /// The configuration providing initialization code which allows extending the library. + /// + /// + /// A new unless is in which case it returns + /// + internal static Image Create(int width, int height, Configuration configuration) + where TColor : struct, IPixel + { + return Image.Create(width, height, null, configuration); + } } } diff --git a/src/ImageSharp/Image.cs b/src/ImageSharp/Image.cs index 195af6056..00688afc9 100644 --- a/src/ImageSharp/Image.cs +++ b/src/ImageSharp/Image.cs @@ -53,5 +53,20 @@ namespace ImageSharp : base(other) { } + + /// + /// Initializes a new instance of the class + /// with the height and the width of the image. + /// + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The metadata. + /// + /// The configuration providing initialization code which allows extending the library. + /// + internal Image(int width, int height, ImageMetaData metadata, Configuration configuration) + : base(width, height, metadata, configuration) + { + } } } diff --git a/src/ImageSharp/Image/Image{TColor}.cs b/src/ImageSharp/Image/Image{TColor}.cs index 82991948c..d063c3ff1 100644 --- a/src/ImageSharp/Image/Image{TColor}.cs +++ b/src/ImageSharp/Image/Image{TColor}.cs @@ -36,15 +36,8 @@ namespace ImageSharp /// The configuration providing initialization code which allows extending the library. /// public Image(int width, int height, Configuration configuration) - : base(width, height, configuration) + : this(width, height, new ImageMetaData(), configuration) { - if (!this.Configuration.ImageFormats.Any()) - { - throw new InvalidOperationException("No image formats have been configured."); - } - - this.MetaData = new ImageMetaData(); - this.CurrentImageFormat = this.Configuration.ImageFormats.First(); } /// @@ -87,12 +80,35 @@ namespace ImageSharp public Image(ImageBase other) : base(other) { + this.MetaData = new ImageMetaData(); + } + + /// + /// Initializes a new instance of the class + /// with the height and the width of the image. + /// + /// The width of the image in pixels. + /// The height of the image in pixels. + /// The images metadata. + /// + /// The configuration providing initialization code which allows extending the library. + /// + internal Image(int width, int height, ImageMetaData metadata, Configuration configuration) + : base(width, height, configuration) + { + if (!this.Configuration.ImageFormats.Any()) + { + throw new InvalidOperationException("No image formats have been configured."); + } + + this.MetaData = metadata ?? new ImageMetaData(); + this.CurrentImageFormat = this.Configuration.ImageFormats.First(); } /// /// Gets the meta data of the image. /// - public ImageMetaData MetaData { get; private set; } = new ImageMetaData(); + public ImageMetaData MetaData { get; private set; } /// /// Gets the width of the image in inches. It is calculated as the width of the image diff --git a/src/ImageSharp/MetaData/ImageMetaData.cs b/src/ImageSharp/MetaData/ImageMetaData.cs index d7d5e88b5..aed6efa2c 100644 --- a/src/ImageSharp/MetaData/ImageMetaData.cs +++ b/src/ImageSharp/MetaData/ImageMetaData.cs @@ -48,7 +48,25 @@ namespace ImageSharp { DebugGuard.NotNull(other, nameof(other)); - this.LoadFrom(other); + this.HorizontalResolution = other.HorizontalResolution; + this.VerticalResolution = other.VerticalResolution; + this.Quality = other.Quality; + this.FrameDelay = other.FrameDelay; + this.RepeatCount = other.RepeatCount; + + foreach (ImageProperty property in other.Properties) + { + this.Properties.Add(new ImageProperty(property)); + } + + if (other.ExifProfile != null) + { + this.ExifProfile = new ExifProfile(other.ExifProfile); + } + else + { + this.ExifProfile = null; + } } /// @@ -130,32 +148,5 @@ namespace ImageSharp { this.ExifProfile?.Sync(this); } - - /// - /// Sets the current metadata values based on a previous metadata object. - /// - /// Meta data object to copy values from. - internal void LoadFrom(ImageMetaData other) - { - this.HorizontalResolution = other.HorizontalResolution; - this.VerticalResolution = other.VerticalResolution; - this.Quality = other.Quality; - this.FrameDelay = other.FrameDelay; - this.RepeatCount = other.RepeatCount; - - foreach (ImageProperty property in other.Properties) - { - this.Properties.Add(new ImageProperty(property)); - } - - if (other.ExifProfile != null) - { - this.ExifProfile = new ExifProfile(other.ExifProfile); - } - else - { - this.ExifProfile = null; - } - } } }