From 5d03d0b10609c1a3f6db6e480c1aceedf98e24eb Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 21 Sep 2016 22:20:42 +1000 Subject: [PATCH] Slight improvement to indexed png Premultiply partial alpha colors above threshold Former-commit-id: e3180b0889327530c971611680ae34e71d24db79 Former-commit-id: 135d6264188a87e221d38e68e9c905782c4a5d7e Former-commit-id: 546f847865693d9b0a72f73438cd9a1d83c3a669 --- src/ImageProcessorCore/Formats/Png/PngEncoderCore.cs | 10 +++++++++- .../Quantizers/Octree/OctreeQuantizer.cs | 11 ----------- src/ImageProcessorCore/Quantizers/Octree/Quantizer.cs | 2 +- 3 files changed, 10 insertions(+), 13 deletions(-) diff --git a/src/ImageProcessorCore/Formats/Png/PngEncoderCore.cs b/src/ImageProcessorCore/Formats/Png/PngEncoderCore.cs index 8e959155cc..fc6ad3f672 100644 --- a/src/ImageProcessorCore/Formats/Png/PngEncoderCore.cs +++ b/src/ImageProcessorCore/Formats/Png/PngEncoderCore.cs @@ -516,11 +516,19 @@ namespace ImageProcessorCore.Formats { int offset = i * 3; Color color = new Color(palette[i].ToVector4()); + int alpha = color.A; + + // Premultiply the color. This helps prevent banding. + if (alpha < 255 && alpha > this.Threshold) + { + color = Color.Multiply(color, new Color(alpha, alpha, alpha, 255)); + } + colorTable[offset] = color.R; colorTable[offset + 1] = color.G; colorTable[offset + 2] = color.B; - if (color.A <= this.Threshold) + if (alpha <= this.Threshold) { transparentPixels.Add((byte)offset); } diff --git a/src/ImageProcessorCore/Quantizers/Octree/OctreeQuantizer.cs b/src/ImageProcessorCore/Quantizers/Octree/OctreeQuantizer.cs index 0e53cbbab9..d6eeb535ff 100644 --- a/src/ImageProcessorCore/Quantizers/Octree/OctreeQuantizer.cs +++ b/src/ImageProcessorCore/Quantizers/Octree/OctreeQuantizer.cs @@ -91,17 +91,6 @@ namespace ImageProcessorCore.Quantizers protected override List GetPalette() { return this.octree.Palletize(Math.Max(this.colors, 1)); - - // First off convert the Octree to maxColors colors - //List palette = this.octree.Palletize(Math.Max(this.colors, 1)); - - //int diff = this.colors - palette.Count; - //if (diff > 0) - //{ - // palette.AddRange(Enumerable.Repeat(default(TColor), diff)); - //} - - //return palette; } /// diff --git a/src/ImageProcessorCore/Quantizers/Octree/Quantizer.cs b/src/ImageProcessorCore/Quantizers/Octree/Quantizer.cs index cbaa8435ed..1354808798 100644 --- a/src/ImageProcessorCore/Quantizers/Octree/Quantizer.cs +++ b/src/ImageProcessorCore/Quantizers/Octree/Quantizer.cs @@ -95,7 +95,7 @@ namespace ImageProcessorCore.Quantizers /// The output pixel array /// The width in pixels of the image /// The height in pixels of the image - protected virtual void SecondPass(PixelAccessor source, byte[] output, int width, int height) + protected virtual void SecondPass(PixelAccessor source, byte[] output, int width, int height) { Parallel.For( 0,