-
-
Notifications
You must be signed in to change notification settings - Fork 893
Expand file tree
/
Copy pathFrameQuantizer{TPixel}.cs
More file actions
308 lines (271 loc) · 12.6 KB
/
Copy pathFrameQuantizer{TPixel}.cs
File metadata and controls
308 lines (271 loc) · 12.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using System;
using System.Runtime.CompilerServices;
using SixLabors.ImageSharp.Advanced;
using SixLabors.ImageSharp.Memory;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors.Dithering;
namespace SixLabors.ImageSharp.Processing.Processors.Quantization
{
/// <summary>
/// The base class for all <see cref="IFrameQuantizer{TPixel}"/> implementations
/// </summary>
/// <typeparam name="TPixel">The pixel format.</typeparam>
public abstract class FrameQuantizer<TPixel> : IFrameQuantizer<TPixel>
where TPixel : struct, IPixel<TPixel>
{
private readonly bool singlePass;
private EuclideanPixelMap<TPixel> pixelMap;
private bool isDisposed;
/// <summary>
/// Initializes a new instance of the <see cref="FrameQuantizer{TPixel}"/> class.
/// </summary>
/// <param name="configuration">The configuration which allows altering default behaviour or extending the library.</param>
/// <param name="options">The quantizer options defining quantization rules.</param>
/// <param name="singlePass">
/// If <see langword="true"/>, the quantization process only needs to loop through the source pixels once.
/// </param>
/// <remarks>
/// If you construct this class with a <value>true</value> for <paramref name="singlePass"/>, then the code will
/// only call the <see cref="SecondPass(ImageFrame{TPixel}, Rectangle, Memory{byte}, ReadOnlyMemory{TPixel})"/> method.
/// If two passes are required, the code will also call <see cref="FirstPass(ImageFrame{TPixel}, Rectangle)"/>.
/// </remarks>
protected FrameQuantizer(Configuration configuration, QuantizerOptions options, bool singlePass)
{
Guard.NotNull(configuration, nameof(configuration));
Guard.NotNull(options, nameof(options));
this.Configuration = configuration;
this.Options = options;
this.IsDitheringQuantizer = options.Dither != null;
this.singlePass = singlePass;
}
/// <inheritdoc/>
public QuantizerOptions Options { get; }
/// <summary>
/// Gets the configuration which allows altering default behaviour or extending the library.
/// </summary>
protected Configuration Configuration { get; }
/// <summary>
/// Gets a value indicating whether the frame quantizer utilizes a dithering method.
/// </summary>
protected bool IsDitheringQuantizer { get; }
/// <inheritdoc/>
public void Dispose()
{
this.Dispose(true);
GC.SuppressFinalize(this);
}
/// <inheritdoc/>
public IQuantizedFrame<TPixel> QuantizeFrame(ImageFrame<TPixel> image, Rectangle bounds)
{
Guard.NotNull(image, nameof(image));
var interest = Rectangle.Intersect(image.Bounds(), bounds);
// Call the FirstPass function if not a single pass algorithm.
// For something like an Octree quantizer, this will run through
// all image pixels, build a data structure, and create a palette.
if (!this.singlePass)
{
this.FirstPass(image, interest);
}
// Collect the palette. Required before the second pass runs.
ReadOnlyMemory<TPixel> palette = this.GenerateQuantizedPalette();
MemoryAllocator memoryAllocator = this.Configuration.MemoryAllocator;
this.pixelMap = new EuclideanPixelMap<TPixel>(palette);
var quantizedFrame = new QuantizedFrame<TPixel>(memoryAllocator, interest.Width, interest.Height, palette);
Memory<byte> output = quantizedFrame.GetWritablePixelMemory();
if (this.Options.Dither is null)
{
this.SecondPass(image, interest, output, palette);
}
else
{
// We clone the image as we don't want to alter the original via error diffusion based dithering.
using (ImageFrame<TPixel> clone = image.Clone())
{
this.SecondPass(clone, interest, output, palette);
}
}
return quantizedFrame;
}
/// <summary>
/// Disposes the object and frees resources for the Garbage Collector.
/// </summary>
/// <param name="disposing">Whether to dispose managed and unmanaged objects.</param>
protected virtual void Dispose(bool disposing)
{
if (this.isDisposed)
{
return;
}
this.isDisposed = true;
}
/// <summary>
/// Execute the first pass through the pixels in the image to create the palette.
/// </summary>
/// <param name="source">The source data.</param>
/// <param name="bounds">The bounds within the source image to quantize.</param>
protected virtual void FirstPass(ImageFrame<TPixel> source, Rectangle bounds)
{
}
/// <summary>
/// Execute a second pass through the image to assign the pixels to a palette entry.
/// </summary>
/// <param name="source">The source image.</param>
/// <param name="bounds">The bounds within the source image to quantize.</param>
/// <param name="output">The output pixel array.</param>
/// <param name="palette">The output color palette.</param>
protected virtual void SecondPass(
ImageFrame<TPixel> source,
Rectangle bounds,
Memory<byte> output,
ReadOnlyMemory<TPixel> palette)
{
ReadOnlySpan<TPixel> paletteSpan = palette.Span;
IDither dither = this.Options.Dither;
if (dither is null)
{
var operation = new RowIntervalOperation(source, output, bounds, this, palette);
ParallelRowIterator.IterateRows(
this.Configuration,
bounds,
in operation);
return;
}
// Error diffusion.
// The difference between the source and transformed color is spread to neighboring pixels.
// TODO: Investigate parallel strategy.
Span<byte> outputSpan = output.Span;
int bitDepth = ImageMaths.GetBitsNeededForColorDepth(paletteSpan.Length);
if (dither.DitherType == DitherType.ErrorDiffusion)
{
float ditherScale = this.Options.DitherScale;
int width = bounds.Width;
int offsetY = bounds.Top;
int offsetX = bounds.Left;
for (int y = bounds.Top; y < bounds.Bottom; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y);
int rowStart = (y - offsetY) * width;
for (int x = bounds.Left; x < bounds.Right; x++)
{
TPixel sourcePixel = row[x];
outputSpan[rowStart + x - offsetX] = this.GetQuantizedColor(sourcePixel, paletteSpan, out TPixel transformed);
dither.Dither(source, bounds, sourcePixel, transformed, x, y, bitDepth, ditherScale);
}
}
return;
}
// Ordered dithering. We are only operating on a single pixel so we can work in parallel.
var ditherOperation = new DitherRowIntervalOperation(source, output, bounds, this, palette, bitDepth);
ParallelRowIterator.IterateRows(
this.Configuration,
bounds,
in ditherOperation);
}
/// <summary>
/// Returns the index and color from the quantized palette corresponding to the give to the given color.
/// </summary>
/// <param name="color">The color to match.</param>
/// <param name="palette">The output color palette.</param>
/// <param name="match">The matched color.</param>
/// <returns>The <see cref="byte"/> index.</returns>
[MethodImpl(InliningOptions.ShortMethod)]
protected virtual byte GetQuantizedColor(TPixel color, ReadOnlySpan<TPixel> palette, out TPixel match)
=> this.pixelMap.GetClosestColor(color, out match);
/// <summary>
/// Generates the palette for the quantized image.
/// </summary>
/// <returns>
/// <see cref="ReadOnlyMemory{TPixel}"/>
/// </returns>
protected abstract ReadOnlyMemory<TPixel> GenerateQuantizedPalette();
private readonly struct RowIntervalOperation : IRowIntervalOperation
{
private readonly ImageFrame<TPixel> source;
private readonly Memory<byte> output;
private readonly Rectangle bounds;
private readonly FrameQuantizer<TPixel> quantizer;
private readonly ReadOnlyMemory<TPixel> palette;
[MethodImpl(InliningOptions.ShortMethod)]
public RowIntervalOperation(
ImageFrame<TPixel> source,
Memory<byte> output,
Rectangle bounds,
FrameQuantizer<TPixel> quantizer,
ReadOnlyMemory<TPixel> palette)
{
this.source = source;
this.output = output;
this.bounds = bounds;
this.quantizer = quantizer;
this.palette = palette;
}
[MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(in RowInterval rows)
{
ReadOnlySpan<TPixel> paletteSpan = this.palette.Span;
Span<byte> outputSpan = this.output.Span;
int width = this.bounds.Width;
int offsetY = this.bounds.Top;
int offsetX = this.bounds.Left;
for (int y = rows.Min; y < rows.Max; y++)
{
Span<TPixel> row = this.source.GetPixelRowSpan(y);
int rowStart = (y - offsetY) * width;
for (int x = this.bounds.Left; x < this.bounds.Right; x++)
{
outputSpan[rowStart + x - offsetX] = this.quantizer.GetQuantizedColor(row[x], paletteSpan, out TPixel _);
}
}
}
}
private readonly struct DitherRowIntervalOperation : IRowIntervalOperation
{
private readonly ImageFrame<TPixel> source;
private readonly Memory<byte> output;
private readonly Rectangle bounds;
private readonly FrameQuantizer<TPixel> quantizer;
private readonly ReadOnlyMemory<TPixel> palette;
private readonly int bitDepth;
[MethodImpl(InliningOptions.ShortMethod)]
public DitherRowIntervalOperation(
ImageFrame<TPixel> source,
Memory<byte> output,
Rectangle bounds,
FrameQuantizer<TPixel> quantizer,
ReadOnlyMemory<TPixel> palette,
int bitDepth)
{
this.source = source;
this.output = output;
this.bounds = bounds;
this.quantizer = quantizer;
this.palette = palette;
this.bitDepth = bitDepth;
}
[MethodImpl(InliningOptions.ShortMethod)]
public void Invoke(in RowInterval rows)
{
ReadOnlySpan<TPixel> paletteSpan = this.palette.Span;
Span<byte> outputSpan = this.output.Span;
int width = this.bounds.Width;
int offsetY = this.bounds.Top;
int offsetX = this.bounds.Left;
IDither dither = this.quantizer.Options.Dither;
float scale = this.quantizer.Options.DitherScale;
TPixel transformed = default;
for (int y = rows.Min; y < rows.Max; y++)
{
Span<TPixel> row = this.source.GetPixelRowSpan(y);
int rowStart = (y - offsetY) * width;
for (int x = this.bounds.Left; x < this.bounds.Right; x++)
{
TPixel dithered = dither.Dither(this.source, this.bounds, row[x], transformed, x, y, this.bitDepth, scale);
outputSpan[rowStart + x - offsetX] = this.quantizer.GetQuantizedColor(dithered, paletteSpan, out TPixel _);
}
}
}
}
}
}