diff --git a/include/aare/RawSubFile.hpp b/include/aare/RawSubFile.hpp index 4d78670..89c278e 100644 --- a/include/aare/RawSubFile.hpp +++ b/include/aare/RawSubFile.hpp @@ -66,6 +66,9 @@ class RawSubFile { size_t pixels_per_frame() const { return m_rows * m_cols; } size_t bytes_per_pixel() const { return m_bitdepth / 8; } +private: + template + void read_with_map(std::byte *image_buf); }; diff --git a/src/ClusterFile.cpp b/src/ClusterFile.cpp index 855e0e7..0e5b93e 100644 --- a/src/ClusterFile.cpp +++ b/src/ClusterFile.cpp @@ -256,7 +256,7 @@ std::vector ClusterFile::read_cluster_with_cut(size_t n_clusters, } NDArray calculate_eta2(ClusterVector &clusters) { - NDArray eta2({clusters.size(), 2}); + NDArray eta2({static_cast(clusters.size()), 2}); for (size_t i = 0; i < clusters.size(); i++) { // int32_t t2; // auto* ptr = reinterpret_cast (clusters.element_ptr(i) + 2 * diff --git a/src/RawSubFile.cpp b/src/RawSubFile.cpp index 6fae7ce..4612747 100644 --- a/src/RawSubFile.cpp +++ b/src/RawSubFile.cpp @@ -9,11 +9,13 @@ namespace aare { RawSubFile::RawSubFile(const std::filesystem::path &fname, DetectorType detector, size_t rows, size_t cols, size_t bitdepth, uint32_t pos_row, uint32_t pos_col) - : m_detector_type(detector), m_bitdepth(bitdepth), m_fname(fname), m_rows(rows), m_cols(cols), - m_bytes_per_frame((m_bitdepth / 8) * m_rows * m_cols), m_pos_row(pos_row), m_pos_col(pos_col) { + : m_detector_type(detector), m_bitdepth(bitdepth), m_fname(fname), + m_rows(rows), m_cols(cols), + m_bytes_per_frame((m_bitdepth / 8) * m_rows * m_cols), m_pos_row(pos_row), + m_pos_col(pos_col) { if (m_detector_type == DetectorType::Moench03_old) { m_pixel_map = GenerateMoench03PixelMap(); - }else if(m_detector_type == DetectorType::Eiger && m_pos_row % 2 == 0){ + } else if (m_detector_type == DetectorType::Eiger && m_pos_row % 2 == 0) { m_pixel_map = GenerateEigerFlipRowsPixelMap(); } @@ -51,37 +53,48 @@ 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){ - m_file.read(reinterpret_cast(header), sizeof(DetectorHeader)); + if (header) { + m_file.read(reinterpret_cast(header), sizeof(DetectorHeader)); } else { m_file.seekg(sizeof(DetectorHeader), std::ios::cur); } - //TODO! expand support for different bitdepths - if(m_pixel_map){ + // TODO! expand support for different bitdepths + if (m_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(part_buffer), bytes_per_frame()); - auto *data = reinterpret_cast(image_buf); - auto *part_data = reinterpret_cast(part_buffer); - for (size_t i = 0; i < pixels_per_frame(); i++) { - data[i] = part_data[(*m_pixel_map)(i)]; + // TODO! add 4 bit support + if(m_bitdepth == 8){ + read_with_map(image_buf); + }else if (m_bitdepth == 16) { + read_with_map(image_buf); + } else if (m_bitdepth == 32) { + read_with_map(image_buf); + }else{ + throw std::runtime_error("Unsupported bitdepth for read with pixel map"); } - delete[] part_buffer; + } else { // read directly into the buffer m_file.read(reinterpret_cast(image_buf), bytes_per_frame()); } } +template +void RawSubFile::read_with_map(std::byte *image_buf) { + auto part_buffer = new std::byte[bytes_per_frame()]; + m_file.read(reinterpret_cast(part_buffer), bytes_per_frame()); + auto *data = reinterpret_cast(image_buf); + auto *part_data = reinterpret_cast(part_buffer); + for (size_t i = 0; i < pixels_per_frame(); i++) { + data[i] = part_data[(*m_pixel_map)(i)]; + } + delete[] part_buffer; +} 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); @@ -94,5 +107,4 @@ size_t RawSubFile::frame_number(size_t frame_index) { return h.frameNumber; } - } // namespace aare \ No newline at end of file