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 Dec 30, 2024
1 parent eca2dc3 commit ca1da14
Show file tree
Hide file tree
Showing 24 changed files with 66 additions and 76 deletions.
16 changes: 16 additions & 0 deletions nQuantCpp/CIELABConvertor.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,20 @@
#pragma once
#ifndef _WIN32
#include <guiddef.h>
#include <gdiplus.h>
using namespace Gdiplus;

#ifndef ARGB
typedef unsigned long ARGB;
#endif
#ifndef BYTE
typedef unsigned char BYTE;
#endif
#ifndef BYTE_MAX
#include <climits>
#define BYTE_MAX UCHAR_MAX
#endif
#endif

class CIELABConvertor
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/DivQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include "stdafx.h"
#include "DivQuantizer.h"
#include "bitmapUtilities.h"
#include "CIELABConvertor.h"
#include "BlueNoise.h"
#include <algorithm>
Expand Down
2 changes: 2 additions & 0 deletions nQuantCpp/DivQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#pragma once
#include "bitmapUtilities.h"

#include <memory>
#include <vector>
using namespace std;
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/Dl3Quantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

#include "stdafx.h"
#include "Dl3Quantizer.h"
#include "bitmapUtilities.h"
#include "BlueNoise.h"
#include <unordered_map>

Expand Down
4 changes: 1 addition & 3 deletions nQuantCpp/Dl3Quantizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <memory>
#include <vector>
using namespace std;
#include "bitmapUtilities.h"

namespace Dl3Quant
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/EdgeAwareSQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include "stdafx.h"
#include "EdgeAwareSQuantizer.h"
#include "MedianCut.h"
#include "bitmapUtilities.h"
#include "CIELABConvertor.h"
#include "GilbertCurve.h"

Expand Down
5 changes: 1 addition & 4 deletions nQuantCpp/EdgeAwareSQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#pragma once
#include <memory>
#include <vector>

using namespace std;
#include "bitmapUtilities.h"

namespace EdgeAwareSQuant
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/MedianCut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include "stdafx.h"
#include "MedianCut.h"
#include "bitmapUtilities.h"
#include "CIELABConvertor.h"
#include "BlueNoise.h"
#include <unordered_map>
Expand Down
3 changes: 0 additions & 3 deletions nQuantCpp/MedianCut.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,9 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#pragma once
#include <string>
#include <memory>
#include <vector>
#include <limits>
#include "EdgeAwareSQuantizer.h"

using namespace std;
using namespace EdgeAwareSQuant;

//#define VITER_CACHE_LINE_GAP ((64+sizeof(viter_state)-1)/sizeof(viter_state))
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/MoDEQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Copyright (c) 2018 - 2021 Miller Cy Chan

#include "stdafx.h"
#include "MoDEQuantizer.h"
#include "bitmapUtilities.h"
#include <ctime>
#include <iomanip> // std::setprecision
#include <unordered_map>
Expand Down
4 changes: 1 addition & 3 deletions nQuantCpp/MoDEQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <memory>
#include <vector>
using namespace std;
#include "bitmapUtilities.h"

namespace MoDEQuant
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/NeuQuantizer.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "stdafx.h"
#include "NeuQuantizer.h"
#include "bitmapUtilities.h"
#include "CIELABConvertor.h"
#include <unordered_map>

Expand Down
4 changes: 1 addition & 3 deletions nQuantCpp/NeuQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <memory>
#include <vector>
using namespace std;
#include "bitmapUtilities.h"

namespace NeuralNet
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/Otsu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include "stdafx.h"
#include "Otsu.h"
#include "bitmapUtilities.h"
#include "GilbertCurve.h"
#define _USE_MATH_DEFINES
#include <math.h>
Expand Down
1 change: 1 addition & 0 deletions nQuantCpp/Otsu.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include "bitmapUtilities.h"

namespace OtsuThreshold
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/PnnQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Copyright (c) 2018-2024 Miller Cy Chan

#include "stdafx.h"
#include "PnnQuantizer.h"
#include "bitmapUtilities.h"
#include "GilbertCurve.h"
#include "BlueNoise.h"
#include <unordered_map>
Expand Down
4 changes: 1 addition & 3 deletions nQuantCpp/PnnQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <memory>
#include <vector>
using namespace std;
#include "bitmapUtilities.h"

namespace PnnQuant
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/SpatialQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

#include "stdafx.h"
#include "SpatialQuantizer.h"
#include "bitmapUtilities.h"
#include "CIELABConvertor.h"
#include "GilbertCurve.h"

Expand Down
4 changes: 1 addition & 3 deletions nQuantCpp/SpatialQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <memory>
#include <vector>
using namespace std;
#include "bitmapUtilities.h"

namespace SpatialQuant
{
Expand Down
1 change: 0 additions & 1 deletion nQuantCpp/WuQuantizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

#include "stdafx.h"
#include "WuQuantizer.h"
#include "bitmapUtilities.h"
#include "BlueNoise.h"
#include <unordered_map>

Expand Down
4 changes: 1 addition & 3 deletions nQuantCpp/WuQuantizer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once
#include <memory>
#include <vector>
using namespace std;
#include "bitmapUtilities.h"

// =============================================================
// Quantizer objects and functions
Expand Down
8 changes: 8 additions & 0 deletions nQuantCpp/bitmapUtilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
#include <vector>
using namespace std;

#ifndef _WIN32
#include <guiddef.h>
#include <minwindef.h>
#include <gdiplus.h>
using namespace Gdiplus;
#endif


//////////////////////////////////////////////////////////////////////////
//
// GetBitmapHeaderSize
Expand Down
60 changes: 27 additions & 33 deletions nQuantCpp/nQuantCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ namespace fs = std::filesystem;
#define new DEBUG_NEW
#endif

#ifdef UNICODE
auto& tcout = std::wcout;
#else
auto& tcout = std::cout;
#endif

GdiplusStartupInput m_gdiplusStartupInput;
ULONG_PTR m_gdiplusToken;

Expand All @@ -48,19 +42,19 @@ unordered_map<LPCWSTR, CLSID> extensionMap;

void PrintUsage()
{
tcout << endl;
tcout << "usage: nQuantCpp <input image path> [options]" << endl;
tcout << endl;
tcout << "Valid options:" << endl;
tcout << " /a : Algorithm used - Choose one of them, otherwise give you the defaults from [";
wcout << endl;
wcout << "usage: nQuantCpp <input image path> [options]" << endl;
wcout << endl;
wcout << "Valid options:" << endl;
wcout << " /a : Algorithm used - Choose one of them, otherwise give you the defaults from [";
int i = 0;
for(; i<sizeof(algs)/sizeof(string) - 1; ++i)
tcout << algs[i] << ", ";
tcout << algs[i] << "] ." << endl;
tcout << " /m : Max Colors (pixel-depth) - Maximum number of colors for the output format to support. The default is 256 (8-bit)." << endl;
tcout << " /d : Dithering or not? y or n." << endl;
tcout << " /f : Frame delay in milliseconds for PNNLAB+ only." << endl;
tcout << " /o : Output image file dir. The default is <source image path directory>" << endl;
wcout << algs[i] << ", ";
wcout << algs[i] << "] ." << endl;
wcout << " /m : Max Colors (pixel-depth) - Maximum number of colors for the output format to support. The default is 256 (8-bit)." << endl;
wcout << " /d : Dithering or not? y or n." << endl;
wcout << " /f : Frame delay in milliseconds for PNNLAB+ only." << endl;
wcout << " /o : Output image file dir. The default is <source image path directory>" << endl;
}

bool isdigit(const wchar_t* chars, const bool positiveOnly = true) {
Expand Down Expand Up @@ -175,9 +169,9 @@ bool OutputImage(const fs::path& sourcePath, const wstring& algorithm, const UIN
destPath += std::to_wstring(nMaxColors) + targetExtension;
auto status = pDest->Save(destPath.c_str(), &extensionMap[targetExtension]);
if (status == Status::Ok)
tcout << L"Converted image: " << destPath << endl;
wcout << L"Converted image: " << destPath << endl;
else
tcout << L"Failed to save image in '" << destPath << L"' file" << endl;
wcout << L"Failed to save image in '" << destPath << L"' file" << endl;

return status == Status::Ok;
}
Expand All @@ -203,7 +197,7 @@ bool QuantizeImage(const wstring& algorithm, const wstring& sourceFile, wstring&
nQuantGA::APNsgaIII alg(pnnLABGAQuantizer);
alg.run(9999, -numeric_limits<double>::epsilon());
auto pGAq = alg.getResult();
tcout << L"\n" << pGAq->getResult().c_str() << endl;
wcout << L"\n" << pGAq->getResult().c_str() << endl;
vector<shared_ptr<Bitmap> > dests;
dests.emplace_back(pDest);
bSucceeded = pGAq->QuantizeImage(dests, dither);
Expand Down Expand Up @@ -287,7 +281,7 @@ static void OutputImages(const fs::path& sourceDir, wstring& targetDir, const UI
for (int i = 0; i < pSources.size(); ++i) {
ostringstream ss;
ss << "\r" << i << " of " << pSources.size() << " completed." << showpoint;
tcout << ss.str().c_str();
wcout << ss.str().c_str();

PnnLABQuant::PnnLABQuantizer pnnLABQuantizer;
pnnLABQuantizer.QuantizeImage(pSources[i].get(), pDests[i].get(), maxColors, dither);
Expand All @@ -300,20 +294,20 @@ static void OutputImages(const fs::path& sourceDir, wstring& targetDir, const UI
for (int i = 0; i < pSources.size(); ++i) {
ostringstream ss;
ss << "\r" << i << " of " << pSources.size() << " completed." << showpoint;
tcout << ss.str().c_str();
wcout << ss.str().c_str();

PnnQuant::PnnQuantizer pnnQuantizer;
pnnQuantizer.QuantizeImage(pSources[i].get(), pDests[i].get(), maxColors, dither);
}
}
tcout << L"\rWell done!!! " << endl;
wcout << L"\rWell done!!! " << endl;

GifEncode::GifWriter gifWriter(destPath, false, abs(delay));
auto status = gifWriter.AddImages(pDests);
if (status == Status::Ok)
tcout << L"Converted image: " << destPath << endl;
wcout << L"Converted image: " << destPath << endl;
else
tcout << L"Failed to save image in '" << destPath << L"' file" << endl;
wcout << L"Failed to save image in '" << destPath << L"' file" << endl;
}
}
else {
Expand All @@ -322,7 +316,7 @@ static void OutputImages(const fs::path& sourceDir, wstring& targetDir, const UI
nQuantGA::APNsgaIII alg(pnnLABGAQuantizer);
alg.run(9999, -numeric_limits<double>::epsilon());
auto pGAq = alg.getResult();
tcout << L"\n" << pGAq->getResult().c_str() << endl;
wcout << L"\n" << pGAq->getResult().c_str() << endl;
if (pGAq->QuantizeImage(pDests, dither)) {
if (nMaxColors > 256 || delay < 0) {
int i = 0;
Expand All @@ -339,18 +333,18 @@ static void OutputImages(const fs::path& sourceDir, wstring& targetDir, const UI
GifEncode::GifWriter gifWriter(destPath, pGAq->hasAlpha(), abs(delay));
auto status = gifWriter.AddImages(pDests);
if (status == Status::Ok)
tcout << L"Converted image: " << destPath << endl;
wcout << L"Converted image: " << destPath << endl;
else
tcout << L"Failed to save image in '" << destPath << L"' file" << endl;
wcout << L"Failed to save image in '" << destPath << L"' file" << endl;
}
}
}

auto dur = chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - start).count() / 1000000.0;
tcout << "Completed in " << dur << " secs." << endl;
wcout << "Completed in " << dur << " secs." << endl;
}

int _tmain(int argc, _TCHAR** argv)
int wmain(int argc, wchar_t** argv)
{
#ifdef _WIN32
_setmode(_fileno(stdout), _O_U16TEXT);
Expand Down Expand Up @@ -388,7 +382,7 @@ int _tmain(int argc, _TCHAR** argv)
#endif

if(!fileExists(sourceFile)) {
tcout << "The source file you specified does not exist." << endl;
wcout << "The source file you specified does not exist." << endl;
return 0;
}

Expand Down Expand Up @@ -432,10 +426,10 @@ int _tmain(int argc, _TCHAR** argv)
QuantizeImage(algo, sourceFile, targetDir, pSource, nMaxColors, dither);

auto dur = chrono::duration_cast<chrono::microseconds>(chrono::steady_clock::now() - start).count() / 1000000.0;
tcout << "Completed in " << dur << " secs." << endl;
wcout << "Completed in " << dur << " secs." << endl;
}
else
tcout << "Failed to read image in '" << sourceFile.c_str() << "' file";
wcout << "Failed to read image in '" << sourceFile.c_str() << "' file";
}
GdiplusShutdown(m_gdiplusToken);
return 0;
Expand Down
13 changes: 5 additions & 8 deletions nQuantCpp/stdafx.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

#define COM_NO_WINDOWS_H
#define GDIPVER 0x0110 //Use more advanced GDI+ features
#include <unknwn.h> // Needed for non-MFC/ATL use

#ifdef _WIN32
#pragma comment(lib, "gdiplus")

#include <unknwn.h> // Needed for non-MFC/ATL use
#include <gdiplus.h>
using namespace Gdiplus;
#ifdef _WIN64
Expand All @@ -17,18 +17,15 @@
#pragma comment(linker, "\"/manifestdependency:type='Win32' name='Microsoft.Windows.GdiPlus' version='1.1.0.0' processorArchitecture='X86' publicKeyToken='6595b64144ccf1df' language='*'\"")
#endif
#else
#include <shlobj.h>
#include <guiddef.h>
#include <gdiplus.h>
using namespace Gdiplus;
#ifndef BYTE
typedef unsigned char BYTE;
#endif
#ifndef UINT
typedef unsigned int UINT;

#ifndef LPCWSTR
typedef _Null_terminated_ CONST WCHAR* LPCWSTR, * PCWSTR;
#endif
#endif


#ifndef BYTE_MAX
#include <climits>
#define BYTE_MAX UCHAR_MAX
Expand Down

0 comments on commit ca1da14

Please sign in to comment.