Browse Source

Review changes

af/merge-core
Brian Popow 7 years ago
parent
commit
7b81ea5e6b
  1. 7
      src/ImageSharp/Formats/Bmp/BmpEncoder.cs
  2. 61
      src/ImageSharp/Formats/Bmp/BmpEncoderCore.cs
  3. 9
      src/ImageSharp/Formats/Bmp/IBmpEncoderOptions.cs

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

@ -4,6 +4,7 @@
using System.IO;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Formats.Bmp
{
@ -25,6 +26,12 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// </summary>
public bool SupportTransparency { get; set; }
/// <summary>
/// Gets or sets the quantizer for reducing the color count for 8-Bit images.
/// Defaults to OctreeQuantizer.
/// </summary>
public IQuantizer Quantizer { get; set; }
/// <inheritdoc/>
public void Encode<TPixel>(Image<TPixel> image, Stream stream)
where TPixel : struct, IPixel<TPixel>

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

@ -2,6 +2,7 @@
// Licensed under the Apache License, Version 2.0.
using System;
using System.Buffers;
using System.IO;
using SixLabors.ImageSharp.Advanced;
@ -62,6 +63,11 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// </summary>
private readonly bool writeV4Header;
/// <summary>
/// The quantizer for reducing the color count for 8-Bit images.
/// </summary>
private readonly IQuantizer quantizer;
/// <summary>
/// Initializes a new instance of the <see cref="BmpEncoderCore"/> class.
/// </summary>
@ -72,6 +78,7 @@ namespace SixLabors.ImageSharp.Formats.Bmp
this.memoryAllocator = memoryAllocator;
this.bitsPerPixel = options.BitsPerPixel;
this.writeV4Header = options.SupportTransparency;
this.quantizer = options.Quantizer ?? new OctreeQuantizer(dither: true, maxColors: 256);
}
/// <summary>
@ -298,39 +305,35 @@ namespace SixLabors.ImageSharp.Formats.Bmp
private void Write8Bit<TPixel>(Stream stream, ImageFrame<TPixel> image)
where TPixel : struct, IPixel<TPixel>
{
#if NETCOREAPP2_1
Span<byte> colorPalette = stackalloc byte[ColorPaletteSize8Bit];
#else
byte[] colorPalette = new byte[ColorPaletteSize8Bit];
#endif
var quantizer = new OctreeQuantizer(dither: true, maxColors: 256);
QuantizedFrame<TPixel> quantized = quantizer.CreateFrameQuantizer<TPixel>(this.configuration).QuantizeFrame(image);
int idx = 0;
var color = default(Rgba32);
foreach (TPixel quantizedColor in quantized.Palette)
using (IMemoryOwner<byte> colorPaletteBuffer = this.memoryAllocator.AllocateManagedByteBuffer(ColorPaletteSize8Bit, AllocationOptions.Clean))
using (QuantizedFrame<TPixel> quantized = this.quantizer.CreateFrameQuantizer<TPixel>(this.configuration, 256).QuantizeFrame(image))
{
quantizedColor.ToRgba32(ref color);
colorPalette[idx] = color.B;
colorPalette[idx + 1] = color.G;
colorPalette[idx + 2] = color.R;
// Padding byte, always 0
colorPalette[idx + 3] = 0;
idx += 4;
}
stream.Write(colorPalette, 0, ColorPaletteSize8Bit);
Span<byte> colorPalette = colorPaletteBuffer.GetSpan();
int idx = 0;
var color = default(Rgba32);
foreach (TPixel quantizedColor in quantized.Palette)
{
quantizedColor.ToRgba32(ref color);
colorPalette[idx] = color.B;
colorPalette[idx + 1] = color.G;
colorPalette[idx + 2] = color.R;
// Padding byte, always 0
colorPalette[idx + 3] = 0;
idx += 4;
}
for (int y = image.Height - 1; y >= 0; y--)
{
Span<byte> pixelSpan = quantized.GetRowSpan(y);
stream.Write(pixelSpan);
stream.Write(colorPalette);
for (int i = 0; i < this.padding; i++)
for (int y = image.Height - 1; y >= 0; y--)
{
stream.WriteByte(0);
Span<byte> pixelSpan = quantized.GetRowSpan(y);
stream.Write(pixelSpan);
for (int i = 0; i < this.padding; i++)
{
stream.WriteByte(0);
}
}
}
}

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

@ -1,10 +1,12 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Formats.Bmp
{
/// <summary>
/// Configuration options for use during bmp encoding
/// Configuration options for use during bmp encoding.
/// </summary>
internal interface IBmpEncoderOptions
{
@ -20,5 +22,10 @@ namespace SixLabors.ImageSharp.Formats.Bmp
/// Instead a bitmap version 4 info header will be written with the BITFIELDS compression.
/// </summary>
bool SupportTransparency { get; }
/// <summary>
/// Gets the quantizer for reducing the color count for 8-Bit images.
/// </summary>
IQuantizer Quantizer { get; }
}
}
Loading…
Cancel
Save