From b8bb3293cb07aa30138a10e506e4e95e1bf73c79 Mon Sep 17 00:00:00 2001 From: Peter Tribe Date: Thu, 19 Sep 2019 09:30:13 -0700 Subject: [PATCH] Added: ability to skip unneeded chunks for optimization mode --- src/ImageSharp/Formats/Png/IPngEncoderOptions.cs | 5 +++++ src/ImageSharp/Formats/Png/PngEncoderCore.cs | 13 +++++++++---- src/ImageSharp/Formats/Png/PngEncoderOptions.cs | 6 ++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs b/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs index 87fd2582a..0f416cb7b 100644 --- a/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs +++ b/src/ImageSharp/Formats/Png/IPngEncoderOptions.cs @@ -57,5 +57,10 @@ namespace SixLabors.ImageSharp.Formats.Png /// Gets a value indicating whether this instance should write an Adam7 interlaced image. /// PngInterlaceMode? InterlaceMethod { get; } + + /// + /// Gets a value indicating whether this instance should skip certain chunks to decrease file size + /// + bool Optimized { get; } } } diff --git a/src/ImageSharp/Formats/Png/PngEncoderCore.cs b/src/ImageSharp/Formats/Png/PngEncoderCore.cs index 09575bb28..4442bdb0d 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderCore.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderCore.cs @@ -155,10 +155,15 @@ namespace SixLabors.ImageSharp.Formats.Png this.WriteHeaderChunk(stream); this.WritePaletteChunk(stream, quantized); this.WriteTransparencyChunk(stream, pngMetadata); - this.WritePhysicalChunk(stream, metadata); - this.WriteGammaChunk(stream); - this.WriteExifChunk(stream, metadata); - this.WriteTextChunks(stream, pngMetadata); + + if (!this.options.Optimized) + { + this.WritePhysicalChunk(stream, metadata); + this.WriteGammaChunk(stream); + this.WriteExifChunk(stream, metadata); + this.WriteTextChunks(stream, pngMetadata); + } + this.WriteDataChunks(image.Frames.RootFrame, quantized, stream); this.WriteEndChunk(stream); stream.Flush(); diff --git a/src/ImageSharp/Formats/Png/PngEncoderOptions.cs b/src/ImageSharp/Formats/Png/PngEncoderOptions.cs index dd6c66cb7..5d4be8f14 100644 --- a/src/ImageSharp/Formats/Png/PngEncoderOptions.cs +++ b/src/ImageSharp/Formats/Png/PngEncoderOptions.cs @@ -29,6 +29,7 @@ namespace SixLabors.ImageSharp.Formats.Png this.Quantizer = source.Quantizer; this.Threshold = source.Threshold; this.InterlaceMethod = source.InterlaceMethod; + this.Optimized = source.Optimized; } /// @@ -78,5 +79,10 @@ namespace SixLabors.ImageSharp.Formats.Png /// Gets or sets a value indicating whether this instance should write an Adam7 interlaced image. /// public PngInterlaceMode? InterlaceMethod { get; set; } + + /// + /// Gets or sets a value indicating whether this instance should skip certain chunks to decrease file size + /// + public bool Optimized { get; set; } } }