libpniio
cbf_reader.hpp
Go to the documentation of this file.
1 //
2 // (c) Copyright 2011 DESY, Eugen Wintersberger <eugen.wintersberger@desy.de>
3 //
4 // This file is part of libpniio.
5 //
6 // libpniio is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // libpniio is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with libpniio. If not, see <http://www.gnu.org/licenses/>.
18 // ===========================================================================
19 //
20 // Created on: Apr 19, 2011
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 #pragma once
25 
26 #include<iostream>
27 #include<fstream>
28 #include<sstream>
29 #include<cstdio>
30 #include<cstdlib>
31 #include<vector>
32 
33 #include<boost/regex.hpp>
34 
35 #include <pni/core/types.hpp>
36 
37 #include "../image_reader.hpp"
38 #include "dectris_reader.hpp"
39 #include "types.hpp"
40 
41 namespace pni{
42 namespace io{
43 
44 
67  class cbf_reader: public image_reader
68  {
69  private:
73  std::vector<image_info> _image_info;
75  std::streampos _data_offset;
78 
79  //-----------------------------------------------------------------
85  void _parse_file();
86 
87  public:
88  //=================constructors and destructor==================
90  cbf_reader();
91 
92  //-----------------------------------------------------------------
103  cbf_reader(const pni::core::string &fname);
104 
105  //-----------------------------------------------------------------
107  virtual ~cbf_reader();
108 
109  //-----------------------------------------------------------------
111  cbf_reader(const cbf_reader &r) = delete;
112 
113  //====================assignment operators=======================
116 
118  cbf_reader &operator=(const cbf_reader &r) = delete;
119 
120  //----------------------------------------------------------------
121  virtual void close()
122  {
123  //close the stream
125  //reset data offset
126  _data_offset = 0;
127  //clear the _image_info vector
128  _image_info.clear();
129  }
130 
131  //-----------------------------------------------------------------
132  virtual void open()
133  {
134  close();
136  _parse_file();
137  }
138 
139  //-----------------------------------------------------------------
140  virtual size_t nimages() const
141  {
142  return _image_info.size();
143  }
144 
145  //-----------------------------------------------------------------
146  virtual image_info info(size_t i) const
147  {
148  return _image_info[i];
149  }
150 
151  //-----------------------------------------------------------------
162  template<typename CTYPE> CTYPE image(size_t i,size_t c=0);
163 
164  //-----------------------------------------------------------------
178  template<typename CTYPE>
179  void image(CTYPE &array,size_t i,size_t c=0);
180 
181  };
182 
183  //-------------------------------------------------------------------------
184  template<typename CTYPE> CTYPE cbf_reader::image(size_t i,size_t c)
185  {
186  using namespace pni::core;
188  CTYPE data;
189  try
190  {
191  data=CTYPE(info.npixels());
192  }
193  catch(...)
194  {
195  throw memory_allocation_error(EXCEPTION_RECORD,
196  "Allocation of container for image data failed!");
197  }
198 
199  try
200  {
201  image(data,i,c);
202  }
203  catch(file_error &error)
204  {
205  //propagate exception
206  error.append(EXCEPTION_RECORD);
207  throw error;
208  }
209 
210  return data;
211  }
212 
213  //-------------------------------------------------------------------------
214  template<typename CTYPE>
215  void cbf_reader::image(CTYPE &data,size_t i,size_t c)
216  {
217  using namespace pni::core;
218  //load image information and throw exception if image and container size
219  //to not match
220  image_info inf = _image_info[i];
221  if(data.size()!= inf.npixels())
222  {
223  std::stringstream ss;
224  ss<<"Container size ("<<data.size()<<") does not match image ";
225  ss<<"size ("<<inf.npixels()<<")!";
226  throw size_mismatch_error(EXCEPTION_RECORD,ss.str());
227  }
228 
229  //load the channel information
230  image_channel_info channel = inf.get_channel(c);
231 
233  {
234  if(channel.type_id() == type_id_t::INT16)
235  //read 16Bit signed data
236  cbf::dectris_reader::read_data_byte_offset<int16>(
237  _get_stream(),inf,data);
238  if(channel.type_id() == type_id_t::INT32)
239  //read 32Bit signed data
240  cbf::dectris_reader::read_data_byte_offset<int32>(
241  _get_stream(),inf,data);
242  else
243  {
244  file_error error(EXCEPTION_RECORD,
245  "No data reader for this data type!");
246  throw error;
247  }
248 
249  }
250  else
251  throw file_error(EXCEPTION_RECORD,"Unknown detector vendor!");
252 
253  }
254 //end of namespace
255 }
256 }
257 
compression_id
CBF compression id.
Definition: types.hpp:33
image information type
Definition: image_info.hpp:43
CTYPE image(size_t i, size_t c=0)
read image
Definition: cbf_reader.hpp:184
pni::core::type_id_t type_id() const
get type ID
Definition: image_channel_info.hpp:71
base class for image readers
Definition: image_reader.hpp:53
virtual void close()
close the file
Definition: cbf_reader.hpp:121
image channel information class
Definition: image_channel_info.hpp:39
std::streampos _data_offset
store data offset
Definition: cbf_reader.hpp:75
virtual void close()
close the file
Definition: data_reader.cpp:114
std::ifstream & _get_stream() const
get stream
Definition: data_reader.hpp:84
base clase for CBF readers
Definition: cbf_reader.hpp:67
virtual void open()
open file
Definition: cbf_reader.hpp:132
vendor_id
CBF vendor id.
Definition: types.hpp:39
Definition: cbf_reader.hpp:41
cbf_reader & operator=(cbf_reader &&r)
move assignment
Definition: cbf_reader.cpp:60
virtual void open()
open file
Definition: data_reader.cpp:121
cbf::vendor_id _detector_vendor
string holding the detector vendor ID
Definition: cbf_reader.hpp:71
virtual size_t nimages() const
get number of images
Definition: cbf_reader.hpp:140
virtual image_info info(size_t i) const
get image info
Definition: cbf_reader.hpp:146
size_t npixels() const
get total number of pixels
Definition: image_info.hpp:109
cbf::compression_id _compression_type
compression type
Definition: cbf_reader.hpp:77
virtual ~cbf_reader()
destructor
Definition: cbf_reader.cpp:56
image_channel_info get_channel(size_t i) const
get channel information
Definition: image_info.cpp:127
cbf_reader()
default constructor
Definition: cbf_reader.cpp:40
std::vector< image_info > _image_info
info structure for data
Definition: cbf_reader.hpp:73
void _parse_file()
parse the file
Definition: cbf_reader.cpp:69