diff --git a/src/ImageSharp/Quantizers/Octree/OctreeQuantizer.cs b/src/ImageSharp/Quantizers/Octree/OctreeQuantizer.cs index 2590f297e..f95ae5fce 100644 --- a/src/ImageSharp/Quantizers/Octree/OctreeQuantizer.cs +++ b/src/ImageSharp/Quantizers/Octree/OctreeQuantizer.cs @@ -58,18 +58,12 @@ namespace ImageSharp.Quantizers public override QuantizedImage Quantize(ImageBase image, int maxColors) { this.colors = maxColors.Clamp(1, 255); - this.octree = new Octree(this.GetBitsNeededForColorDepth(maxColors)); + this.octree = new Octree(this.GetBitsNeededForColorDepth(this.colors)); - return base.Quantize(image, maxColors); + return base.Quantize(image, this.colors); } - /// - /// Execute a second pass through the bitmap - /// - /// The source image. - /// The output pixel array - /// The width in pixels of the image - /// The height in pixels of the image + /// protected override void SecondPass(PixelAccessor source, byte[] output, int width, int height) { // Load up the values for the first pixel. We can use these to speed up the second @@ -115,36 +109,17 @@ namespace ImageSharp.Quantizers } } - /// - /// Process the pixel in the first pass of the algorithm - /// - /// - /// The pixel to quantize - /// - /// - /// This function need only be overridden if your quantize algorithm needs two passes, - /// such as an Octree quantizer. - /// + /// protected override void InitialQuantizePixel(TColor pixel) { // Add the color to the Octree this.octree.AddColor(pixel, this.pixelBuffer); } - /// - /// Retrieve the palette for the quantized image. - /// - /// - /// The new color palette - /// + /// protected override TColor[] GetPalette() { - if (this.palette == null) - { - this.palette = this.octree.Palletize(Math.Max(this.colors, 1)); - } - - return this.palette; + return this.palette ?? (this.palette = this.octree.Palletize(Math.Max(this.colors, 1))); } /// @@ -175,6 +150,7 @@ namespace ImageSharp.Quantizers /// /// The /// + [MethodImpl(MethodImplOptions.AggressiveInlining)] private int GetBitsNeededForColorDepth(int colorCount) { return (int)Math.Ceiling(Math.Log(colorCount, 2)); @@ -189,7 +165,7 @@ namespace ImageSharp.Quantizers /// Mask used when getting the appropriate pixels for a given node /// // ReSharper disable once StaticMemberInGenericType - private static readonly int[] Mask = { 0x100, 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; + private static readonly int[] Mask = { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }; /// /// The root of the Octree @@ -379,11 +355,6 @@ namespace ImageSharp.Quantizers /// private int blue; - /// - /// Alpha component - /// - private int alpha; - /// /// The index of this node in the palette /// @@ -406,7 +377,7 @@ namespace ImageSharp.Quantizers // Construct the new node this.leaf = level == colorBits; - this.red = this.green = this.blue = this.alpha = 0; + this.red = this.green = this.blue = 0; this.pixelCount = 0; // If a leaf, increment the leaf count @@ -454,10 +425,9 @@ namespace ImageSharp.Quantizers int shift = 7 - level; pixel.ToXyzwBytes(buffer, 0); - int index = ((buffer[3] & Mask[0]) >> (shift - 3)) | - ((buffer[2] & Mask[level + 1]) >> (shift - 2)) | - ((buffer[1] & Mask[level + 1]) >> (shift - 1)) | - ((buffer[0] & Mask[level + 1]) >> shift); + int index = ((buffer[2] & Mask[level]) >> (shift - 2)) | + ((buffer[1] & Mask[level]) >> (shift - 1)) | + ((buffer[0] & Mask[level]) >> shift); OctreeNode child = this.children[index]; @@ -479,7 +449,7 @@ namespace ImageSharp.Quantizers /// The number of leaves removed public int Reduce() { - this.red = this.green = this.blue = this.alpha = 0; + this.red = this.green = this.blue = 0; int childNodes = 0; // Loop through all children and add their information to this node @@ -490,7 +460,6 @@ namespace ImageSharp.Quantizers this.red += this.children[index].red; this.green += this.children[index].green; this.blue += this.children[index].blue; - this.alpha += this.children[index].alpha; this.pixelCount += this.children[index].pixelCount; ++childNodes; this.children[index] = null; @@ -517,11 +486,10 @@ namespace ImageSharp.Quantizers byte r = (this.red / this.pixelCount).ToByte(); byte g = (this.green / this.pixelCount).ToByte(); byte b = (this.blue / this.pixelCount).ToByte(); - byte a = (this.alpha / this.pixelCount).ToByte(); // And set the color of the palette entry TColor pixel = default(TColor); - pixel.PackFromBytes(r, g, b, a); + pixel.PackFromBytes(r, g, b, 255); palette[index] = pixel; // Consume the next palette index @@ -558,10 +526,9 @@ namespace ImageSharp.Quantizers int shift = 7 - level; pixel.ToXyzwBytes(buffer, 0); - int pixelIndex = ((buffer[3] & Mask[0]) >> (shift - 3)) | - ((buffer[2] & Mask[level + 1]) >> (shift - 2)) | - ((buffer[1] & Mask[level + 1]) >> (shift - 1)) | - ((buffer[0] & Mask[level + 1]) >> shift); + int pixelIndex = ((buffer[2] & Mask[level]) >> (shift - 2)) | + ((buffer[1] & Mask[level]) >> (shift - 1)) | + ((buffer[0] & Mask[level]) >> shift); if (this.children[pixelIndex] != null) { @@ -588,9 +555,8 @@ namespace ImageSharp.Quantizers this.red += buffer[0]; this.green += buffer[1]; this.blue += buffer[2]; - this.alpha += buffer[3]; } } } } -} +} \ No newline at end of file diff --git a/src/ImageSharp/Quantizers/Options/Quantization.cs b/src/ImageSharp/Quantizers/Options/Quantization.cs index 428c8e801..039404384 100644 --- a/src/ImageSharp/Quantizers/Options/Quantization.cs +++ b/src/ImageSharp/Quantizers/Options/Quantization.cs @@ -12,17 +12,20 @@ namespace ImageSharp { /// /// An adaptive Octree quantizer. Fast with good quality. + /// The quantizer only supports a single alpha value. /// Octree, /// /// Xiaolin Wu's Color Quantizer which generates high quality output. + /// The quantizer supports multiple alpha values. /// Wu, /// /// Palette based, Uses the collection of web-safe colors by default. + /// The quantizer supports multiple alpha values. /// Palette } -} +} \ No newline at end of file diff --git a/tests/ImageSharp.Tests/FileTestBase.cs b/tests/ImageSharp.Tests/FileTestBase.cs index 765ff3a42..1fa26063d 100644 --- a/tests/ImageSharp.Tests/FileTestBase.cs +++ b/tests/ImageSharp.Tests/FileTestBase.cs @@ -28,7 +28,7 @@ namespace ImageSharp.Tests // TestFile.Create(TestImages.Jpeg.Baseline.GammaDalaiLamaGray), // Perf: Enable for local testing only // TestFile.Create(TestImages.Jpeg.Progressive.Bad.BadEOF), // Perf: Enable for local testing only TestFile.Create(TestImages.Bmp.Car), - // TestFile.Create(TestImages.Bmp.Neg_height), // Perf: Enable for local testing only + // TestFile.Create(TestImages.Bmp.NegHeight), // Perf: Enable for local testing only TestFile.Create(TestImages.Png.Splash), // TestFile.Create(TestImages.Png.ChunkLength1), // Perf: Enable for local testing only // TestFile.Create(TestImages.Png.ChunkLength2), // Perf: Enable for local testing only @@ -46,6 +46,7 @@ namespace ImageSharp.Tests // TestFile.Create(TestImages.Png.P1), // Perf: Enable for local testing only // TestFile.Create(TestImages.Png.Pd), // Perf: Enable for local testing only TestFile.Create(TestImages.Gif.Rings), + // TestFile.Create(TestImages.Gif.Trans), // Perf: Enable for local testing only // TestFile.Create(TestImages.Gif.Cheers), // Perf: Enable for local testing only // TestFile.Create(TestImages.Gif.Giphy) // Perf: Enable for local testing only }; diff --git a/tests/ImageSharp.Tests/TestImages.cs b/tests/ImageSharp.Tests/TestImages.cs index e9d658887..5be1240ef 100644 --- a/tests/ImageSharp.Tests/TestImages.cs +++ b/tests/ImageSharp.Tests/TestImages.cs @@ -103,6 +103,7 @@ namespace ImageSharp.Tests public const string Rings = "Gif/rings.gif"; public const string Giphy = "Gif/giphy.gif"; public const string Cheers = "Gif/cheers.gif"; + public const string Trans = "Gif/trans.gif"; } } } diff --git a/tests/ImageSharp.Tests/TestImages/Formats/Gif/trans.gif b/tests/ImageSharp.Tests/TestImages/Formats/Gif/trans.gif new file mode 100644 index 000000000..6a7577fa1 --- /dev/null +++ b/tests/ImageSharp.Tests/TestImages/Formats/Gif/trans.gif @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:99b20b417a63c30f62fa69a00fa0a76c2cb9848988e5def1b886460a129197f7 +size 13707