Skip to content

Commit 3d46ee0

Browse files
authored
Add files via upload
1 parent fcea277 commit 3d46ee0

3 files changed

Lines changed: 34 additions & 34 deletions

File tree

nQuant.Master/GilbertCurve.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private GilbertCurve(int width, int height, int[] pixels, Color[] palette, int[]
6868
this.ditherable = ditherable;
6969
this.saliencies = saliencies;
7070
this.dither = dither;
71-
this.m_hasAlpha = weight < 0;
71+
this.m_hasAlpha = weight < 0;
7272

7373
errorq = new();
7474
this.weight = Math.Abs(weight);
@@ -83,7 +83,9 @@ private GilbertCurve(int width, int height, int[] pixels, Color[] palette, int[]
8383
else if (weight < .0015 || (palette.Length > 32 && palette.Length < 256))
8484
beta += .1f;
8585
if (palette.Length >= 64 && (weight > .012 && weight < .0125) || (weight > .025 && weight < .03))
86-
beta *= 2;
86+
beta += .05f;
87+
else if (palette.Length > 32 && palette.Length < 64 && weight < .015)
88+
beta = .55f;
8789
}
8890
else
8991
beta *= .95f;
@@ -92,10 +94,8 @@ private GilbertCurve(int width, int height, int[] pixels, Color[] palette, int[]
9294
beta *= .4f;
9395
if (palette.Length > 64 && weight < .02)
9496
beta = .2f;
95-
else if (palette.Length > 32 && palette.Length < 64 && weight < .015)
96-
beta = .55f;
9797

98-
DITHER_MAX = (byte)(weight < .015 ? (weight > .0025) ? 25 : 16 : 9);
98+
DITHER_MAX = (byte)(weight < .015 ? (weight > .0025) ? 25 : 16 : 9);
9999
var edge = m_hasAlpha ? 1 : Math.Exp(weight) + .25;
100100
var deviation = !m_hasAlpha && weight > .002 ? .25 : 1;
101101
ditherMax = (m_hasAlpha || DITHER_MAX > 9) ? (byte) BitmapUtilities.Sqr(Math.Sqrt(DITHER_MAX) + edge * deviation) : (byte)(DITHER_MAX * 1.5);
@@ -157,11 +157,11 @@ private int DitherPixel(int x, int y, Color c2, float beta)
157157
kappa = beta * NormalDistribution(beta, weight < .0008 ? 2.5f : 1.75f) * saliencies[bidx];
158158
else if (palette.Length >= 32 || CIELABConvertor.Y_Diff(c1, c2) > (beta * Math.PI * acceptedDiff))
159159
{
160-
if (saliencies[bidx] < .9)
160+
if (saliencies[bidx] < .9)
161161
kappa = beta * (!sortedByYDiff && weight < .0025 ? .55f : .5f) / saliencies[bidx];
162162
else
163-
kappa = beta * NormalDistribution(beta, !sortedByYDiff && weight < .0025 ? .55f : .5f) / saliencies[bidx];
164-
}
163+
kappa = beta * NormalDistribution(beta, !sortedByYDiff && weight < .0025 ? .55f : .5f) / saliencies[bidx];
164+
}
165165
}
166166

167167
c2 = BlueNoise.Diffuse(c1, palette[qPixelIndex], kappa, strength, x, y);

nQuant.Master/PnnLABQuantizer.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -347,15 +347,15 @@ internal override void Pnnquan(int[] pixels, ref Color[] palettes, ref int nMaxC
347347

348348
internal override ushort NearestColorIndex(Color[] palette, int pixel, int pos)
349349
{
350-
var nMaxColors = palette.Length;
351-
int offset = nMaxColors > 32 ? pixel : GetColorIndex(pixel);
352-
if (nearestMap.TryGetValue(offset, out var k))
350+
var nMaxColors = palette.Length;
351+
int offset = weight > .015 ? pixel : GetColorIndex(pixel);
352+
if (nearestMap.TryGetValue(offset, out var k))
353353
return k;
354354

355355
var c = Color.FromArgb(pixel);
356356
if (c.A <= alphaThreshold)
357-
return k;
358-
if (palette.Length > 2 && HasAlpha && c.A > alphaThreshold)
357+
return k;
358+
if (palette.Length > 2 && HasAlpha && c.A > alphaThreshold)
359359
k = 1;
360360

361361
double mindist = 1e100;
@@ -427,9 +427,9 @@ internal override ushort NearestColorIndex(Color[] palette, int pixel, int pos)
427427

428428
internal ushort HybridColorIndex(Color[] palette, int pixel, int pos)
429429
{
430-
var nMaxColors = palette.Length;
431-
int offset = nMaxColors > 32 ? pixel : GetColorIndex(pixel);
432-
if (nearestMap.TryGetValue(offset, out var k))
430+
var nMaxColors = palette.Length;
431+
int offset = weight > .015 ? pixel : GetColorIndex(pixel);
432+
if (nearestMap.TryGetValue(offset, out var k))
433433
return k;
434434

435435
var c = Color.FromArgb(pixel);
@@ -486,16 +486,16 @@ internal ushort HybridColorIndex(Color[] palette, int pixel, int pos)
486486

487487
protected override ushort ClosestColorIndex(Color[] palette, int pixel, int pos)
488488
{
489-
if (PG < coeffs[0, 1] && BlueNoise.TELL_BLUE_NOISE[pos & 4095] > -88)
490-
return HybridColorIndex(palette, pixel, pos);
489+
if (PG < coeffs[0, 1] && BlueNoise.TELL_BLUE_NOISE[pos & 4095] > -88)
490+
return HybridColorIndex(palette, pixel, pos);
491491

492-
var c = Color.FromArgb(pixel);
492+
var c = Color.FromArgb(pixel);
493493
if (c.A <= alphaThreshold)
494494
return NearestColorIndex(palette, pixel, pos);
495495

496-
var nMaxColors = palette.Length;
497-
int offset = nMaxColors > 32 ? pixel : GetColorIndex(pixel);
498-
if (!closestMap.TryGetValue(offset, out var closest))
496+
var nMaxColors = palette.Length;
497+
int offset = weight > .015 ? pixel : GetColorIndex(pixel);
498+
if (!closestMap.TryGetValue(offset, out var closest))
499499
{
500500
closest = new ushort[4];
501501
closest[2] = closest[3] = ushort.MaxValue;

nQuant.Master/PnnQuantizer.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
/* Fast pairwise nearest neighbor based algorithm for multilevel thresholding
88
Copyright (C) 2004-2016 Mark Tyler and Dmitry Groshev
9-
Copyright (c) 2018-2023 Miller Cy Chan
9+
Copyright (c) 2018-2026 Miller Cy Chan
1010
* error measure; time used is proportional to number of bins squared - WJ */
1111

1212
namespace PnnQuant
@@ -282,7 +282,7 @@ internal virtual void Pnnquan(int[] pixels, ref Color[] palettes, ref int nMaxCo
282282
internal virtual ushort NearestColorIndex(Color[] palette, int pixel, int pos)
283283
{
284284
var nMaxColors = palette.Length;
285-
int offset = nMaxColors > 32 ? pixel : GetColorIndex(pixel);
285+
int offset = weight > .015 ? pixel : GetColorIndex(pixel);
286286
if (nearestMap.TryGetValue(offset, out var k))
287287
return k;
288288

@@ -328,10 +328,10 @@ protected virtual ushort ClosestColorIndex(Color[] palette, int pixel, int pos)
328328
ushort k = 0;
329329
var c = Color.FromArgb(pixel);
330330
if (c.A <= alphaThreshold)
331-
return NearestColorIndex(palette, pixel, pos);
332-
331+
return NearestColorIndex(palette, pixel, pos);
332+
333333
var nMaxColors = palette.Length;
334-
int offset = nMaxColors > 32 ? pixel : GetColorIndex(pixel);
334+
int offset = weight > .015 ? pixel : GetColorIndex(pixel);
335335
if (!closestMap.TryGetValue(offset, out var closest))
336336
{
337337
closest = new ushort[4];
@@ -476,13 +476,13 @@ public Bitmap QuantizeImage(Bitmap source, PixelFormat pixelFormat, int nMaxColo
476476
m_palette = palettes;
477477

478478
if (m_transparentPixelIndex >= 0)
479-
{
480-
var k = NearestColorIndex(m_palette, pixels[m_transparentPixelIndex], m_transparentPixelIndex);
481-
if (nMaxColors > 2)
482-
m_palette[0] = m_transparentColor;
483-
else if (m_palette[k] != m_transparentColor)
484-
BitmapUtilities.Swap(ref m_palette[0], ref m_palette[1]);
485-
}
479+
{
480+
var k = NearestColorIndex(m_palette, pixels[m_transparentPixelIndex], m_transparentPixelIndex);
481+
if (nMaxColors > 2)
482+
m_palette[0] = m_transparentColor;
483+
else if (m_palette[k] != m_transparentColor)
484+
BitmapUtilities.Swap(ref m_palette[0], ref m_palette[1]);
485+
}
486486

487487
var qPixels = Dither(pixels, m_palette, bitmapWidth, bitmapHeight, dither);
488488

0 commit comments

Comments
 (0)