Browse Source

Attempted fix for transparency palette

Former-commit-id: e3e3fc74b8fe73b1e0c27ef4aaa343a82e54e2b9
Former-commit-id: 16dde1e9798765762fa9314cd77e210081d2dd0a
af/merge-core
James South 11 years ago
parent
commit
2d48566c2f
  1. 12
      src/ImageProcessor.Playground/Program.cs
  2. 2
      src/ImageProcessor/Imaging/Formats/PngFormat.cs
  3. 2
      src/ImageProcessor/Imaging/Quantizers/OctreeQuantizer.cs
  4. 6
      src/ImageProcessor/Imaging/Quantizers/WuQuantizer/PaletteLookup.cs
  5. 9
      src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs
  6. 43
      src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs

12
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<FileInfo> files = GetFilesByExtensions(di, ".png");
//FileInfo fileInfo = new FileInfo(Path.Combine(resolvedPath, "effect_24bit.png"));
IEnumerable<FileInfo> files = GetFilesByExtensions(di, ".png");
//IEnumerable<FileInfo> 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();
}

2
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);

2
src/ImageProcessor/Imaging/Quantizers/OctreeQuantizer.cs

@ -106,7 +106,7 @@ namespace ImageProcessor.Imaging.Quantizers
/// <returns>The new color palette</returns>
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

6
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)

9
src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizer.cs

@ -12,7 +12,9 @@ namespace ImageProcessor.Imaging.Quantizers.WuQuantizer
using System.Drawing.Imaging;
/// <summary>
/// The wu quantizer.
/// Encapsulates methods to calculate the color palette of an image using
/// a Wu color quantizer <see href="http://www.ece.mcmaster.ca/~xwu/cq.c"/>.
/// Adapted from <see href="https://github.com/drewnoakes"/>
/// </summary>
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);

43
src/ImageProcessor/Imaging/Quantizers/WuQuantizer/WuQuantizerBase.cs

@ -1,4 +1,16 @@
namespace ImageProcessor.Imaging.Quantizers.WuQuantizer
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="WuQuantizerBase.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to calculate the color palette of an image using
// a Wu color quantizer <see href="http://www.ece.mcmaster.ca/~xwu/cq.c" />.
// Adapted from <see href="https://github.com/drewnoakes" />
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Imaging.Quantizers.WuQuantizer
{
using System;
using System.Drawing;
@ -130,14 +142,14 @@
/// </param>
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;
}

Loading…
Cancel
Save