From 257791531b976e00cc6c4b04af071aec684186da Mon Sep 17 00:00:00 2001 From: Miller Cy Chan Date: Thu, 27 Jul 2023 21:14:35 +0800 Subject: [PATCH] Add files via upload --- nQuant.Master/PnnLABGAQuantizer.cs | 24 ++++++++++++------------ nQuant.Master/PnnLABQuantizer.cs | 7 ++++--- 2 files changed, 16 insertions(+), 15 deletions(-) diff --git a/nQuant.Master/PnnLABGAQuantizer.cs b/nQuant.Master/PnnLABGAQuantizer.cs index 056e359..513de3d 100644 --- a/nQuant.Master/PnnLABGAQuantizer.cs +++ b/nQuant.Master/PnnLABGAQuantizer.cs @@ -6,7 +6,7 @@ using System.Linq; using System.Text; -/* Fast pairwise nearest neighbor based genetic algorithm with CIELAB color space genetic algorithm +/* Fast pairwise nearest neighbor based genetic algorithm with CIELAB color space Copyright (c) 2023 Miller Cy Chan * error measure; time used is proportional to number of bins squared - WJ */ @@ -50,7 +50,7 @@ public PnnLABGAQuantizer(PnnLABQuantizer pq, Bitmap source, int nMaxColors) var hasSemiTransparency = false; m_pixels = m_pq.GrabPixels(source, _nMaxColors, ref hasSemiTransparency); minRatio = (hasSemiTransparency || nMaxColors < 64) ? .0111 : .85; - maxRatio = Math.Min(1.0, nMaxColors / ((nMaxColors < 64) ? 500.0 : 50.0)); + maxRatio = Math.Min(1.0, nMaxColors / ((nMaxColors < 64) ? 400.0 : 50.0)); _dp = maxRatio < .1 ? 10000 : 100; } @@ -73,13 +73,13 @@ private string RatioKey if (difference <= 0.0000001) return sb.ToString(); - sb.Append(";").Append((int) (ratioY * _dp * 100)); + sb.Append(';').Append((int) (ratioY * _dp * 100)); return sb.ToString(); } - } - - protected delegate double AmplifyFn(double val); - private AmplifyFn GetAmplifyFn(bool tooSmall) + } + + protected delegate double AmplifyFn(double val); + private static AmplifyFn GetAmplifyFn(bool tooSmall) { if (tooSmall) return val => Math.PI * val; @@ -101,7 +101,7 @@ private void CalculateError(double[] errors) tooSmall = true; } - var amplifyFn = GetAmplifyFn(tooSmall); + var amplifyFn = PnnLABGAQuantizer.GetAmplifyFn(tooSmall); for (int i = 0; i < errors.Length; ++i) { if (i == 0 && errors[i] > maxError) @@ -222,7 +222,7 @@ public float Fitness get => (float) _fitness; } - private double RotateLeft(double u, double v, double delta) { + private double RotateLeft(double u, double v, double delta = 0.0) { var theta = Math.PI * Randrange(minRatio, maxRatio) / Math.Exp(delta); var result = u * Math.Sin(theta) + v * Math.Cos(theta); if(result <= minRatio || result >= maxRatio) @@ -230,7 +230,7 @@ private double RotateLeft(double u, double v, double delta) { return result; } - private double RotateRight(double u, double v, double delta) { + private double RotateRight(double u, double v, double delta = 0.0) { var theta = Math.PI * Randrange(minRatio, maxRatio) / Math.Exp(delta); var result = u * Math.Cos(theta) - v * Math.Sin(theta); if(result <= minRatio || result >= maxRatio) @@ -244,8 +244,8 @@ public PnnLABGAQuantizer Crossover(PnnLABGAQuantizer mother, int numberOfCrossov if (_random.Next(100) <= crossoverProbability) return child; - var ratioX = RotateRight(this.ratioX, mother.Ratios[1], 0.0); - var ratioY = RotateLeft(this.ratioY, mother.Ratios[0], 0.0); + var ratioX = RotateRight(this.ratioX, mother.Ratios[1]); + var ratioY = RotateLeft(this.ratioY, mother.Ratios[0]); child.SetRatio(ratioX, ratioY); child.CalculateFitness(); return child; diff --git a/nQuant.Master/PnnLABQuantizer.cs b/nQuant.Master/PnnLABQuantizer.cs index be9c99a..c2dca4c 100644 --- a/nQuant.Master/PnnLABQuantizer.cs +++ b/nQuant.Master/PnnLABQuantizer.cs @@ -524,7 +524,8 @@ public override ushort DitherColorIndex(Color[] palette, int pixel, int pos) private void Clear() { - closestMap.Clear(); + saliencies = null; + closestMap.Clear(); nearestMap.Clear(); } @@ -543,7 +544,8 @@ protected override int[] Dither(int[] pixels, Color[] palettes, int width, int h } pixelMap.Clear(); - return qPixels; + Clear(); + return qPixels; } internal Bitmap QuantizeImage(int[] pixels, int bitmapWidth, int nMaxColors, bool dither) @@ -595,7 +597,6 @@ internal Bitmap QuantizeImage(int[] pixels, int bitmapWidth, int nMaxColors, boo else if (m_palette[k] != m_transparentColor) BitmapUtilities.Swap(ref m_palette[0], ref m_palette[1]); } - Clear(); if (nMaxColors > 256) return BitmapUtilities.ProcessImagePixels(dest, qPixels, hasSemiTransparency, m_transparentPixelIndex);