From 5d544245885a838b6c793684dfbd8394de968639 Mon Sep 17 00:00:00 2001 From: lltcggie Date: Thu, 4 Jun 2015 02:11:46 +0900 Subject: [PATCH] =?UTF-8?q?=E5=88=86=E5=89=B2=E3=82=B5=E3=82=A4=E3=82=BA?= =?UTF-8?q?=E3=82=92=E7=94=BB=E5=83=8F=E3=81=AE=E7=B8=A6=E6=A8=AA=E3=82=B5?= =?UTF-8?q?=E3=82=A4=E3=82=BA=E3=81=AE=E5=85=AC=E7=B4=84=E6=95=B0=E3=81=AE?= =?UTF-8?q?=E3=83=AA=E3=82=B9=E3=83=88=E3=81=8B=E3=82=89=E9=81=B8=E3=81=B9?= =?UTF-8?q?=E3=82=8B=E3=82=88=E3=81=86=E3=81=AB=E3=81=97=E3=81=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- waifu2x-caffe-gui/Resource.rc | Bin 8870 -> 8920 bytes waifu2x-caffe-gui/Source.cpp | 164 +++++++++++++++++++++++++++++----- waifu2x-caffe-gui/resource.h | Bin 2910 -> 3002 bytes 3 files changed, 143 insertions(+), 21 deletions(-) diff --git a/waifu2x-caffe-gui/Resource.rc b/waifu2x-caffe-gui/Resource.rc index ef3eb63ad4f924fd61a7ffaab965b835d04cdfd6..e5dd0db3c69e76edcc26b363e208f77f0d015af6 100644 GIT binary patch delta 102 zcmZ4Hdc$?Y6iH5J27d-$1}6sp$@W6xlLNRokVFnkDyy3@=rEWuSO7_51_L1J3{)G; m5YOPk5Cqg20HpmH!WsM~zm!nt4PkHu%Z7ktHVaBk #include #include +#include +#include #include "resource.h" #include "../common/waifu2x.h" @@ -21,6 +23,9 @@ const size_t AR_PATH_MAX(1024); +const int MinCommonDivisor = 50; +const int DefaultCommonDivisor = 128; + // http://stackoverflow.com/questions/10167382/boostfilesystem-get-relative-path boost::filesystem::path relativePath(const boost::filesystem::path &path, const boost::filesystem::path &relative_to) @@ -62,6 +67,26 @@ boost::filesystem::path relativePath(const boost::filesystem::path &path, const return result; } +std::vector CommonDivisorList(const int N) +{ + std::vector list; + + const int sq = sqrt(N); + for (int i = 1; i <= sq; i++) + { + if (N % i == 0) + list.push_back(i); + } + + const int sqs = list.size(); + for (int i = 0; i < sqs; i++) + list.push_back(N / list[i]); + + std::sort(list.begin(), list.end()); + + return list; +} + // ダイアログ用 class DialogEvent { @@ -80,6 +105,8 @@ private: int crop_size; int batch_size; + std::vector extList; + std::thread processThread; std::atomic_bool cancelFlag; @@ -106,7 +133,7 @@ private: return addstr; } - bool SyncMember() + bool SyncMember(const bool NotSyncCropSize) { bool ret = true; @@ -177,11 +204,23 @@ private: buf[_countof(buf) - 1] = '\0'; inputFileExt = buf; + + // input_extention_listを文字列の配列にする + + typedef boost::char_separator char_separator; + typedef boost::tokenizer tokenizer; + + char_separator sep(":", "", boost::drop_empty_tokens); + tokenizer tokens(inputFileExt, sep); + + for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) + extList.push_back("." + *tok_iter); } + if (!NotSyncCropSize) { char buf[AR_PATH_MAX] = ""; - GetWindowTextA(GetDlgItem(dh, IDC_EDIT_CROP_SIZE), buf, _countof(buf)); + GetWindowTextA(GetDlgItem(dh, IDC_COMBO_CROP_SIZE), buf, _countof(buf)); buf[_countof(buf) - 1] = '\0'; char *ptr = nullptr; @@ -198,6 +237,95 @@ private: return ret; } + void SetCropSizeList(const boost::filesystem::path &input_path) + { + HWND hcrop = GetDlgItem(dh, IDC_COMBO_CROP_SIZE); + + int gcd = 1; + if (boost::filesystem::is_directory(input_path)) + { + BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::recursive_directory_iterator(input_path), + boost::filesystem::recursive_directory_iterator())) + { + if (!boost::filesystem::is_directory(p) && std::find(extList.begin(), extList.end(), p.extension().string()) != extList.end()) + { + auto mat = cv::imread(p.string(), cv::IMREAD_UNCHANGED); + if (mat.empty()) + continue; + + auto size = mat.size(); + mat.release(); + + gcd = boost::math::gcd(size.width, size.height); + } + } + } + else + { + auto mat = cv::imread(input_path.string(), cv::IMREAD_UNCHANGED); + if (mat.empty()) + return; + + auto size = mat.size(); + mat.release(); + + gcd = boost::math::gcd(size.width, size.height); + } + + while (SendMessage(hcrop, CB_GETCOUNT, 0, 0) != 0) + SendMessage(hcrop, CB_DELETESTRING, 0, 0); + + // 最大公約数の約数のリスト取得 + std::vector list(CommonDivisorList(gcd)); + + // MinCommonDivisor未満の約数削除 + list.erase(std::remove_if(list.begin(), list.end(), [](const int v) + { + return v < MinCommonDivisor; + } + ), list.end()); + + if (list.size() == 0) + { + // gcdがMinCommonDivisor未満だったら2の累乗を適当に追加していく + for (int i = 64; i <= 512; i *= 2) + list.push_back(i); + } + else + { + int mindiff = INT_MAX; + for (int i = 0; i < list.size(); i++) + mindiff = std::min(mindiff, abs(DefaultCommonDivisor - list[i])); + + // 全ての公約数とDefaultCommonDivisorとの最小の差が64以上ならDefaultCommonDivisor追加 + if (mindiff >= 64) + { + list.push_back(DefaultCommonDivisor); + + std::sort(list.begin(), list.end()); + } + } + + int mindiff = INT_MAX; + int defaultIndex = 0; + for (int i = 0; i < list.size(); i++) + { + const int n = list[i]; + + std::string str(std::to_string(n)); + SendMessageA(hcrop, CB_ADDSTRING, 0, (LPARAM)str.c_str()); + + const int diff = abs(DefaultCommonDivisor - n); + if (diff < mindiff) + { + mindiff = diff; + defaultIndex = i; + } + } + + SendMessage(hcrop, CB_SETCURSEL, defaultIndex, 0); + } + void ProcessWaifu2x() { const boost::filesystem::path input_path(boost::filesystem::absolute(input_str)); @@ -220,22 +348,8 @@ private: } } - std::vector extList; - { - // input_extention_listを文字列の配列にする - - typedef boost::char_separator char_separator; - typedef boost::tokenizer tokenizer; - - char_separator sep(":", "", boost::drop_empty_tokens); - tokenizer tokens(inputFileExt, sep); - - for (tokenizer::iterator tok_iter = tokens.begin(); tok_iter != tokens.end(); ++tok_iter) - extList.push_back("." + *tok_iter); - } - // 変換する画像の入力、出力パスを取得 - const auto func = [this, &extList, &input_path, &output_path, &file_paths](const boost::filesystem::path &path) + const auto func = [this, &input_path, &output_path, &file_paths](const boost::filesystem::path &path) { BOOST_FOREACH(const boost::filesystem::path& p, std::make_pair(boost::filesystem::recursive_directory_iterator(path), boost::filesystem::recursive_directory_iterator())) @@ -337,7 +451,7 @@ private: void ReplaceAddString() { - SyncMember(); + SyncMember(true); const boost::filesystem::path output_path(output_str); std::string stem = output_path.stem().string(); @@ -426,7 +540,7 @@ public: if (processThread.joinable()) return; - SyncMember(); + SyncMember(false); if (input_str.length() == 0) { @@ -575,7 +689,7 @@ public: SetWindowTextA(GetDlgItem(hWnd, IDC_EDIT_SCALE_RATIO), text); SetWindowTextA(GetDlgItem(hWnd, IDC_EDIT_OUT_EXT), outputExt.c_str()); SetWindowTextA(GetDlgItem(hWnd, IDC_EDIT_INPUT_EXT_LIST), inputFileExt.c_str()); - SetWindowTextA(GetDlgItem(hWnd, IDC_EDIT_CROP_SIZE), std::to_string(crop_size).c_str()); + SetWindowTextA(GetDlgItem(hWnd, IDC_COMBO_CROP_SIZE), std::to_string(crop_size).c_str()); } void Cancel(HWND hWnd, WPARAM wParam, LPARAM lParam, LPVOID lpData) @@ -623,7 +737,13 @@ public: boost::filesystem::path path(szTmp); - if (!SyncMember()) + if (!boost::filesystem::exists(path)) + { + MessageBox(dh, TEXT("入力ファイル/フォルダが存在しません"), TEXT("エラー"), MB_OK | MB_ICONERROR); + return 0L; + } + + if (!SyncMember(true)) return 0L; if (boost::filesystem::is_directory(path)) @@ -657,6 +777,8 @@ public: SetWindowTextA(hWnd, szTmp); } + + SetCropSizeList(path); } return 0L; diff --git a/waifu2x-caffe-gui/resource.h b/waifu2x-caffe-gui/resource.h index 7df8cd7e0943cf21dad0153762a7695b69818070..f30b0509824864e945e1aa84ccdb113fbfb75932 100644 GIT binary patch delta 72 zcmca7wo81&HV$rQ27d-$1}7kCG