Skip to content

Commit bf43044

Browse files
Merge pull request #2473 from SixLabors/js/2469-color-LUT-memory
Use more accurate bit for alpha lookup.
2 parents 119885f + d3e8833 commit bf43044

File tree

82 files changed

+209
-180
lines changed

Some content is hidden

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

82 files changed

+209
-180
lines changed

src/ImageSharp/Processing/Processors/Quantization/EuclideanPixelMap{TPixel}.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -149,13 +149,13 @@ private static int DistanceSquared(Rgba32 a, Rgba32 b)
149149
/// The granularity of the cache has been determined based upon the current
150150
/// suite of test images and provides the lowest possible memory usage while
151151
/// providing enough match accuracy.
152-
/// Entry count is currently limited to 1185921 entries (2371842 bytes ~2.26MB).
152+
/// Entry count is currently limited to 2335905 entries (4671810 bytes ~4.45MB).
153153
/// </para>
154154
/// </remarks>
155155
private unsafe struct ColorDistanceCache : IDisposable
156156
{
157157
private const int IndexBits = 5;
158-
private const int IndexAlphaBits = 5;
158+
private const int IndexAlphaBits = 6;
159159
private const int IndexCount = (1 << IndexBits) + 1;
160160
private const int IndexAlphaCount = (1 << IndexAlphaBits) + 1;
161161
private const int RgbShift = 8 - IndexBits;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using SixLabors.ImageSharp.Formats.Png;
88
using SixLabors.ImageSharp.Memory;
99
using SixLabors.ImageSharp.PixelFormats;
10+
using SixLabors.ImageSharp.Processing.Processors.Quantization;
1011
using SixLabors.ImageSharp.Tests.TestUtilities;
1112
using SixLabors.ImageSharp.Tests.TestUtilities.ImageComparison;
1213
using SixLabors.ImageSharp.Tests.TestUtilities.ReferenceCodecs;
@@ -537,7 +538,6 @@ public void Issue2209_Decode_HasTransparencyIsTrue<TPixel>(TestImageProvider<TPi
537538
where TPixel : unmanaged, IPixel<TPixel>
538539
{
539540
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
540-
image.DebugSave(provider);
541541
PngMetadata metadata = image.Metadata.GetPngMetadata();
542542
Assert.True(metadata.HasTransparency);
543543
}

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

Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,8 @@ public void IsNotBoundToSinglePixelType<TPixel>(TestImageProvider<TPixel> provid
129129
PngFilterMethod.Adaptive,
130130
PngBitDepth.Bit8,
131131
interlaceMode,
132-
appendPixelType: true,
133-
appendPngColorType: true);
132+
appendPngColorType: true,
133+
appendPixelType: true);
134134
}
135135
}
136136

@@ -321,7 +321,7 @@ public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider)
321321
where TPixel : unmanaged, IPixel<TPixel>
322322
{
323323
using Image<TPixel> image = provider.GetImage();
324-
using var ms = new MemoryStream();
324+
using MemoryStream ms = new();
325325
image.Save(ms, PngEncoder);
326326

327327
byte[] data = ms.ToArray().Take(8).ToArray();
@@ -344,13 +344,13 @@ public void WritesFileMarker<TPixel>(TestImageProvider<TPixel> provider)
344344
[MemberData(nameof(RatioFiles))]
345345
public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolution, PixelResolutionUnit resolutionUnit)
346346
{
347-
var testFile = TestFile.Create(imagePath);
347+
TestFile testFile = TestFile.Create(imagePath);
348348
using Image<Rgba32> input = testFile.CreateRgba32Image();
349-
using var memStream = new MemoryStream();
349+
using MemoryStream memStream = new();
350350
input.Save(memStream, PngEncoder);
351351

352352
memStream.Position = 0;
353-
using var output = Image.Load<Rgba32>(memStream);
353+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
354354
ImageMetadata meta = output.Metadata;
355355
Assert.Equal(xResolution, meta.HorizontalResolution);
356356
Assert.Equal(yResolution, meta.VerticalResolution);
@@ -361,13 +361,13 @@ public void Encode_PreserveRatio(string imagePath, int xResolution, int yResolut
361361
[MemberData(nameof(PngBitDepthFiles))]
362362
public void Encode_PreserveBits(string imagePath, PngBitDepth pngBitDepth)
363363
{
364-
var testFile = TestFile.Create(imagePath);
364+
TestFile testFile = TestFile.Create(imagePath);
365365
using Image<Rgba32> input = testFile.CreateRgba32Image();
366-
using var memStream = new MemoryStream();
366+
using MemoryStream memStream = new();
367367
input.Save(memStream, PngEncoder);
368368

369369
memStream.Position = 0;
370-
using var output = Image.Load<Rgba32>(memStream);
370+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
371371
PngMetadata meta = output.Metadata.GetPngMetadata();
372372

373373
Assert.Equal(pngBitDepth, meta.BitDepth);
@@ -380,8 +380,8 @@ public void Encode_PreserveBits(string imagePath, PngBitDepth pngBitDepth)
380380
public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType colorType)
381381
{
382382
// arrange
383-
var image = new Image<Rgba32>(50, 50);
384-
var encoder = new PngEncoder()
383+
Image<Rgba32> image = new(50, 50);
384+
PngEncoder encoder = new()
385385
{
386386
TransparentColorMode = PngTransparentColorMode.Clear,
387387
ColorType = colorType
@@ -391,7 +391,7 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
391391
{
392392
for (int y = 0; y < image.Height; y++)
393393
{
394-
System.Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
394+
Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
395395

396396
// Half of the test image should be transparent.
397397
if (y > 25)
@@ -407,12 +407,12 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
407407
});
408408

409409
// act
410-
using var memStream = new MemoryStream();
410+
using MemoryStream memStream = new();
411411
image.Save(memStream, encoder);
412412

413413
// assert
414414
memStream.Position = 0;
415-
using var actual = Image.Load<Rgba32>(memStream);
415+
using Image<Rgba32> actual = Image.Load<Rgba32>(memStream);
416416
Rgba32 expectedColor = Color.Blue;
417417
if (colorType is PngColorType.Grayscale or PngColorType.GrayscaleWithAlpha)
418418
{
@@ -424,7 +424,7 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
424424
{
425425
for (int y = 0; y < accessor.Height; y++)
426426
{
427-
System.Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
427+
Span<Rgba32> rowSpan = accessor.GetRowSpan(y);
428428

429429
if (y > 25)
430430
{
@@ -443,15 +443,15 @@ public void Encode_WithPngTransparentColorBehaviorClear_Works(PngColorType color
443443
[MemberData(nameof(PngTrnsFiles))]
444444
public void Encode_PreserveTrns(string imagePath, PngBitDepth pngBitDepth, PngColorType pngColorType)
445445
{
446-
var testFile = TestFile.Create(imagePath);
446+
TestFile testFile = TestFile.Create(imagePath);
447447
using Image<Rgba32> input = testFile.CreateRgba32Image();
448448
PngMetadata inMeta = input.Metadata.GetPngMetadata();
449449
Assert.True(inMeta.HasTransparency);
450450

451-
using var memStream = new MemoryStream();
451+
using MemoryStream memStream = new();
452452
input.Save(memStream, PngEncoder);
453453
memStream.Position = 0;
454-
using var output = Image.Load<Rgba32>(memStream);
454+
using Image<Rgba32> output = Image.Load<Rgba32>(memStream);
455455
PngMetadata outMeta = output.Metadata.GetPngMetadata();
456456
Assert.True(outMeta.HasTransparency);
457457

@@ -501,8 +501,8 @@ public void Encode_WorksWithDiscontiguousBuffers<TPixel>(TestImageProvider<TPixe
501501
PngFilterMethod.Adaptive,
502502
PngBitDepth.Bit8,
503503
interlaceMode,
504-
appendPixelType: true,
505-
appendPngColorType: true);
504+
appendPngColorType: true,
505+
appendPixelType: true);
506506
}
507507
}
508508

@@ -523,8 +523,8 @@ static void RunTest(string serialized)
523523
PngFilterMethod.Adaptive,
524524
PngBitDepth.Bit8,
525525
interlaceMode,
526-
appendPixelType: true,
527-
appendPngColorType: true);
526+
appendPngColorType: true,
527+
appendPixelType: true);
528528
}
529529
}
530530

@@ -538,13 +538,27 @@ static void RunTest(string serialized)
538538
public void EncodeFixesInvalidOptions()
539539
{
540540
// https://github.com/SixLabors/ImageSharp/issues/935
541-
using var ms = new MemoryStream();
542-
var testFile = TestFile.Create(TestImages.Png.Issue935);
541+
using MemoryStream ms = new();
542+
TestFile testFile = TestFile.Create(TestImages.Png.Issue935);
543543
using Image<Rgba32> image = testFile.CreateRgba32Image(PngDecoder.Instance);
544544

545545
image.Save(ms, new PngEncoder { ColorType = PngColorType.RgbWithAlpha });
546546
}
547547

548+
// https://github.com/SixLabors/ImageSharp/issues/2469
549+
[Theory]
550+
[WithFile(TestImages.Png.Issue2469, PixelTypes.Rgba32)]
551+
public void Issue2469_Quantized_Encode_Artifacts<TPixel>(TestImageProvider<TPixel> provider)
552+
where TPixel : unmanaged, IPixel<TPixel>
553+
{
554+
using Image<TPixel> image = provider.GetImage(PngDecoder.Instance);
555+
PngEncoder encoder = new() { BitDepth = PngBitDepth.Bit8, ColorType = PngColorType.Palette };
556+
557+
string actualOutputFile = provider.Utility.SaveTestOutputFile(image, "png", encoder);
558+
using Image<Rgba32> encoded = Image.Load<Rgba32>(actualOutputFile);
559+
encoded.CompareToReferenceOutput(ImageComparer.Exact, provider);
560+
}
561+
548562
private static void TestPngEncoderCore<TPixel>(
549563
TestImageProvider<TPixel> provider,
550564
PngColorType pngColorType,
@@ -563,7 +577,7 @@ private static void TestPngEncoderCore<TPixel>(
563577
where TPixel : unmanaged, IPixel<TPixel>
564578
{
565579
using Image<TPixel> image = provider.GetImage();
566-
var encoder = new PngEncoder
580+
PngEncoder encoder = new()
567581
{
568582
ColorType = pngColorType,
569583
FilterMethod = pngFilterMethod,

tests/ImageSharp.Tests/Memory/Allocators/UniformUnmanagedMemoryPoolTests.Trim.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,9 +113,15 @@ static void LeakPoolInstance()
113113
public static readonly bool Is32BitProcess = !Environment.Is64BitProcess;
114114
private static readonly List<byte[]> PressureArrays = new();
115115

116-
[ConditionalFact(nameof(Is32BitProcess))]
116+
[Fact]
117117
public static void GC_Collect_OnHighLoad_TrimsEntirePool()
118118
{
119+
if (!Is32BitProcess)
120+
{
121+
// This test is only relevant for 32-bit processes.
122+
return;
123+
}
124+
119125
RemoteExecutor.Invoke(RunTest).Dispose();
120126

121127
static void RunTest()

tests/ImageSharp.Tests/TestImages.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,12 @@ public static class Png
129129
// Issue 2209: https://github.com/SixLabors/ImageSharp/issues/2209
130130
public const string Issue2209IndexedWithTransparency = "Png/issues/Issue_2209.png";
131131

132-
// Issue 2259: https://github.com/SixLabors/ImageSharp/issues/2259
132+
// Issue 2259: https://github.com/SixLabors/ImageSharp/issues/2469
133133
public const string Issue2259 = "Png/issues/Issue_2259.png";
134134

135+
// Issue 2259: https://github.com/SixLabors/ImageSharp/issues/2469
136+
public const string Issue2469 = "Png/issues/issue_2469.png";
137+
135138
// Issue 2447: https://github.com/SixLabors/ImageSharp/issues/2447
136139
public const string Issue2447 = "Png/issues/issue_2447.png";
137140

Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading
Lines changed: 2 additions & 2 deletions
Loading

0 commit comments

Comments
 (0)