Browse Source

publish non-generic QuantizeProcessor

af/merge-core
Anton Firszov 7 years ago
parent
commit
fefb0e66a4
  1. 1
      ImageSharp.sln.DotSettings
  2. 41
      src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor.cs
  3. 57
      src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs
  4. 13
      src/ImageSharp/Processing/QuantizeExtensions.cs

1
ImageSharp.sln.DotSettings

@ -388,4 +388,5 @@
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002ECSharpPlaceAttributeOnSameLineMigration/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateBlankLinesAroundFieldToBlankLinesAroundProperty/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/Environment/SettingsMigration/IsMigratorApplied/=JetBrains_002EReSharper_002EPsi_002ECSharp_002ECodeStyle_002ESettingsUpgrade_002EMigrateThisQualifierSettings/@EntryIndexedValue">True</s:Boolean>
<s:Boolean x:Key="/Default/UserDictionary/Words/=Quantizer/@EntryIndexedValue">True</s:Boolean>
</wpf:ResourceDictionary>

41
src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor.cs

@ -1,59 +1,34 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{
/// <summary>
/// Enables the quantization of images to reduce the number of colors used in the image palette.
/// Defines quantization processing for images to reduce the number of colors used in the image palette.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class QuantizeProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
public class QuantizeProcessor : IImageProcessor
{
/// <summary>
/// Initializes a new instance of the <see cref="QuantizeProcessor{TPixel}"/> class.
/// Initializes a new instance of the <see cref="QuantizeProcessor"/> class.
/// </summary>
/// <param name="quantizer">The quantizer used to reduce the color palette</param>
/// <param name="quantizer">The quantizer used to reduce the color palette.</param>
public QuantizeProcessor(IQuantizer quantizer)
{
Guard.NotNull(quantizer, nameof(quantizer));
this.Quantizer = quantizer;
}
/// <summary>
/// Gets the quantizer
/// Gets the quantizer.
/// </summary>
public IQuantizer Quantizer { get; }
/// <inheritdoc />
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
public IImageProcessor<TPixel> CreatePixelSpecificProcessor<TPixel>()
where TPixel : struct, IPixel<TPixel>
{
using (IFrameQuantizer<TPixel> executor = this.Quantizer.CreateFrameQuantizer<TPixel>(configuration))
using (QuantizedFrame<TPixel> quantized = executor.QuantizeFrame(source))
{
int paletteCount = quantized.Palette.Length - 1;
// Not parallel to remove "quantized" closure allocation.
// We can operate directly on the source here as we've already read it to get the
// quantized result
for (int y = 0; y < source.Height; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y);
ReadOnlySpan<byte> quantizedPixelSpan = quantized.GetPixelSpan();
int yy = y * source.Width;
for (int x = 0; x < source.Width; x++)
{
int i = x + yy;
row[x] = quantized.Palette[Math.Min(paletteCount, quantizedPixelSpan[i])];
}
}
}
return new QuantizeProcessor<TPixel>(this.Quantizer);
}
}
}

57
src/ImageSharp/Processing/Processors/Quantization/QuantizeProcessor{TPixel}.cs

@ -0,0 +1,57 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{
/// <summary>
/// Enables the quantization of images to reduce the number of colors used in the image palette.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
internal class QuantizeProcessor<TPixel> : ImageProcessor<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly IQuantizer quantizer;
/// <summary>
/// Initializes a new instance of the <see cref="QuantizeProcessor{TPixel}"/> class.
/// </summary>
/// <param name="quantizer">The quantizer used to reduce the color palette.</param>
public QuantizeProcessor(IQuantizer quantizer)
{
Guard.NotNull(quantizer, nameof(quantizer));
this.quantizer = quantizer;
}
/// <inheritdoc />
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
{
using (IFrameQuantizer<TPixel> executor = this.quantizer.CreateFrameQuantizer<TPixel>(configuration))
using (QuantizedFrame<TPixel> quantized = executor.QuantizeFrame(source))
{
int paletteCount = quantized.Palette.Length - 1;
// Not parallel to remove "quantized" closure allocation.
// We can operate directly on the source here as we've already read it to get the
// quantized result
for (int y = 0; y < source.Height; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y);
ReadOnlySpan<byte> quantizedPixelSpan = quantized.GetPixelSpan();
int yy = y * source.Width;
for (int x = 0; x < source.Width; x++)
{
int i = x + yy;
row[x] = quantized.Palette[Math.Min(paletteCount, quantizedPixelSpan[i])];
}
}
}
}
}
}

13
src/ImageSharp/Processing/QuantizeExtensions.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Quantization;
namespace SixLabors.ImageSharp.Processing
@ -15,22 +14,18 @@ namespace SixLabors.ImageSharp.Processing
/// <summary>
/// Applies quantization to the image using the <see cref="OctreeQuantizer"/>.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext<TPixel> Quantize<TPixel>(this IImageProcessingContext<TPixel> source)
where TPixel : struct, IPixel<TPixel>
=> Quantize(source, KnownQuantizers.Octree);
public static IImageProcessingContext Quantize(this IImageProcessingContext source) =>
Quantize(source, KnownQuantizers.Octree);
/// <summary>
/// Applies quantization to the image.
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <param name="source">The image this method extends.</param>
/// <param name="quantizer">The quantizer to apply to perform the operation.</param>
/// <returns>The <see cref="IImageProcessingContext"/> to allow chaining of operations.</returns>
public static IImageProcessingContext<TPixel> Quantize<TPixel>(this IImageProcessingContext<TPixel> source, IQuantizer quantizer)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new QuantizeProcessor<TPixel>(quantizer));
public static IImageProcessingContext Quantize(this IImageProcessingContext source, IQuantizer quantizer) =>
source.ApplyProcessor(new QuantizeProcessor(quantizer));
}
}
Loading…
Cancel
Save