Browse Source

Added options for the encoder.

pull/22/merge
Dirk Lemstra 9 years ago
committed by Dirk Lemstra
parent
commit
eb5e8a83af
  1. 2
      src/ImageSharp.Formats.Bmp/BmpEncoder.cs
  2. 2
      src/ImageSharp.Formats.Gif/GifEncoder.cs
  3. 2
      src/ImageSharp.Formats.Jpeg/JpegEncoder.cs
  4. 2
      src/ImageSharp.Formats.Png/PngEncoder.cs
  5. 46
      src/ImageSharp/Formats/EncoderOptions.cs
  6. 18
      src/ImageSharp/Formats/IEncoderOptions.cs
  7. 3
      src/ImageSharp/Formats/IImageEncoder.cs
  8. 16
      src/ImageSharp/Image/Image{TColor}.cs

2
src/ImageSharp.Formats.Bmp/BmpEncoder.cs

@ -20,7 +20,7 @@ namespace ImageSharp.Formats
public BmpBitsPerPixel BitsPerPixel { get; set; } = BmpBitsPerPixel.Pixel24;
/// <inheritdoc/>
public void Encode<TColor>(Image<TColor> image, Stream stream)
public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor>
{
BmpEncoderCore encoder = new BmpEncoderCore();

2
src/ImageSharp.Formats.Gif/GifEncoder.cs

@ -32,7 +32,7 @@ namespace ImageSharp.Formats
public IQuantizer Quantizer { get; set; }
/// <inheritdoc/>
public void Encode<TColor>(Image<TColor> image, Stream stream)
public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor>
{
GifEncoderCore encoder = new GifEncoderCore

2
src/ImageSharp.Formats.Jpeg/JpegEncoder.cs

@ -61,7 +61,7 @@ namespace ImageSharp.Formats
}
/// <inheritdoc/>
public void Encode<TColor>(Image<TColor> image, Stream stream)
public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor>
{
// Ensure that quality can be set but has a fallback.

2
src/ImageSharp.Formats.Png/PngEncoder.cs

@ -56,7 +56,7 @@ namespace ImageSharp.Formats
public bool WriteGamma { get; set; }
/// <inheritdoc/>
public void Encode<TColor>(Image<TColor> image, Stream stream)
public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor>
{
PngEncoderCore encoder = new PngEncoderCore

46
src/ImageSharp/Formats/EncoderOptions.cs

@ -0,0 +1,46 @@
// <copyright file="EncoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
/// <summary>
/// Encapsulates the shared encoder options.
/// </summary>
public class EncoderOptions : IEncoderOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="EncoderOptions"/> class.
/// </summary>
public EncoderOptions()
{
this.InitializeWithDefaults();
}
/// <summary>
/// Initializes a new instance of the <see cref="EncoderOptions"/> class.
/// </summary>
/// <param name="options">The encoder options</param>
protected EncoderOptions(IEncoderOptions options)
{
if (options == null)
{
this.InitializeWithDefaults();
return;
}
this.IgnoreMetadata = options.IgnoreMetadata;
}
/// <summary>
/// Gets or sets a value indicating whether the metadata should be ignored when the image is being encoded.
/// </summary>
public bool IgnoreMetadata { get; set; }
private void InitializeWithDefaults()
{
this.IgnoreMetadata = false;
}
}
}

18
src/ImageSharp/Formats/IEncoderOptions.cs

@ -0,0 +1,18 @@
// <copyright file="IEncoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp
{
/// <summary>
/// Encapsulates the shared encoder options.
/// </summary>
public interface IEncoderOptions
{
/// <summary>
/// Gets a value indicating whether the metadata should be ignored when the image is being encoded.
/// </summary>
bool IgnoreMetadata { get; }
}
}

3
src/ImageSharp/Formats/IImageEncoder.cs

@ -19,7 +19,8 @@ namespace ImageSharp.Formats
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <param name="image">The <see cref="Image{TColor}"/> to encode from.</param>
/// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
void Encode<TColor>(Image<TColor> image, Stream stream)
/// <param name="options">The options for the encoder.</param>
void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor>;
}
}

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

@ -206,12 +206,13 @@ namespace ImageSharp
/// Saves the image to the given stream using the currently loaded image format.
/// </summary>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="options">The options for the encoder.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream is null.</exception>
/// <returns>The <see cref="Image{TColor}"/></returns>
public Image<TColor> Save(Stream stream)
public Image<TColor> Save(Stream stream, IEncoderOptions options = null)
{
Guard.NotNull(stream, nameof(stream));
this.CurrentImageFormat.Encoder.Encode(this, stream);
this.CurrentImageFormat.Encoder.Encode(this, stream, options);
return this;
}
@ -220,13 +221,13 @@ namespace ImageSharp
/// </summary>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="format">The format to save the image as.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream or format is null.</exception>
/// <param name="options">The options for the encoder.</param>
/// <returns>The <see cref="Image{TColor}"/></returns>
public Image<TColor> Save(Stream stream, IImageFormat format)
public Image<TColor> Save(Stream stream, IImageFormat format, IEncoderOptions options = null)
{
Guard.NotNull(stream, nameof(stream));
Guard.NotNull(format, nameof(format));
format.Encoder.Encode(this, stream);
format.Encoder.Encode(this, stream, options);
return this;
}
@ -235,15 +236,16 @@ namespace ImageSharp
/// </summary>
/// <param name="stream">The stream to save the image to.</param>
/// <param name="encoder">The encoder to save the image with.</param>
/// <param name="options">The options for the encoder.</param>
/// <exception cref="System.ArgumentNullException">Thrown if the stream or encoder is null.</exception>
/// <returns>
/// The <see cref="Image{TColor}"/>.
/// </returns>
public Image<TColor> Save(Stream stream, IImageEncoder encoder)
public Image<TColor> Save(Stream stream, IImageEncoder encoder, IEncoderOptions options = null)
{
Guard.NotNull(stream, nameof(stream));
Guard.NotNull(encoder, nameof(encoder));
encoder.Encode(this, stream);
encoder.Encode(this, stream, options);
// Reset to the start of the stream.
if (stream.CanSeek)

Loading…
Cancel
Save