Browse Source

Implemented the IEncoderOptions inside the bmp encoder.

pull/22/merge
Dirk Lemstra 9 years ago
committed by Dirk Lemstra
parent
commit
19c81c5d8e
  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>
public class BmpEncoder : IImageEncoder
{
/// <summary>
/// Gets or sets the number of bits per pixel.
/// </summary>
public BmpBitsPerPixel BitsPerPixel { get; set; } = BmpBitsPerPixel.Pixel24;
/// <inheritdoc/>
public void Encode<TColor>(Image<TColor> image, Stream stream, IEncoderOptions options)
where TColor : struct, IPixel<TColor>
{
BmpEncoderCore encoder = new BmpEncoderCore();
encoder.Encode(image, stream, this.BitsPerPixel);
IBmpEncoderOptions bmpOptions = BmpEncoderOptions.Create(options);
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
{
/// <summary>
/// The number of bits per pixel.
/// The options for the encoder.
/// </summary>
private BmpBitsPerPixel bmpBitsPerPixel;
private IBmpEncoderOptions options;
/// <summary>
/// The amount to pad each row by.
/// </summary>
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>
/// Encodes the image to the specified stream from the <see cref="ImageBase{TColor}"/>.
/// </summary>
/// <typeparam name="TColor">The pixel format.</typeparam>
/// <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="bitsPerPixel">The <see cref="BmpBitsPerPixel"/></param>
public void Encode<TColor>(ImageBase<TColor> image, Stream stream, BmpBitsPerPixel bitsPerPixel)
public void Encode<TColor>(ImageBase<TColor> image, Stream stream)
where TColor : struct, IPixel<TColor>
{
Guard.NotNull(image, nameof(image));
Guard.NotNull(stream, nameof(stream));
this.bmpBitsPerPixel = bitsPerPixel;
// 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);
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.
EndianBinaryWriter writer = new EndianBinaryWriter(Endianness.LittleEndian, stream);
@ -128,7 +134,7 @@ namespace ImageSharp.Formats
{
using (PixelAccessor<TColor> pixels = image.Lock())
{
switch (this.bmpBitsPerPixel)
switch (this.options.BitsPerPixel)
{
case BmpBitsPerPixel.Pixel32:
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
{
using System.IO;
using Xunit;
public class BitmapTests : FileTestBase
@ -16,7 +14,7 @@ namespace ImageSharp.Tests
public static readonly TheoryData<BmpBitsPerPixel> BitsPerPixel
= new TheoryData<BmpBitsPerPixel>
{
BmpBitsPerPixel.Pixel24 ,
BmpBitsPerPixel.Pixel24,
BmpBitsPerPixel.Pixel32
};
@ -31,7 +29,7 @@ namespace ImageSharp.Tests
string filename = file.GetFileNameWithoutExtension(bitsPerPixel);
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