libpniio
dectris_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 23, 2012
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 
25 #pragma once
26 
27 #include<iostream>
28 #include<fstream>
29 #include<vector>
30 
31 #include "../image_info.hpp"
32 #include "types.hpp"
33 
34 
35 namespace pni{
36 namespace io{
37 namespace cbf{
38 
45  //1
47  {
48  public:
62  static std::streampos read_header(std::ifstream &is,
63  std::vector<pni::io::image_info> &info,compression_id &ct);
64 
65  //-----------------------------------------------------------------
78  template<
79  typename CBFT,
80  typename CTYPE
81  >
82  static void read_data_byte_offset(std::ifstream &is,
83  const pni::io::image_info &info,
84  CTYPE &data);
85 
86 
87  };
88 
89  //-------------------------------------------------------------------------
90  template<
91  typename CBFT,
92  typename CTYPE
93  >
94  void dectris_reader::read_data_byte_offset(std::ifstream &is,
95  const pni::io::image_info &, CTYPE &data)
96  {
97  using namespace pni::core;
98  //unsigned long i;
99  //size_t ecnt = 0; // element counter
100  CBFT buffer = 0; // single element buffer
101 
102  //initializing the container with 0
103  std::fill(data.begin(),data.end(),0);
104  typename CTYPE::iterator iter = data.begin();
105  typename CTYPE::value_type v_old = 0;
106 
107  for(typename CTYPE::value_type &v: data)
108  {
109  v = v_old; //set the new value to the previous
110 
111  buffer = 0; //reset the read buffer
112  //read one byte from the stream an throw an exception in case of an
113  //error
114  try { is.read((char *)(&buffer),1); }
115  catch(...)
116  {
117  throw file_error(EXCEPTION_RECORD,
118  "Error reading 1Byte from the CBF stream!");
119  }
120 
121  if (((unsigned char) buffer) != 0x80)
122  {
123  v += (char) buffer;
124  v_old = v;
125  continue;
126  }
127 
128  //read two byte from the stream and throw an exception in case of an
129  //error
130  try { is.read((char *) (&buffer), 2); }
131  catch(...)
132  {
133  throw file_error(EXCEPTION_RECORD,
134  "Error reading 2Byte from the CBF stream!");
135  }
136 
137  if (((unsigned short) buffer) != 0x8000)
138  {
139  v += (short) buffer;
140  v_old = v;
141  continue;
142  }
143 
144  //read 4 byte from the stream and throw an exception in the case of
145  //an error
146  try { is.read((char*) (&buffer), 4); }
147  catch(...)
148  {
149  throw file_error(EXCEPTION_RECORD,
150  "Error reading 4byte from the CBF stream!");
151  }
152 
153  if (((unsigned int) buffer) != 0x800000)
154  {
155  v += (int) buffer;
156  v_old = v;
157  continue;
158  }
159 
160  }
161 
162  }
163 
164 //end of namespace
165 }
166 }
167 }
compression_id
CBF compression id.
Definition: types.hpp:33
image information type
Definition: image_info.hpp:43
reader for DECTRIS detector data
Definition: dectris_reader.hpp:46
Definition: cbf_reader.hpp:41
static void read_data_byte_offset(std::ifstream &is, const pni::io::image_info &info, CTYPE &data)
read data
Definition: dectris_reader.hpp:94
static std::streampos read_header(std::ifstream &is, std::vector< pni::io::image_info > &info, compression_id &ct)
read header information
Definition: dectris_reader.cpp:36