BufferSave.cpp 4.26 KB
Newer Older
1
2
#include "BufferSave.h"

3
#include <ctime>
kirov's avatar
kirov committed
4
#include <cstdio>
kirov's avatar
kirov committed
5
#include <cstring>
6
#include <fstream>
7
#include <sstream>
8
9
10
11
12
13
#include <unistd.h>
#include <sys/time.h>

using namespace lima;
using namespace std;

14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#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( ) 
{
	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);

}
56

57
58
59
60
61
62
63
64
65
66
67
68
69
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 )
70
71
72
{
	time_t ctime_now;
	time(&ctime_now);
73
74

	struct timeval tod_now;
75
	gettimeofday(&tod_now, NULL);
76
77

	char time_str[64];
78
79
80
	ctime_r(&ctime_now, time_str);
	time_str[strlen(time_str) - 1] = '\0';

81
82
83
84
85
86
87
	const FrameDim *fdim = finfo.frame_dim;
	const Size& frame_size = fdim->getSize();
	int depth = fdim->getDepth();
	int image_nb = m_written_frames + 1;

	char buffer[EDF_HEADER_BUFFER_LEN];
	char *p = buffer;
88
	p += sprintf(p, "{\n");
89
90
	p += sprintf(p, "HeaderID = EH:%06u:000000:000000 ;\n", image_nb);
	p += sprintf(p, "Image = %u ;\n", image_nb);
91
92
	p += sprintf(p, "ByteOrder = LowByteFirst ;\n");
	p += sprintf(p, "DataType = %s ;\n", 
93
94
95
96
97
		      (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());
98

99
	p += sprintf(p, "acq_frame_nb = %d ;\n", finfo.acq_frame_nb);
100
101
102
	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);
103
104
105
        if (finfo.frame_timestamp.isSet())
                p += sprintf(p, "time_of_frame = %.6f ;\n",
                             double(finfo.frame_timestamp));
106

107
108
109
	int l = p - buffer;
	int len = l;
	int rem = len % EDF_HEADER_LEN;
110
111
112
113
114
	if (rem > 0)
		len += EDF_HEADER_LEN - rem;
	p += sprintf(p, "%*s}\n", len - (l + 2), "");
	len = p - buffer;

115
	m_fout->write(buffer, len);
116
117
}

118
119
120
121
122
123
124
125
126
127
128
129
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());
130

131
132
133
134
135
136
	m_written_frames++;
	if (m_written_frames == m_tot_file_frames)
		closeFile();
}

bool BufferSave::isFileOpen() const
137
{
138
	return (m_fout != NULL);
139
140
}

141
142
143
144
145
146
147
void BufferSave::setPrefix(const String& prefix)
{
	if (prefix == m_prefix)
		return;

	if (isFileOpen())
		throw LIMA_HW_EXC(InvalidValue, "Set prefix with open file");
148

149
150
151
152
	m_prefix = prefix;
}

void BufferSave::getPrefix(String& prefix) const
153
{
154
	prefix = m_prefix;
155
156
}

157
158
159
160
161
162
163
164
165
166
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();
167

168
169
170
}

void BufferSave::getFormat(FileFormat& format) const
171
{
172
173
	format = m_format;
}
174

175
176
177
178
void BufferSave::setIndex(int idx)
{
	if (idx == m_idx)
		return;
179

180
181
	if (isFileOpen())
		throw LIMA_HW_EXC(InvalidValue, "Set idx with open file");
182

183
184
	m_idx = idx;
}
185

186
187
188
189
void BufferSave::getIndex(int& idx) const
{
	idx = m_idx;
}
190

191
192
193
194
195
196
197
198
199
200
201
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;
}
202

203
204
205
void BufferSave::getTotFileFrames(int& tot_file_frames) const
{
	tot_file_frames = m_tot_file_frames;
206
}
207