Browse Source

Move metadata loading into constructor/creator.

af/merge-core
Scott Williams 9 years ago
parent
commit
69100f4f0c
  1. 3
      src/ImageSharp/Formats/Gif/GifDecoderCore.cs
  2. 4
      src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs
  3. 3
      src/ImageSharp/Formats/Png/PngDecoderCore.cs
  4. 26
      src/ImageSharp/Image.Create.cs
  5. 15
      src/ImageSharp/Image.cs
  6. 34
      src/ImageSharp/Image/Image{TColor}.cs
  7. 47
      src/ImageSharp/MetaData/ImageMetaData.cs

3
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<TColor>(imageWidth, imageHeight, this.configuration);
this.image.MetaData.LoadFrom(this.metaData);
this.image = Image.Create<TColor>(imageWidth, imageHeight, this.metaData, this.configuration);
this.SetFrameDelay(this.metaData);

4
src/ImageSharp/Formats/Jpeg/JpegDecoderCore.cs

@ -505,9 +505,7 @@ namespace ImageSharp.Formats
private Image<TColor> ConvertJpegPixelsToImagePixels<TColor>(ImageMetaData metadata)
where TColor : struct, IPixel<TColor>
{
Image<TColor> image = Image.Create<TColor>(this.ImageWidth, this.ImageHeight, this.configuration);
image.MetaData.LoadFrom(metadata);
Image<TColor> image = Image.Create<TColor>(this.ImageWidth, this.ImageHeight, metadata, this.configuration);
if (this.grayImage.IsInitialized)
{

3
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<TColor>.MaxWidth}x{Image<TColor>.MaxHeight}'");
}
Image<TColor> image = Image.Create<TColor>(this.header.Width, this.header.Height, this.configuration);
image.MetaData.LoadFrom(metadata);
Image<TColor> image = Image.Create<TColor>(this.header.Width, this.header.Height, metadata, this.configuration);
using (PixelAccessor<TColor> pixels = image.Lock())
{

26
src/ImageSharp/Image.Create.cs

@ -24,23 +24,43 @@ namespace ImageSharp
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images matadata to preload.</param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <returns>
/// A new <see cref="Image{TColor}"/> unless <typeparamref name="TColor"/> is <see cref="Color"/> in which case it returns <see cref="Image" />
/// </returns>
internal static Image<TColor> Create<TColor>(int width, int height, Configuration configuration)
internal static Image<TColor> Create<TColor>(int width, int height, ImageMetaData metadata, Configuration configuration)
where TColor : struct, IPixel<TColor>
{
if (typeof(TColor) == typeof(Color))
{
return new Image(width, height, configuration) as Image<TColor>;
return new Image(width, height, metadata, configuration) as Image<TColor>;
}
else
{
return new Image<TColor>(width, height, configuration);
return new Image<TColor>(width, height, metadata, configuration);
}
}
/// <summary>
/// Create a new instance of the <see cref="Image{TColor}"/> class
/// with the height and the width of the image.
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
/// <returns>
/// A new <see cref="Image{TColor}"/> unless <typeparamref name="TColor"/> is <see cref="Color"/> in which case it returns <see cref="Image" />
/// </returns>
internal static Image<TColor> Create<TColor>(int width, int height, Configuration configuration)
where TColor : struct, IPixel<TColor>
{
return Image.Create<TColor>(width, height, null, configuration);
}
}
}

15
src/ImageSharp/Image.cs

@ -53,5 +53,20 @@ namespace ImageSharp
: base(other)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Image"/> class
/// with the height and the width of the image.
/// </summary>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The metadata.</param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
internal Image(int width, int height, ImageMetaData metadata, Configuration configuration)
: base(width, height, metadata, configuration)
{
}
}
}

34
src/ImageSharp/Image/Image{TColor}.cs

@ -36,15 +36,8 @@ namespace ImageSharp
/// The configuration providing initialization code which allows extending the library.
/// </param>
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();
}
/// <summary>
@ -87,12 +80,35 @@ namespace ImageSharp
public Image(ImageBase<TColor> other)
: base(other)
{
this.MetaData = new ImageMetaData();
}
/// <summary>
/// Initializes a new instance of the <see cref="Image{TColor}"/> class
/// with the height and the width of the image.
/// </summary>
/// <param name="width">The width of the image in pixels.</param>
/// <param name="height">The height of the image in pixels.</param>
/// <param name="metadata">The images metadata.</param>
/// <param name="configuration">
/// The configuration providing initialization code which allows extending the library.
/// </param>
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();
}
/// <summary>
/// Gets the meta data of the image.
/// </summary>
public ImageMetaData MetaData { get; private set; } = new ImageMetaData();
public ImageMetaData MetaData { get; private set; }
/// <summary>
/// Gets the width of the image in inches. It is calculated as the width of the image

47
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;
}
}
/// <summary>
@ -130,32 +148,5 @@ namespace ImageSharp
{
this.ExifProfile?.Sync(this);
}
/// <summary>
/// Sets the current metadata values based on a previous metadata object.
/// </summary>
/// <param name="other">Meta data object to copy values from.</param>
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;
}
}
}
}

Loading…
Cancel
Save