10 #include <ImageStreamIO/ImageStruct.h>
11 #include <ImageStreamIO/ImageStreamIO.h>
13 #include <xrif/xrif.h>
15 #include <mx/ioutils/fileUtils.hpp>
16 #include <mx/improc/eigenCube.hpp>
17 #include <mx/ioutils/fits/fitsFile.hpp>
19 #include <mx/sys/timeUtils.hpp>
21 #include "../../libMagAOX/libMagAOX.hpp"
28 std::this_thread::sleep_for( std::chrono::microseconds( usec ) );
49 static_cast<void>( signum );
50 static_cast<void>( siginf );
51 static_cast<void>( ucont );
74 std::vector<std::string>
140 "The directory to search for files. Can be empty if pull path given in files." );
149 "List of files to use. If dir is not empty, it will be pre-pended to each name." );
150 config.add(
"numFrames",
158 "The number of frames to store in memory. This defines how many different images will be streamed. "
159 "If 0 (the default), all frames found using dir and files are loaded and stored." );
160 config.add(
"earliest",
168 "If set or true, then the earliest numFrames in the archive are used. By default (if not set) the "
169 "latest numFrames are used." );
170 config.add(
"shmimName",
178 "The name of the shared memory buffer to stream to. Default is \"xrif2shmim\"" );
179 config.add(
"circBuffLength",
187 "The length of the shared memory circular buffer. Default is 1." );
197 "The rate, in frames per second, at which to stream images. Default is 10 fps." );
202 config(
m_dir,
"dir" );
208 config(
m_fps,
"fps" );
214 struct sigaction act;
218 act.sa_flags = SA_SIGINFO;
223 if( sigaction( SIGTERM, &act, 0 ) < 0 )
225 std::cerr <<
" (" << invokedName <<
"): error setting SIGTERM handler: " << strerror( errno ) <<
"\n";
230 if( sigaction( SIGQUIT, &act, 0 ) < 0 )
232 std::cerr <<
" (" << invokedName <<
"): error setting SIGQUIT handler: " << strerror( errno ) <<
"\n";
237 if( sigaction( SIGINT, &act, 0 ) < 0 )
239 std::cerr <<
" (" << invokedName <<
"): error setting SIGINT handler: " << strerror( errno ) <<
"\n";
251 m_files = mx::ioutils::getFileNames(
m_dir,
"",
"",
".xrif" );
261 for(
size_t n = 0; n <
m_files.size(); ++n )
269 std::cerr <<
" (" << invokedName <<
"): No files found.\n";
278 std::cerr <<
" (" << invokedName <<
"): Error allocating xrif.\n";
293 char header[XRIF_HEADER_SIZE];
298 for(
long n = st; n != ed; n += stp )
301 FILE *fp_xrif = fopen(
m_files[n].c_str(),
"rb" );
305 mx::errno_report(
"xrif2shmim", errno, __FILE__, __LINE__,
"null ptr from fopen for " +
m_files[n] );
309 if( ferror( fp_xrif ) || errno != 0 )
311 mx::errno_report(
"xrif2shmim", errno, __FILE__, __LINE__,
"error from fopen for " +
m_files[n] );
315 size_t nr = fread( header, 1, XRIF_HEADER_SIZE, fp_xrif );
318 if( nr != XRIF_HEADER_SIZE )
320 std::cerr <<
" (" << invokedName <<
"): Error reading header of " <<
m_files[n] <<
"\n";
324 uint32_t header_size;
325 xrif_read_header(
m_xrif, &header_size, header );
337 std::cerr <<
" (" << invokedName <<
"): width mis-match in " <<
m_files[n] <<
"\n";
342 std::cerr <<
" (" << invokedName <<
"): height mis-match in " <<
m_files[n] <<
"\n";
347 std::cerr <<
" (" << invokedName <<
"): data type mismatch in " <<
m_files[n] <<
"\n";
353 std::cerr <<
" (" << invokedName <<
"): Cubes detected in " <<
m_files[n] <<
"\n";
359 std::cerr <<
" (" << invokedName <<
"): Only 16-bit signed integers (short) supported" <<
"\n";
363 nframes +=
m_xrif->frames;
374 std::cerr <<
" (" << invokedName <<
"): exiting.\n";
384 std::cerr <<
" (" << invokedName <<
"): Reading " <<
m_numFrames <<
" frames in " << ( ed - st ) * stp <<
" file";
385 if( ( ed - st ) * stp > 1 )
407 for(
long n = st; n != ed; n += stp )
415 FILE *fp_xrif = fopen(
m_files[n].c_str(),
"rb" );
419 mx::errno_report(
"xrif2shmim", errno, __FILE__, __LINE__,
"null ptr from fopen for " +
m_files[n] );
423 if( ferror( fp_xrif ) || errno != 0 )
425 mx::errno_report(
"xrif2shmim", errno, __FILE__, __LINE__,
"error from fopen for " +
m_files[n] );
429 size_t nr = fread( header, 1, XRIF_HEADER_SIZE, fp_xrif );
431 if( nr != XRIF_HEADER_SIZE )
433 std::cerr <<
" (" << invokedName <<
"): Error reading header of " <<
m_files[n] <<
"\n";
438 uint32_t header_size;
439 xrif_read_header(
m_xrif, &header_size, header );
441 xrif_allocate_raw(
m_xrif );
443 xrif_allocate_reordered(
m_xrif );
445 nr = fread(
m_xrif->raw_buffer, 1,
m_xrif->compressed_size, fp_xrif );
454 if( nr !=
m_xrif->compressed_size )
456 std::cerr <<
" (" << invokedName <<
"): Error reading data from " <<
m_files[n] <<
"\n";
467 mx::improc::eigenCube<uint16_t> tmpc(
472 long ped = tmpc.planes();
475 pst = tmpc.planes() - 1;
479 for(
int p = pst; p != ped; p += stp )
481 m_frames.image( findex ) = tmpc.image( p );
492 std::cerr <<
" (" << invokedName <<
"): exiting.\n";
519 CIRCULAR_BUFFER | ZAXIS_TEMPORAL,
525 uint64_t next_cnt1 = 0;
532 double lastSend = mx::sys::get_curr_time();
542 clock_gettime( CLOCK_REALTIME, &
m_imageStream.md->writetime );
573 double ct = mx::sys::get_curr_time();
574 delta += 0.1 * ( ct - lastSend - 1.0 /
m_fps );
577 if( 1. /
m_fps - delta > 0 )
584 std::cerr <<
" (" << invokedName <<
"): exited normally.\n";
A utility to stream MagaO-X images from xrif compressed archives to an ImageStreamIO stream.
std::vector< std::string > m_files
List of files to use. If dir is not empty, it will be pre-pended to each name.
uint32_t m_height
The height of the image.
uint32_t m_width
The width of the image.
size_t m_typeSize
The size of the type, in bytes. Result of sizeof.
uint8_t m_dataType
The ImageStreamIO type code.
double m_fps
The rate, in frames per second, at which to stream images. Default is 10 fps.
IMAGE m_imageStream
The ImageStreamIO shared memory buffer.
std::string m_shmimName
The name of the shared memory buffer to stream to. Default is "xrif2shmim".
virtual void setupConfig()
mx::improc::eigenCube< uint16_t > m_frames
virtual void loadConfig()
uint32_t m_circBuffLength
The length of the shared memory circular buffer. Default is 1.
void sigTermHandler(int signum, siginfo_t *siginf, void *ucont)
void microsleep(unsigned usec)
Sleep for a specified period in microseconds.