Commit 6aa4df04 authored by kirov's avatar kirov
Browse files

Added FrameBuilder for the simulator device.



git-svn-id: https://scm.blissgarden.org/svn/lima/trunk@21 45c4679d-1946-429d-baad-37912b19538b
parent 2ccaaa08
MAKE_CMD = $(MAKE)
all:
$(MAKE_CMD) -C src all
test: all
$(MAKE_CMD) -C test all
clean:
$(MAKE_CMD) -C src clean
$(MAKE_CMD) -C test clean
#ifndef FRAMEBUILDER_H
#define FRAMEBUILDER_H
#include <vector>
namespace lima {
struct GaussPeak {
int x0, y0;
unsigned fwhm;
double max;
};
class FrameBuilder {
public:
int bin_X, bin_Y;
int width, height, depth;
std::vector<struct GaussPeak> peaks;
double inc;
FrameBuilder( int bin_X, int bin_Y, int width, int height, int depth,
std::vector<struct GaussPeak> &peaks );
~FrameBuilder();
void getNextFrame( unsigned char *ptr );
void resetFrameNr( int frame_nr=0 );
unsigned long getCurrFrameNr();
private:
unsigned long _frame_nr;
int writeFrameData( unsigned char *ptr );
double dataXY( int x, int y );
};
}
#endif /* FRAMEBUILDER_H */
#include <ctime>
#include <cmath>
#include <vector>
#include <sys/time.h>
#include <unistd.h>
#include "FrameBuilder.h"
using namespace lima;
using namespace std;
FrameBuilder::FrameBuilder( int bin_X, int bin_Y,
int width, int height, int depth,
std::vector<struct GaussPeak> &peaks ):
bin_X(bin_X), bin_Y(bin_Y),
width(width), height(height), depth(depth),
peaks(peaks)
{
this->_frame_nr = 0;
}
FrameBuilder::~FrameBuilder()
{
}
void FrameBuilder::resetFrameNr(int frame_nr)
{
this->_frame_nr = frame_nr;
}
unsigned long FrameBuilder::getCurrFrameNr()
{
return this->_frame_nr;
}
#define SGM_FWHM 0.42466090014400952136075141705144 // 1/(2*sqrt(2*ln(2)))
double gauss2D( double x, double y, double x0, double y0, double fwhm, double max )
{
double sigma = SGM_FWHM * fwhm;
return max * exp(-((x-x0)*(x-x0) + (y-y0)*(y-y0))/(2*sigma*sigma));
}
double FrameBuilder::dataXY( int x, int y )
{
double value=0.0;
vector<GaussPeak>::iterator p;
for( p = peaks.begin( ); p != peaks.end( ); ++p) {
value += gauss2D(x, y, p->x0, p->y0, p->fwhm, p->max);
}
return value;
}
int FrameBuilder::writeFrameData(unsigned char *ptr)
{
int x, y;
double data;
unsigned char *p1;
unsigned short *p2;
unsigned long *p4;
switch( depth ) {
case 1 :
p1 = (unsigned char *) ptr;
break;
case 2 :
p2 = (unsigned short *) ptr;
break;
case 4 :
p4 = (unsigned long *) ptr;
break;
default:
return -1;
}
for( y=0; y<height; y++ ) {
for( x=0; x<width; x++ ) {
data = dataXY(x, y);
switch( depth ) {
case 1 :
*p1 = (unsigned char) (255.0 * data);
p1++;
break;
case 2 :
*p2 = (unsigned short) (65535.0 * data);
p2++;
break;
case 4 :
*p4 = (unsigned long) (4294967295.0 * data);
p4++;
break;
}
}
}
return 0;
}
void FrameBuilder::getNextFrame( unsigned char *ptr )
{
int ret = writeFrameData( ptr );
if( ret < 0 ) {
// throw an exception
} else {
_frame_nr++;
}
}
objs = FrameBuilder.o
CXX = g++
CXXFLAGS += -I../include -I../../../common/include -Wall
all: clean $(objs)
clean:
rm -f $(objs)
#include <ctime>
#include <fstream>
#include <unistd.h>
#include <sys/time.h>
#include "BufferSave.h"
using namespace lima;
using namespace std;
int BufferSave::writeEdfHeader( const FrameInfoType &finfo,
std::ofstream &file )
{
time_t ctime_now;
struct timeval tod_now;
char time_str[64], buffer[EDF_HEADER_BUFFER_LEN], *p;
int l, len, rem;
time(&ctime_now);
gettimeofday(&tod_now, NULL);
ctime_r(&ctime_now, time_str);
time_str[strlen(time_str) - 1] = '\0';
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, "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);
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);
l = p - buffer;
len = l;
rem = len % EDF_HEADER_LEN;
if (rem > 0)
len += EDF_HEADER_LEN - rem;
p += sprintf(p, "%*s}\n", len - (l + 2), "");
len = p - buffer;
file.write(buffer, len);
return len;
}
BufferSave::BufferSave( const string &prefix, enum FileFormat format ) :
prefix(prefix), format(format)
{
}
BufferSave::~BufferSave( )
{
}
int BufferSave::writeFrame( FrameInfoType &finfo )
{
string name=this->prefix;
if( name.find(".edf") == string::npos )
name += ".edf";
ofstream file(name.c_str(), ios_base::out | ios_base::binary);
if( this->format == FMT_EDF )
writeEdfHeader(finfo, file);
file.write((char *)finfo.frame_ptr, finfo.width*finfo.height*finfo.depth);
file.close();
return 0;
}
#ifndef BUFFERSAVE_H
#define BUFFERSAVE_H
#include <string>
#include <fstream>
#include "FrameBuilder.h"
namespace lima {
#define EDF_HEADER_LEN 1024
#define EDF_HEADER_BUFFER_LEN (10 * EDF_HEADER_LEN)
typedef struct FrameInfo {
unsigned long acq_frame_nb;
void *frame_ptr;
int width, height, depth;
double frame_time_stamp;
} FrameInfoType;
enum FileFormat {
FMT_RAW,
FMT_EDF,
};
class BufferSave {
public :
std::string prefix;
enum FileFormat format;
BufferSave( const std::string &prefix, enum FileFormat format );
~BufferSave( );
int writeFrame( FrameInfoType &finfo );
private:
int writeEdfHeader( const FrameInfoType &finfo, std::ofstream &file );
};
}
#endif /* BUFFERSAVE_H */
objs = BufferSave.o
CXX = g++
CXXFLAGS += -I../include -I../../../common/include -Wall
all: clean $(objs)
$(CXX) $(CXXFLAGS) test.cpp -o test $(objs) ../src/FrameBuilder.o
clean:
rm -f $(objs) test *.edf
#include <vector>
#include "FrameBuilder.h"
#include "BufferSave.h"
using namespace std;
using namespace lima;
int main( void )
{
GaussPeak p[2]={{333, 512, 50, 1}, {666, 512, 100, 1}};
vector<GaussPeak> pv( p, p + sizeof(p)/sizeof(*p) );
FrameBuilder fb(1, 1, 1024, 1024, 2, pv);
BufferSave bs("boza", FMT_EDF);
FrameInfoType finfo;
unsigned char *_buffer;
_buffer = NULL;
try {
int size = fb.width*fb.height*fb.depth;
_buffer = new unsigned char[size];
} catch( bad_alloc& ) {
/* ??? */
}
for( int i=0; i<5; i++ ) {
fb.getNextFrame( _buffer );
finfo.acq_frame_nb = fb.getCurrFrameNr();
finfo.frame_ptr = _buffer;
finfo.width = fb.width;
finfo.height = fb.height;
finfo.depth = fb.depth;
finfo.frame_time_stamp = 0; /* XXX */
bs.writeFrame(finfo);
}
if( _buffer )
delete[] _buffer;
return 0;
}
Markdown is supported
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