Browse Source

Better transparency handling in encoders

Former-commit-id: 4ac66346af234c105628b588a84a4cb746c3e6ad
Former-commit-id: 3c249d1fb3e2860e3df77b1cb8e30bd60fda1db4
Former-commit-id: fb192a7e2fdaedf1b75173c8283b44642614d81d
af/merge-core
James Jackson-South 10 years ago
parent
commit
47e4f66d9e
  1. 10
      src/ImageProcessor/Colors/Color.cs
  2. 10
      src/ImageProcessor/Filters/BackgroundColor.cs
  3. 23
      src/ImageProcessor/Formats/Bmp/BmpEncoder.cs
  4. 16
      src/ImageProcessor/Formats/Gif/GifEncoder.cs
  5. 11
      src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs
  6. 5
      src/ImageProcessor/Formats/IImageEncoder.cs
  7. 21
      src/ImageProcessor/Formats/Jpg/JpegEncoder.cs
  8. 19
      src/ImageProcessor/Formats/Png/PngEncoder.cs
  9. 14
      src/ImageProcessor/Samplers/Resampler.cs
  10. 2
      tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs

10
src/ImageProcessor/Colors/Color.cs

@ -473,13 +473,9 @@ namespace ImageProcessor
public static Color Lerp(Color from, Color to, float amount)
{
amount = amount.Clamp(0f, 1f);
if (to.A < 1)
{
return (from * (1 - amount)) + to;
}
return (from * (1 - amount)) + (to * amount);
// Premultiplied.
return (from * (1 - amount)) + to;
}
/// <summary>

10
src/ImageProcessor/Filters/BackgroundColor.cs

@ -1,4 +1,4 @@
// <copyright file="Blend.cs" company="James Jackson-South">
// <copyright file="BackgroundColor.cs" company="James Jackson-South">
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
// </copyright>
@ -13,7 +13,7 @@ namespace ImageProcessor.Filters
public class BackgroundColor : ParallelImageProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="Blend"/> class.
/// Initializes a new instance of the <see cref="BackgroundColor"/> class.
/// </summary>
/// <param name="color">The <see cref="Color"/> to set the background color to.</param>
public BackgroundColor(Color color)
@ -46,7 +46,11 @@ namespace ImageProcessor.Filters
{
Color color = source[x, y];
if (color.A < 1)
if (color == Color.Empty)
{
color = backgroundColor;
}
else if (color.A < 1)
{
color = Color.Lerp(color, backgroundColor, .5f);
}

23
src/ImageProcessor/Formats/Bmp/BmpEncoder.cs

@ -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);

16
src/ImageProcessor/Formats/Gif/GifEncoder.cs

@ -14,6 +14,11 @@ namespace ImageProcessor.Formats
/// </summary>
public class GifEncoder : IImageEncoder
{
/// <summary>
/// The the transparency threshold.
/// </summary>
private int threshold = 128;
/// <summary>
/// Gets or sets the quality of output for images.
/// </summary>
@ -25,6 +30,15 @@ namespace ImageProcessor.Formats
/// <inheritdoc/>
public string MimeType => "image/gif";
/// <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)
@ -123,7 +137,7 @@ namespace ImageProcessor.Formats
private QuantizedImage WriteColorTable(ImageBase image, Stream stream, int quality, int bitDepth)
{
// Quantize the image returning a pallete.
IQuantizer quantizer = new OctreeQuantizer(quality.Clamp(1, 255), bitDepth);
IQuantizer quantizer = new OctreeQuantizer(quality.Clamp(1, 255), bitDepth) {Threshold = this.threshold};
QuantizedImage quantizedImage = quantizer.Quantize(image);
// Grab the pallete and write it to the stream.

11
src/ImageProcessor/Formats/Gif/Quantizer/OctreeQuantizer.cs

@ -24,6 +24,11 @@ namespace ImageProcessor.Formats
/// </summary>
private readonly int maxColors;
/// <summary>
/// The the transparency threshold.
/// </summary>
private int threshold = 128;
/// <summary>
/// Initializes a new instance of the <see cref="OctreeQuantizer"/> class.
/// </summary>
@ -63,7 +68,11 @@ namespace ImageProcessor.Formats
/// <summary>
/// Gets or sets the transparency threshold.
/// </summary>
public byte Threshold { get; set; } = 128;
public int Threshold
{
get { return this.threshold; }
set { this.threshold = value.Clamp(0, 255); }
}
/// <summary>
/// Process the pixel in the first pass of the algorithm

5
src/ImageProcessor/Formats/IImageEncoder.cs

@ -22,6 +22,11 @@ namespace ImageProcessor.Formats
/// </summary>
int Quality { get; set; }
/// <summary>
/// Gets or sets the transparency threshold.
/// </summary>
int Threshold { get; set; }
/// <summary>
/// Gets the standard identifier used on the Internet to indicate the type of data that a file contains.
/// </summary>

21
src/ImageProcessor/Formats/Jpg/JpegEncoder.cs

@ -17,10 +17,15 @@ namespace ImageProcessor.Formats
public class JpegEncoder : IImageEncoder
{
/// <summary>
/// The jpeg quality.
/// The quality.
/// </summary>
private int quality = 100;
/// <summary>
/// The the transparency threshold.
/// </summary>
private int threshold = 128;
/// <summary>
/// Gets or sets the quality, that will be used to encode the image. Quality
/// index must be between 0 and 100 (compression from max to min).
@ -32,6 +37,15 @@ namespace ImageProcessor.Formats
set { this.quality = value.Clamp(1, 100); }
}
/// <summary>
/// Gets or sets the transparency threshold.
/// </summary>
public int Threshold
{
get { return this.threshold; }
set { this.threshold = value.Clamp(0, 255); }
}
/// <inheritdoc/>
public string MimeType => "image/jpeg";
@ -114,6 +128,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);
}
samples[start] = color.R;
samples[start + 1] = color.G;
samples[start + 2] = color.B;

19
src/ImageProcessor/Formats/Png/PngEncoder.cs

@ -18,6 +18,11 @@ namespace ImageProcessor.Formats
/// </summary>
private const int MaxBlockSize = 0xFFFF;
/// <summary>
/// The the transparency threshold.
/// </summary>
private int threshold;
/// <summary>
/// Initializes a new instance of the <see cref="PngEncoder"/> class.
/// </summary>
@ -32,6 +37,15 @@ namespace ImageProcessor.Formats
/// <remarks>Png is a lossless format so this is not used in this encoder.</remarks>
public int Quality { get; set; }
/// <summary>
/// Gets or sets the transparency threshold.
/// </summary>
public int Threshold
{
get { return this.threshold; }
set { this.threshold = value.Clamp(0, 255); }
}
/// <inheritdoc/>
public string MimeType => "image/png";
@ -329,6 +343,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);
}
data[dataOffset] = color.R;
data[dataOffset + 1] = color.G;
data[dataOffset + 2] = color.B;

14
src/ImageProcessor/Samplers/Resampler.cs

@ -157,15 +157,15 @@ namespace ImageProcessor.Samplers
// Restrict alpha values in an attempt to prevent bleed.
// This is a baaaaaaad hack!!!
if (destination.A <= 0.03)
{
destination = Color.Empty;
}
else
{
//if (destination.A <= 0.03)
//{
// destination = Color.Empty;
//}
//else
//{
destination = Color.Compand(destination);
destination.A = (float)Math.Round(destination.A, 2);
}
//}
target[x, y] = destination;
}

2
tests/ImageProcessor.Tests/Processors/Samplers/SamplerTests.cs

@ -43,7 +43,7 @@ namespace ImageProcessor.Tests
{
Stopwatch watch = Stopwatch.StartNew();
Image image = new Image(stream);
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + ".jpg";
string filename = Path.GetFileNameWithoutExtension(file) + "-" + name + Path.GetExtension(file);
using (FileStream output = File.OpenWrite($"TestOutput/Resized/{filename}"))
{
image.Resize(image.Width / 2, image.Height / 2, sampler).Save(output);

Loading…
Cancel
Save