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 Sep 20, 2023
1 parent 77575af commit 12dbcbd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 41 deletions.
3 changes: 2 additions & 1 deletion nQuantCpp/CIELABConvertor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#define _USE_MATH_DEFINES
#include <algorithm>
#include <math.h>
#include <iostream>

using namespace std;

Expand Down Expand Up @@ -208,5 +209,5 @@ double CIELABConvertor::Y_Diff(const Color& c1, const Color& c2)

auto y = color2Y(c1);
auto y2 = color2Y(c2);
return abs(y2 - y) / XYZ_WHITE_REFERENCE_Y;
return abs(y2 - y) * XYZ_WHITE_REFERENCE_Y;
}
8 changes: 4 additions & 4 deletions nQuantCpp/GilbertCurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ namespace Peano
auto denoise = m_pPalette->Count > 2;
auto diffuse = BlueNoise::TELL_BLUE_NOISE[bidx & 4095] > thresold;
auto yDiff = diffuse ? 1 : CIELABConvertor::Y_Diff(pixel, c2);
auto illusion = !diffuse && BlueNoise::TELL_BLUE_NOISE[(int)(yDiff * 4096)] > thresold;
auto illusion = !diffuse && BlueNoise::TELL_BLUE_NOISE[(int)(yDiff * 4096) & 4095] > thresold;

int errLength = denoise ? error.length() - 1 : 0;
for (int j = 0; j < errLength; ++j) {
Expand All @@ -109,9 +109,9 @@ namespace Peano
error[j] = (float)tanh(error.p[j] / maxErr * 8) * (ditherMax - 1);
else {
if (illusion)
error[j] /= (float)(1 + _sqrt(ditherMax));
error[j] = (float)(error.p[j] / maxErr * yDiff) * (ditherMax - 1);
else
error[j] = (float)(error.p[j] / maxErr * yDiff) * (ditherMax - 1);
error[j] /= (float)(1 + _sqrt(ditherMax));
}
}
}
Expand Down Expand Up @@ -190,7 +190,7 @@ namespace Peano
ditherMax = (hasAlpha || DITHER_MAX > 9) ? (BYTE)sqr(_sqrt(DITHER_MAX) + edge) : DITHER_MAX;
if (pPalette->Count / weight > 5000 && (weight > .045 || (weight > .01 && pPalette->Count <= 64)))
ditherMax = (BYTE)sqr(4.75);
thresold = DITHER_MAX > 9 ? -112 : -88;
thresold = DITHER_MAX > 9 ? -112 : -64;
auto pWeights = make_unique<float[]>(DITHER_MAX);
m_weights = pWeights.get();
auto pLookup = make_unique<short[]>(USHRT_MAX + 1);
Expand Down
68 changes: 35 additions & 33 deletions nQuantCpp/PnnLABGAQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once
#include "PnnLABQuantizer.h"
#include "APNsgaIII.h"

#include <string>

Expand All @@ -23,42 +24,43 @@ namespace PnnLABQuant

class PnnLABGAQuantizer
{
private:
//Asserts floating point compatibility at compile time
static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 required");
private:
//Asserts floating point compatibility at compile time
static_assert(std::numeric_limits<float>::is_iec559, "IEEE 754 required");

double _fitness = -numeric_limits<double>::infinity();
double _ratioX = 0, _ratioY = 0;
vector<double> _convertedObjectives;
vector<double> _objectives;
vector<vector<ARGB> > m_pixelsList;
unique_ptr<PnnLABQuantizer> m_pq;
double _fitness = -numeric_limits<double>::infinity();
double _ratioX = 0, _ratioY = 0;
vector<double> _convertedObjectives;
vector<double> _objectives;
vector<vector<ARGB> > m_pixelsList;
unique_ptr<PnnLABQuantizer> m_pq;

void calculateError(vector<double>& errors);
void calculateFitness();
string getRatioKey() const;
auto findByRatioKey(const string& ratioKey) const;
void clear();
void calculateError(vector<double>& errors);
void calculateFitness();
string getRatioKey() const;
auto findByRatioKey(const string& ratioKey) const;
void clear();

public:
PnnLABGAQuantizer(PnnLABQuantizer& pq, const vector<shared_ptr<Bitmap> >& pSources, UINT nMaxColors);
PnnLABGAQuantizer(PnnLABQuantizer& pq, const vector<vector<ARGB> >& pixelsList, const vector<UINT>& bitmapWidths, UINT nMaxColors);

float getFitness();
shared_ptr<PnnLABGAQuantizer> crossover(const PnnLABGAQuantizer& mother, int numberOfCrossoverPoints, float crossoverProbability);
bool dominates(const PnnLABGAQuantizer* right);
void mutation(int mutationSize, float mutationProbability);
vector<double> getObjectives() const;
vector<double>& getConvertedObjectives();
void resizeConvertedObjectives(int numObj);
shared_ptr<PnnLABGAQuantizer> makeNewFromPrototype();
public:
PnnLABGAQuantizer(PnnLABQuantizer& pq, const vector<shared_ptr<Bitmap> >& pSources, UINT nMaxColors);
PnnLABGAQuantizer(PnnLABQuantizer& pq, const vector<vector<ARGB> >& pixelsList, const vector<UINT>& bitmapWidths, UINT nMaxColors);

UINT getMaxColors() const;
string getResult() const;
bool hasAlpha() const {
return m_pq->hasAlpha();
}
void setRatio(double ratioX, double ratioY);
bool QuantizeImage(vector<shared_ptr<Bitmap> >& pBitmaps, bool dither = true);
float getFitness();
shared_ptr<PnnLABGAQuantizer> crossover(const PnnLABGAQuantizer& mother, int numberOfCrossoverPoints, float crossoverProbability);
bool dominates(const PnnLABGAQuantizer* right);
void mutation(int mutationSize, float mutationProbability);
vector<double> getObjectives() const;
vector<double>& getConvertedObjectives();
void resizeConvertedObjectives(int numObj);
shared_ptr<PnnLABGAQuantizer> makeNewFromPrototype();

UINT getMaxColors() const;
string getResult() const;
bool hasAlpha() const {
return m_pq->hasAlpha();
}
void setRatio(double ratioX, double ratioY);
bool QuantizeImage(vector<shared_ptr<Bitmap> >& pBitmaps, bool dither = true);
};

}
5 changes: 2 additions & 3 deletions nQuantCpp/nQuantCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ namespace fs = std::filesystem;
#include "PnnQuantizer.h"
#include "NeuQuantizer.h"
#include "WuQuantizer.h"
#include "APNsgaIII.h"
#include "PnnLABQuantizer.h"
#include "PnnLABGAQuantizer.h"
#include "EdgeAwareSQuantizer.h"
Expand Down Expand Up @@ -197,7 +196,7 @@ bool QuantizeImage(const wstring& algorithm, const wstring& sourceFile, wstring&
PnnLABQuant::PnnLABQuantizer pnnLABQuantizer;
vector<shared_ptr<Bitmap> > sources(1, pSource);
PnnLABQuant::PnnLABGAQuantizer pnnLABGAQuantizer(pnnLABQuantizer, sources, nMaxColors);
nQuantGA::APNsgaIII<PnnLABQuant::PnnLABGAQuantizer> alg(pnnLABGAQuantizer);
nQuantGA::APNsgaIII alg(pnnLABGAQuantizer);
alg.run(9999, -numeric_limits<double>::epsilon());
auto pGAq = alg.getResult();
tcout << L"\n" << pGAq->getResult().c_str() << endl;
Expand Down Expand Up @@ -267,7 +266,7 @@ void OutputImages(const fs::path& sourceDir, wstring& targetDir, const UINT& nMa

PnnLABQuant::PnnLABQuantizer pnnLABQuantizer;
PnnLABQuant::PnnLABGAQuantizer pnnLABGAQuantizer(pnnLABQuantizer, pSources, nMaxColors);
nQuantGA::APNsgaIII<PnnLABQuant::PnnLABGAQuantizer> alg(pnnLABGAQuantizer);
nQuantGA::APNsgaIII alg(pnnLABGAQuantizer);
alg.run(9999, -numeric_limits<double>::epsilon());
auto pGAq = alg.getResult();
tcout << L"\n" << pGAq->getResult().c_str() << endl;
Expand Down

0 comments on commit 12dbcbd

Please sign in to comment.