Skip to content

Commit 0ea2404

Browse files
Revert to coarse cache default, additional feedback.
1 parent 4ace814 commit 0ea2404

File tree

143 files changed

+307
-287
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

143 files changed

+307
-287
lines changed

src/ImageSharp/Advanced/AotCompilerTools.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ private static void Seed<TPixel>()
138138
AotCompileResamplers<TPixel>();
139139
AotCompileQuantizers<TPixel>();
140140
AotCompilePixelSamplingStrategys<TPixel>();
141+
AotCompilePixelMaps<TPixel>();
141142
AotCompileDithers<TPixel>();
142143
AotCompileMemoryManagers<TPixel>();
143144

@@ -514,6 +515,20 @@ private static void AotCompilePixelSamplingStrategys<TPixel>()
514515
default(ExtensivePixelSamplingStrategy).EnumeratePixelRegions(default(ImageFrame<TPixel>));
515516
}
516517

518+
/// <summary>
519+
/// This method pre-seeds the all <see cref="IColorIndexCache{T}" /> in the AoT compiler.
520+
/// </summary>
521+
/// <typeparam name="TPixel">The pixel format.</typeparam>
522+
[Preserve]
523+
private static void AotCompilePixelMaps<TPixel>()
524+
where TPixel : unmanaged, IPixel<TPixel>
525+
{
526+
default(EuclideanPixelMap<TPixel, HybridCache>).GetClosestColor(default, out _);
527+
default(EuclideanPixelMap<TPixel, AccurateCache>).GetClosestColor(default, out _);
528+
default(EuclideanPixelMap<TPixel, CoarseCache>).GetClosestColor(default, out _);
529+
default(EuclideanPixelMap<TPixel, NullCache>).GetClosestColor(default, out _);
530+
}
531+
517532
/// <summary>
518533
/// This method pre-seeds the all <see cref="IDither" /> in the AoT compiler.
519534
/// </summary>

src/ImageSharp/Processing/Processors/Dithering/PaletteDitherProcessor{TPixel}.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public DitherProcessor(
8989
float ditherScale)
9090
{
9191
this.Configuration = configuration;
92-
this.pixelMap = PixelMapFactory.Create(configuration, palette, ColorMatchingMode.Hybrid);
92+
this.pixelMap = PixelMapFactory.Create(configuration, palette, ColorMatchingMode.Coarse);
9393
this.Palette = palette;
9494
this.DitherScale = ditherScale;
9595
}

src/ImageSharp/Processing/Processors/Quantization/IColorIndexCache.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,13 @@ internal interface IColorIndexCache : IDisposable
7474
/// </remarks>
7575
internal unsafe struct HybridCache : IColorIndexCache<HybridCache>
7676
{
77-
private ExactCache exactCache;
77+
private AccurateCache accurateCache;
7878
private CoarseCache coarseCache;
7979

8080
[RequiresPreviewFeatures]
8181
private HybridCache(MemoryAllocator allocator)
8282
{
83-
this.exactCache = ExactCache.Create(allocator);
83+
this.accurateCache = AccurateCache.Create(allocator);
8484
this.coarseCache = CoarseCache.Create(allocator);
8585
}
8686

@@ -90,7 +90,7 @@ private HybridCache(MemoryAllocator allocator)
9090
[MethodImpl(InliningOptions.ShortMethod)]
9191
public bool TryAdd(Rgba32 color, short index)
9292
{
93-
if (this.exactCache.TryAdd(color, index))
93+
if (this.accurateCache.TryAdd(color, index))
9494
{
9595
return true;
9696
}
@@ -101,7 +101,7 @@ public bool TryAdd(Rgba32 color, short index)
101101
[MethodImpl(InliningOptions.ShortMethod)]
102102
public bool TryGetValue(Rgba32 color, out short value)
103103
{
104-
if (this.exactCache.TryGetValue(color, out value))
104+
if (this.accurateCache.TryGetValue(color, out value))
105105
{
106106
return true;
107107
}
@@ -111,13 +111,13 @@ public bool TryGetValue(Rgba32 color, out short value)
111111

112112
public void Clear()
113113
{
114-
this.exactCache.Clear();
114+
this.accurateCache.Clear();
115115
this.coarseCache.Clear();
116116
}
117117

118118
public void Dispose()
119119
{
120-
this.exactCache.Dispose();
120+
this.accurateCache.Dispose();
121121
this.coarseCache.Dispose();
122122
}
123123
}
@@ -209,7 +209,7 @@ public void Dispose()
209209
/// typically very short; in the worst-case, the number of iterations is bounded by 256.
210210
/// This guarantees highly efficient and predictable performance for small, fixed-size color palettes.
211211
/// </remarks>
212-
internal unsafe struct ExactCache : IColorIndexCache<ExactCache>
212+
internal unsafe struct AccurateCache : IColorIndexCache<AccurateCache>
213213
{
214214
// Buckets array: each bucket holds the index (0-based) into the entries array
215215
// of the first entry in the chain, or -1 if empty.
@@ -226,7 +226,7 @@ internal unsafe struct ExactCache : IColorIndexCache<ExactCache>
226226

227227
public const int Capacity = 512;
228228

229-
private ExactCache(MemoryAllocator allocator)
229+
private AccurateCache(MemoryAllocator allocator)
230230
{
231231
this.count = 0;
232232

@@ -244,7 +244,7 @@ private ExactCache(MemoryAllocator allocator)
244244
}
245245

246246
[RequiresPreviewFeatures]
247-
public static ExactCache Create(MemoryAllocator allocator) => new(allocator);
247+
public static AccurateCache Create(MemoryAllocator allocator) => new(allocator);
248248

249249
public bool TryAdd(Rgba32 color, short value)
250250
{

src/ImageSharp/Processing/Processors/Quantization/QuantizerOptions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public int MaxColors
4141

4242
/// <summary>
4343
/// Gets or sets the color matching mode used for matching pixel values to palette colors.
44+
/// Defaults to <see cref="ColorMatchingMode.Coarse"/>.
4445
/// </summary>
45-
public ColorMatchingMode ColorMatchingMode { get; set; } = ColorMatchingMode.Hybrid;
46+
public ColorMatchingMode ColorMatchingMode { get; set; } = ColorMatchingMode.Coarse;
4647
}

tests/ImageSharp.Tests/Formats/Png/PngEncoderTests.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ public void Encode_APng<TPixel>(TestImageProvider<TPixel> provider)
485485
}
486486

487487
[Theory]
488-
[WithFile(TestImages.Gif.Leo, PixelTypes.Rgba32, 0.7629F)]
488+
[WithFile(TestImages.Gif.Leo, PixelTypes.Rgba32, 0.7921F)]
489489
[WithFile(TestImages.Gif.Issues.Issue2866, PixelTypes.Rgba32, 1.06F)]
490490
public void Encode_AnimatedFormatTransform_FromGif<TPixel>(TestImageProvider<TPixel> provider, float percentage)
491491
where TPixel : unmanaged, IPixel<TPixel>
@@ -718,11 +718,15 @@ public void Issue_2862()
718718
}
719719

720720
using MemoryStream ms = new();
721+
PaletteQuantizer quantizer = new(
722+
palette.Select(Color.FromPixel).ToArray(),
723+
new QuantizerOptions() { ColorMatchingMode = ColorMatchingMode.Hybrid});
724+
721725
image.Save(ms, new PngEncoder
722726
{
723727
ColorType = PngColorType.Palette,
724728
BitDepth = PngBitDepth.Bit8,
725-
Quantizer = new PaletteQuantizer(palette.Select(Color.FromPixel).ToArray())
729+
Quantizer = quantizer
726730
});
727731

728732
ms.Position = 0;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:6785d108cab9e3b508e98ce10cef383037471beb2a6d14a69df6069b5fbf5f3d
2+
oid sha256:11375b15df083d98335f4a4baf0717e7fdd6b21ab2132a6815cadc787ac17e7d
33
size 9270
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
version https://git-lfs.github.com/spec/v1
2-
oid sha256:5d9f2745de2b6e7fc3b1403fe651f3bbba835c67a6fb410fc8a9d91a15b44328
2+
oid sha256:e063e97cd8a000de6830adcc3961a7dc41785d40cd4d83af10ca38d96e071362
33
size 9270
Lines changed: 2 additions & 2 deletions
Lines changed: 2 additions & 2 deletions
Lines changed: 2 additions & 2 deletions

0 commit comments

Comments
 (0)