Commit cd108f10 authored by ahoms's avatar ahoms
Browse files

* added FrameDim * and / operators with Bin in SizeUtils

* extended BufferSave to integrate functionality in espia_acq_lib
* fixed StdBufferCbMgr frame cb timestamp to be relative to the acq. start
* added real buffer filling and frame callback to Simulator
* modified test and testsimulator to use latest class interfaces



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@37 45c4679d-1946-429d-baad-37912b19538b
parent c06c861a
......@@ -34,6 +34,9 @@ class Simulator
void setBin(const Bin& bin);
void getBin(Bin& bin);
void setFrameDim(const FrameDim& frame_dim);
void getFrameDim(FrameDim& frame_dim);
Status getStatus();
private:
......
......@@ -42,6 +42,9 @@ void Simulator::SimuThread::execStartAcq()
StdBufferCbMgr& buffer_mgr = m_simu.m_buffer_cb_mgr;
buffer_mgr.setStartTimestamp(Timestamp::now());
FrameBuilder& frame_builder = m_simu.m_frame_builder;
frame_builder.resetFrameNr();
struct timespec treq, trem;
double exp_time = m_simu.m_exp_time;
treq.tv_sec = int(floor(exp_time));
......@@ -52,7 +55,14 @@ void Simulator::SimuThread::execStartAcq()
for (frame_nb = 0; frame_nb < nb_frames; frame_nb++) {
setStatus(Exposure);
nanosleep(&treq, &trem);
setStatus(Readout);
int buffer_nb = frame_nb % buffer_mgr.getNbBuffers();
typedef unsigned char *BufferPtr;
BufferPtr ptr = BufferPtr(buffer_mgr.getBufferPtr(buffer_nb));
frame_builder.getNextFrame(ptr);
buffer_mgr.newFrameReady(frame_nb);
}
setStatus(Ready);
}
......@@ -113,6 +123,16 @@ void Simulator::getBin(Bin& bin)
m_frame_builder.getBin(bin);
}
void Simulator::setFrameDim(const FrameDim& frame_dim)
{
m_frame_builder.setFrameDim(frame_dim);
}
void Simulator::getFrameDim(FrameDim& frame_dim)
{
m_frame_builder.getFrameDim(frame_dim);
}
Simulator::Status Simulator::getStatus()
{
int thread_status = m_thread.getStatus();
......
#include "BufferSave.h"
#include <ctime>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <sstream>
#include <unistd.h>
#include <sys/time.h>
#include "BufferSave.h"
using namespace lima;
using namespace std;
#define EDF_HEADER_LEN 1024
#define EDF_HEADER_BUFFER_LEN (10 * EDF_HEADER_LEN)
BufferSave::BufferSave( FileFormat format, const String& prefix, int idx,
const String& suffix, bool overwrite,
int tot_file_frames )
: m_format(format), m_prefix(prefix), m_idx(idx), m_suffix(suffix),
m_overwrite(overwrite), m_tot_file_frames(tot_file_frames)
{
m_written_frames = 0;
m_fout = NULL;
if (m_suffix == "")
m_suffix = getDefSuffix();
}
BufferSave::BufferSave( )
: m_format(Raw), m_prefix(), m_idx(0), m_overwrite(false),
m_tot_file_frames(1)
{
m_written_frames = 0;
m_fout = NULL;
if (m_suffix == "")
m_suffix = getDefSuffix();
}
BufferSave::~BufferSave( )
{
closeFile();
}
BufferSave::String BufferSave::getDefSuffix() const
{
return (m_format == EDF) ? ".edf" : ".raw";
}
void BufferSave::openFile()
{
if (isFileOpen())
return;
ostringstream idx;
idx.width(4);
idx.fill('0');
idx << m_idx;
m_file_name = m_prefix + idx.str() + m_suffix;
m_fout = new ofstream(m_file_name.c_str(),
ios_base::out | ios_base::binary);
}
int BufferSave::writeEdfHeader( const FrameInfoType &finfo,
std::ofstream &file )
void BufferSave::closeFile()
{
if (!isFileOpen())
return;
delete m_fout;
m_fout = NULL;
m_file_name = "";
m_written_frames = 0;
m_idx++;
}
void BufferSave::writeEdfHeader( const FrameInfoType& finfo )
{
time_t ctime_now;
struct timeval tod_now;
char time_str[64], buffer[EDF_HEADER_BUFFER_LEN], *p;
int l, len, rem;
const FrameDim *fdim = finfo.frame_dim;
const Size& frame_size = fdim->getSize();
int depth = fdim->getDepth();
int image = m_written_frames + 1;
time(&ctime_now);
gettimeofday(&tod_now, NULL);
......@@ -25,19 +97,23 @@ int BufferSave::writeEdfHeader( const FrameInfoType &finfo,
p = buffer;
p += sprintf(p, "{\n");
p += sprintf(p, "HeaderID = EH:%06lu:000000:000000 ;\n", finfo.acq_frame_nb);
p += sprintf(p, "Image = %lu ;\n", finfo.acq_frame_nb);
p += sprintf(p, "HeaderID = EH:%06u:000000:000000 ;\n", image);
p += sprintf(p, "Image = %u ;\n", image);
p += sprintf(p, "ByteOrder = LowByteFirst ;\n");
p += sprintf(p, "DataType = %s ;\n",
(finfo.depth == 1) ? "UnsignedByte" :
((finfo.depth == 2) ? "UnsignedShort" : "UnsignedLong"));
p += sprintf(p, "Size = %d ;\n", finfo.width*finfo.height*finfo.depth);
p += sprintf(p, "Dim_1 = %d ;\n", finfo.width);
p += sprintf(p, "Dim_2 = %d ;\n", finfo.height);
(depth == 1) ? "UnsignedByte" :
((depth == 2) ? "UnsignedShort" : "UnsignedLong"));
p += sprintf(p, "Size = %d ;\n", fdim->getMemSize());
p += sprintf(p, "Dim_1 = %d ;\n", frame_size.getWidth());
p += sprintf(p, "Dim_2 = %d ;\n", frame_size.getHeight());
p += sprintf(p, "acq_frame_nb = %d ;\n", finfo.acq_frame_nb);
p += sprintf(p, "time = %s ;\n", time_str);
p += sprintf(p, "time_of_day = %ld.%06ld ;\n",
tod_now.tv_sec, tod_now.tv_usec);
if (finfo.frame_timestamp.isSet())
p += sprintf(p, "time_of_frame = %.6f ;\n",
double(finfo.frame_timestamp));
l = p - buffer;
len = l;
......@@ -47,40 +123,96 @@ int BufferSave::writeEdfHeader( const FrameInfoType &finfo,
p += sprintf(p, "%*s}\n", len - (l + 2), "");
len = p - buffer;
file.write(buffer, len);
return len;
m_fout->write(buffer, len);
}
void BufferSave::writeFrame( const FrameInfoType& finfo )
{
const FrameDim *fdim = finfo.frame_dim;
if (fdim == NULL)
throw LIMA_HW_EXC(InvalidValue, "Null finfo.fdim");
openFile();
if ( m_format == EDF )
writeEdfHeader(finfo);
m_fout->write((char *)finfo.frame_ptr, fdim->getMemSize());
BufferSave::BufferSave( const string &prefix, enum FileFormat format ) :
m_prefix(prefix), m_format(format)
m_written_frames++;
if (m_written_frames == m_tot_file_frames)
closeFile();
}
bool BufferSave::isFileOpen() const
{
return (m_fout != NULL);
}
void BufferSave::setPrefix(const String& prefix)
{
if (prefix == m_prefix)
return;
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue, "Set prefix with open file");
BufferSave::~BufferSave( )
m_prefix = prefix;
}
void BufferSave::getPrefix(String& prefix) const
{
prefix = m_prefix;
}
void BufferSave::setFormat(FileFormat format)
{
if (format == m_format)
return;
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue, "Set format with open file");
m_format = format;
m_suffix = getDefSuffix();
int BufferSave::writeFrame( FrameInfoType &finfo )
}
void BufferSave::getFormat(FileFormat& format) const
{
string name;
char num_str[64];
format = m_format;
}
name = m_prefix;
snprintf(num_str, 63, "%06lu", finfo.acq_frame_nb);
name += string(num_str);
name += ".edf";
void BufferSave::setIndex(int idx)
{
if (idx == m_idx)
return;
ofstream file(name.c_str(), ios_base::out | ios_base::binary);
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue, "Set idx with open file");
if( m_format == FMT_EDF )
writeEdfHeader(finfo, file);
m_idx = idx;
}
file.write((char *)finfo.frame_ptr, finfo.width*finfo.height*finfo.depth);
void BufferSave::getIndex(int& idx) const
{
idx = m_idx;
}
file.close();
void BufferSave::setTotFileFrames(int tot_file_frames)
{
if (tot_file_frames == m_tot_file_frames)
return;
if (isFileOpen())
throw LIMA_HW_EXC(InvalidValue,
"Set tot_file_frames with open file");
m_tot_file_frames = tot_file_frames;
}
return 0;
void BufferSave::getTotFileFrames(int& tot_file_frames) const
{
tot_file_frames = m_tot_file_frames;
}
#ifndef BUFFERSAVE_H
#define BUFFERSAVE_H
#include <stdio.h>
#include <string>
#include <fstream>
#include "FrameBuilder.h"
#include "HwFrameCallback.h"
namespace lima {
#define EDF_HEADER_LEN 1024
#define EDF_HEADER_BUFFER_LEN (10 * EDF_HEADER_LEN)
class BufferSave {
public :
enum FileFormat {
Raw, EDF,
};
typedef std::string String;
typedef struct FrameInfo {
unsigned long acq_frame_nb;
void *frame_ptr;
int width, height, depth;
double frame_time_stamp;
} FrameInfoType;
BufferSave( );
BufferSave( FileFormat format, const String& prefix,
int idx = 0, const String& suffix = "",
bool overwrite = false , int tot_file_frames = 1);
~BufferSave( );
void writeFrame( const FrameInfoType& finfo );
enum FileFormat {
FMT_RAW,
FMT_EDF,
};
void setPrefix(const String& prefix);
void getPrefix(String& prefix) const;
void setFormat(FileFormat format);
void getFormat(FileFormat& format) const;
class BufferSave {
public :
std::string m_prefix;
enum FileFormat m_format;
void setIndex(int idx);
void getIndex(int& idx) const;
BufferSave( const std::string &prefix, enum FileFormat format );
~BufferSave( );
void setTotFileFrames(int tot_file_frames);
void getTotFileFrames(int& tot_file_frames) const;
int writeFrame( FrameInfoType &finfo );
void getOpenFileName(String& file_name) const;
bool isFileOpen() const;
private:
int writeEdfHeader( const FrameInfoType &finfo, std::ofstream &file );
String getDefSuffix() const;
void openFile();
void closeFile();
void writeEdfHeader( const FrameInfoType& finfo );
FileFormat m_format;
String m_prefix;
int m_idx;
String m_suffix;
String m_file_name;
bool m_overwrite;
int m_written_frames;
int m_tot_file_frames;
FrameInfoType m_last_frame;
std::ofstream *m_fout;
};
......
......@@ -4,6 +4,7 @@
#include "BufferSave.h"
#include "SizeUtils.h"
#include "Exceptions.h"
#include "AutoObj.h"
using namespace std;
using namespace lima;
......@@ -12,45 +13,31 @@ int main( void )
{
FrameBuilder fb;
FrameDim full_fd;
fb.getFrameDim(full_fd);
Bin bin = Bin(2,2);
fb.setBin(bin);
int binX = bin.getX();
int binY = bin.getY();
FrameDim fd;
fb.getFrameDim(fd);
int width = fd.getSize().getWidth();
int height = fd.getSize().getHeight();
int depth = fd.getDepth();
BufferSave bs("boza", FMT_EDF);
unsigned char *buffer;
FrameInfoType finfo;
buffer = NULL;
try {
int size = width * height * depth;
buffer = new unsigned char[size];
} catch( bad_alloc& ) {
goto end;
}
FrameDim fd = full_fd / bin;
BufferSave bs(BufferSave::EDF, "boza");
bs.setTotFileFrames(5);
int size = fd.getMemSize();
AutoPtr<unsigned char, true> buffer;
buffer = new unsigned char[size];
Timestamp start = Timestamp::now();
for( int i=0; i<10; i++ ) {
int frame_nb = fb.getFrameNr();
fb.getNextFrame( buffer );
finfo.acq_frame_nb = fb.getFrameNr();
finfo.frame_ptr = buffer;
finfo.width = width/binX;
finfo.height = height/binY;
finfo.depth = depth;
finfo.frame_time_stamp = 0; /* XXX */
Timestamp t = Timestamp::now() - start;
FrameInfoType finfo(frame_nb, buffer, &fd, t);
bs.writeFrame(finfo);
}
end:
if( buffer )
delete[] buffer;
return 0;
}
......@@ -5,9 +5,37 @@
using namespace lima;
using namespace std;
class TestFrameCallback : public HwFrameCallback
{
public:
TestFrameCallback(Simulator& simu) : m_simu(simu) {}
protected:
virtual bool newFrameReady(const FrameInfoType& frame_info);
private:
Simulator& m_simu;
};
bool TestFrameCallback::newFrameReady(const FrameInfoType& frame_info)
{
cout << "acq_frame_nb=" << frame_info.acq_frame_nb << ", "
<< "ts=" << frame_info.frame_timestamp << ", "
<< "simu=" << m_simu << endl;
return true;
}
int main(int argc, char *argv[])
{
Simulator simu;
TestFrameCallback cb(simu);
FrameDim frame_dim;
simu.getFrameDim(frame_dim);
BufferCtrlMgr& buffer_mgr = simu.getBufferMgr();
buffer_mgr.setFrameDim(frame_dim);
buffer_mgr.setNbBuffers(1);
buffer_mgr.registerFrameCallback(&cb);
cout << "simu=" << simu << endl;
simu.startAcq();
......@@ -23,5 +51,14 @@ int main(int argc, char *argv[])
simu.stopAcq();
cout << "simu=" << simu << endl;
simu.setExpTime(2);
simu.setNbFrames(3);
cout << "simu=" << simu << endl;
simu.startAcq();
cout << "simu=" << simu << endl;
simu.stopAcq();
cout << "simu=" << simu << endl;
return 0;
}
......@@ -375,6 +375,9 @@ class FrameDim
static int getImageTypeBpp(ImageType type);
static int getImageTypeDepth(ImageType type);
FrameDim& operator *=(const Bin& bin);
FrameDim& operator /=(const Bin& bin);
private:
Size m_size;
ImageType m_type;
......@@ -419,6 +422,18 @@ inline int FrameDim::getMemSize() const
return Point(m_size).getArea() * m_depth;
}
inline FrameDim& FrameDim::operator *=(const Bin& bin)
{
m_size *= bin;
return *this;
}
inline FrameDim& FrameDim::operator /=(const Bin& bin)
{
m_size /= bin;
return *this;
}
inline bool operator ==(const FrameDim& f1, const FrameDim& f2)
{
return ((f1.getSize() == f2.getSize()) &&
......@@ -430,7 +445,18 @@ inline bool operator !=(const FrameDim& f1, const FrameDim& f2)
return !(f1 == f2);
}
inline FrameDim operator *(const FrameDim& fdim, const Bin& bin)
{
FrameDim bin_fdim = fdim;
return bin_fdim *= bin;
}
inline FrameDim operator /(const FrameDim& fdim, const Bin& bin)
{
FrameDim bin_fdim = fdim;
return bin_fdim /= bin;
}
} // namespace lima
#endif // SIZEUTILS_H
......@@ -207,8 +207,10 @@ bool StdBufferCbMgr::newFrameReady(int acq_frame_nb)
return false;
int buffer_nb = acq_frame_nb % getNbBuffers();
Timestamp now = Timestamp::now();
m_ts_list[buffer_nb] = now;
FrameInfoType frame_info(acq_frame_nb, getBufferPtr(buffer_nb),
&getFrameDim(), Timestamp::now());
&getFrameDim(), now - m_start_ts);
return HwFrameCallbackGen::newFrameReady(frame_info);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment