mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-14 00:07:13 +02:00
Merge branch 'main' into developer
This commit is contained in:
@ -13,11 +13,13 @@ RawFile::RawFile(const std::filesystem::path &fname, const std::string &mode)
|
||||
: m_master(fname) {
|
||||
m_mode = mode;
|
||||
if (mode == "r") {
|
||||
|
||||
n_subfiles = find_number_of_subfiles(); // f0,f1...fn
|
||||
n_subfile_parts =
|
||||
m_master.geometry().col * m_master.geometry().row; // d0,d1...dn
|
||||
find_geometry();
|
||||
update_geometry_with_roi();
|
||||
|
||||
open_subfiles();
|
||||
} else {
|
||||
throw std::runtime_error(LOCATION +
|
||||
@ -41,11 +43,11 @@ void RawFile::read_into(std::byte *image_buf, size_t n_frames) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void RawFile::read_into(std::byte *image_buf) {
|
||||
return get_frame_into(m_current_frame++, image_buf);
|
||||
};
|
||||
|
||||
|
||||
void RawFile::read_into(std::byte *image_buf, DetectorHeader *header) {
|
||||
|
||||
return get_frame_into(m_current_frame++, image_buf, header);
|
||||
@ -53,6 +55,7 @@ void RawFile::read_into(std::byte *image_buf, DetectorHeader *header) {
|
||||
|
||||
size_t RawFile::n_mod() const { return n_subfile_parts; }
|
||||
|
||||
|
||||
size_t RawFile::bytes_per_frame() {
|
||||
return m_rows * m_cols * m_master.bitdepth() / 8;
|
||||
}
|
||||
@ -88,6 +91,7 @@ void RawFile::open_subfiles() {
|
||||
v[j] = new RawSubFile(m_master.data_fname(j, i),
|
||||
m_master.detector_type(), pos.height,
|
||||
pos.width, m_master.bitdepth());
|
||||
|
||||
}
|
||||
subfiles.push_back(v);
|
||||
}
|
||||
@ -128,12 +132,14 @@ int RawFile::find_number_of_subfiles() {
|
||||
fmt::print("Found: {} subfiles\n", n_files);
|
||||
#endif
|
||||
return n_files;
|
||||
|
||||
}
|
||||
|
||||
void RawFile::find_geometry() {
|
||||
uint16_t r{};
|
||||
uint16_t c{};
|
||||
|
||||
|
||||
for (size_t i = 0; i < n_subfile_parts; i++) {
|
||||
auto h = this->read_header(m_master.data_fname(i, 0));
|
||||
r = std::max(r, h.row);
|
||||
@ -145,6 +151,7 @@ void RawFile::find_geometry() {
|
||||
g.width = m_master.pixels_x();
|
||||
g.height = m_master.pixels_y();
|
||||
m_module_pixel_0.push_back(g);
|
||||
|
||||
}
|
||||
|
||||
r++;
|
||||
@ -154,6 +161,7 @@ void RawFile::find_geometry() {
|
||||
m_cols = (c * m_master.pixels_x());
|
||||
|
||||
m_rows += static_cast<size_t>((r - 1) * cfg.module_gap_row);
|
||||
|
||||
#ifdef AARE_VERBOSE
|
||||
fmt::print("\nRawFile::find_geometry()\n");
|
||||
for (size_t i = 0; i < m_module_pixel_0.size(); i++) {
|
||||
@ -227,6 +235,7 @@ void RawFile::update_geometry_with_roi() {
|
||||
fmt::print("Updated image size: {}x{}\n\n", m_rows, m_cols);
|
||||
fmt::print("\n");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
Frame RawFile::get_frame(size_t frame_index) {
|
||||
@ -236,18 +245,22 @@ Frame RawFile::get_frame(size_t frame_index) {
|
||||
return f;
|
||||
}
|
||||
|
||||
|
||||
size_t RawFile::bytes_per_pixel() const {
|
||||
return m_master.bitdepth() / 8;
|
||||
}
|
||||
|
||||
void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, DetectorHeader *header) {
|
||||
|
||||
if (frame_index > total_frames()) {
|
||||
throw std::runtime_error(LOCATION + "Frame number out of range");
|
||||
}
|
||||
std::vector<size_t> frame_numbers(n_subfile_parts);
|
||||
std::vector<size_t> frame_indices(n_subfile_parts, frame_index);
|
||||
|
||||
|
||||
// sync the frame numbers
|
||||
|
||||
if (n_subfile_parts != 1) {
|
||||
for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) {
|
||||
auto subfile_id = frame_index / m_master.max_frames_per_file();
|
||||
@ -302,6 +315,7 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect
|
||||
//TODO! should we read row by row?
|
||||
|
||||
// create a buffer large enough to hold a full module
|
||||
|
||||
auto bytes_per_part = m_master.pixels_y() * m_master.pixels_x() *
|
||||
m_master.bitdepth() /
|
||||
8; // TODO! replace with image_size_in_bytes
|
||||
@ -331,6 +345,7 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer, Detect
|
||||
part_buffer + cur_row * pos.width *
|
||||
m_master.bitdepth() / 8,
|
||||
pos.width * m_master.bitdepth() / 8);
|
||||
|
||||
}
|
||||
}
|
||||
delete[] part_buffer;
|
||||
|
@ -8,8 +8,6 @@ RawFileNameComponents::RawFileNameComponents(
|
||||
m_base_name = fname.stem();
|
||||
m_ext = fname.extension();
|
||||
|
||||
AARE_ASSERT(false);
|
||||
|
||||
if (m_ext != ".json" && m_ext != ".raw") {
|
||||
throw std::runtime_error(LOCATION +
|
||||
"Unsupported file type. (only .json or .raw)");
|
||||
@ -145,8 +143,10 @@ ScanParameters RawMasterFile::scan_parameters() const {
|
||||
return m_scan_parameters;
|
||||
}
|
||||
|
||||
|
||||
std::optional<ROI> RawMasterFile::roi() const { return m_roi; }
|
||||
|
||||
|
||||
void RawMasterFile::parse_json(const std::filesystem::path &fpath) {
|
||||
std::ifstream ifs(fpath);
|
||||
json j;
|
||||
@ -244,6 +244,7 @@ void RawMasterFile::parse_json(const std::filesystem::path &fpath) {
|
||||
// not a scan
|
||||
}
|
||||
|
||||
|
||||
try{
|
||||
ROI tmp_roi;
|
||||
auto obj = j.at("Receiver Roi");
|
||||
@ -276,6 +277,7 @@ void RawMasterFile::parse_json(const std::filesystem::path &fpath) {
|
||||
|
||||
|
||||
|
||||
|
||||
// Update detector type for Moench
|
||||
// TODO! How does this work with old .raw master files?
|
||||
#ifdef AARE_VERBOSE
|
||||
|
91
src/SubFile.cpp
Normal file
91
src/SubFile.cpp
Normal file
@ -0,0 +1,91 @@
|
||||
#include "aare/SubFile.hpp"
|
||||
#include "aare/PixelMap.hpp"
|
||||
#include <cstring> // memcpy
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
|
||||
namespace aare {
|
||||
|
||||
SubFile::SubFile(const std::filesystem::path &fname, DetectorType detector, size_t rows, size_t cols, size_t bitdepth,
|
||||
const std::string &mode)
|
||||
: m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols), m_mode(mode), m_detector_type(detector) {
|
||||
|
||||
|
||||
if (m_detector_type == DetectorType::Moench03_old) {
|
||||
pixel_map = GenerateMoench03PixelMap();
|
||||
}
|
||||
|
||||
if (std::filesystem::exists(fname)) {
|
||||
n_frames = std::filesystem::file_size(fname) / (sizeof(DetectorHeader) + rows * cols * bitdepth / 8);
|
||||
} else {
|
||||
n_frames = 0;
|
||||
}
|
||||
|
||||
if (mode == "r") {
|
||||
fp = fopen(m_fname.string().c_str(), "rb");
|
||||
} else {
|
||||
throw std::runtime_error(LOCATION + "Unsupported mode. Can only read RawFiles.");
|
||||
}
|
||||
if (fp == nullptr) {
|
||||
throw std::runtime_error(LOCATION + fmt::format("Could not open file {}", m_fname.string()));
|
||||
}
|
||||
#ifdef AARE_VERBOSE
|
||||
fmt::print("Opened file: {} with {} frames\n", m_fname.string(), n_frames);
|
||||
fmt::print("m_rows: {}, m_cols: {}, m_bitdepth: {}\n", m_rows, m_cols, m_bitdepth);
|
||||
#endif
|
||||
}
|
||||
|
||||
size_t SubFile::get_part(std::byte *buffer, size_t frame_index) {
|
||||
if (frame_index >= n_frames) {
|
||||
throw std::runtime_error("Frame number out of range");
|
||||
}
|
||||
fseek(fp, sizeof(DetectorHeader) + (sizeof(DetectorHeader) + bytes_per_part()) * frame_index, // NOLINT
|
||||
SEEK_SET);
|
||||
|
||||
if (pixel_map){
|
||||
// read into a temporary buffer and then copy the data to the buffer
|
||||
// in the correct order
|
||||
auto part_buffer = new std::byte[bytes_per_part()];
|
||||
auto wc = fread(part_buffer, bytes_per_part(), 1, fp);
|
||||
auto *data = reinterpret_cast<uint16_t *>(buffer);
|
||||
auto *part_data = reinterpret_cast<uint16_t *>(part_buffer);
|
||||
for (size_t i = 0; i < pixels_per_part(); i++) {
|
||||
data[i] = part_data[(*pixel_map)(i)];
|
||||
}
|
||||
delete[] part_buffer;
|
||||
return wc;
|
||||
}else{
|
||||
// read directly into the buffer
|
||||
return fread(buffer, this->bytes_per_part(), 1, this->fp);
|
||||
}
|
||||
|
||||
}
|
||||
size_t SubFile::write_part(std::byte *buffer, DetectorHeader header, size_t frame_index) {
|
||||
if (frame_index > n_frames) {
|
||||
throw std::runtime_error("Frame number out of range");
|
||||
}
|
||||
fseek(fp, static_cast<int64_t>((sizeof(DetectorHeader) + bytes_per_part()) * frame_index), SEEK_SET);
|
||||
auto wc = fwrite(reinterpret_cast<char *>(&header), sizeof(header), 1, fp);
|
||||
wc += fwrite(buffer, bytes_per_part(), 1, fp);
|
||||
|
||||
return wc;
|
||||
}
|
||||
|
||||
size_t SubFile::frame_number(size_t frame_index) {
|
||||
DetectorHeader h{};
|
||||
fseek(fp, (sizeof(DetectorHeader) + bytes_per_part()) * frame_index, SEEK_SET); // NOLINT
|
||||
size_t const rc = fread(reinterpret_cast<char *>(&h), sizeof(h), 1, fp);
|
||||
if (rc != 1)
|
||||
throw std::runtime_error(LOCATION + "Could not read header from file");
|
||||
|
||||
return h.frameNumber;
|
||||
}
|
||||
|
||||
SubFile::~SubFile() {
|
||||
if (fp) {
|
||||
fclose(fp);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace aare
|
@ -1,6 +1,7 @@
|
||||
#include "aare/defs.hpp"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include <fmt/core.h>
|
||||
namespace aare {
|
||||
|
||||
@ -12,6 +13,7 @@ void assert_failed(const std::string &msg)
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Convert a DetectorType to a string
|
||||
* @param type DetectorType
|
||||
|
Reference in New Issue
Block a user