From 5e0fa4eba32ca784ff1a84c32ee97913d3bb7c35 Mon Sep 17 00:00:00 2001 From: James South Date: Fri, 7 Nov 2014 00:45:59 +0000 Subject: [PATCH] Fixed Wu Quantizer transparency Former-commit-id: 79819485015944b40893f385643943b566fba85a Former-commit-id: 253f7dd509357930ba5aacc2a5d40c1938161810 --- src/ImageProcessor.Playground/Program.cs | 2 +- .../images/input/cow_PNG2140.png.REMOVED.git-id | 1 + .../input/eye-162182_1280.png.REMOVED.git-id | 1 + .../images/input/grad.png.REMOVED.git-id | 1 + .../images/input/rose_PNG658.png.REMOVED.git-id | 1 + src/ImageProcessor/Imaging/Formats/PngFormat.cs | 14 +++++++------- .../Imaging/Quantizers/WuQuantizer/WuQuantizer.cs | 4 +++- .../Quantizers/WuQuantizer/WuQuantizerBase.cs | 5 +++-- 8 files changed, 18 insertions(+), 11 deletions(-) create mode 100644 src/ImageProcessor.Playground/images/input/cow_PNG2140.png.REMOVED.git-id create mode 100644 src/ImageProcessor.Playground/images/input/eye-162182_1280.png.REMOVED.git-id create mode 100644 src/ImageProcessor.Playground/images/input/grad.png.REMOVED.git-id create mode 100644 src/ImageProcessor.Playground/images/input/rose_PNG658.png.REMOVED.git-id diff --git a/src/ImageProcessor.Playground/Program.cs b/src/ImageProcessor.Playground/Program.cs index 9e41509f7..8fc789a8a 100644 --- a/src/ImageProcessor.Playground/Program.cs +++ b/src/ImageProcessor.Playground/Program.cs @@ -50,7 +50,7 @@ namespace ImageProcessor.PlayGround } // Image mask = Image.FromFile(Path.Combine(resolvedPath, "mask2.png")); - //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "effect_24bit.png")); + //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "circle.png")); IEnumerable files = GetFilesByExtensions(di, ".png"); //IEnumerable files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif"); diff --git a/src/ImageProcessor.Playground/images/input/cow_PNG2140.png.REMOVED.git-id b/src/ImageProcessor.Playground/images/input/cow_PNG2140.png.REMOVED.git-id new file mode 100644 index 000000000..5932019f7 --- /dev/null +++ b/src/ImageProcessor.Playground/images/input/cow_PNG2140.png.REMOVED.git-id @@ -0,0 +1 @@ +04b381ec37fffdbffa55d5b02478ad81375eaa4b \ No newline at end of file diff --git a/src/ImageProcessor.Playground/images/input/eye-162182_1280.png.REMOVED.git-id b/src/ImageProcessor.Playground/images/input/eye-162182_1280.png.REMOVED.git-id new file mode 100644 index 000000000..0c0b81578 --- /dev/null +++ b/src/ImageProcessor.Playground/images/input/eye-162182_1280.png.REMOVED.git-id @@ -0,0 +1 @@ +0693daaf5dc3451fcad6b9ad910fbdfcfa3f9882 \ No newline at end of file diff --git a/src/ImageProcessor.Playground/images/input/grad.png.REMOVED.git-id b/src/ImageProcessor.Playground/images/input/grad.png.REMOVED.git-id new file mode 100644 index 000000000..e19918107 --- /dev/null +++ b/src/ImageProcessor.Playground/images/input/grad.png.REMOVED.git-id @@ -0,0 +1 @@ +c30305a6617a68a2f5671b6a69dbe832d9f73e08 \ No newline at end of file diff --git a/src/ImageProcessor.Playground/images/input/rose_PNG658.png.REMOVED.git-id b/src/ImageProcessor.Playground/images/input/rose_PNG658.png.REMOVED.git-id new file mode 100644 index 000000000..a0609f374 --- /dev/null +++ b/src/ImageProcessor.Playground/images/input/rose_PNG658.png.REMOVED.git-id @@ -0,0 +1 @@ +60379fd852541c3f736148862adf54691f39fdee \ No newline at end of file diff --git a/src/ImageProcessor/Imaging/Formats/PngFormat.cs b/src/ImageProcessor/Imaging/Formats/PngFormat.cs index 000dde131..88a187d7e 100644 --- a/src/ImageProcessor/Imaging/Formats/PngFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/PngFormat.cs @@ -100,8 +100,8 @@ namespace ImageProcessor.Imaging.Formats if (this.IsIndexed) { // The Wu Quantizer expects a 32bbp image. - if (Image.GetPixelFormatSize(image.PixelFormat) != 32) - { + //if (Image.GetPixelFormatSize(image.PixelFormat) != 32) + //{ Bitmap clone = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); clone.SetResolution(image.HorizontalResolution, image.VerticalResolution); @@ -116,11 +116,11 @@ namespace ImageProcessor.Imaging.Formats image = new WuQuantizer().QuantizeImage(clone); // image = new OctreeQuantizer(255, 8).Quantize(image); - } - else - { - image = new WuQuantizer().QuantizeImage((Bitmap)image); - } + //} + //else + //{ + // image = new WuQuantizer().QuantizeImage((Bitmap)image); + //} } return base.Save(path, image); diff --git a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs index 5fc498552..7a103d054 100644 --- a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs +++ b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs @@ -92,12 +92,14 @@ namespace ImageProcessor.Imaging.Quantizers.WuQuantizer byte[] lineIndexes = new byte[image.Image.Width]; PaletteLookup lookup = new PaletteLookup(lookups); + // Determine the correct fallback color. + byte fallback = (byte)(lookups.Length < AlphaColor ? 0 : AlphaColor); foreach (Pixel[] pixelLine in image.PixelLines) { for (int pixelIndex = 0; pixelIndex < pixelLine.Length; pixelIndex++) { Pixel pixel = pixelLine[pixelIndex]; - byte bestMatch = 0; + byte bestMatch = fallback; if (pixel.Alpha > alphaThreshold) { bestMatch = lookup.GetPaletteIndex(pixel); diff --git a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs index 57500718d..00a7934fa 100644 --- a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs +++ b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs @@ -168,9 +168,10 @@ namespace ImageProcessor.Imaging.Quantizers.WuQuantizer moments[pixelAlpha, pixelRed, pixelGreen, pixelBlue].Add(pixel); } } - - moments[0, 0, 0, 0].Add(new Pixel(0, 255, 255, 255)); } + + // Set a default pixel for images with less than 256 colors. + moments[0, 0, 0, 0].Add(new Pixel(0, 0, 0, 0)); } private static void CalculateMoments(ColorMoment[, , ,] moments)