From b233771cb086182b8e6e745f07d916ae1be18575 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 19 Oct 2021 14:57:15 +1100 Subject: [PATCH 1/2] Move filtering setup to the correct place. --- src/ImageSharp/Formats/Png/PngEncoderOptions.cs | 8 ++------ .../Formats/Png/PngEncoderOptionsHelpers.cs | 12 ++++++++++++ 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderOptions.cs b/src/ImageSharp/Formats/Png/PngEncoderOptions.cs index 3c17c2463..0bcea037a 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderOptions.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderOptions.cs @@ -18,11 +18,7 @@ namespace SixLabors.ImageSharp.Formats.Png { this.BitDepth = source.BitDepth; this.ColorType = source.ColorType; - - // Specification recommends default filter method None for paletted images and Paeth for others. - this.FilterMethod = source.FilterMethod ?? (source.ColorType == PngColorType.Palette - ? PngFilterMethod.None - : PngFilterMethod.Paeth); + this.FilterMethod = source.FilterMethod; this.CompressionLevel = source.CompressionLevel; this.TextCompressionThreshold = source.TextCompressionThreshold; this.Gamma = source.Gamma; @@ -41,7 +37,7 @@ namespace SixLabors.ImageSharp.Formats.Png public PngColorType? ColorType { get; set; } /// - public PngFilterMethod? FilterMethod { get; } + public PngFilterMethod? FilterMethod { get; set; } /// public PngCompressionLevel CompressionLevel { get; } = PngCompressionLevel.DefaultCompression; diff --git a/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs b/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs index 23ca86993..1250db6fe 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderOptionsHelpers.cs @@ -34,6 +34,18 @@ namespace SixLabors.ImageSharp.Formats.Png // a sensible default based upon the pixel format. options.ColorType ??= pngMetadata.ColorType ?? SuggestColorType(); options.BitDepth ??= pngMetadata.BitDepth ?? SuggestBitDepth(); + if (!options.FilterMethod.HasValue) + { + // Specification recommends default filter method None for paletted images and Paeth for others. + if (options.ColorType == PngColorType.Palette) + { + options.FilterMethod = PngFilterMethod.None; + } + else + { + options.FilterMethod = PngFilterMethod.Paeth; + } + } // Ensure bit depth and color type are a supported combination. // Bit8 is the only bit depth supported by all color types. From 743cd629c0d6cf87ae45d85aac269aac396f393b Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Tue, 19 Oct 2021 14:57:30 +1100 Subject: [PATCH 2/2] Fix todo's --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 38 ++++++++------------ 1 file changed, 15 insertions(+), 23 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 4f6fb7356..f10db7a6c 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -268,35 +268,27 @@ namespace SixLabors.ImageSharp.Formats.Png if (this.use16Bit) { // 16 bit grayscale + alpha - // TODO: Should we consider in the future a GrayAlpha32 type. - using (IMemoryOwner rgbaBuffer = this.memoryAllocator.Allocate(rowSpan.Length)) - { - Span rgbaSpan = rgbaBuffer.GetSpan(); - ref Rgba64 rgbaRef = ref MemoryMarshal.GetReference(rgbaSpan); - PixelOperations.Instance.ToRgba64(this.configuration, rowSpan, rgbaSpan); + using IMemoryOwner laBuffer = this.memoryAllocator.Allocate(rowSpan.Length); + Span laSpan = laBuffer.GetSpan(); + ref La32 laRef = ref MemoryMarshal.GetReference(laSpan); + PixelOperations.Instance.ToLa32(this.configuration, rowSpan, laSpan); - // Can't map directly to byte array as it's big endian. - for (int x = 0, o = 0; x < rgbaSpan.Length; x++, o += 4) - { - Rgba64 rgba = Unsafe.Add(ref rgbaRef, x); - ushort luminance = ColorNumerics.Get16BitBT709Luminance(rgba.R, rgba.G, rgba.B); - BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), luminance); - BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), rgba.A); - } + // Can't map directly to byte array as it's big endian. + for (int x = 0, o = 0; x < laSpan.Length; x++, o += 4) + { + La32 la = Unsafe.Add(ref laRef, x); + BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o, 2), la.L); + BinaryPrimitives.WriteUInt16BigEndian(rawScanlineSpan.Slice(o + 2, 2), la.A); } } else { // 8 bit grayscale + alpha - // TODO: Should we consider in the future a GrayAlpha16 type. - Rgba32 rgba = default; - for (int x = 0, o = 0; x < rowSpan.Length; x++, o += 2) - { - Unsafe.Add(ref rowSpanRef, x).ToRgba32(ref rgba); - Unsafe.Add(ref rawScanlineSpanRef, o) = - ColorNumerics.Get8BitBT709Luminance(rgba.R, rgba.G, rgba.B); - Unsafe.Add(ref rawScanlineSpanRef, o + 1) = rgba.A; - } + PixelOperations.Instance.ToLa16Bytes( + this.configuration, + rowSpan, + rawScanlineSpan, + rowSpan.Length); } } }