From 350c4e7ed99ac52fb3bbc735601f05dda8816331 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 26 Jun 2018 14:40:01 +1000 Subject: [PATCH] Use single cache base. --- src/ImageSharp/Formats/Gif/LzwEncoder.cs | 2 +- .../Processors/PaletteDitherProcessorBase.cs | 1 + .../FrameQuantizerBase{TPixel}.cs | 28 +++++++++------ .../OctreeFrameQuantizer{TPixel}.cs | 34 ++++++++++++++----- .../PaletteFrameQuantizer{TPixel}.cs | 9 ++--- .../WuFrameQuantizer{TPixel}.cs | 8 +---- 6 files changed, 47 insertions(+), 35 deletions(-) diff --git a/src/ImageSharp/Formats/Gif/LzwEncoder.cs b/src/ImageSharp/Formats/Gif/LzwEncoder.cs index 2ec569781..347609a54 100644 --- a/src/ImageSharp/Formats/Gif/LzwEncoder.cs +++ b/src/ImageSharp/Formats/Gif/LzwEncoder.cs @@ -374,7 +374,7 @@ namespace SixLabors.ImageSharp.Formats.Gif [MethodImpl(MethodImplOptions.AggressiveInlining)] private int NextPixel(Span indexedPixels) { - return indexedPixels[this.position++] & 0xff; + return indexedPixels[this.position++] & 0xFF; } /// diff --git a/src/ImageSharp/Processing/Dithering/Processors/PaletteDitherProcessorBase.cs b/src/ImageSharp/Processing/Dithering/Processors/PaletteDitherProcessorBase.cs index 9317a6aad..c475e5d6a 100644 --- a/src/ImageSharp/Processing/Dithering/Processors/PaletteDitherProcessorBase.cs +++ b/src/ImageSharp/Processing/Dithering/Processors/PaletteDitherProcessorBase.cs @@ -12,6 +12,7 @@ namespace SixLabors.ImageSharp.Processing.Dithering.Processors /// /// The base class for dither and diffusion processors that consume a palette. /// + /// The pixel format. internal abstract class PaletteDitherProcessorBase : ImageProcessor where TPixel : struct, IPixel { diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs index 0e764b108..b2c7436ae 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/FrameQuantizerBase{TPixel}.cs @@ -17,6 +17,11 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers public abstract class FrameQuantizerBase : IFrameQuantizer where TPixel : struct, IPixel { + /// + /// A lookup table for colors + /// + private readonly Dictionary distanceCache = new Dictionary(); + /// /// Flag used to indicate whether a single pass or two passes are needed for quantization. /// @@ -119,21 +124,21 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// The color. /// The color palette. - /// The cache to store the result in. - /// The + /// The [MethodImpl(MethodImplOptions.AggressiveInlining)] - protected byte GetClosestPixel(TPixel pixel, TPixel[] colorPalette, Dictionary cache) + protected byte GetClosestPixel(TPixel pixel, TPixel[] colorPalette) { // Check if the color is in the lookup table - // if (cache.TryGetValue(pixel, out byte value)) - // { - // return value; - // } - return this.GetClosestPixelSlow(pixel, colorPalette, cache); + if (this.distanceCache.TryGetValue(pixel, out byte value)) + { + return value; + } + + return this.GetClosestPixelSlow(pixel, colorPalette); } [MethodImpl(MethodImplOptions.NoInlining)] - private byte GetClosestPixelSlow(TPixel pixel, TPixel[] colorPalette, Dictionary cache) + private byte GetClosestPixelSlow(TPixel pixel, TPixel[] colorPalette) { // Loop through the palette and find the nearest match. int colorIndex = 0; @@ -159,8 +164,9 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers } // Now I have the index, pop it into the cache for next time - // cache.Add(pixel, colorIndex); - return (byte)colorIndex; + byte result = (byte)colorIndex; + this.distanceCache.Add(pixel, result); + return result; } } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs index ea30e3f35..e9c37ef96 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/OctreeFrameQuantizer{TPixel}.cs @@ -29,11 +29,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// private readonly Octree octree; - /// - /// A lookup table for colors - /// - private Dictionary colorMap = new Dictionary(); - /// /// The reduced image palette /// @@ -182,7 +177,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers { // The colors have changed so we need to use Euclidean distance calculation to find the closest value. // This palette can never be null here. - return this.GetClosestPixel(pixel, this.palette, this.colorMap); + return this.GetClosestPixel(pixel, this.palette); } pixel.ToRgba32(ref rgba); @@ -258,12 +253,23 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// Gets or sets the number of leaves in the tree /// - public int Leaves { get; set; } + public int Leaves + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get; + + [MethodImpl(MethodImplOptions.AggressiveInlining)] + set; + } /// /// Gets the array of reducible nodes /// - private OctreeNode[] ReducibleNodes { get; } + private OctreeNode[] ReducibleNodes + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get; + } /// /// Add a given color value to the Octree @@ -302,6 +308,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// An with the palletized colors /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public TPixel[] Palletize(int colorCount) { while (this.Leaves > colorCount) @@ -327,6 +334,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// The . /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] public int GetPaletteIndex(ref TPixel pixel, ref Rgba32 rgba) { return this.root.GetPaletteIndex(ref pixel, 0, ref rgba); @@ -338,6 +346,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// The node last quantized /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] protected void TrackPrevious(OctreeNode node) { this.previousNode = node; @@ -446,7 +455,11 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// Gets the next reducible node /// - public OctreeNode NextReducible { get; } + public OctreeNode NextReducible + { + [MethodImpl(MethodImplOptions.AggressiveInlining)] + get; + } /// /// Add a color into the tree @@ -525,6 +538,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// The palette /// The current palette index + [MethodImpl(MethodImplOptions.NoInlining)] public void ConstructPalette(TPixel[] palette, ref int index) { if (this.leaf) @@ -557,6 +571,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// The representing the index of the pixel in the palette. /// + [MethodImpl(MethodImplOptions.NoInlining)] public int GetPaletteIndex(ref TPixel pixel, int level, ref Rgba32 rgba) { int index = this.paletteIndex; @@ -589,6 +604,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// /// The pixel to add. /// The color to map to. + [MethodImpl(MethodImplOptions.AggressiveInlining)] public void Increment(ref TPixel pixel, ref Rgba32 rgba) { pixel.ToRgba32(ref rgba); diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs index 14f4b1c39..7108f0fbd 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/PaletteFrameQuantizer{TPixel}.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. using System; -using System.Collections.Generic; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; using SixLabors.ImageSharp.Advanced; @@ -18,11 +17,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers internal sealed class PaletteFrameQuantizer : FrameQuantizerBase where TPixel : struct, IPixel { - /// - /// A lookup table for colors - /// - private readonly Dictionary colorMap = new Dictionary(); - /// /// List of all colors in the palette. /// @@ -36,6 +30,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers public PaletteFrameQuantizer(PaletteQuantizer quantizer, TPixel[] colors) : base(quantizer, true) { + Guard.MustBeLessThanOrEqualTo(256, colors.Length, "Maximum color count must be 256."); this.colors = colors; } @@ -99,6 +94,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// The quantized value /// [MethodImpl(MethodImplOptions.AggressiveInlining)] - private byte QuantizePixel(TPixel pixel) => this.GetClosestPixel(pixel, this.GetPalette(), this.colorMap); + private byte QuantizePixel(TPixel pixel) => this.GetClosestPixel(pixel, this.GetPalette()); } } \ No newline at end of file diff --git a/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs b/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs index 154263959..3cf965815 100644 --- a/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs +++ b/src/ImageSharp/Processing/Quantization/FrameQuantizers/WuFrameQuantizer{TPixel}.cs @@ -68,11 +68,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers /// private const int TableLength = IndexCount * IndexCount * IndexCount * IndexAlphaCount; - /// - /// A lookup table for colors - /// - private readonly Dictionary colorMap = new Dictionary(); - /// /// Moment of P(c). /// @@ -480,7 +475,6 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers using (IBuffer volumeB = memoryAllocator.Allocate(IndexCount * IndexAlphaCount)) using (IBuffer volumeA = memoryAllocator.Allocate(IndexCount * IndexAlphaCount)) using (IBuffer volume2 = memoryAllocator.Allocate(IndexCount * IndexAlphaCount)) - using (IBuffer area = memoryAllocator.Allocate(IndexAlphaCount)) using (IBuffer areaR = memoryAllocator.Allocate(IndexAlphaCount)) using (IBuffer areaG = memoryAllocator.Allocate(IndexAlphaCount)) @@ -855,7 +849,7 @@ namespace SixLabors.ImageSharp.Processing.Quantization.FrameQuantizers { // The colors have changed so we need to use Euclidean distance calculation to find the closest value. // This palette can never be null here. - return this.GetClosestPixel(pixel, this.palette, this.colorMap); + return this.GetClosestPixel(pixel, this.palette); } // Expected order r->g->b->a