BufferSave.cpp 4.3 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
#define EDF_HEADER_LEN		1024
#define EDF_HEADER_BUFFER_LEN	(10 * EDF_HEADER_LEN)


18
19
BufferSave::BufferSave( FileFormat format, const string& prefix, int idx,
			const string& suffix, bool overwrite, 
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
			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();
}

36
string BufferSave::getDefSuffix() const
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
{
	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
void BufferSave::closeFile()
{
	if (!isFileOpen())
		return;

	delete m_fout;
	m_fout = NULL;
	m_file_name = "";
	m_written_frames = 0;
	m_idx++;
}

69
void BufferSave::writeEdfHeader( const HwFrameInfoType& 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
	p += sprintf(p, "valid_pixels = %d ;\n", finfo.valid_pixels);
107

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

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

119
void BufferSave::writeFrame( const HwFrameInfoType& finfo )
120
121
{
	const FrameDim *fdim = finfo.frame_dim;
122
	if (!fdim)
123
124
125
126
127
128
129
130
		throw LIMA_HW_EXC(InvalidValue, "Null finfo.fdim");

	openFile();

	if ( m_format == EDF )
		writeEdfHeader(finfo);

	m_fout->write((char *)finfo.frame_ptr, fdim->getMemSize());
131

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

bool BufferSave::isFileOpen() const
138
{
139
	return bool(m_fout);
140
141
}

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

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

150
151
152
	m_prefix = prefix;
}

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

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

169
170
171
}

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

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

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

184
185
	m_idx = idx;
}
186

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

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

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