Skip to content
This repository has been archived by the owner on Jun 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #85 from YukihoAA/unicode_yuki
Browse files Browse the repository at this point in the history
Unicode support
  • Loading branch information
YukihoAA authored Dec 27, 2018
2 parents 8cccbd2 + 2adce36 commit 0222b18
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 104 deletions.
4 changes: 4 additions & 0 deletions BUILDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ https://github.com/DeadSix27/waifu2x-converter-cpp/releases

Install both SDK's as shown above, but later add "-DFORCE_DUAL=ON" to the cmake command.

#### Building with UNICODE support:

Add "-DBUILD_UNICODE=ON" to the cmake command.

### Building:
##### We will be using `K:/w2x` as our base folder for this guide.
##### If you want to build for both GPU brands, just install both SDKs (see above).
Expand Down
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ endif()
add_library(w2xc SHARED
src/modelHandler.cpp ${OPT_SOURCES}
src/modelHandler_OpenCL.cpp src/convertRoutine.cpp src/threadPool.cpp
src/modelHandler_CUDA.cpp src/w2xconv.cpp src/common.cpp
src/modelHandler_CUDA.cpp src/w2xconv.cpp src/common.cpp src/wcsfunc.cpp
src/cvwrap.cpp
src/Env.cpp src/Buffer.cpp
)
Expand Down Expand Up @@ -313,7 +313,7 @@ add_custom_target(gensrcs ALL DEPENDS ${GPU_CODE})
enable_testing()

if (${HAVE_OPENCV})
add_executable(waifu2x-converter-cpp src/main.cpp)
add_executable(waifu2x-converter-cpp src/main.cpp src/wcsfunc.cpp)

if(MSVC)
target_link_libraries(waifu2x-converter-cpp LINK_PUBLIC w2xc)
Expand Down
134 changes: 39 additions & 95 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
#include <deque>
#include <map>
#include <stdio.h>
#include "tclap/CmdLine.h"
#include "sec.hpp"
#include <stdlib.h>
#include <experimental/filesystem>
#include <algorithm>

#include "tclap/CmdLine.h"
#include "sec.hpp"

#if defined(WIN32) && defined(UNICODE)
#include <Windows.h>
#include <io.h>
Expand All @@ -31,6 +33,7 @@
#endif

#include "w2xconv.h"
#include "wcsfunc.hpp"

#ifndef DEFAULT_MODELS_DIRECTORY
#define DEFAULT_MODELS_DIRECTORY "models_rgb"
Expand Down Expand Up @@ -144,65 +147,6 @@ void check_for_errors(W2XConv* converter, int error) {
}


std::string ReplaceString(std::string subject, const std::string& search, const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}

#if defined(WIN32) && defined(UNICODE)
std::wstring ReplaceStringW(std::wstring subject, const std::wstring& search, const std::wstring& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::wstring::npos) {
subject.replace(pos, search.length(), replace);
pos += replace.length();
}
return subject;
}
#endif

#if defined(WIN32) && defined(UNICODE)
std::wstring to_wcs(std::string str){
std::wstring result;
result.assign(str.begin(), str.end());
return result;
}

std::string to_mbs(std::wstring str){
std::string result;
result.assign(str.begin(), str.end());
return result;
}
#endif

std::string basename(const std::string& str) {
size_t found = str.find_last_of("/\\");
return str.substr(found + 1);
}

#if defined(WIN32) && defined(UNICODE)
std::wstring basename(const std::wstring& str) {
size_t found = str.find_last_of(L"/\\");
return str.substr(found + 1);
}

#endif

std::string trim(const std::string& str)
{
size_t first = str.find_first_not_of(' ');
if (std::string::npos == first)
{
return str;
}
size_t last = str.find_last_not_of(' ');
return str.substr(first, (last - first + 1));
}


std::map<std::string,bool> opencv_formats = {
// Windows Bitmaps
{"BMP", false},
Expand Down Expand Up @@ -245,7 +189,6 @@ std::map<std::string,bool> opencv_formats = {
{"PIC", false}
};

#if defined(WIN32) && defined(UNICODE)
std::map<std::wstring,bool> opencv_formatsW = {
// Windows Bitmaps
{L"BMP", false},
Expand Down Expand Up @@ -287,7 +230,7 @@ std::map<std::wstring,bool> opencv_formatsW = {
{L"HDR", false},
{L"PIC", false}
};
#endif


bool check_output_extension(std::string extension) {
for(std::string::iterator it = extension.begin(); it != extension.end(); ++it){
Expand All @@ -300,16 +243,15 @@ bool check_output_extension(std::string extension) {
return false;
}

#if defined(WIN32) && defined(UNICODE)
bool check_output_extensionW(std::wstring extension) {
bool check_output_extension(std::wstring extension) {
std::transform(extension.begin(), extension.end(), extension.begin(), ::toupper);
auto index = opencv_formatsW.find(extension);
if (index != opencv_formatsW.end()) {
return index->second;
}
return false;
}
#endif


std::string generate_output_location(std::string inputFileName, std::string outputFileName, std::string mode, int NRLevel, double scaleRatio) {

Expand Down Expand Up @@ -371,7 +313,7 @@ std::string generate_output_location(std::string inputFileName, std::string outp
}

#if defined(WIN32) && defined(UNICODE)
std::wstring generate_output_locationW(std::wstring inputFileName, std::wstring outputFileName, std::string mode, int NRLevel, double scaleRatio) {
std::wstring generate_output_location(std::wstring inputFileName, std::wstring outputFileName, std::string mode, int NRLevel, double scaleRatio) {

size_t lastSlashPos = outputFileName.find_last_of(L"/\\");
size_t lastDotPos = outputFileName.find_last_of(L'.');
Expand Down Expand Up @@ -402,7 +344,7 @@ std::wstring generate_output_locationW(std::wstring inputFileName, std::wstring
//We pass tmp into generate_output_location because we will use the default way of naming processed files.
//We will remove everything, in the tmp string, prior to the last slash to get the filename.
//This removes all contextual information about where a file originated from if "recursive_directory" was enabled.
std::wstring tmp = generate_output_locationW(inputFileName, L"auto", mode, NRLevel, scaleRatio);
std::wstring tmp = generate_output_location(inputFileName, L"auto", mode, NRLevel, scaleRatio);
//tmp = full formatted output file path
size_t lastSlash = tmp.find_last_of(L'/');
if (lastSlash != std::wstring::npos){
Expand All @@ -419,7 +361,7 @@ std::wstring generate_output_locationW(std::wstring inputFileName, std::wstring
//We may have a regular output file here or something went wrong.
//outputFileName is already what it should be thus nothing needs to be done.
#ifdef HAVE_OPENCV
if(check_output_extensionW(outputFileName.substr(lastDotPos+1))==false){
if(check_output_extension(outputFileName.substr(lastDotPos+1))==false){
throw std::runtime_error("Unsupported output extension.");
}
#endif
Expand All @@ -431,6 +373,7 @@ std::wstring generate_output_locationW(std::wstring inputFileName, std::wstring
}
#endif


void convert_file(ConvInfo info, fs::path inputName, fs::path output) {
//std::cout << "Operating on: " << fs::absolute(inputName).string() << std::endl;
std::string outputName = generate_output_location(fs::absolute(inputName).string(), output.string(), info.mode, info.NRLevel, info.scaleRatio);
Expand Down Expand Up @@ -458,7 +401,7 @@ void convert_file(ConvInfo info, fs::path inputName, fs::path output) {
#if defined(WIN32) && defined(UNICODE)
void convert_fileW(ConvInfo info, fs::path inputName, fs::path output) {
//std::cout << "Operating on: " << fs::absolute(inputName).string() << std::endl;
std::wstring outputName = generate_output_locationW(fs::absolute(inputName).wstring(), output.wstring(), info.mode, info.NRLevel, info.scaleRatio);
std::wstring outputName = generate_output_location(fs::absolute(inputName).wstring(), output.wstring(), info.mode, info.NRLevel, info.scaleRatio);

int _nrLevel = -1;

Expand Down Expand Up @@ -599,46 +542,48 @@ void debug_show_opencv_formats()
}
}

#if defined(WIN32) && defined(UNICODE)

//CommandLineToArgvA source from: http://alter.org.ua/en/docs/win/args/
PCHAR* CommandLineToArgvA( PCHAR CmdLine, int* _argc ) {
PCHAR* argv;
PCHAR _argv;
char** CommandLineToArgvA( char* CmdLine, int* _argc ) {
char** argv;
char* _argv;
size_t len;
int argc;
CHAR a;
char a;
size_t i, j;
BOOLEAN in_QM;
BOOLEAN in_TEXT;
BOOLEAN in_SPACE;
bool in_QM;
bool in_TEXT;
bool in_SPACE;
len = strlen(CmdLine);
i = ((len+2)/2)*sizeof(PVOID) + sizeof(PVOID);
argv = (PCHAR*)GlobalAlloc(GMEM_FIXED, i + (len+2)*sizeof(CHAR));
_argv = (PCHAR)(((PUCHAR)argv)+i);
i = ((len+2)/2)*sizeof(void*) + sizeof(void*);
argv = (char**)malloc(i + (len+2)*sizeof(char));
_argv = (char*)(((unsigned char*)argv)+i);
argc = 0;
argv[argc] = _argv;
in_QM = FALSE;
in_TEXT = FALSE;
in_SPACE = TRUE;
in_QM = false;
in_TEXT = false;
in_SPACE = true;
i = 0;
j = 0;
while( a = CmdLine[i] ) {
if(in_QM) {
if(a == '\"') {
in_QM = FALSE;
in_QM = false;
} else {
_argv[j] = a;
j++;
}
} else {
switch(a) {
case '\"':
in_QM = TRUE;
in_TEXT = TRUE;
in_QM = true;
in_TEXT = true;
if(in_SPACE) {
argv[argc] = _argv+j;
argc++;
}
in_SPACE = FALSE;
in_SPACE = false;
break;
case ' ':
case '\t':
Expand All @@ -648,37 +593,36 @@ PCHAR* CommandLineToArgvA( PCHAR CmdLine, int* _argc ) {
_argv[j] = '\0';
j++;
}
in_TEXT = FALSE;
in_SPACE = TRUE;
in_TEXT = false;
in_SPACE = true;
break;
default:
in_TEXT = TRUE;
in_TEXT = true;
if(in_SPACE) {
argv[argc] = _argv+j;
argc++;
}
_argv[j] = a;
j++;
in_SPACE = FALSE;
in_SPACE = false;
break;
}
}
i++;
}
_argv[j] = '\0';
argv[argc] = NULL;
argv[argc] = nullptr;

(*_argc) = argc;
return argv;
}


#if defined(WIN32) && defined(UNICODE)
int wmain(void){
int ret = 1;
int argc = 0, argc_w = 0;
std::wstring inputFileName, outputFileName=L"auto";
PCHAR *argv = CommandLineToArgvA(GetCommandLineA(), &argc);
char **argv = CommandLineToArgvA(GetCommandLineA(), &argc);
LPWSTR *argv_w = CommandLineToArgvW(GetCommandLineW(), &argc_w);
HWND hWnd = GetConsoleWindow();

Expand Down Expand Up @@ -938,7 +882,7 @@ int wmain(void){
}

w2xconv_fini(converter);
GlobalFree(argv);
free(argv);
LocalFree(argv_w);
return 0;
}
Expand Down
26 changes: 24 additions & 2 deletions src/w2xconv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@

#include <limits.h>
#include <sstream>

#if defined(WIN32) && defined(UNICODE)
#include <experimental/filesystem>

namespace fs = std::experimental::filesystem;

#endif

#include "w2xconv.h"
#include "sec.hpp"
#include "Buffer.hpp"
Expand Down Expand Up @@ -503,6 +511,20 @@ setPathError(W2XConv *conv,
conv->last_error.u.path = strdup(path.c_str());
}

#if defined(WIN32) && defined(UNICODE)
static void
setPathError(W2XConv *conv,
enum W2XConvErrorCode code,
std::wstring const &path)
{
fs::path fspath = path;
clearError(conv);

conv->last_error.code = code;
conv->last_error.u.path = strdup(fspath.string().c_str());
}
#endif

static void
setError(W2XConv *conv,
enum W2XConvErrorCode code)
Expand Down Expand Up @@ -1527,7 +1549,7 @@ w2xconv_convert_fileW(struct W2XConv *conv,
png_fp = _wfopen(src_path, L"rb");

if (png_fp == NULL) {
setPathError(conv, W2XCONV_ERROR_IMREAD_FAILED, "src_path");
setPathError(conv, W2XCONV_ERROR_IMREAD_FAILED, src_path);
return -1;
}

Expand Down Expand Up @@ -1687,7 +1709,7 @@ w2xconv_convert_fileW(struct W2XConv *conv,
if (!write_imageW(dst_path, image_dst)) {
setPathError(conv,
W2XCONV_ERROR_IMWRITE_FAILED,
"dst_path");
dst_path);
return -1;
}

Expand Down
10 changes: 5 additions & 5 deletions src/w2xconv.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
#include <stdio.h>
#include <stdbool.h>

#if defined(WIN32) && defined(UNICODE)
#include <Windows.h>
#include "wcsfunc.hpp"
#endif

#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -17,11 +22,6 @@ extern "C" {
#define W2XCONV_EXPORT __declspec(dllimport)
#endif

#ifdef UNICODE
#include <Windows.h>
#endif


#else

#ifdef W2XCONV_IMPL
Expand Down
Loading

0 comments on commit 0222b18

Please sign in to comment.