|
|
|
@ -14,6 +14,11 @@ namespace ImageProcessor.Formats |
|
|
|
/// <remarks>The encoder can currently only write 24-bit rgb images to streams.</remarks>
|
|
|
|
public class BmpEncoder : IImageEncoder |
|
|
|
{ |
|
|
|
/// <summary>
|
|
|
|
/// The the transparency threshold.
|
|
|
|
/// </summary>
|
|
|
|
private int threshold = 128; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the quality of output for images.
|
|
|
|
/// </summary>
|
|
|
|
@ -26,6 +31,15 @@ namespace ImageProcessor.Formats |
|
|
|
/// <inheritdoc/>
|
|
|
|
public string Extension => "bmp"; |
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Gets or sets the transparency threshold.
|
|
|
|
/// </summary>
|
|
|
|
public int Threshold |
|
|
|
{ |
|
|
|
get { return this.threshold; } |
|
|
|
set { this.threshold = value.Clamp(0, 255); } |
|
|
|
} |
|
|
|
|
|
|
|
/// <inheritdoc/>
|
|
|
|
public bool IsSupportedFileExtension(string extension) |
|
|
|
{ |
|
|
|
@ -77,7 +91,7 @@ namespace ImageProcessor.Formats |
|
|
|
|
|
|
|
WriteInfo(writer, infoHeader); |
|
|
|
|
|
|
|
WriteImage(writer, image); |
|
|
|
this.WriteImage(writer, image); |
|
|
|
|
|
|
|
writer.Flush(); |
|
|
|
} |
|
|
|
@ -91,7 +105,7 @@ namespace ImageProcessor.Formats |
|
|
|
/// <param name="image">
|
|
|
|
/// The <see cref="ImageBase"/> containing pixel data.
|
|
|
|
/// </param>
|
|
|
|
private static void WriteImage(BinaryWriter writer, ImageBase image) |
|
|
|
private void WriteImage(BinaryWriter writer, ImageBase image) |
|
|
|
{ |
|
|
|
// TODO: Add more compression formats.
|
|
|
|
int amount = (image.Width * 3) % 4; |
|
|
|
@ -119,6 +133,11 @@ namespace ImageProcessor.Formats |
|
|
|
// Implicit cast to Bgra32 handles premultiplication conversion.
|
|
|
|
Bgra32 color = new Color(r, g, b, a); |
|
|
|
|
|
|
|
if (color.A < this.Threshold) |
|
|
|
{ |
|
|
|
color = new Bgra32(0, 0, 0, 0); |
|
|
|
} |
|
|
|
|
|
|
|
writer.Write(color.B); |
|
|
|
writer.Write(color.G); |
|
|
|
writer.Write(color.R); |
|
|
|
|