libpniio
tiff_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: Jun 15, 2011
21 // Author: Eugen Wintersberger <eugen.wintersberger@desy.de>
22 //
23 //
24 #pragma once
25 
26 #include<iostream>
27 #include<vector>
28 #include<boost/current_function.hpp>
29 
30 #include "../image_reader.hpp"
31 #include "../image_info.hpp"
32 #include "ifd.hpp"
33 #include "ifd_entry.hpp"
34 #include "strip_reader.hpp"
35 
36 
37 namespace pni{
38 namespace io{
39 
49  class tiff_reader:public image_reader
50  {
51  private:
53  std::vector<tiff::ifd> _ifds;
54 
55  //=====================private methods=============================
70  static bool _is_little_endian(std::ifstream &stream);
71 
72  //-----------------------------------------------------------------
85  static void _check_if_tiff(std::ifstream &stream);
86 
87  //------------------------------------------------------------------
97  static int32 _read_ifd_offset(std::ifstream &stream);
98 
99  //-----------------------------------------------------------------
109  static size_t _read_ifd_size(std::ifstream &stream);
110 
111  //-----------------------------------------------------------------
123  static std::vector<size_t>
124  _get_bits_per_sample(std::ifstream &stream,const tiff::ifd &ifd);
125 
126  //-----------------------------------------------------------------
139  static std::vector<size_t>
140  _get_sample_format(std::ifstream &stream,const tiff::ifd &ifd);
141 
142  //-----------------------------------------------------------------
156  static type_id_t _get_type_id(size_t bps,size_t sf);
157 
158  //-----------------------------------------------------------------
165  void _read_ifds();
166 
167  //----------------------------------------------------------------
179  template<typename CTYPE>
180  void _read_data(size_t i,size_t c,CTYPE &data);
181  public:
182  //==============constructors and destructor========================
184  tiff_reader();
185 
186  //-----------------------------------------------------------------
193 
194  //-----------------------------------------------------------------
196  explicit tiff_reader(const pni::core::string &fname);
197 
198  //-----------------------------------------------------------------
200  tiff_reader(const tiff_reader &) = delete;
201 
202  //-----------------------------------------------------------------
204  ~tiff_reader();
205 
206  //======================assignment operators=======================
209 
210  //--------------------------------------------------------------
212  tiff_reader &operator=(const tiff_reader &r) = delete;
213 
214  //=====================class methods===========================
221  virtual size_t nimages() const;
222 
223  //-----------------------------------------------------------------
231  virtual image_info info(size_t i) const;
232 
233  //-----------------------------------------------------------------
235  virtual void open();
236 
237  //-----------------------------------------------------------------
239  virtual void close();
240 
241  //-----------------------------------------------------------------
253  template<typename CTYPE> CTYPE image(size_t i,size_t c=0)
254  {
255  using namespace pni::core;
256  image_info info = this->info(i);
257  CTYPE data;
258  try { data = CTYPE(info.npixels()); }
259  catch(...)
260  {
261  throw memory_allocation_error(EXCEPTION_RECORD,
262  "Allocation of image data container failed!");
263  }
264 
265  //here we read the data
266  this->_read_data(i,c,data);
267  return data;
268  }
269 
270  //-----------------------------------------------------------------
288  template<typename CTYPE>
289  void image(CTYPE &data,size_t i,size_t c=0)
290  {
291  using namespace pni::core;
292  image_info info = this->info(i);
293  if(data.size() != info.npixels())
294  {
295  std::stringstream ss;
296  ss<<"Container size ("<<data.size()<<") does not match";
297  ss<<"number of pixels ("<<info.npixels()<<")!";
298  throw size_mismatch_error(EXCEPTION_RECORD,ss.str());
299  }
300 
301  //read data
302  _read_data(i,c,data);
303  }
304 
305  //-----------------------------------------------------------------
307  friend std::ostream &operator<<(std::ostream &o,
308  const tiff_reader &r);
309 
310  };
311 
312  template<typename CTYPE>
313  void tiff_reader::_read_data(size_t i,size_t c,CTYPE &data)
314  {
315  //obtain the proper IFD
316  tiff::ifd &ifd = this->_ifds.at(i);
317  std::ifstream &stream = this->_get_stream();
318 
319  //assume here that the image is stored using strips
320  tiff::strip_reader reader(tiff::strip_reader::create(stream,ifd,this->info(i)));
321  //std::cout<<reader<<std::endl;
322 
323  reader.read(c,stream,data);
324 
325  }
326 //end of namespace
327 }
328 }
tiff_reader & operator=(tiff_reader &&r)
move assignment operator
Definition: tiff_reader.cpp:259
image information type
Definition: image_info.hpp:43
base class for image readers
Definition: image_reader.hpp:53
virtual size_t nimages() const
get number of images
Definition: tiff_reader.cpp:270
~tiff_reader()
destructor
Definition: tiff_reader.cpp:255
static std::vector< size_t > _get_bits_per_sample(std::ifstream &stream, const tiff::ifd &ifd)
get bits per sample
Definition: tiff_reader.cpp:147
CTYPE image(size_t i, size_t c=0)
read image data
Definition: tiff_reader.hpp:253
friend std::ostream & operator<<(std::ostream &o, const tiff_reader &r)
output operator of an TIFFReader object
Definition: tiff_reader.cpp:332
static strip_reader create(std::ifstream &stream, const ifd &image_dir, const image_info &info)
create StripReader instance
Definition: strip_reader.cpp:97
tiff_reader()
default constructor
Definition: tiff_reader.cpp:232
virtual image_info info(size_t i) const
get ImageInfo
Definition: tiff_reader.cpp:276
static size_t _read_ifd_size(std::ifstream &stream)
read IFD size
Definition: tiff_reader.cpp:90
static type_id_t _get_type_id(size_t bps, size_t sf)
determine channel data type
Definition: tiff_reader.cpp:180
std::ifstream & _get_stream() const
get stream
Definition: data_reader.hpp:84
void read(size_t c, std::ifstream &stream, CTYPE &data)
template to read image data of various type
Definition: strip_reader.hpp:147
static void _check_if_tiff(std::ifstream &stream)
check if image is TIFF
Definition: tiff_reader.cpp:60
Definition: cbf_reader.hpp:41
void _read_ifds()
read IDF data from the file
Definition: tiff_reader.cpp:99
TIFF file reader.
Definition: tiff_reader.hpp:49
size_t npixels() const
get total number of pixels
Definition: image_info.hpp:109
virtual void open()
open the file
Definition: tiff_reader.cpp:317
IFD - Image File Directory class.
Definition: ifd.hpp:57
virtual void close()
close the file
Definition: tiff_reader.cpp:325
static int32 _read_ifd_offset(std::ifstream &stream)
read IFD offset
Definition: tiff_reader.cpp:81
reader for strip data in a TIFF file
Definition: strip_reader.hpp:43
static std::vector< size_t > _get_sample_format(std::ifstream &stream, const tiff::ifd &ifd)
get sample format
Definition: tiff_reader.cpp:165
void image(CTYPE &data, size_t i, size_t c=0)
read image data
Definition: tiff_reader.hpp:289
std::vector< tiff::ifd > _ifds
IFD list.
Definition: tiff_reader.hpp:53
bool _little_endian
true if data is stored as little endian
Definition: tiff_reader.hpp:52
static bool _is_little_endian(std::ifstream &stream)
check binary encoding
Definition: tiff_reader.cpp:40
void _read_data(size_t i, size_t c, CTYPE &data)
read data from the file
Definition: tiff_reader.hpp:313