Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
mcychan authored Jul 27, 2023
1 parent d63b04b commit 2577915
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
24 changes: 12 additions & 12 deletions nQuant.Master/PnnLABGAQuantizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 */

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
Expand All @@ -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)
Expand Down Expand Up @@ -222,15 +222,15 @@ 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)
result = RotateLeft(u, v, delta + .5);
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)
Expand All @@ -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;
Expand Down
7 changes: 4 additions & 3 deletions nQuant.Master/PnnLABQuantizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 2577915

Please sign in to comment.