Browse Source

Implemented the IEncoderOptions inside the bmp encoder.

af/merge-core
Dirk Lemstra 9 years ago
committed by Dirk Lemstra
parent
commit
26aec54e05
  1. 24
      src/ImageSharp.Formats.Bmp/BmpEncoder.cs
  2. 24
      src/ImageSharp.Formats.Bmp/BmpEncoderCore.cs
  3. 51
      src/ImageSharp.Formats.Bmp/BmpEncoderOptions.cs
  4. 18
      src/ImageSharp.Formats.Bmp/IBmpEncoderOptions.cs
  5. 6
      tests/ImageSharp.Tests/Formats/Bmp/BitmapTests.cs

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

@ -14,17 +14,27 @@ namespace ImageSharp.Formats
/// <remarks>The encoder can currently only write 24-bit rgb images to streams.</remarks> /// <remarks>The encoder can currently only write 24-bit rgb images to streams.</remarks>
public class BmpEncoder : IImageEncoder public class BmpEncoder : IImageEncoder
{ {
/// <summary>
/// Gets or sets the number of bits per pixel.
/// </summary>
public BmpBitsPerPixel BitsPerPixel { get; set; } = BmpBitsPerPixel.Pixel24;
/// <inheritdoc/> /// <inheritdoc/>
public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options) public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor> where TColor : struct, IPixel<TColor>
{ {
BmpEncoderCore encoder = new BmpEncoderCore(); IBmpEncoderOptions bmpOptions = BmpEncoderOptions.Create(options);
encoder.Encode(image, stream, this.BitsPerPixel);
this.Encode(image, stream, bmpOptions);
}
/// <summary>
/// Encodes the image to the specified stream from the <see cref="Image{TColor}"/>.
/// </summary>
/// <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>
/// <param name="options">The options for the encoder.</param>
public void Encode<TColor>(Image<TColor> image, Stream stream, IBmpEncoderOptions options)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
BmpEncoderCore encoder = new BmpEncoderCore(options);
encoder.Encode(image, stream);
} }
} }
} }

24
src/ImageSharp.Formats.Bmp/BmpEncoderCore.cs

@ -16,34 +16,40 @@ namespace ImageSharp.Formats
internal sealed class BmpEncoderCore internal sealed class BmpEncoderCore
{ {
/// <summary> /// <summary>
/// The number of bits per pixel. /// The options for the encoder.
/// </summary> /// </summary>
private BmpBitsPerPixel bmpBitsPerPixel; private IBmpEncoderOptions options;
/// <summary> /// <summary>
/// The amount to pad each row by. /// The amount to pad each row by.
/// </summary> /// </summary>
private int padding; private int padding;
/// <summary>
/// Initializes a new instance of the <see cref="BmpEncoderCore"/> class.
/// </summary>
/// <param name="options">The options for the encoder.</param>
public BmpEncoderCore(IBmpEncoderOptions options)
{
this.options = options ?? new BmpEncoderOptions();
}
/// <summary> /// <summary>
/// Encodes the image to the specified stream from the <see cref="ImageBase{TColor}"/>. /// Encodes the image to the specified stream from the <see cref="ImageBase{TColor}"/>.
/// </summary> /// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam> /// <typeparam name="TColor">The pixel format.</typeparam>
/// <param name="image">The <see cref="ImageBase{TColor}"/> to encode from.</param> /// <param name="image">The <see cref="ImageBase{TColor}"/> to encode from.</param>
/// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param> /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
/// <param name="bitsPerPixel">The <see cref="BmpBitsPerPixel"/></param> public void Encode<TColor>(ImageBase<TColor> image, Stream stream)
public void Encode<TColor>(ImageBase<TColor> image, Stream stream, BmpBitsPerPixel bitsPerPixel)
where TColor : struct, IPixel<TColor> where TColor : struct, IPixel<TColor>
{ {
Guard.NotNull(image, nameof(image)); Guard.NotNull(image, nameof(image));
Guard.NotNull(stream, nameof(stream)); Guard.NotNull(stream, nameof(stream));
this.bmpBitsPerPixel = bitsPerPixel;
// Cast to int will get the bytes per pixel // Cast to int will get the bytes per pixel
short bpp = (short)(8 * (int)bitsPerPixel); short bpp = (short)(8 * (int)this.options.BitsPerPixel);
int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32); int bytesPerLine = 4 * (((image.Width * bpp) + 31) / 32);
this.padding = bytesPerLine - (image.Width * (int)bitsPerPixel); this.padding = bytesPerLine - (image.Width * (int)this.options.BitsPerPixel);
// Do not use IDisposable pattern here as we want to preserve the stream. // Do not use IDisposable pattern here as we want to preserve the stream.
EndianBinaryWriter writer = new EndianBinaryWriter(Endianness.LittleEndian, stream); EndianBinaryWriter writer = new EndianBinaryWriter(Endianness.LittleEndian, stream);
@ -128,7 +134,7 @@ namespace ImageSharp.Formats
{ {
using (PixelAccessor<TColor> pixels = image.Lock()) using (PixelAccessor<TColor> pixels = image.Lock())
{ {
switch (this.bmpBitsPerPixel) switch (this.options.BitsPerPixel)
{ {
case BmpBitsPerPixel.Pixel32: case BmpBitsPerPixel.Pixel32:
this.Write32Bit(writer, pixels); this.Write32Bit(writer, pixels);

51
src/ImageSharp.Formats.Bmp/BmpEncoderOptions.cs

@ -0,0 +1,51 @@
// <copyright file="BmpEncoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
/// <summary>
/// Encapsulates the options for the <see cref="BmpEncoder"/>.
/// </summary>
public sealed class BmpEncoderOptions : EncoderOptions, IBmpEncoderOptions
{
/// <summary>
/// Initializes a new instance of the <see cref="BmpEncoderOptions"/> class.
/// </summary>
public BmpEncoderOptions()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="BmpEncoderOptions"/> class.
/// </summary>
/// <param name="options">The options for the encoder.</param>
private BmpEncoderOptions(IEncoderOptions options)
: base(options)
{
}
/// <summary>
/// Gets or sets the number of bits per pixel.
/// </summary>
public BmpBitsPerPixel BitsPerPixel { get; set; } = BmpBitsPerPixel.Pixel24;
/// <summary>
/// Converts the options to a <see cref="BmpEncoderOptions"/> instance with a cast
/// or by creating a new instance with the specfied options.
/// </summary>
/// <param name="options">The options for the encoder.</param>
/// <returns>The options for the <see cref="BmpEncoder"/>.</returns>
internal static IBmpEncoderOptions Create(IEncoderOptions options)
{
IBmpEncoderOptions bmpOptions = options as IBmpEncoderOptions;
if (bmpOptions != null)
{
return bmpOptions;
}
return new BmpEncoderOptions(options);
}
}
}

18
src/ImageSharp.Formats.Bmp/IBmpEncoderOptions.cs

@ -0,0 +1,18 @@
// <copyright file="IBmpEncoderOptions.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
namespace ImageSharp.Formats
{
/// <summary>
/// Encapsulates the options for the <see cref="BmpEncoder"/>.
/// </summary>
public interface IBmpEncoderOptions : IEncoderOptions
{
/// <summary>
/// Gets the number of bits per pixel.
/// </summary>
BmpBitsPerPixel BitsPerPixel { get; }
}
}

6
tests/ImageSharp.Tests/Formats/Bmp/BitmapTests.cs

@ -7,8 +7,6 @@ using ImageSharp.Formats;
namespace ImageSharp.Tests namespace ImageSharp.Tests
{ {
using System.IO;
using Xunit; using Xunit;
public class BitmapTests : FileTestBase public class BitmapTests : FileTestBase
@ -16,7 +14,7 @@ namespace ImageSharp.Tests
public static readonly TheoryData<BmpBitsPerPixel> BitsPerPixel public static readonly TheoryData<BmpBitsPerPixel> BitsPerPixel
= new TheoryData<BmpBitsPerPixel> = new TheoryData<BmpBitsPerPixel>
{ {
BmpBitsPerPixel.Pixel24 , BmpBitsPerPixel.Pixel24,
BmpBitsPerPixel.Pixel32 BmpBitsPerPixel.Pixel32
}; };
@ -31,7 +29,7 @@ namespace ImageSharp.Tests
string filename = file.GetFileNameWithoutExtension(bitsPerPixel); string filename = file.GetFileNameWithoutExtension(bitsPerPixel);
using (Image image = file.CreateImage()) using (Image image = file.CreateImage())
{ {
image.Save($"{path}/{filename}.bmp", new BmpEncoder { BitsPerPixel = bitsPerPixel }); image.Save($"{path}/{filename}.bmp", new BmpEncoderOptions { BitsPerPixel = bitsPerPixel });
} }
} }
} }

Loading…
Cancel
Save