mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2025-06-17 17:57:13 +02:00
WIP
This commit is contained in:
186
src/RawFile.cpp
186
src/RawFile.cpp
@ -13,10 +13,11 @@ 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
|
||||
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_from_roi();
|
||||
update_geometry_with_roi();
|
||||
open_subfiles();
|
||||
} else {
|
||||
throw std::runtime_error(LOCATION +
|
||||
@ -39,10 +40,19 @@ void RawFile::read_into(std::byte *image_buf, size_t n_frames) {
|
||||
image_buf += bytes_per_frame();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
};
|
||||
|
||||
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;
|
||||
}
|
||||
@ -72,12 +82,12 @@ xy RawFile::geometry() { return m_master.geometry(); }
|
||||
void RawFile::open_subfiles() {
|
||||
if (m_mode == "r")
|
||||
for (size_t i = 0; i != n_subfiles; ++i) {
|
||||
auto v = std::vector<SubFile *>(n_subfile_parts);
|
||||
auto v = std::vector<RawSubFile *>(n_subfile_parts);
|
||||
for (size_t j = 0; j != n_subfile_parts; ++j) {
|
||||
v[j] =
|
||||
new SubFile(m_master.data_fname(j, i),
|
||||
m_master.detector_type(), m_master.pixels_y(),
|
||||
m_master.pixels_x(), m_master.bitdepth());
|
||||
auto pos = m_module_pixel_0[j];
|
||||
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);
|
||||
}
|
||||
@ -111,38 +121,30 @@ bool RawFile::is_master_file(const std::filesystem::path &fpath) {
|
||||
int RawFile::find_number_of_subfiles() {
|
||||
int n_files = 0;
|
||||
// f0,f1...fn How many files is the data split into?
|
||||
while (std::filesystem::exists(m_master.data_fname(0, ++n_files)))
|
||||
;
|
||||
while (std::filesystem::exists(m_master.data_fname(0, n_files)))
|
||||
n_files++; // increment after test
|
||||
|
||||
#ifdef AARE_VERBOSE
|
||||
fmt::print("Found: {} subfiles\n", n_subfiles);
|
||||
#endif
|
||||
#ifdef AARE_VERBOSE
|
||||
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++) {
|
||||
// for (size_t j = 0; j != n_subfiles; ++j) {
|
||||
// auto h = this->read_header(m_master.data_fname(i, j));
|
||||
// r = std::max(r, h.row);
|
||||
// c = std::max(c, h.column);
|
||||
|
||||
// positions.push_back({h.row, h.column});
|
||||
// }
|
||||
// }
|
||||
|
||||
|
||||
std::vector<xy> module_position;
|
||||
|
||||
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);
|
||||
c = std::max(c, h.column);
|
||||
positions.push_back({h.row, h.column});
|
||||
xy pos = {h.row * m_master.pixels_y(), h.column* m_master.pixels_x()};
|
||||
module_position.emplace_back(pos);
|
||||
auto h = this->read_header(m_master.data_fname(i, 0));
|
||||
r = std::max(r, h.row);
|
||||
c = std::max(c, h.column);
|
||||
positions.push_back({h.row, h.column});
|
||||
ModuleGeometry g;
|
||||
g.x = h.column * m_master.pixels_x();
|
||||
g.y = h.row * m_master.pixels_y();
|
||||
g.width = m_master.pixels_x();
|
||||
g.height = m_master.pixels_y();
|
||||
m_module_pixel_0.push_back(g);
|
||||
}
|
||||
|
||||
r++;
|
||||
@ -152,10 +154,79 @@ void RawFile::find_geometry() {
|
||||
m_cols = (c * m_master.pixels_x());
|
||||
|
||||
m_rows += static_cast<size_t>((r - 1) * cfg.module_gap_row);
|
||||
|
||||
for (size_t i=0; i < module_position.size(); i++){
|
||||
fmt::print("Module {} at position: ({},{})\n", i, module_position[i].row, module_position[i].col);
|
||||
#ifdef AARE_VERBOSE
|
||||
fmt::print("\nRawFile::find_geometry()\n");
|
||||
for (size_t i = 0; i < m_module_pixel_0.size(); i++) {
|
||||
fmt::print("Module {} at position: (r:{},c:{})\n", i,
|
||||
m_module_pixel_0[i].y, m_module_pixel_0[i].x);
|
||||
}
|
||||
fmt::print("Image size: {}x{}\n\n", m_rows, m_cols);
|
||||
#endif
|
||||
}
|
||||
|
||||
void RawFile::update_geometry_with_roi() {
|
||||
// TODO! implement this
|
||||
if (m_master.roi()) {
|
||||
auto roi = m_master.roi().value();
|
||||
|
||||
// TODO! can we do this cleaner?
|
||||
int pos_y = 0;
|
||||
int pos_y_increment = 0;
|
||||
for (size_t row = 0; row < m_master.geometry().row; row++) {
|
||||
int pos_x = 0;
|
||||
for (size_t col = 0; col < m_master.geometry().col; col++) {
|
||||
auto &m = m_module_pixel_0[row * m_master.geometry().col + col];
|
||||
auto original_height = m.height;
|
||||
auto original_width = m.width;
|
||||
|
||||
// module is to the left of the roi
|
||||
if (m.x + m.width < roi.xmin) {
|
||||
m.width = 0;
|
||||
|
||||
// roi is in module
|
||||
} else {
|
||||
// here we only arrive when the roi is in or to the left of
|
||||
// the module
|
||||
if (roi.xmin > m.x) {
|
||||
m.width -= roi.xmin - m.x;
|
||||
}
|
||||
if (roi.xmax < m.x + m.width) {
|
||||
m.width -= m.x + original_width - roi.xmax;
|
||||
}
|
||||
m.x = pos_x;
|
||||
pos_x += m.width;
|
||||
}
|
||||
|
||||
if (m.y + m.height < roi.ymin) {
|
||||
m.height = 0;
|
||||
} else {
|
||||
if (roi.ymin < m.y + m.height) {
|
||||
m.height -= roi.ymin;
|
||||
}
|
||||
if (roi.ymax < m.y + m.height) {
|
||||
m.height -= m.y + original_height - roi.ymax;
|
||||
}
|
||||
m.y = pos_y;
|
||||
pos_y_increment = m.height;
|
||||
}
|
||||
}
|
||||
// increment pos_y
|
||||
pos_y += pos_y_increment;
|
||||
}
|
||||
|
||||
m_rows = roi.height();
|
||||
m_cols = roi.width();
|
||||
}
|
||||
|
||||
#ifdef AARE_VERBOSE
|
||||
fmt::print("RawFile::update_geometry_with_roi()\n");
|
||||
for (const auto &m : m_module_pixel_0) {
|
||||
fmt::print("Module at position: (r:{}, c:{}, h:{}, w:{})\n", m.y, m.x,
|
||||
m.height, m.width);
|
||||
}
|
||||
fmt::print("Updated image size: {}x{}\n\n", m_rows, m_cols);
|
||||
fmt::print("\n");
|
||||
#endif
|
||||
}
|
||||
|
||||
Frame RawFile::get_frame(size_t frame_index) {
|
||||
@ -165,13 +236,18 @@ Frame RawFile::get_frame(size_t frame_index) {
|
||||
return f;
|
||||
}
|
||||
|
||||
void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer) {
|
||||
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();
|
||||
@ -208,15 +284,24 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer) {
|
||||
for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) {
|
||||
auto corrected_idx = frame_indices[part_idx];
|
||||
auto subfile_id = corrected_idx / m_master.max_frames_per_file();
|
||||
auto part_offset = subfiles[subfile_id][part_idx]->bytes_per_part();
|
||||
subfiles[subfile_id][part_idx]->get_part(
|
||||
frame_buffer + part_idx * part_offset,
|
||||
corrected_idx % m_master.max_frames_per_file());
|
||||
|
||||
// This is where we start writing
|
||||
auto offset = (m_module_pixel_0[part_idx].y * m_cols +
|
||||
m_module_pixel_0[part_idx].x)*m_master.bitdepth()/8;
|
||||
|
||||
if (m_module_pixel_0[part_idx].x!=0)
|
||||
throw std::runtime_error(LOCATION + "Implementation error. x pos not 0.");
|
||||
|
||||
subfiles[subfile_id][part_idx]->seek(corrected_idx % m_master.max_frames_per_file());
|
||||
subfiles[subfile_id][part_idx]->read_into(frame_buffer + offset, header);
|
||||
if (header)
|
||||
++header;
|
||||
}
|
||||
|
||||
} else {
|
||||
//TODO! should we read row by row?
|
||||
|
||||
// create a buffer that will hold a the frame part
|
||||
// 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
|
||||
@ -226,23 +311,26 @@ void RawFile::get_frame_into(size_t frame_index, std::byte *frame_buffer) {
|
||||
// level
|
||||
|
||||
for (size_t part_idx = 0; part_idx != n_subfile_parts; ++part_idx) {
|
||||
auto pos = m_module_pixel_0[part_idx];
|
||||
auto corrected_idx = frame_indices[part_idx];
|
||||
auto subfile_id = corrected_idx / m_master.max_frames_per_file();
|
||||
|
||||
subfiles[subfile_id][part_idx]->get_part(
|
||||
part_buffer, corrected_idx % m_master.max_frames_per_file());
|
||||
for (size_t cur_row = 0; cur_row < (m_master.pixels_y());
|
||||
subfiles[subfile_id][part_idx]->seek(corrected_idx % m_master.max_frames_per_file());
|
||||
subfiles[subfile_id][part_idx]->read_into(part_buffer, header);
|
||||
if(header)
|
||||
++header;
|
||||
|
||||
for (size_t cur_row = 0; cur_row < (pos.height);
|
||||
cur_row++) {
|
||||
auto irow = cur_row + (part_idx / m_master.geometry().col) *
|
||||
m_master.pixels_y();
|
||||
auto icol =
|
||||
(part_idx % m_master.geometry().col) * m_master.pixels_x();
|
||||
|
||||
auto irow = (pos.y + cur_row);
|
||||
auto icol = pos.x;
|
||||
auto dest = (irow * this->m_cols + icol);
|
||||
dest = dest * m_master.bitdepth() / 8;
|
||||
memcpy(frame_buffer + dest,
|
||||
part_buffer + cur_row * m_master.pixels_x() *
|
||||
part_buffer + cur_row * pos.width *
|
||||
m_master.bitdepth() / 8,
|
||||
m_master.pixels_x() * m_master.bitdepth() / 8);
|
||||
pos.width * m_master.bitdepth() / 8);
|
||||
}
|
||||
}
|
||||
delete[] part_buffer;
|
||||
|
@ -8,6 +8,8 @@ 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)");
|
||||
@ -254,6 +256,12 @@ void RawMasterFile::parse_json(const std::filesystem::path &fpath) {
|
||||
//if any of the values are set update the roi
|
||||
if (tmp_roi.xmin != 4294967295 || tmp_roi.xmax != 4294967295 ||
|
||||
tmp_roi.ymin != 4294967295 || tmp_roi.ymax != 4294967295) {
|
||||
|
||||
if(v<7.21){
|
||||
tmp_roi.xmax++;
|
||||
tmp_roi.ymax++;
|
||||
}
|
||||
|
||||
m_roi = tmp_roi;
|
||||
}
|
||||
|
||||
|
99
src/RawSubFile.cpp
Normal file
99
src/RawSubFile.cpp
Normal file
@ -0,0 +1,99 @@
|
||||
#include "aare/RawSubFile.hpp"
|
||||
#include "aare/PixelMap.hpp"
|
||||
#include <cstring> // memcpy
|
||||
#include <fmt/core.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace aare {
|
||||
|
||||
RawSubFile::RawSubFile(const std::filesystem::path &fname,
|
||||
DetectorType detector, size_t rows, size_t cols,
|
||||
size_t bitdepth)
|
||||
: m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols),
|
||||
m_detector_type(detector),
|
||||
m_bytes_per_frame((m_bitdepth / 8) * m_rows * m_cols) {
|
||||
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 {
|
||||
throw std::runtime_error(
|
||||
LOCATION + fmt::format("File {} does not exist", m_fname.string()));
|
||||
}
|
||||
|
||||
// fp = fopen(m_fname.string().c_str(), "rb");
|
||||
m_file.open(m_fname, std::ios::binary);
|
||||
if (!m_file.is_open()) {
|
||||
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);
|
||||
fmt::print("file size: {}\n", std::filesystem::file_size(fname));
|
||||
#endif
|
||||
}
|
||||
|
||||
void RawSubFile::seek(size_t frame_index) {
|
||||
if (frame_index >= n_frames) {
|
||||
throw std::runtime_error("Frame number out of range");
|
||||
}
|
||||
m_file.seekg((sizeof(DetectorHeader) + bytes_per_frame()) * frame_index);
|
||||
}
|
||||
|
||||
size_t RawSubFile::tell() {
|
||||
return m_file.tellg() / (sizeof(DetectorHeader) + bytes_per_frame());
|
||||
}
|
||||
|
||||
|
||||
void RawSubFile::read_into(std::byte *image_buf, DetectorHeader *header) {
|
||||
if(header){
|
||||
fmt::print("Reading header\n");
|
||||
m_file.read(reinterpret_cast<char *>(header), sizeof(DetectorHeader));
|
||||
} else {
|
||||
m_file.seekg(sizeof(DetectorHeader), std::ios::cur);
|
||||
fmt::print("Skipping heading header\n");
|
||||
}
|
||||
|
||||
//TODO! expand support for different bitdepths
|
||||
if(pixel_map){
|
||||
// read into a temporary buffer and then copy the data to the buffer
|
||||
// in the correct order
|
||||
// currently this only supports 16 bit data!
|
||||
auto part_buffer = new std::byte[bytes_per_frame()];
|
||||
m_file.read(reinterpret_cast<char *>(part_buffer), bytes_per_frame());
|
||||
auto *data = reinterpret_cast<uint16_t *>(image_buf);
|
||||
auto *part_data = reinterpret_cast<uint16_t *>(part_buffer);
|
||||
for (size_t i = 0; i < pixels_per_frame(); i++) {
|
||||
data[i] = part_data[(*pixel_map)(i)];
|
||||
}
|
||||
delete[] part_buffer;
|
||||
} else {
|
||||
// read directly into the buffer
|
||||
m_file.read(reinterpret_cast<char *>(image_buf), bytes_per_frame());
|
||||
}
|
||||
}
|
||||
|
||||
size_t RawSubFile::rows() const { return m_rows; }
|
||||
size_t RawSubFile::cols() const { return m_cols; }
|
||||
|
||||
|
||||
void RawSubFile::get_part(std::byte *buffer, size_t frame_index) {
|
||||
seek(frame_index);
|
||||
read_into(buffer, nullptr);
|
||||
}
|
||||
|
||||
size_t RawSubFile::frame_number(size_t frame_index) {
|
||||
seek(frame_index);
|
||||
DetectorHeader h{};
|
||||
m_file.read(reinterpret_cast<char *>(&h), sizeof(DetectorHeader));
|
||||
return h.frameNumber;
|
||||
}
|
||||
|
||||
|
||||
} // namespace aare
|
@ -1,82 +0,0 @@
|
||||
#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::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
|
10
src/defs.cpp
10
src/defs.cpp
@ -1,9 +1,17 @@
|
||||
#include "aare/defs.hpp"
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#include <fmt/core.h>
|
||||
namespace aare {
|
||||
|
||||
|
||||
void assert_failed(const std::string &msg)
|
||||
{
|
||||
fmt::print(msg);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Convert a DetectorType to a string
|
||||
* @param type DetectorType
|
||||
|
Reference in New Issue
Block a user