comparison foosdk/sdk/foobar2000/helpers/image_load_save.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "StdAfx.h"
2 #include "image_load_save.h"
3 #include <memory>
4 #include <SDK/imageLoaderLite.h>
5 #include <SDK/popup_message.h>
6 #include <SDK/album_art_helpers.h>
7
8 namespace fb2k {
9 bool imageSaveDialog(album_art_data_ptr content, HWND wndParent, const char* initDir, bool bAsync) {
10 pfc::string8 fileTypes = "All files|*.*";
11 pfc::string8 ext;
12
13 try {
14 auto info = fb2k::imageLoaderLite::get()->getInfo(content);
15 if (info.formatName) {
16 pfc::string8 nameCapitalized = pfc::stringToUpper( info.formatName );
17 ext = pfc::stringToLower( info.formatName );
18 if (nameCapitalized == "WEBP") nameCapitalized = "WebP";
19 pfc::string8 extmask;
20 if (ext == "jpeg-xl") ext = "jxl";
21 if (ext == "jpeg") {
22 ext = "jpg";
23 extmask = "*.jpg;*.jpeg";
24 } else {
25 extmask << "*." << ext;
26 }
27 fileTypes.reset();
28 fileTypes << nameCapitalized << " files|" << extmask;
29 }
30 } catch (...) {}
31 pfc::string8 fn;
32
33 if (!uGetOpenFileName(wndParent, fileTypes, 0, ext.length() > 0 ? ext.c_str() : nullptr, "Export picture file", initDir, fn, TRUE)) return false;
34
35 auto bErrord = std::make_shared<bool>(false);
36 auto work = [content, fn, bErrord] {
37 try {
38 auto f = fileOpenWriteNew(fn, fb2k::noAbort, 0.5);
39 f->write(content->get_ptr(), content->get_size(), fb2k::noAbort);
40 } catch(std::exception const & e) {
41 * bErrord = true;
42 pfc::string8 msg;
43 msg << "Image file could not be written: " << e;
44 fb2k::inMainThread([msg] {
45 popup_message::g_show(msg, "Information");
46 });
47 }
48 };
49 if (bAsync) {
50 fb2k::splitTask(work);
51 return true;
52 } else {
53 work();
54 return ! *bErrord;
55 }
56 }
57
58 bool imageLoadDialog(pfc::string_base& outFN, HWND wndParent, const char* initDir) {
59 return !!uGetOpenFileName(wndParent, FB2K_GETOPENFILENAME_PICTUREFILES_ALL, 0, nullptr, "Import picture file", initDir, outFN, FALSE);
60 }
61 album_art_data::ptr imageLoadDialog(HWND wndParent, const char* initDir) {
62 album_art_data::ptr ret;
63 pfc::string8 fn;
64 if (imageLoadDialog(fn, wndParent, initDir)) {
65 try {
66 ret = readPictureFile(fn, fb2k::noAbort);
67 } catch (std::exception const& e) {
68 popup_message::g_show(PFC_string_formatter() << "Image file could not be read: " << e, "Information");
69 }
70 }
71 return ret;
72 }
73
74 album_art_data_ptr readPictureFile(const char* p_path, abort_callback& p_abort) {
75 PFC_ASSERT(p_path != nullptr);
76 PFC_ASSERT(*p_path != 0);
77 p_abort.check();
78
79 // Pointless, not a media file, path often from openfiledialog and not canonical
80 // file_lock_ptr lock = file_lock_manager::get()->acquire_read(p_path, p_abort);
81
82 file_ptr l_file;
83 filesystem::g_open_timeout(l_file, p_path, filesystem::open_mode_read, 0.5, p_abort);
84 service_ptr_t<album_art_data_impl> instance = new service_impl_t<album_art_data_impl>();
85 t_filesize size = l_file->get_size_ex(p_abort);
86 if (size > 1024 * 1024 * 64) throw std::runtime_error("File too large");
87 instance->from_stream(l_file.get_ptr(), (t_size)size, p_abort);
88 return instance;
89 }
90
91 }