bitdepths

This commit is contained in:
Erik Fröjdh 2025-01-07 12:27:01 +01:00
parent da67f58323
commit d07da42745
3 changed files with 34 additions and 19 deletions

View File

@ -66,6 +66,9 @@ class RawSubFile {
size_t pixels_per_frame() const { return m_rows * m_cols; } size_t pixels_per_frame() const { return m_rows * m_cols; }
size_t bytes_per_pixel() const { return m_bitdepth / 8; } size_t bytes_per_pixel() const { return m_bitdepth / 8; }
private:
template <typename T>
void read_with_map(std::byte *image_buf);
}; };

View File

@ -256,7 +256,7 @@ std::vector<Cluster3x3> ClusterFile::read_cluster_with_cut(size_t n_clusters,
} }
NDArray<double, 2> calculate_eta2(ClusterVector<int> &clusters) { NDArray<double, 2> calculate_eta2(ClusterVector<int> &clusters) {
NDArray<double, 2> eta2({clusters.size(), 2}); NDArray<double, 2> eta2({static_cast<int64_t>(clusters.size()), 2});
for (size_t i = 0; i < clusters.size(); i++) { for (size_t i = 0; i < clusters.size(); i++) {
// int32_t t2; // int32_t t2;
// auto* ptr = reinterpret_cast<int32_t*> (clusters.element_ptr(i) + 2 * // auto* ptr = reinterpret_cast<int32_t*> (clusters.element_ptr(i) + 2 *

View File

@ -9,11 +9,13 @@ namespace aare {
RawSubFile::RawSubFile(const std::filesystem::path &fname, RawSubFile::RawSubFile(const std::filesystem::path &fname,
DetectorType detector, size_t rows, size_t cols, DetectorType detector, size_t rows, size_t cols,
size_t bitdepth, uint32_t pos_row, uint32_t pos_col) 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_detector_type(detector), m_bitdepth(bitdepth), m_fname(fname),
m_bytes_per_frame((m_bitdepth / 8) * m_rows * m_cols), m_pos_row(pos_row), m_pos_col(pos_col) { 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) { if (m_detector_type == DetectorType::Moench03_old) {
m_pixel_map = GenerateMoench03PixelMap(); 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(); m_pixel_map = GenerateEigerFlipRowsPixelMap();
} }
@ -51,37 +53,48 @@ size_t RawSubFile::tell() {
return m_file.tellg() / (sizeof(DetectorHeader) + bytes_per_frame()); return m_file.tellg() / (sizeof(DetectorHeader) + bytes_per_frame());
} }
void RawSubFile::read_into(std::byte *image_buf, DetectorHeader *header) { void RawSubFile::read_into(std::byte *image_buf, DetectorHeader *header) {
if(header){ if (header) {
m_file.read(reinterpret_cast<char *>(header), sizeof(DetectorHeader)); m_file.read(reinterpret_cast<char *>(header), sizeof(DetectorHeader));
} else { } else {
m_file.seekg(sizeof(DetectorHeader), std::ios::cur); m_file.seekg(sizeof(DetectorHeader), std::ios::cur);
} }
//TODO! expand support for different bitdepths // TODO! expand support for different bitdepths
if(m_pixel_map){ if (m_pixel_map) {
// read into a temporary buffer and then copy the data to the buffer // read into a temporary buffer and then copy the data to the buffer
// in the correct order // in the correct order
// currently this only supports 16 bit data! // TODO! add 4 bit support
auto part_buffer = new std::byte[bytes_per_frame()]; if(m_bitdepth == 8){
m_file.read(reinterpret_cast<char *>(part_buffer), bytes_per_frame()); read_with_map<uint8_t>(image_buf);
auto *data = reinterpret_cast<uint16_t *>(image_buf); }else if (m_bitdepth == 16) {
auto *part_data = reinterpret_cast<uint16_t *>(part_buffer); read_with_map<uint16_t>(image_buf);
for (size_t i = 0; i < pixels_per_frame(); i++) { } else if (m_bitdepth == 32) {
data[i] = part_data[(*m_pixel_map)(i)]; read_with_map<uint32_t>(image_buf);
}else{
throw std::runtime_error("Unsupported bitdepth for read with pixel map");
} }
delete[] part_buffer;
} else { } else {
// read directly into the buffer // read directly into the buffer
m_file.read(reinterpret_cast<char *>(image_buf), bytes_per_frame()); m_file.read(reinterpret_cast<char *>(image_buf), bytes_per_frame());
} }
} }
template <typename T>
void RawSubFile::read_with_map(std::byte *image_buf) {
auto part_buffer = new std::byte[bytes_per_frame()];
m_file.read(reinterpret_cast<char *>(part_buffer), bytes_per_frame());
auto *data = reinterpret_cast<T *>(image_buf);
auto *part_data = reinterpret_cast<T *>(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::rows() const { return m_rows; }
size_t RawSubFile::cols() const { return m_cols; } size_t RawSubFile::cols() const { return m_cols; }
void RawSubFile::get_part(std::byte *buffer, size_t frame_index) { void RawSubFile::get_part(std::byte *buffer, size_t frame_index) {
seek(frame_index); seek(frame_index);
read_into(buffer, nullptr); read_into(buffer, nullptr);
@ -94,5 +107,4 @@ size_t RawSubFile::frame_number(size_t frame_index) {
return h.frameNumber; return h.frameNumber;
} }
} // namespace aare } // namespace aare