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 Apr 6, 2024
1 parent 6341acd commit aec6ca8
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 47 deletions.
3 changes: 2 additions & 1 deletion nQuantCpp/DivQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1066,7 +1066,8 @@ namespace DivQuant
pixelMap.clear();
nearestMap.clear();

return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

}
4 changes: 3 additions & 1 deletion nQuantCpp/Dl3Quantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,9 @@ namespace Dl3Quant
else if (pPalette->Entries[k] != m_transparentColor)
swap(pPalette->Entries[0], pPalette->Entries[1]);
}
return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);

pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

}
3 changes: 2 additions & 1 deletion nQuantCpp/EdgeAwareSQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -772,7 +772,8 @@ namespace EdgeAwareSQuant
}

pixelMap.clear();
return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

}
3 changes: 2 additions & 1 deletion nQuantCpp/MedianCut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,7 @@ namespace MedianCutQuant
closestMap.clear();
nearestMap.clear();

return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}
}
3 changes: 2 additions & 1 deletion nQuantCpp/MoDEQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,8 @@ namespace MoDEQuant
}
closestMap.clear();

return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

}
3 changes: 2 additions & 1 deletion nQuantCpp/NeuQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,7 @@ namespace NeuralNet
}

Clear();
return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}
}
3 changes: 2 additions & 1 deletion nQuantCpp/Otsu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ namespace OtsuThreshold
}

nearestMap.clear();
return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}
}
23 changes: 13 additions & 10 deletions nQuantCpp/PnnLABQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -625,12 +625,7 @@ namespace PnnLABQuant
pixelMap.clear();
clear();

auto pPaletteBytes = make_unique<BYTE[]>(sizeof(ColorPalette) + nMaxColors * sizeof(ARGB));
auto pPals = (ColorPalette*) pPaletteBytes.get();
pPals->Count = nMaxColors;
for (UINT k = 0; k < nMaxColors; ++k)
pPals->Entries[k] = pPalette[k];
return ProcessImagePixels(pDest, pPals, qPixels.get(), m_transparentPixelIndex >= 0);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

bool PnnLABQuantizer::QuantizeImage(Bitmap* pSource, Bitmap* pDest, UINT& nMaxColors, bool dither)
Expand All @@ -643,10 +638,18 @@ namespace PnnLABQuant
int semiTransCount = 0;
grabPixels(pSource, pixels, nMaxColors, hasSemiTransparency);

auto pPalettes = make_unique<ARGB[]>(nMaxColors);
auto pPalette = pPalettes.get();

return QuantizeImage(pixels, bitmapWidth, pPalette, pDest, nMaxColors, dither);
if (nMaxColors > 256) {
auto pPalettes = make_unique<ARGB[]>(nMaxColors);
auto pPalette = pPalettes.get();
return QuantizeImage(pixels, bitmapWidth, pPalette, pDest, nMaxColors, dither);
}

auto pPaletteBytes = make_unique<BYTE[]>(sizeof(ColorPalette) + nMaxColors * sizeof(ARGB));
auto pPalette = (ColorPalette*)pPaletteBytes.get();
pPalette->Count = nMaxColors;
auto result = QuantizeImage(pixels, bitmapWidth, pPalette->Entries, pDest, nMaxColors, dither);
pDest->SetPalette(pPalette);
return result;
}

}
56 changes: 33 additions & 23 deletions nQuantCpp/PnnQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,28 +378,18 @@ namespace PnnQuant

BlueNoise::dither(width, height, pixels, pPalette->Entries, nMaxColors, ditherFn, GetColorIndex, qPixels);
return true;
}
}

bool PnnQuantizer::QuantizeImage(Bitmap* pSource, Bitmap* pDest, UINT& nMaxColors, bool dither)
bool PnnQuantizer::QuantizeImage(const vector<ARGB>& pixels, const UINT bitmapWidth, ARGB* pPalette, Bitmap* pDest, UINT& nMaxColors, bool dither)
{
const auto bitmapWidth = pSource->GetWidth();
const auto bitmapHeight = pSource->GetHeight();
const auto area = (size_t) (bitmapWidth * bitmapHeight);

vector<ARGB> pixels(area);
int semiTransCount = 0;
GrabPixels(pSource, pixels, semiTransCount, m_transparentPixelIndex, m_transparentColor, alphaThreshold, nMaxColors);
hasSemiTransparency = semiTransCount > 0;

auto pPalettes = make_unique<ARGB[]>(nMaxColors);
auto pPalette = pPalettes.get();

if (nMaxColors <= 32)
PR = PG = PB = PA = 1;
else {
PR = coeffs[0][0]; PG = coeffs[0][1]; PB = coeffs[0][2];
}

const auto bitmapHeight = pixels.size() / bitmapWidth;

if (nMaxColors > 2)
pnnquan(pixels, pPalette, nMaxColors);
else {
Expand All @@ -412,15 +402,15 @@ namespace PnnQuant
pPalette[1] = Color::White;
}
}

DitherFn ditherFn = dither ? nearestColorIndex : closestColorIndex;
if (hasSemiTransparency)
weight *= -1;

vector<float> saliencies;
vector<float> saliencies;

if (nMaxColors > 256) {
auto qPixels = make_unique<ARGB[]>(area);
auto qPixels = make_unique<ARGB[]>(pixels.size());
Peano::GilbertCurve::dither(bitmapWidth, bitmapHeight, pixels.data(), pPalette, nMaxColors, ditherFn, GetColorIndex, qPixels.get(), saliencies.data(), weight);

closestMap.clear();
Expand All @@ -431,7 +421,7 @@ namespace PnnQuant
auto qPixels = make_unique<unsigned short[]>(pixels.size());
Peano::GilbertCurve::dither(bitmapWidth, bitmapHeight, pixels.data(), pPalette, nMaxColors, ditherFn, GetColorIndex, qPixels.get(), saliencies.data(), weight);

if(!dither)
if (!dither)
BlueNoise::dither(bitmapWidth, bitmapHeight, pixels.data(), pPalette, nMaxColors, ditherFn, GetColorIndex, qPixels.get());

if (m_transparentPixelIndex >= 0) {
Expand All @@ -444,12 +434,32 @@ namespace PnnQuant
closestMap.clear();
nearestMap.clear();

return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

bool PnnQuantizer::QuantizeImage(Bitmap* pSource, Bitmap* pDest, UINT& nMaxColors, bool dither)
{
const auto bitmapWidth = pSource->GetWidth();
const auto bitmapHeight = pSource->GetHeight();
const auto area = (size_t) (bitmapWidth * bitmapHeight);

vector<ARGB> pixels(area);
int semiTransCount = 0;
GrabPixels(pSource, pixels, semiTransCount, m_transparentPixelIndex, m_transparentColor, alphaThreshold, nMaxColors);
hasSemiTransparency = semiTransCount > 0;

if (nMaxColors > 256) {
auto pPalettes = make_unique<ARGB[]>(nMaxColors);
auto pPalette = pPalettes.get();
return QuantizeImage(pixels, bitmapWidth, pPalette, pDest, nMaxColors, dither);
}

auto pPaletteBytes = make_unique<BYTE[]>(sizeof(ColorPalette) + nMaxColors * sizeof(ARGB));
auto pPals = (ColorPalette*)pPaletteBytes.get();
pPals->Count = nMaxColors;
for (UINT k = 0; k < nMaxColors; ++k)
pPals->Entries[k] = pPalette[k];
return ProcessImagePixels(pDest, pPals, qPixels.get(), m_transparentPixelIndex >= 0);
auto pPalette = (ColorPalette*)pPaletteBytes.get();
pPalette->Count = nMaxColors;
auto result = QuantizeImage(pixels, bitmapWidth, pPalette->Entries, pDest, nMaxColors, dither);
pDest->SetPalette(pPalette);
return result;
}

}
1 change: 1 addition & 0 deletions nQuantCpp/PnnQuantizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace PnnQuant
class PnnQuantizer
{
public:
bool QuantizeImage(const vector<ARGB>& pixels, const UINT bitmapWidth, ARGB* pPalette, Bitmap* pDest, UINT& nMaxColors, bool dither = true);
bool QuantizeImage(Bitmap* pSource, Bitmap* pDest, UINT& nMaxColors, bool dither = true);
};
}
3 changes: 2 additions & 1 deletion nQuantCpp/SpatialQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,8 @@ namespace SpatialQuant
}
pixelMap.clear();

return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

}
3 changes: 2 additions & 1 deletion nQuantCpp/WuQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -929,7 +929,8 @@ namespace nQuant
closestMap.clear();
nearestMap.clear();

return ProcessImagePixels(pDest, pPalette, qPixels.get(), m_transparentPixelIndex >= 0);
pDest->SetPalette(pPalette);
return ProcessImagePixels(pDest, qPixels.get(), m_transparentPixelIndex >= 0);
}

}
6 changes: 2 additions & 4 deletions nQuantCpp/bitmapUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -736,7 +736,7 @@ bool ProcessImagePixels(Bitmap* pDest, const ARGB* qPixels, const bool& hasSemiT
return pDest->GetLastStatus() == Ok;
}

bool ProcessImagePixels(Bitmap* pDest, const ColorPalette* pPalette, const unsigned short* qPixels, const bool hasTransparent)
bool ProcessImagePixels(Bitmap* pDest, const unsigned short* qPixels, const bool hasTransparent)
{
if (hasTransparent) {
BYTE value = 0;
Expand All @@ -748,9 +748,7 @@ bool ProcessImagePixels(Bitmap* pDest, const ColorPalette* pPalette, const unsig
pPropertyItem.get()->value = &value;

pDest->SetPropertyItem(pPropertyItem.get());
}

pDest->SetPalette(pPalette);
}

BitmapData targetData;
UINT w = pDest->GetWidth();
Expand Down
2 changes: 1 addition & 1 deletion nQuantCpp/bitmapUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ bool dithering_image(const ARGB* pixels, const ColorPalette* pPalette, DitherFn

bool ProcessImagePixels(Bitmap* pDest, const ARGB* qPixels, const bool& hasSemiTransparency, const int& transparentPixelIndex);

bool ProcessImagePixels(Bitmap* pDest, const ColorPalette* pPalette, const unsigned short* qPixels, const bool hasTransparent);
bool ProcessImagePixels(Bitmap* pDest, const unsigned short* qPixels, const bool hasTransparent);

bool GrabPixels(Bitmap* pSource, vector<ARGB>& pixels, int& semiTransCount, int& transparentPixelIndex, ARGB& transparentColor, const BYTE alphaThreshold, const UINT nMaxColors = 2);

Expand Down

0 comments on commit aec6ca8

Please sign in to comment.