From 153b7d25ae65abc9ea5b8efa4bfb970441fd5e01 Mon Sep 17 00:00:00 2001 From: Brian Popow Date: Wed, 29 Apr 2020 14:44:39 +0200 Subject: [PATCH] Refactor --- src/ImageSharp/Formats/Png/PngEncoderCore.cs | 81 ++++++++++++-------- 1 file changed, 49 insertions(+), 32 deletions(-) diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 416c26b0f..7d00e20e4 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -144,6 +144,34 @@ namespace SixLabors.ImageSharp.Formats.Png PngMetadata pngMetadata = metadata.GetFormatMetadata(PngFormat.Instance); PngEncoderOptionsHelpers.AdjustOptions(this.options, pngMetadata, out this.use16Bit, out this.bytesPerPixel); + IndexedImageFrame quantized = this.CreateQuantizedImage(image); + + stream.Write(PngConstants.HeaderBytes); + + this.WriteHeaderChunk(stream); + this.WriteGammaChunk(stream); + this.WritePaletteChunk(stream, quantized); + this.WriteTransparencyChunk(stream, pngMetadata); + this.WritePhysicalChunk(stream, metadata); + this.WriteExifChunk(stream, metadata); + this.WriteTextChunks(stream, pngMetadata); + this.WriteDataChunks(image.Frames.RootFrame, quantized, stream); + this.WriteEndChunk(stream); + + stream.Flush(); + + quantized?.Dispose(); + } + + /// + /// Creates the quantized image and sets calculates and sets the bit depth. + /// + /// The type of the pixel. + /// The image to quantize. + /// The quantized image. + private IndexedImageFrame CreateQuantizedImage(Image image) + where TPixel : unmanaged, IPixel + { IndexedImageFrame quantized; if (this.options.MakeTransparentBlack) { @@ -178,38 +206,7 @@ namespace SixLabors.ImageSharp.Formats.Png this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, image, quantized); } - stream.Write(PngConstants.HeaderBytes); - - this.WriteHeaderChunk(stream); - - if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludeGammaChunk) != PngChunkFilter.ExcludeGammaChunk) - { - this.WriteGammaChunk(stream); - } - - this.WritePaletteChunk(stream, quantized); - this.WriteTransparencyChunk(stream, pngMetadata); - - if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludePhysicalChunk) != PngChunkFilter.ExcludePhysicalChunk) - { - this.WritePhysicalChunk(stream, metadata); - } - - if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludeExifChunk) != PngChunkFilter.ExcludeExifChunk) - { - this.WriteExifChunk(stream, metadata); - } - - if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludeTextChunks) != PngChunkFilter.ExcludeTextChunks) - { - this.WriteTextChunks(stream, pngMetadata); - } - - this.WriteDataChunks(image.Frames.RootFrame, quantized, stream); - this.WriteEndChunk(stream); - stream.Flush(); - - quantized?.Dispose(); + return quantized; } /// @@ -652,6 +649,11 @@ namespace SixLabors.ImageSharp.Formats.Png /// The image metadata. private void WritePhysicalChunk(Stream stream, ImageMetadata meta) { + if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludePhysicalChunk) == PngChunkFilter.ExcludePhysicalChunk) + { + return; + } + PhysicalChunkData.FromMetadata(meta).WriteTo(this.chunkDataBuffer); this.WriteChunk(stream, PngChunkType.Physical, this.chunkDataBuffer, 0, PhysicalChunkData.Size); @@ -664,6 +666,11 @@ namespace SixLabors.ImageSharp.Formats.Png /// The image metadata. private void WriteExifChunk(Stream stream, ImageMetadata meta) { + if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludeExifChunk) == PngChunkFilter.ExcludeExifChunk) + { + return; + } + if (meta.ExifProfile is null || meta.ExifProfile.Values.Count == 0) { return; @@ -681,6 +688,11 @@ namespace SixLabors.ImageSharp.Formats.Png /// The image metadata. private void WriteTextChunks(Stream stream, PngMetadata meta) { + if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludeTextChunks) == PngChunkFilter.ExcludeTextChunks) + { + return; + } + const int MaxLatinCode = 255; for (int i = 0; i < meta.TextData.Count; i++) { @@ -773,6 +785,11 @@ namespace SixLabors.ImageSharp.Formats.Png /// The containing image data. private void WriteGammaChunk(Stream stream) { + if (((this.options.ChunkFilter ?? PngChunkFilter.None) & PngChunkFilter.ExcludeGammaChunk) == PngChunkFilter.ExcludeGammaChunk) + { + return; + } + if (this.options.Gamma > 0) { // 4-byte unsigned integer of gamma * 100,000.