mirror of https://github.com/SixLabors/ImageSharp
87 changed files with 573 additions and 303 deletions
@ -0,0 +1,38 @@ |
|||
// <copyright file="OrderedDither4x4.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Dithering.Ordered |
|||
{ |
|||
/// <summary>
|
|||
/// The base class for performing ordered ditheroing using a 4x4 matrix.
|
|||
/// </summary>
|
|||
public abstract class OrderedDither4x4 : IOrderedDither |
|||
{ |
|||
/// <summary>
|
|||
/// The dithering matrix
|
|||
/// </summary>
|
|||
private Fast2DArray<byte> matrix; |
|||
|
|||
/// <summary>
|
|||
/// Initializes a new instance of the <see cref="OrderedDither4x4"/> class.
|
|||
/// </summary>
|
|||
/// <param name="matrix">The thresholding matrix. </param>
|
|||
internal OrderedDither4x4(Fast2DArray<byte> matrix) |
|||
{ |
|||
this.matrix = matrix; |
|||
} |
|||
|
|||
/// <inheritdoc />
|
|||
public void Dither<TColor>(PixelAccessor<TColor> pixels, TColor source, TColor upper, TColor lower, byte[] bytes, int index, int x, int y, int width, int height) |
|||
where TColor : struct, IPixel<TColor> |
|||
{ |
|||
// TODO: This doesn't really cut it for me.
|
|||
// I'd rather be using float but we need to add some sort of movalization vector methods to all IPixel implementations
|
|||
// before we can do that as the vectors all cover different ranges.
|
|||
source.ToXyzwBytes(bytes, 0); |
|||
pixels[x, y] = this.matrix[y % 3, x % 3] >= bytes[index] ? lower : upper; |
|||
} |
|||
} |
|||
} |
|||
@ -0,0 +1,104 @@ |
|||
// <copyright file="EncodeIndexedPng.cs" company="James Jackson-South">
|
|||
// Copyright (c) James Jackson-South and contributors.
|
|||
// Licensed under the Apache License, Version 2.0.
|
|||
// </copyright>
|
|||
|
|||
namespace ImageSharp.Benchmarks.Image |
|||
{ |
|||
using System.IO; |
|||
|
|||
using BenchmarkDotNet.Attributes; |
|||
|
|||
using ImageSharp; |
|||
using ImageSharp.Formats; |
|||
using ImageSharp.Quantizers; |
|||
|
|||
/// <summary>
|
|||
/// Benchmarks saving png files using different quantizers. System.Drawing cannot save indexed png files so we cannot compare.
|
|||
/// </summary>
|
|||
public class EncodeIndexedPng : BenchmarkBase |
|||
{ |
|||
// System.Drawing needs this.
|
|||
private Stream bmpStream; |
|||
private Image bmpCore; |
|||
|
|||
[Params(false)] |
|||
public bool LargeImage { get; set; } |
|||
|
|||
[Setup] |
|||
public void ReadImages() |
|||
{ |
|||
if (this.bmpStream == null) |
|||
{ |
|||
string path = this.LargeImage |
|||
? "../ImageSharp.Tests/TestImages/Formats/Jpg/baseline/jpeg420exif.jpg" |
|||
: "../ImageSharp.Tests/TestImages/Formats/Bmp/Car.bmp"; |
|||
this.bmpStream = File.OpenRead(path); |
|||
this.bmpCore = new Image(this.bmpStream); |
|||
this.bmpStream.Position = 0; |
|||
} |
|||
} |
|||
|
|||
[Cleanup] |
|||
public void Cleanup() |
|||
{ |
|||
this.bmpStream.Dispose(); |
|||
this.bmpCore.Dispose(); |
|||
} |
|||
|
|||
[Benchmark(Baseline = true, Description = "ImageSharp Octree Png")] |
|||
public void PngCoreOctree() |
|||
{ |
|||
using (MemoryStream memoryStream = new MemoryStream()) |
|||
{ |
|||
PngEncoderOptions options = new PngEncoderOptions() { Quantizer = new OctreeQuantizer<Color>(), Quality = 256 }; |
|||
|
|||
this.bmpCore.SaveAsPng(memoryStream, options); |
|||
} |
|||
} |
|||
|
|||
[Benchmark(Description = "ImageSharp Octree NoDither Png")] |
|||
public void PngCoreOctreeNoDIther() |
|||
{ |
|||
using (MemoryStream memoryStream = new MemoryStream()) |
|||
{ |
|||
PngEncoderOptions options = new PngEncoderOptions { Quantizer = new OctreeQuantizer<Color> { Dither = false }, Quality = 256 }; |
|||
|
|||
this.bmpCore.SaveAsPng(memoryStream, options); |
|||
} |
|||
} |
|||
|
|||
[Benchmark(Description = "ImageSharp Palette Png")] |
|||
public void PngCorePalette() |
|||
{ |
|||
using (MemoryStream memoryStream = new MemoryStream()) |
|||
{ |
|||
PngEncoderOptions options = new PngEncoderOptions { Quantizer = new PaletteQuantizer<Color>(), Quality = 256 }; |
|||
|
|||
this.bmpCore.SaveAsPng(memoryStream, options); |
|||
} |
|||
} |
|||
|
|||
[Benchmark(Description = "ImageSharp Palette NoDither Png")] |
|||
public void PngCorePaletteNoDither() |
|||
{ |
|||
using (MemoryStream memoryStream = new MemoryStream()) |
|||
{ |
|||
PngEncoderOptions options = new PngEncoderOptions { Quantizer = new PaletteQuantizer<Color> { Dither = false }, Quality = 256 }; |
|||
|
|||
this.bmpCore.SaveAsPng(memoryStream, options); |
|||
} |
|||
} |
|||
|
|||
[Benchmark(Description = "ImageSharp Wu Png")] |
|||
public void PngCoreWu() |
|||
{ |
|||
using (MemoryStream memoryStream = new MemoryStream()) |
|||
{ |
|||
PngEncoderOptions options = new PngEncoderOptions() { Quantizer = new WuQuantizer<Color>(), Quality = 256 }; |
|||
|
|||
this.bmpCore.SaveAsPng(memoryStream, options); |
|||
} |
|||
} |
|||
} |
|||
} |
|||
Loading…
Reference in new issue