-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageLoader.cpp
More file actions
executable file
·89 lines (71 loc) · 2.37 KB
/
ImageLoader.cpp
File metadata and controls
executable file
·89 lines (71 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//
// Created by Arkadiy on 20/03/2016.
//
#include <assert.h>
#include <regex>
#include <iomanip>
#include <sstream>
#include <future>
#include <iostream>
#include "ImageLoader.h"
//#include "cbf.h"
#include "CBFDataReader.h"
#include "Hdf5HelperFuncitons.h"
ImageLoader::ImageLoader(ReconstructionParameters par) :
current_frame_number(par.first_image),
filename_template(par.data_filename_template),
last_frame_number(par.last_image),
frame_increment(par.frame_increment),
mask_is_defined(false)
{
//get file size
CBFDataReader first_frame(current_frame_filename());
current_frame_number-=frame_increment;
//allocate memory
m_dim1=first_frame.dim1();
m_dim2=first_frame.dim2();
data = (int *)malloc(sizeof(int)*m_dim1*m_dim2);
buffer = (int *)malloc(sizeof(int)*m_dim1*m_dim2); //Tick-tock buffer.
if(par.mask_filename != "") {
// load mask
if(!file_exists(par.mask_filename)) {
throw FileNotFound(par.mask_filename);
}
H5File file(par.mask_filename, H5F_ACC_RDONLY);
auto sz = getDatasetDimensions(file, "data");
if(sz[0]!=m_dim2 || sz[1]!=m_dim1) {
std::stringstream message;
message << "Error with input mask dimensions. Expected " << m_dim1 << "x" << m_dim2 << " found " << sz[0] <<"x" << sz[1] << "." << endl;
throw MaskError(message.str());
}
mask = readVector<int>(file, "data");
mask_is_defined = true;
}
load_frame_to_buffer();
}
void ImageLoader::load_frame_to_buffer() {
next_frame_f = async(launch::async, //async|deferred
[=](int current_frame_number)
{
CBFDataReader frame(format_template(filename_template, current_frame_number + frame_increment));
assert(frame.dim1() == ny() and frame.dim2() == nx());
frame.read_data(buffer);
}, current_frame_number);
}
template <typename T>
void swap_a_for_b(T& a, T& b) {
T t=a;
a=b;
b=t;
}
// TODO: check this works with stepping over some frames
bool ImageLoader::load_next_frame() {
current_frame_number+=frame_increment;
if (current_frame_number > last_frame_number)
return false;
next_frame_f.get();
swap_a_for_b(data, buffer);
if (current_frame_number+frame_increment <= last_frame_number)
load_frame_to_buffer();
return true;
}