Scope
ScopeImage.h
1 #pragma once
2 
3 #include "pixel.h"
4 #include "DaqChunk.h"
5 #include "ScopeException.h"
6 #include "helpers.h"
7 
8 namespace scope {
9 
10 template<class T>
12 
13 template<class T>
15 
22 template<class T>
23 class ScopeImage {
24 
27 
30 
31 public:
33  typedef std::pair<typename std::vector<T>::iterator, typename std::vector<T>::iterator> datapart_t;
34 
36  typename typedef std::vector<T>::const_iterator citerator;
37 
39  typename typedef std::vector<T>::size_type size_type;
40 
41 protected:
43  const uint32_t area;
44 
46  uint32_t lines;
47 
49  uint32_t linewidth;
50 
53 
56 
59 
61  std::vector<T> data;
62 
64  typename std::vector<T>::iterator inserter;
65 
67  datapart_t newpart;
68 
70  mutable std::mutex pixelmutex;
71 
73  mutable std::atomic<int16_t> reading_access;
74 
76  mutable std::condition_variable readcond;
77 
78 public:
80  ScopeImage(const uint32_t& _lines = 128, const uint32_t& _linewidth = 128, const uint32_t& _area = 0, const bool& _complete_avg = false, const bool& _complete_frame = false)
81  : area(_area)
82  , lines(_lines)
83  , linewidth(_linewidth)
84  , complete_frame(_complete_frame)
85  , percent_complete(0.0)
86  , complete_avg(_complete_avg)
87  , data(_lines*_linewidth, T(0))
88  , inserter(data.begin())
89  , newpart(data.begin(), data.begin())
90  , reading_access(0) {
91  }
92 
94  ScopeImage(const ScopeImage& _si)
95  : area(_si.area)
96  , lines(_si.lines)
97  , linewidth(_si.linewidth)
98  , complete_frame(_si.complete_frame)
99  , percent_complete(_si.percent_complete)
100  , complete_avg(_si.complete_avg)
101  , reading_access(0) {
102  ScopeImageConstAccess<T> acc(_si);
103  data = *acc.GetConstData();
104  inserter = data.begin();
105  newpart = datapart_t(data.begin(), data.begin());
106  }
107 
110  // Avoid self-assignment
111  if ( this != &_si ) {
112  assert(lines == _si.lines);
113  assert(linewidth == _si.linewidth);
114  area = _si.area;
115  complete_frame = _si.complete_frame;
116  percent_complete = _si.percent_complete;
117  complete_avg = _si.complete_avg;
118  ScopeImageAccess<T> acc(_si);
119  data = *acc.GetData();
120  inserter = data.begin();
121  newpart = datapart_t(data.begin(), data.begin());
122  }
123  return *this;
124  }
125 
128  uint32_t Area() const { return area; }
129  uint32_t Lines() const { return lines; }
130  uint32_t Linewidth() const { return linewidth; }
131  datapart_t Newpart() const { return newpart; }
132  bool IsCompleteFrame() const { return complete_frame; }
133  double PercentComplete() const { return percent_complete; }
134  bool IsCompleteAvg() const { return complete_avg; }
139  void SetCompleteFrame(const bool& _complete) { complete_frame = _complete; }
140  void SetPercentComplete(const double& _percent) { percent_complete = _percent; }
141  void SetCompleteAvg(const bool& _complete) { complete_avg = _complete; }
145  T Pixel (const uint32_t& _column, const uint32_t& _line) const {
146  ScopeImageConstAccess<T> acc(*this);
147  return acc.GetConstData()->at(_line * linewidth + _column);
148  }
149 
151  void FillRandom() {
152  std::lock_guard<std::mutex> lock(pixelmutex);
153  std::mt19937 mt;
154  mt.seed(static_cast<unsigned long>(GetTickCount64()));
155  std::generate( std::begin(data), std::end(data), mt);
156  newpart = std::make_pair(data.begin(), data.end());
157  }
158 
159 protected:
162  std::vector<T>* GetData() {
163  std::unique_lock<std::mutex> lock(pixelmutex);
164  while ( reading_access > 0 )
165  readcond.wait(lock);
166  lock.release(); // mutex stays locked
167  return &data;
168  }
169 
171  const std::vector<T>* GetDataConst() const {
172  pixelmutex.lock(); // waits until a write access is finished
173  reading_access++;
174  pixelmutex.unlock();
175  return &data;
176  }
177 
179  void ReleaseData() {
180  pixelmutex.unlock();
181  }
182 
184  void ReleaseDataConst() const {
185  assert(reading_access > 0);
186  reading_access--;
187  readcond.notify_all();
188  }
189 };
190 
193 template<class T>
194 class ScopeImageAccess {
195 protected:
199  std::vector<T>* const pData;
200 
201 public:
204  : image(&_image)
205  , pData(image->GetData()) {
206  }
207 
210  image->ReleaseData();
211  }
212 
214  std::vector<T>* GetData() const {
215  return pData;
216  }
217 
219  T* GetPointer() const {
220  return pData->data();
221  }
222 };
223 
226 template<class T>
227 class ScopeImageConstAccess {
228 protected:
230  const ScopeImage<T>* const image;
232  const std::vector<T>* const pData;
233 
234 public:
237  : image(&_image)
238  , pData(image->GetDataConst()) {
239  }
240 
243  image->ReleaseDataConst();
244  }
245 
247  const std::vector<T>* GetConstData() const {
248  return pData;
249  }
250 };
251 
253 typedef std::shared_ptr<ScopeImage<uint16_t>> ScopeImageU16Ptr;
255 typedef std::shared_ptr<const ScopeImage<uint16_t>> ScopeImageU16CPtr;
257 typedef ScopeImageAccess<uint16_t> ScopeImageAccessU16;
259 typedef ScopeImageConstAccess<uint16_t> ScopeImageConstAccessU16;
260 
268 bool InsertPixels(ScopeImageU16Ptr const _img, std::vector<uint16_t>::iterator& _where, const DaqChunk::iterator& _from, const DaqChunk::iterator& _to);
269 
280 bool InsertAndAveragePixels(ScopeImageU16Ptr const _img, std::vector<uint16_t>::iterator& _where, const DaqChunk::iterator _from, const DaqChunk::iterator _to, const uint16_t& _currentavgcount);
281 
282 }
uint32_t linewidth
width of a line, x resolution
Definition: ScopeImage.h:49
~ScopeImageAccess()
Release data on destruction, unlocks mutex.
Definition: ScopeImage.h:209
std::vector< T > * GetData()
Definition: ScopeImage.h:162
std::condition_variable readcond
condition variable for read access
Definition: ScopeImage.h:76
std::vector< T >::size_type size_type
the size type
Definition: ScopeImage.h:39
std::vector< uint16_t >::iterator iterator
Iterator over the data vector.
Definition: DaqChunk.h:23
double percent_complete
how many percent of the frame are already filled
Definition: ScopeImage.h:58
const std::vector< T > * GetDataConst() const
Definition: ScopeImage.h:171
std::atomic< int16_t > reading_access
>0 if someone got a pointer to the const datavector
Definition: ScopeImage.h:73
void ReleaseDataConst() const
Decreases reading_access counter.
Definition: ScopeImage.h:184
const ScopeImage< T > *const image
pointer to the ScopeImage
Definition: ScopeImage.h:230
std::vector< T >::iterator inserter
current insertion position
Definition: ScopeImage.h:64
T * GetPointer() const
Definition: ScopeImage.h:219
~ScopeImageConstAccess()
Release data on destruction, unlocks mutex.
Definition: ScopeImage.h:242
std::vector< T > data
vector with pixel data
Definition: ScopeImage.h:61
Gives RAII safe access (read&write) to the pixeldata of a ScopeImage.
Definition: ScopeImage.h:11
The BGRA8Pixel struct and various related helpers for color conversions.
std::vector< T > * GetData() const
Definition: ScopeImage.h:214
A general image class.
Definition: ScopeImage.h:23
std::vector< T > *const pData
Pointer to the ScopeImage's data vector.
Definition: ScopeImage.h:199
bool complete_avg
false if this is a not completely averaged frame, it is then only for display purpose and will not be...
Definition: ScopeImage.h:55
const uint32_t area
area of the image
Definition: ScopeImage.h:43
ScopeImage< T > *const image
pointer to the ScopeImage
Definition: ScopeImage.h:197
ScopeImage & operator=(const ScopeImage &_si)
Safe assignment.
Definition: ScopeImage.h:109
bool complete_frame
false if frame not complete, allows for partial display during acquisition
Definition: ScopeImage.h:52
void FillRandom()
Fills the complete image with random pixel data.
Definition: ScopeImage.h:151
ScopeImageAccess(ScopeImage< T > &_image)
Get the data, locks mutex inside ScopeImage.
Definition: ScopeImage.h:203
datapart_t newpart
range of freshly inserted pixels
Definition: ScopeImage.h:67
ScopeImage(const uint32_t &_lines=128, const uint32_t &_linewidth=128, const uint32_t &_area=0, const bool &_complete_avg=false, const bool &_complete_frame=false)
Initialize with zeros.
Definition: ScopeImage.h:80
const std::vector< T > * GetConstData() const
Definition: ScopeImage.h:247
T Pixel(const uint32_t &_column, const uint32_t &_line) const
Definition: ScopeImage.h:145
ScopeImage(const ScopeImage &_si)
Safe copy.
Definition: ScopeImage.h:94
std::mutex pixelmutex
mutex for protection of pixel operations
Definition: ScopeImage.h:70
uint32_t lines
number of lines, y resolution
Definition: ScopeImage.h:46
Various helper functions and classes for Scope.
std::pair< typename std::vector< T >::iterator, typename std::vector< T >::iterator > datapart_t
pair of two iterators over the data vector
Definition: ScopeImage.h:33
Gives RAII safe const access (read-only) to the pixeldata of a ScopeImage.
Definition: ScopeImage.h:14
ScopeImageConstAccess(const ScopeImage< T > &_image)
Get the data, locks mutex inside ScopeImage.
Definition: ScopeImage.h:236
std::vector< T >::const_iterator citerator
const iterator over the data vector
Definition: ScopeImage.h:36
const std::vector< T > *const pData
Pointer to the ScopeImage's data vector.
Definition: ScopeImage.h:232