Scope
ScopeImage.cpp
1 #include "stdafx.h"
2 #include "ScopeImage.h"
3 
4 namespace scope {
5 
6 bool InsertPixels(ScopeImageU16Ptr const _img, std::vector<uint16_t>::iterator& _where, const DaqChunk::iterator& _from, const DaqChunk::iterator& _to) {
7  bool retval = false;
8  // Important: this locks the ScopeImage mutex for write access to the data !!
9  ScopeImageAccessU16 imagedata(*_img);
10 
11  assert(_from <= _to); // both conditions should be taken care of by the pixelmapper
12  assert(_where + std::distance(_from, _to) <= imagedata.GetData()->end() ); // do not insert over data end, otherwise exception by std::copy ?!
13 
14  // insert the pixels into ScopeImage and advance inserter
15  if ( _from != _to ) // Check here, otherwise copy throws exception on empty range
16  _where = std::copy( _from, _to, _where);
17 
18  if ( _where == imagedata.GetData()->end() ) // image completely filled
19  retval = true;
20 
21  return retval;
22 }
23 
24 bool InsertAndAveragePixels(ScopeImageU16Ptr const _img, std::vector<uint16_t>::iterator& _where, const DaqChunk::iterator _from, const DaqChunk::iterator _to, const uint16_t& _currentavgcount) {
25  assert(_currentavgcount < UINT16_MAX>>1); // since worst case daqch=65535, i=65535, _multiplier=65535 (=> 65535/2)
26 
27  bool retval = false;
28  // Important: this locks the ScopeImage mutex for write access to the data !!
29  ScopeImageAccessU16 imagedata(*_img);
30  uint32_t divisor = 1;
31  uint32_t halfdivisor = 1;
32  uint32_t multiplier = 0;
33  if ( _currentavgcount > 0 ) { // currenavgcount = 0: first frame, multiply by 0 -> overwrite last image pixel (for running update of old pixels), see PipelineController
34  multiplier = _currentavgcount; // currenavgcount = 1: second frame, multiply by 1, divide by 2
35  divisor = _currentavgcount + 1; // etc etc
36  halfdivisor = divisor >> 2;
37  }
38 
39  uint32_t n = 0;
40  // insert the pixels into ScopeImage and advance inserter
41  _where = std::transform(_from, _to, _where, _where, [&](const uint16_t& daqch, const uint16_t& i) {
42  n = (static_cast<uint32_t>(i) * multiplier) + static_cast<uint32_t>(daqch);
43  return static_cast<uint16_t>( n / divisor + ((n%divisor)>halfdivisor?1u:0u) ); } ); // check residual to fix integer division rounding error
44 
45  if ( _where == imagedata.GetData()->end() ) { // image completely filled
46  retval = true;
47  _where = imagedata.GetData()->begin();
48  }
49 
50  return retval;
51 }
52 
53 
54 
55 }
std::vector< uint16_t >::iterator iterator
Iterator over the data vector.
Definition: DaqChunk.h:23
This is the include file for standard system include files, or project specific include files that ar...