diff --git a/src/ImageProcessor.Playground/Program.cs b/src/ImageProcessor.Playground/Program.cs index bb6344a36..9e41509f7 100644 --- a/src/ImageProcessor.Playground/Program.cs +++ b/src/ImageProcessor.Playground/Program.cs @@ -50,12 +50,12 @@ namespace ImageProcessor.PlayGround } // Image mask = Image.FromFile(Path.Combine(resolvedPath, "mask2.png")); - FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "rgba.png")); - //IEnumerable files = GetFilesByExtensions(di, ".png"); + //FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "effect_24bit.png")); + IEnumerable files = GetFilesByExtensions(di, ".png"); //IEnumerable files = GetFilesByExtensions(di, ".gif", ".webp", ".bmp", ".jpg", ".png", ".tif"); - //foreach (FileInfo fileInfo in files) - //{ + foreach (FileInfo fileInfo in files) + { byte[] photoBytes = File.ReadAllBytes(fileInfo.FullName); Console.WriteLine("Processing: " + fileInfo.Name); @@ -94,7 +94,7 @@ namespace ImageProcessor.PlayGround //.EntropyCrop() //.Filter(MatrixFilters.Invert) //.Contrast(50) - //.Filter(MatrixFilters.Comic) + .Filter(MatrixFilters.Comic) //.Flip() //.Filter(MatrixFilters.HiSatch) //.Pixelate(8) @@ -109,7 +109,7 @@ namespace ImageProcessor.PlayGround Console.WriteLine(@"Completed {0} in {1:s\.fff} secs with peak memory usage of {2}.", fileInfo.Name, stopwatch.Elapsed, Process.GetCurrentProcess().PeakWorkingSet64.ToString("#,#")); //Console.WriteLine("Processed: " + fileInfo.Name + " in " + stopwatch.ElapsedMilliseconds + "ms"); - //} + } Console.ReadLine(); } diff --git a/src/ImageProcessor/Imaging/Formats/PngFormat.cs b/src/ImageProcessor/Imaging/Formats/PngFormat.cs index bc27f519e..000dde131 100644 --- a/src/ImageProcessor/Imaging/Formats/PngFormat.cs +++ b/src/ImageProcessor/Imaging/Formats/PngFormat.cs @@ -14,7 +14,6 @@ namespace ImageProcessor.Imaging.Formats using System.Drawing.Imaging; using System.IO; - using ImageProcessor.Common.Extensions; using ImageProcessor.Imaging.Quantizers; using ImageProcessor.Imaging.Quantizers.WuQuantizer; @@ -103,7 +102,6 @@ namespace ImageProcessor.Imaging.Formats // The Wu Quantizer expects a 32bbp image. if (Image.GetPixelFormatSize(image.PixelFormat) != 32) { - Bitmap clone = new Bitmap(image.Width, image.Height, PixelFormat.Format32bppPArgb); clone.SetResolution(image.HorizontalResolution, image.VerticalResolution); diff --git a/src/ImageProcessor/Imaging/Quantizers/OctreeQuantizer.cs b/src/ImageProcessor/Imaging/Quantizers/OctreeQuantizer.cs index 65aa828a5..3d3d1f595 100644 --- a/src/ImageProcessor/Imaging/Quantizers/OctreeQuantizer.cs +++ b/src/ImageProcessor/Imaging/Quantizers/OctreeQuantizer.cs @@ -106,7 +106,7 @@ namespace ImageProcessor.Imaging.Quantizers /// The new color palette protected override ColorPalette GetPalette(ColorPalette original) { - // First off convert the Octree to _maxColors colors + // First off convert the Octree to maxColors colors ArrayList palette = this.octree.Palletize(this.maxColors - 1); // Then convert the palette based on those colors diff --git a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/PaletteLookup.cs b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/PaletteLookup.cs index 31b747ed6..d808ecfa2 100644 --- a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/PaletteLookup.cs +++ b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/PaletteLookup.cs @@ -89,13 +89,13 @@ namespace ImageProcessor.Imaging.Quantizers.WuQuantizer int deltaAlpha = pixel.Alpha - lookupPixel.Alpha; int distance = deltaAlpha * deltaAlpha; - var deltaRed = pixel.Red - lookupPixel.Red; + int deltaRed = pixel.Red - lookupPixel.Red; distance += deltaRed * deltaRed; - var deltaGreen = pixel.Green - lookupPixel.Green; + int deltaGreen = pixel.Green - lookupPixel.Green; distance += deltaGreen * deltaGreen; - var deltaBlue = pixel.Blue - lookupPixel.Blue; + int deltaBlue = pixel.Blue - lookupPixel.Blue; distance += deltaBlue * deltaBlue; if (distance >= bestDistance) diff --git a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs index 038e2de3f..5fc498552 100644 --- a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs +++ b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs @@ -12,7 +12,9 @@ namespace ImageProcessor.Imaging.Quantizers.WuQuantizer using System.Drawing.Imaging; /// - /// The wu quantizer. + /// Encapsulates methods to calculate the color palette of an image using + /// a Wu color quantizer . + /// Adapted from /// public class WuQuantizer : WuQuantizerBase, IWuQuantizer { @@ -89,13 +91,14 @@ namespace ImageProcessor.Imaging.Quantizers.WuQuantizer { byte[] lineIndexes = new byte[image.Image.Width]; PaletteLookup lookup = new PaletteLookup(lookups); + foreach (Pixel[] pixelLine in image.PixelLines) { for (int pixelIndex = 0; pixelIndex < pixelLine.Length; pixelIndex++) { Pixel pixel = pixelLine[pixelIndex]; - byte bestMatch = AlphaColor; - if (pixel.Alpha >= alphaThreshold) + byte bestMatch = 0; + if (pixel.Alpha > alphaThreshold) { bestMatch = lookup.GetPaletteIndex(pixel); paletteHistogram[bestMatch].AddPixel(pixel); diff --git a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs index c34e9141d..57500718d 100644 --- a/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs +++ b/src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs @@ -1,4 +1,16 @@ -namespace ImageProcessor.Imaging.Quantizers.WuQuantizer +// -------------------------------------------------------------------------------------------------------------------- +// +// Copyright (c) James South. +// Licensed under the Apache License, Version 2.0. +// +// +// Encapsulates methods to calculate the color palette of an image using +// a Wu color quantizer . +// Adapted from +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace ImageProcessor.Imaging.Quantizers.WuQuantizer { using System; using System.Drawing; @@ -130,14 +142,14 @@ /// private static void BuildHistogram(Histogram histogram, ImageBuffer imageBuffer, int alphaThreshold, int alphaFader) { - ColorMoment[,,,] moments = histogram.Moments; + ColorMoment[, , ,] moments = histogram.Moments; foreach (Pixel[] pixelLine in imageBuffer.PixelLines) { foreach (Pixel pixel in pixelLine) { byte pixelAlpha = pixel.Alpha; - if (pixelAlpha >= alphaThreshold) + if (pixelAlpha > alphaThreshold) { if (pixelAlpha < 255) { @@ -156,6 +168,8 @@ moments[pixelAlpha, pixelRed, pixelGreen, pixelBlue].Add(pixel); } } + + moments[0, 0, 0, 0].Add(new Pixel(0, 255, 255, 255)); } } @@ -455,19 +469,24 @@ for (int cubeIndex = 0; cubeIndex < cubes.Length; cubeIndex++) { - var volume = Volume(cubes[cubeIndex], moments); + ColorMoment volume = Volume(cubes[cubeIndex], moments); + + if (volume.Weight <= 0) + { + continue; + } - if (volume.Weight <= 0) continue; + Pixel lookup = new Pixel + { + Alpha = (byte)(volume.Alpha / volume.Weight), + Red = (byte)(volume.Red / volume.Weight), + Green = (byte)(volume.Green / volume.Weight), + Blue = (byte)(volume.Blue / volume.Weight) + }; - var lookup = new Pixel - { - Alpha = (byte)(volume.Alpha / volume.Weight), - Red = (byte)(volume.Red / volume.Weight), - Green = (byte)(volume.Green / volume.Weight), - Blue = (byte)(volume.Blue / volume.Weight) - }; lookups[cubeIndex] = lookup; } + return lookups; }