API
streamWriter_test.cpp
Go to the documentation of this file.
1 
2 #include "../../../tests/catch2/catch.hpp"
3 #include "../../tests/testMacrosINDI.hpp"
4 
5 #include "../streamWriter.hpp"
6 
7 using namespace MagAOX::app;
8 
9 namespace MagAOX
10 {
11 namespace app
12 {
14 {
16 
18  {
19  m_sw = sw;
20  }
21 
22  std::string rawimageDir(){ return m_sw->m_rawimageDir; }
23 
24 
25  int setup_circbufs( int width,
26  int height,
27  int dataType,
28  int circBuffLength
29  )
30  {
31  m_sw->m_width = width;
32  m_sw->m_height = height;
33  m_sw->m_dataType = dataType;
34  m_sw->m_typeSize = ImageStreamIO_typesize(m_sw->m_dataType);
35  std::cerr << m_sw->m_typeSize << "\n";
36 
37  m_sw->m_circBuffLength = circBuffLength;
38 
39  return m_sw->allocate_circbufs();
40  }
41 
42  // Sets m_writeChunkLength and calls allocate_xrif
43  // Call this *only* after setup_circbufs.
44  int setup_xrif( int writeChunkLength )
45  {
46  m_sw->m_writeChunkLength = writeChunkLength;
47 
48  m_sw->initialize_xrif();
49  return m_sw->allocate_xrif();
50  }
51 
52  //Allocates and populates the filename buffer
54  {
55  m_sw->m_fnameBase = "/tmp/swtest_";
56 
57  m_sw->m_fnameSz = m_sw->m_fnameBase.size() + sizeof("YYYYMMDDHHMMSSNNNNNNNNN.xrif"); //the sizeof includes the \0
58  m_sw->m_fname = (char*) malloc(m_sw->m_fnameSz);
59 
60  snprintf(m_sw->m_fname, m_sw->m_fnameSz, "%sYYYYMMDDHHMMSSNNNNNNNNN.xrif", m_sw->m_fnameBase.c_str());
61 
62  return 0;
63  }
64 
65  //Fill the circular buffers.
67  {
68  //fill in image data with increasing 256 bit vals.
69  for(size_t pp =0; pp < m_sw->m_circBuffLength; ++pp)
70  {
71  uint16_t v = pp;
72  for(size_t rr =0; rr < m_sw->m_width; ++rr)
73  {
74  for(size_t cc =0; cc < m_sw->m_height; ++cc)
75  {
76  ((uint16_t *)m_sw->m_rawImageCircBuff)[pp*m_sw->m_width*m_sw->m_height + rr*m_sw->m_height + cc] = v;
77  ++v;
78  }
79  }
80 
81  //fitsFile<uint16_t> ff;
82  //ff.write("cb.fits", m_sw->m_rawImageCircBuff);
83 
84  //Fill in timing values with unique vals.
85  uint64_t * curr_timing = m_sw->m_timingCircBuff + 5*pp;
86  curr_timing[0] = pp; //image number
87  curr_timing[1] = pp + 1000; //atime sec
88  curr_timing[2] = pp + 2000; //atime nsec
89  curr_timing[3] = pp + m_sw->m_circBuffLength + 1000; //wtime sec
90  curr_timing[4] = pp + m_sw->m_circBuffLength + 2000; //wtime nsec
91  }
92  return 0;
93  }
94 
95  int write_frames( int start, // arbitrary.
96  int stop //should be a m_writeChunkLength boundary
97  )
98  {
99  m_sw->m_currSaveStart = start;
100  m_sw->m_currSaveStop = stop;
101  m_sw->m_currSaveStopFrameNo = stop;
102 
103  m_sw->m_writing = WRITING;
104  return m_sw->doEncode();
105  }
106 
107  //Read the xrif archive back in and compare the results.
108  int comp_frames_uint16( size_t start,
109  size_t stop
110  )
111  {
112 
113  std::cout << "Reading: " << m_sw->m_fname << "\n";
114 
115  xrif_t xrif;
116  xrif_error_t xrv = xrif_new(&xrif);
117 
118  char header[XRIF_HEADER_SIZE];
119 
120  FILE * fp_xrif = fopen(m_sw->m_fname, "rb");
121  size_t nr = fread(header, 1, XRIF_HEADER_SIZE, fp_xrif);
122 
123  if(nr != XRIF_HEADER_SIZE)
124  {
125  std::cerr << "Error reading header of " << m_sw->m_fname << "\n";
126  fclose(fp_xrif);
127  return -1;
128  }
129 
130  uint32_t header_size;
131  xrif_read_header(xrif, &header_size , header);
132 
133  int rv = 0;
134  if(xrif_width(xrif) != m_sw->m_width)
135  {
136  std::cerr << "width mismatch\n";
137  rv = -1;
138  }
139 
140  if(xrif_height(xrif) != m_sw->m_height)
141  {
142  std::cerr << "height mismatch\n";
143  rv = -1;
144  }
145 
146  if(xrif_depth(xrif) != 1)
147  {
148  std::cerr << "depth mismatch\n";
149  rv = -1;
150  }
151 
152  if(xrif_frames(xrif) != stop-start )
153  {
154  std::cerr << "frames mismatch\n";
155  rv = -1;
156  }
157 
158  xrif_allocate(xrif);
159 
160 
161  nr = fread(xrif->raw_buffer, 1, xrif->compressed_size, fp_xrif);
162 
163  if(nr != xrif->compressed_size)
164  {
165  std::cerr << "error reading compressed image buffer.\n";
166  return -1;
167  }
168 
169  xrv = xrif_decode(xrif);
170  if(xrv != XRIF_NOERROR)
171  {
172  std::cerr << "error decoding compressed image buffer. Code: " << xrv << "\n";
173  return -1;
174  }
175 
176  size_t badpix = 0;
177 
178  for(size_t n=0; n< m_sw->m_width*m_sw->m_height*m_sw->m_typeSize*(stop-start); ++n)
179  {
180  if( m_sw->m_rawImageCircBuff[start*m_sw->m_width*m_sw->m_height*m_sw->m_typeSize + n] != xrif->raw_buffer[n] ) ++badpix;
181  }
182 
183  if(badpix > 0)
184  {
185  std::cerr << "Buffers don't match: " << badpix << " bad pixels.\n";
186  return -1;
187  }
188 
189  return rv;
190  }
191 
192 };
193 }
194 }
195 
196 using namespace MagAOX::app;
197 
198 SCENARIO( "streamWriter Configuration", "[streamWriter]" )
199 {
200  GIVEN("A default constructed streamWriter")
201  {
202  streamWriter sw;
203  streamWriter_test sw_test(&sw);
204 
205  WHEN("default configurations")
206  {
207  REQUIRE(sw_test.rawimageDir() == "");
208 
209 
210  }
211  }
212 }
213 
214 SCENARIO( "streamWriter encoding data", "[streamWriter]" )
215 {
216  GIVEN("A default constructed streamWriter and a 120x120 uint16 stream")
217  {
218  streamWriter sw;
219  streamWriter_test sw_test(&sw);
220 
221  WHEN("writing full 1st chunk")
222  {
223  int circBuffLength = 10;
224  int writeChunkLength = 5;
225  REQUIRE(sw_test.setup_circbufs(120, 120, XRIF_TYPECODE_UINT16, circBuffLength) == 0);
226  REQUIRE(sw_test.setup_xrif(writeChunkLength) == 0);
227  REQUIRE(sw_test.setup_fname() == 0);
228 
229  REQUIRE(sw_test.fill_circbuf_uint16() == 0);
230 
231  REQUIRE(sw_test.write_frames(0,5) == 0);
232 
233  REQUIRE(sw_test.comp_frames_uint16(0,5) == 0);
234  }
235 
236  WHEN("writing full 2nd chunk")
237  {
238  int circBuffLength = 10;
239  int writeChunkLength = 5;
240  REQUIRE(sw_test.setup_circbufs(120, 120, XRIF_TYPECODE_UINT16, circBuffLength) == 0);
241  REQUIRE(sw_test.setup_xrif(writeChunkLength) == 0);
242  REQUIRE(sw_test.setup_fname() == 0);
243 
244  REQUIRE(sw_test.fill_circbuf_uint16() == 0);
245 
246  REQUIRE(sw_test.write_frames(5,10) == 0);
247 
248  REQUIRE(sw_test.comp_frames_uint16(5,10) == 0);
249  }
250 
251  WHEN("writing partial 1st chunk")
252  {
253  int circBuffLength = 10;
254  int writeChunkLength = 5;
255  REQUIRE(sw_test.setup_circbufs(120, 120, XRIF_TYPECODE_UINT16, circBuffLength) == 0);
256  REQUIRE(sw_test.setup_xrif(writeChunkLength) == 0);
257  REQUIRE(sw_test.setup_fname() == 0);
258 
259  REQUIRE(sw_test.fill_circbuf_uint16() == 0);
260 
261  REQUIRE(sw_test.write_frames(2,5) == 0);
262 
263  REQUIRE(sw_test.comp_frames_uint16(2,5) == 0);
264  }
265 
266  WHEN("writing partial 2nd chunk")
267  {
268  int circBuffLength = 10;
269  int writeChunkLength = 5;
270  REQUIRE(sw_test.setup_circbufs(120, 120, XRIF_TYPECODE_UINT16, circBuffLength) == 0);
271  REQUIRE(sw_test.setup_xrif(writeChunkLength) == 0);
272  REQUIRE(sw_test.setup_fname() == 0);
273 
274  REQUIRE(sw_test.fill_circbuf_uint16() == 0);
275 
276  REQUIRE(sw_test.write_frames(5,8) == 0);
277 
278  REQUIRE(sw_test.comp_frames_uint16(5,8) == 0);
279  }
280  }
281 }
#define GIVEN(desc)
Definition: catch.hpp:17763
#define WHEN(desc)
Definition: catch.hpp:17765
#define REQUIRE(...)
Definition: catch.hpp:17676
size_t m_circBuffLength
The length of the circular buffer, in frames.
int initialize_xrif()
Initialize the xrif system.
uint8_t m_dataType
The ImageStreamIO type code.
int m_typeSize
The pixel byte depth.
uint64_t m_currSaveStart
The circular buffer position at which to start saving.
size_t m_writeChunkLength
The number of frames to write at a time.
size_t m_height
The height of the image.
uint64_t m_currSaveStopFrameNo
The frame number of the image at which saving stopped (for logging)
int allocate_circbufs()
Worker function to allocate the circular buffers.
int allocate_xrif()
Worker function to configure and allocate the xrif handles.
std::string m_rawimageDir
The path where files will be saved.
int m_writing
Controls whether or not images are being written, and sequences start and stop of writing.
int doEncode()
Function called when semaphore is raised to do the encode and write.
uint64_t m_currSaveStop
The circular buffer position at which to stop saving.
size_t m_width
The width of the image.
std::ostream & cerr()
std::ostream & cout()
Definition: dm.hpp:24
#define WRITING
SCENARIO("streamWriter Configuration", "[streamWriter]")
int setup_xrif(int writeChunkLength)
int write_frames(int start, int stop)
int setup_circbufs(int width, int height, int dataType, int circBuffLength)
int comp_frames_uint16(size_t start, size_t stop)