12 #include <QtCore/QString>
13 #include <QtCore/QFileInfo>
20 #include "cass_version.h"
35 void write(
const std::string &filename, std::vector<float>::const_iterator
data,
36 const std::pair<int,int>& shape)
38 const int nx = shape.first;
39 const int ny = shape.second;
43 char MARKBYTE[4] = {
static_cast<char>(0x0C),
44 static_cast<char>(0x1A),
45 static_cast<char>(0x04),
46 static_cast<char>(0xD5)};
49 std::ofstream cbf_file;
50 cbf_file.open(filename.c_str(), std::ios::out|std::ios::binary);
57 for (
int iadr=0; iadr<nx*ny; ++iadr)
59 diff = ((int) data[iadr]) - pixvalue;
60 pixvalue = (int) data[iadr];
73 std::string fbn(bn.toStdString());
76 cbf_file <<
"###CBF: Generated by CASS " << VERSION <<
"\r\n";
77 cbf_file <<
"" <<
"\r\n";
78 cbf_file << fbn <<
"\r\n";
79 cbf_file <<
"" <<
"\r\n";
80 cbf_file <<
"_array_data.header_convention \"XDS special\"" <<
"\r\n";
81 cbf_file <<
"_array_data.header_contents" <<
"\r\n";
82 cbf_file <<
";" <<
"\r\n";
83 cbf_file <<
";" <<
"\r\n";
84 cbf_file <<
"" <<
"\r\n";
85 cbf_file <<
"_array_data.data" <<
"\r\n";
86 cbf_file <<
";" <<
"\r\n";
87 cbf_file <<
"--CIF-BINARY-FORMAT-SECTION--" <<
"\r\n";
88 cbf_file <<
"Content-Type: application/octet-stream;" <<
"\r\n";
89 cbf_file <<
" conversions=\"x-CBF_BYTE_OFFSET\"" <<
"\r\n";
90 cbf_file <<
"Content-Transfer-Encoding: BINARY" <<
"\r\n";
91 cbf_file <<
"X-Binary-Size: " << nbytes <<
"\r\n";
92 cbf_file <<
"X-Binary-ID: 1" <<
"\r\n";
93 cbf_file <<
"X-Binary-Element-Type: \"signed 32-bit integer\"" <<
"\r\n";
94 cbf_file <<
"X-Binary-Element-Byte-Order: LITTLE_ENDIAN" <<
"\r\n";
95 cbf_file <<
"X-Binary-Number-of-Elements: " << nx*ny <<
"\r\n";
96 cbf_file <<
"X-Binary-Size-Fastest-Dimension: " << nx <<
"\r\n";
97 cbf_file <<
"X-Binary-Size-Second-Dimension: " << ny <<
"\r\n";
98 cbf_file <<
"X-Binary-Size-Padding: " << IOBUFSIZ-1 <<
"\r\n";
99 cbf_file <<
"" <<
"\r\n";
100 cbf_file << MARKBYTE[0] << MARKBYTE[1] << MARKBYTE[2] << MARKBYTE[3];
104 int step, first2, last2, first4, last4;
131 signed char onebyte[1];
132 signed char twobytes[2];
133 signed char fourbytes[4];
136 for (
int iadr=0; iadr<nx*ny; ++iadr)
138 diff = ((int) data[iadr]) - pixvalue;
140 pixvalue = (int)data[iadr];
145 cbf_file << onebyte[0];
152 *((
char*)(&twobytes)+0) = *((
char*)(&shortint)+0);
153 *((
char*)(&twobytes)+1) = *((
char*)(&shortint)+1);
155 for (
int ii=first2; ii!=last2+step; ii+=step)
156 cbf_file << twobytes[ii];
160 *((
char*)(&fourbytes)+0) = *((
char*)(&diff)+0);
161 *((
char*)(&fourbytes)+1) = *((
char*)(&diff)+1);
162 *((
char*)(&fourbytes)+2) = *((
char*)(&diff)+2);
163 *((
char*)(&fourbytes)+3) = *((
char*)(&diff)+3);
165 for (
int ii=first4; ii!=last4+step; ii+=step)
166 cbf_file << fourbytes[ii];
170 cbf_file <<
"" <<
"\r\n";
171 cbf_file <<
"--CIF-BINARY-FORMAT-SECTION----" <<
"\r\n";
172 cbf_file <<
";" <<
"\r\n";
173 cbf_file <<
"" <<
"\r\n";
178 for (
int ii=0; ii<IOBUFSIZ; ++ii)
179 cbf_file << zerobyte;
191 void read(
const std::string & filename, std::string &headerstring,
192 std::vector<float>& matrix, std::pair<int,int>& shape)
195 std::ifstream cbf_file(filename.c_str(), std::ios::in | std::ios::binary);
196 cbf_file.seekg(0,std::ios::end);
197 std::streampos fsize(cbf_file.tellg());
198 cbf_file.seekg(0,std::ios::beg);
203 std::vector<char> header(fsize);
204 std::vector<uint8_t> eohMarker(4,0);
206 while (!cbf_file.eof())
208 cbf_file.read(reinterpret_cast<char*>(&eohMarker[0]),1);
210 if (eohMarker[0] == 0x0C)
212 cbf_file.read(reinterpret_cast<char*>(&eohMarker[1]),1);
213 cbf_file.read(reinterpret_cast<char*>(&eohMarker[2]),1);
214 cbf_file.read(reinterpret_cast<char*>(&eohMarker[3]),1);
215 if (eohMarker[1] == 0x1A && eohMarker[2] == 0x04 && eohMarker[3] == 0xD5)
222 header[hidx++] = eohMarker[0];
225 throw std::invalid_argument(filename +
" is not a cbf file");
229 headerstring.assign(header.begin(),header.begin()+hidx);
230 std::istringstream headerstream(headerstring);
231 while(!headerstream.eof())
234 std::getline(headerstream,line);
236 if (line.find(
"X-Binary-Size-Fastest-Dimension:") != std::string::npos)
238 std::istringstream iss(line);
239 std::vector<std::string> tokens;
241 while(std::getline(iss,token,
' '))
242 tokens.push_back(token);
243 shape.first = atoi(tokens.back().c_str());
246 if (line.find(
"X-Binary-Size-Second-Dimension:") != std::string::npos)
248 std::istringstream iss(line);
249 std::vector<std::string> tokens;
251 while(std::getline(iss,token,
' '))
252 tokens.push_back(token);
253 shape.second = atoi(tokens.back().c_str());
258 matrix.resize(shape.first*shape.second);
261 int16_t twobytes = 0;
262 int32_t fourbytes = 0;
264 while (!cbf_file.eof() && cInd < shape.first * shape.second)
266 cbf_file.read(reinterpret_cast<char*>(&onebyte),1);
269 cbf_file.read(reinterpret_cast<char *>(&twobytes),2);
270 if (twobytes == -32768)
272 cbf_file.read(reinterpret_cast<char *>(&fourbytes),4);
fromStdString(const std::string &str)
void read(const std::string &filename, std::string &headerstring, std::vector< float > &matrix, std::pair< int, int > &shape)
read the cbf data into a linearized 2d array
auxiliary data[Processor]
void write(const std::string &filename, std::vector< float >::const_iterator data, const std::pair< int, int > &shape)
write cbf file