130 lines
4.6 KiB
C++
130 lines
4.6 KiB
C++
// SPDX-FileCopyrightText: 2025 Filip Leonarski, Paul Scherrer Institute <filip.leonarski@psi.ch>
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
|
|
|
#include "JFJochImageReadingWorker.h"
|
|
#include <QVector>
|
|
#include <iostream>
|
|
|
|
JFJochImageReadingWorker::JFJochImageReadingWorker(QObject *parent) : QObject(parent) {}
|
|
|
|
void JFJochImageReadingWorker::LoadFile(const QString &filename, qint64 image_number, qint64 summation) {
|
|
try {
|
|
std::shared_ptr<const JFJochReaderDataset> dataset;
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
|
|
if (!http_mode && filename == current_file)
|
|
logger.Info("File {} already loaded", filename.toStdString());
|
|
else {
|
|
if (filename.startsWith("http://")) {
|
|
http_mode = true;
|
|
http_reader.ReadURL(filename.toStdString());
|
|
total_images = http_reader.GetNumberOfImages();
|
|
dataset = http_reader.GetStartMessage();
|
|
if (image_number < 0)
|
|
emit setToolbarMode(JFJochViewerToolbar::ToolbarMode::Autoload);
|
|
else
|
|
emit setToolbarMode(JFJochViewerToolbar::ToolbarMode::None);
|
|
} else {
|
|
http_mode = false;
|
|
reader.ReadFile(filename.toStdString());
|
|
total_images = reader.GetNumberOfImages();
|
|
dataset = reader.GetStartMessage();
|
|
emit setToolbarMode(JFJochViewerToolbar::ToolbarMode::None);
|
|
}
|
|
current_image = INT64_MIN;
|
|
current_summation = 1;
|
|
current_file = filename;
|
|
}
|
|
emit datasetLoaded(dataset);
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
|
logger.Info("Loaded file {} in {} ms", filename.toStdString(), duration);
|
|
|
|
LoadImage(image_number, summation);
|
|
} catch (std::exception &e) {
|
|
logger.Error("Error loading file {} {}", filename.toStdString(), e.what());
|
|
}
|
|
}
|
|
|
|
void JFJochImageReadingWorker::CloseFile() {
|
|
if (http_mode)
|
|
http_reader.Close();
|
|
else
|
|
reader.Close();
|
|
|
|
current_image = INT64_MIN;
|
|
current_summation = 1;
|
|
total_images = 0;
|
|
current_file = "";
|
|
emit imageLoaded({});
|
|
emit datasetLoaded({});
|
|
}
|
|
|
|
void JFJochImageReadingWorker::LoadImage(int64_t image_number, int64_t summation) {
|
|
try {
|
|
if (summation <= 0
|
|
|| image_number + summation > total_images
|
|
|| ((image_number == current_image) && (current_summation == summation)))
|
|
return;
|
|
|
|
std::vector<int32_t> image;
|
|
std::shared_ptr<const JFJochReaderImage> out_image;
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
if (http_mode) {
|
|
if (image_number < 0 && summation != 1)
|
|
return;
|
|
http_reader.LoadImage(image_number, summation);
|
|
out_image = http_reader.CopyImage();
|
|
total_images = http_reader.GetNumberOfImages();
|
|
emit datasetLoaded(http_reader.GetStartMessage());
|
|
} else {
|
|
if (image_number < 0)
|
|
return;
|
|
reader.LoadImage(image_number, summation);
|
|
out_image = reader.CopyImage();
|
|
}
|
|
|
|
if (!out_image) {
|
|
emit imageLoaded({});
|
|
return;
|
|
}
|
|
|
|
current_image = out_image->ImageData().number;
|
|
current_summation = summation;
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
|
|
logger.Info("Loaded image {} in {} ms", image_number, duration);
|
|
|
|
emit imageNumberChanged(total_images, current_image);
|
|
emit imageLoaded(out_image);
|
|
} catch (std::exception &e) {
|
|
logger.Error("Error loading image {}", image_number);
|
|
}
|
|
}
|
|
|
|
void JFJochImageReadingWorker::SetROIBox(QRect box) {
|
|
reader.SetROI(ROIBox("roi1", box.left(), box.right(), box.bottom(), box.top()));
|
|
http_reader.SetROI(ROIBox("roi1", box.left(), box.right(), box.bottom(), box.top()));
|
|
if (http_mode)
|
|
emit imageStatsUpdated(http_reader.CopyImage());
|
|
else
|
|
emit imageStatsUpdated(reader.CopyImage());
|
|
}
|
|
|
|
void JFJochImageReadingWorker::SetROICircle(double x, double y, double radius) {
|
|
if (radius <= 0) {
|
|
reader.ClearROI();
|
|
http_reader.ClearROI();
|
|
} else {
|
|
reader.SetROI(ROICircle("roi1", x, y, radius));
|
|
http_reader.SetROI(ROICircle("roi1", x, y, radius));
|
|
}
|
|
|
|
if (http_mode)
|
|
emit imageStatsUpdated(http_reader.CopyImage());
|
|
else
|
|
emit imageStatsUpdated(reader.CopyImage());
|
|
}
|