|
|
@@ -0,0 +1,101 @@
|
|
|
+#include "image_saver_impl.h"
|
|
|
+#include "core/image_utility.hpp"
|
|
|
+#include "core/imgui_utility.hpp"
|
|
|
+
|
|
|
+#include <opencv2/imgcodecs.hpp>
|
|
|
+#include <opencv2/imgproc.hpp>
|
|
|
+
|
|
|
+#include <boost/asio/post.hpp>
|
|
|
+
|
|
|
+#include <fmt/format.h>
|
|
|
+
|
|
|
+#include <ranges>
|
|
|
+
|
|
|
+using boost::asio::post;
|
|
|
+
|
|
|
+void image_saver::impl::item_store_type::save_png() {
|
|
|
+ auto file_name = fmt::format("{}_{}.png", ui_name, par->save_cnt);
|
|
|
+ auto img_mat = image_as_mat(img_name, &par->stream);
|
|
|
+
|
|
|
+ // swap channels
|
|
|
+ auto img_type = OBJ_TYPE(img_name);
|
|
|
+ if (img_type == typeid(image_u8c3)) { // rgb -> bgr
|
|
|
+ cv::cvtColor(img_mat, img_mat, cv::COLOR_RGB2BGR);
|
|
|
+ }
|
|
|
+
|
|
|
+ CUDA_API_CHECK(cudaStreamSynchronize(par->stream.cuda));
|
|
|
+ cv::imwrite(file_name, img_mat);
|
|
|
+}
|
|
|
+
|
|
|
+void image_saver::impl::item_store_type::process() { // TODO: move save work to another thread
|
|
|
+ switch (save_type) {
|
|
|
+ // @formatter:off
|
|
|
+ case SAVE_PNG: { save_png(); break; }
|
|
|
+// case SAVE_JPG: { save_jpg(); break; }
|
|
|
+// case SAVE_RAW: { save_raw(); break; }
|
|
|
+ // @formatter:on
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+image_saver::impl::impl(const create_config &conf) {
|
|
|
+ ctx = conf.ctx;
|
|
|
+ std::ranges::transform(
|
|
|
+ conf.img_list, std::back_inserter(item_list),
|
|
|
+ [par = this](auto item) {
|
|
|
+ return item_store_type{
|
|
|
+ .ui_name = item.name,
|
|
|
+ .img_name = item.img_name,
|
|
|
+ .par = par};
|
|
|
+ });
|
|
|
+}
|
|
|
+
|
|
|
+void image_saver::impl::process_all() {
|
|
|
+ for (auto &item: item_list) {
|
|
|
+ if (item.checked) {
|
|
|
+ item.process();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+void image_saver::impl::show() {
|
|
|
+ if (ImGui::Button("Save Selected")) {
|
|
|
+ post(*ctx, [this] {
|
|
|
+ ++save_cnt;
|
|
|
+ process_all();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ for (auto &item: item_list) {
|
|
|
+ if (ImGui::TreeNode(item.ui_name.c_str())) {
|
|
|
+ ImGui::Checkbox("##select", &item.checked);
|
|
|
+ ImGui::SameLine();
|
|
|
+ if (ImGui::Button("Save")) {
|
|
|
+ post(*ctx, [ptr = &item, this] {
|
|
|
+ ++save_cnt;
|
|
|
+ ptr->process();
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ // save type
|
|
|
+ ImGui::SameLine();
|
|
|
+ ImGui::RadioButton("PNG", &item.save_type, SAVE_PNG);
|
|
|
+
|
|
|
+ auto guard = imgui_disable_guard(); // not implemented
|
|
|
+ ImGui::SameLine();
|
|
|
+ ImGui::RadioButton("JPG", &item.save_type, SAVE_JPG);
|
|
|
+ ImGui::SameLine();
|
|
|
+ ImGui::RadioButton("RAW", &item.save_type, SAVE_RAW);
|
|
|
+
|
|
|
+ ImGui::TreePop();
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+image_saver::image_saver(const create_config &conf)
|
|
|
+ : pimpl(std::make_unique<impl>(conf)) {
|
|
|
+}
|
|
|
+
|
|
|
+image_saver::~image_saver() = default;
|
|
|
+
|
|
|
+void image_saver::show() {
|
|
|
+ pimpl->show();
|
|
|
+}
|