satdump/src-core/imgui/pfd/pfd_utils.cpp

91 lines
2.5 KiB
C++
Raw Permalink Normal View History

#include <algorithm>
#include <sstream>
#include "portable-file-dialogs.h"
#include "core/config.h"
#include "logger.h"
2023-09-25 23:37:58 -04:00
#include "pfd_utils.h"
#ifdef _MSC_VER
#include <direct.h>
#endif
2024-05-09 01:16:08 +02:00
#include "common/image/io.h"
2024-05-08 21:38:32 +02:00
2023-09-25 23:37:58 -04:00
namespace satdump
{
2024-05-09 01:16:08 +02:00
std::string save_image_dialog(std::string default_name, std::string default_path, std::string window_title, image::Image *image, std::string *default_ext)
2023-09-25 23:37:58 -04:00
{
std::vector<std::string> saveopts = {
"PNG Files", "*.png",
"JPEG 2000 Files", "*.j2k",
2023-10-13 20:45:13 -04:00
"JPEG Files", "*.jpg *.jpeg",
2024-02-27 23:44:16 +01:00
"PBM Files", "*.pbm *.pgm *.ppm",
2024-05-12 17:11:34 +02:00
"TIFF Files", "*.tif *.tiff *.gtif",
"QOI Files", "*.qoi"};
2023-09-25 23:37:58 -04:00
2024-03-09 23:26:14 -05:00
size_t i = 1;
2024-05-08 21:38:32 +02:00
for (auto it = saveopts.begin() + 1;; it += 2)
2024-02-27 23:44:16 +01:00
{
std::stringstream ss(*it);
std::string token;
while (std::getline(ss, token, ' '))
2024-02-27 23:44:16 +01:00
{
if (token.substr(2) == *default_ext)
{
std::rotate(saveopts.begin(), it - 1, it);
std::rotate(saveopts.begin() + 1, it, it + 1);
goto done_ext;
}
2024-02-27 23:44:16 +01:00
}
i += 2;
if (i >= saveopts.size())
break;
2024-02-27 23:44:16 +01:00
}
2023-09-25 23:37:58 -04:00
2024-05-08 21:38:32 +02:00
done_ext:
#ifdef __ANDROID__
*default_ext = config::main_cfg["satdump_general"]["image_format"]["value"].get<std::string>();
#endif
#if defined(_MSC_VER)
if (default_path == ".")
{
2024-02-24 22:38:54 +01:00
char *cwd;
cwd = _getcwd(NULL, 0);
if (cwd != 0)
default_path = cwd;
}
default_path += "\\";
#elif defined(__ANDROID__)
if (default_path == ".")
default_path = "/storage/emulated/0";
default_path += "/";
#else
default_path += "/";
#endif
std::string save_name = default_path + default_name + "." + *default_ext;
std::string path = "";
#ifndef __ANDROID__
auto result = pfd::save_file(window_title, save_name, saveopts);
while (!result.ready(1000))
std::this_thread::sleep_for(std::chrono::milliseconds(1));
if (result.result().size() > 0)
{
path = result.result();
2024-05-09 01:16:08 +02:00
image::save_img(*image, path);
std::string extension = std::filesystem::path(path).extension().string();
if (extension.size() > 1)
*default_ext = extension.substr(1);
}
#else
path = save_name;
2024-05-08 22:15:00 -04:00
image::save_img(*image, save_name);
#endif
return path;
2023-09-25 23:37:58 -04:00
}
}