From fafe109a4ae3dcedc3479e82df0271b7c3fa845f Mon Sep 17 00:00:00 2001 From: Arthur Glowacki Date: Thu, 29 Feb 2024 12:07:52 -0600 Subject: [PATCH] Changed loading tiffs to fix exception and lockup in thread --- src/mvc/FittingDialog.cpp | 1 + src/mvc/MapsWorkspaceFilesWidget.cpp | 43 +++++++++++++++++++++------- src/mvc/TIFF_Model.cpp | 34 ++++++++++++++++++++-- 3 files changed, 66 insertions(+), 12 deletions(-) diff --git a/src/mvc/FittingDialog.cpp b/src/mvc/FittingDialog.cpp index ef947e5..46b9013 100644 --- a/src/mvc/FittingDialog.cpp +++ b/src/mvc/FittingDialog.cpp @@ -29,6 +29,7 @@ FittingDialog::FittingDialog(QWidget *parent) : QDialog(parent) _elements_to_fit = nullptr; _param_fit_routine.set_update_coherent_amplitude_on_fit(false); _hybrid_fit_routine.set_update_coherent_amplitude_on_fit(false); + ////_model.set_num_threads(std::thread::hardware_concurrency()); _createLayout(); } diff --git a/src/mvc/MapsWorkspaceFilesWidget.cpp b/src/mvc/MapsWorkspaceFilesWidget.cpp index e79a214..afc32d1 100644 --- a/src/mvc/MapsWorkspaceFilesWidget.cpp +++ b/src/mvc/MapsWorkspaceFilesWidget.cpp @@ -313,25 +313,48 @@ void MapsWorkspaceFilesWidget::onOpenModel(const QStringList& names_list, MODEL_ } else if (mt == MODEL_TYPE::VLM) { - std::future< VLM_Model*> ret = Global_Thread_Pool::inst()->enqueue([this, name] { return _model->get_VLM_Model(name); }); - std::future_status status; - do - { - status = ret.wait_for(std::chrono::milliseconds(100)); - QCoreApplication::processEvents(); - } while (status != std::future_status::ready); - - VLM_Model* Model = ret.get(); + VLM_Model* Model = _model->get_VLM_Model(name); + if (Model != nullptr) { load_status = LOADED; Model->getImage(); - emit loaded_model(name, mt); } else { load_status = FAILED_LOADING; } + emit loaded_model(name, mt); + QCoreApplication::processEvents(); + /* + std::future< VLM_Model*> ret = Global_Thread_Pool::inst()->enqueue([this, name] { return _model->get_VLM_Model(name); }); + std::future_status status; + int cntr = 0; + do + { + status = ret.wait_for(std::chrono::milliseconds(100)); + QCoreApplication::processEvents(); + cntr++; + if (cntr > 1200) + { + load_status = FAILED_LOADING; + break; + } + } while (status != std::future_status::ready); + if (load_status == UNLOADED) + { + VLM_Model* Model = ret.get(); + if (Model != nullptr) + { + load_status = LOADED; + Model->getImage(); + emit loaded_model(name, mt); + } + else + { + load_status = FAILED_LOADING; + } + }*/ _vlm_tab_widget->loaded_file_status_changed(load_status, name); } diff --git a/src/mvc/TIFF_Model.cpp b/src/mvc/TIFF_Model.cpp index e6830ab..252542d 100644 --- a/src/mvc/TIFF_Model.cpp +++ b/src/mvc/TIFF_Model.cpp @@ -70,11 +70,39 @@ bool TIFF_Model::load(QString filepath) double range = maxVal - minVal; _img = QImage(mat.cols, mat.rows, QImage::Format_ARGB32); + float val = 0.f; for (int w = 0; w < mat.cols; w++) { for (int h = 0; h < mat.rows; h++) { - float val = mat.at(h, w); + uchar depth = mat.type() & CV_MAT_DEPTH_MASK; + switch (depth) { + case CV_8U: + val = (float)mat.at(h, w); + break; + case CV_8S: + val = (float)mat.at(h, w); + break; + case CV_16U: + val = (float)mat.at(h, w); + break; + case CV_16S: + val = (float)mat.at(h, w); + break; + case CV_32S: + val = (float)mat.at(h, w); + break; + case CV_32F: + val = (float)mat.at(h, w); + break; + case CV_64F: + val = (float)mat.at(h, w); + break; + default: + val = 0.f; + break; + } + //float val = mat.at(h, w); _pixel_values(h, w) = val; val = (val - minVal) / range; int c = val * 255; @@ -87,10 +115,12 @@ bool TIFF_Model::load(QString filepath) } catch (std::string& s) { + logE << s << "\n"; return false; } - catch (...) + catch (std::exception e) { + logE << e.what() << "\n"; return false; }