fw4spl
GzBufferImageReader.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2017.
3  * Distributed under the terms of the GNU Lesser General Public License (LGPL) as
4  * published by the Free Software Foundation.
5  * ****** END LICENSE BLOCK ****** */
6 
7 #include "fwDataIO/reader/GzBufferImageReader.hpp"
8 
9 #include "fwDataIO/reader/registry/macros.hpp"
10 
11 #include <fwData/Image.hpp>
12 #include <fwData/location/SingleFile.hpp>
13 
14 #include <fwDataTools/helper/Image.hpp>
15 
16 #include <zlib.h>
17 
18 #include <iostream>
19 
20 fwDataIOReaderRegisterMacro( ::fwDataIO::reader::GzBufferImageReader );
21 
22 namespace fwDataIO
23 {
24 
25 namespace reader
26 {
27 
28 //------------------------------------------------------------------------------
29 
31  ::fwData::location::
32  enableSingleFile< IObjectReader >(
33  this)
34 {
35 }
36 
37 //------------------------------------------------------------------------------
38 
40 {
41 }
42 
43 //------------------------------------------------------------------------------
44 
46 {
47  assert( ::fwData::location::SingleFile::dynamicCast(m_location) );
48  ::boost::filesystem::path file = ::fwData::location::SingleFile::dynamicCast(m_location)->getPath();
49 
50  assert( file.empty() == false );
51 
52  ::fwData::Image::sptr image = getConcreteObject();
53  size_t imageSizeInBytes = image->getSizeInBytes();
54 
55  image->allocate();
56  ::fwDataTools::helper::Image helper(image);
57  char* ptr = static_cast<char*>(helper.getBuffer());
58 
59  gzFile rawFile = gzopen(file.string().c_str(), "rb");
60 
61  SLM_ASSERT("rawFile not instanced", rawFile);
62  if ( rawFile == 0 )
63  {
64  std::string str = "Unable to open ";
65  str += file.string();
66  throw std::ios_base::failure(str);
67  }
68 
69  int uncompressedbytesreaded;
70  size_t readBytes = 0;
71 
72  while ( (uncompressedbytesreaded = gzread(rawFile, ptr + readBytes, imageSizeInBytes - readBytes)) > 0 )
73  {
74  readBytes += uncompressedbytesreaded;
75  }
76 
77  gzclose(rawFile);
78 
79  if ( uncompressedbytesreaded == -1 )
80  {
81  std::string str = "Unable to read ";
82  str += file.string();
83  throw std::ios_base::failure(str);
84  }
85 }
86 
87 //------------------------------------------------------------------------------
88 
90 {
91  return (".raw.gz");
92 }
93 
94 //------------------------------------------------------------------------------
95 
96 } // namespace reader
97 
98 } // namespace fwDataIO
virtual FWDATAIO_API ~GzBufferImageReader()
Destructor. Do nothing.
virtual FWDATAIO_API void read() override
Read the file with zlib API.
This namespace fwDataIO contains reader and writer for several framework&#39;s data.
FWDATAIO_API GzBufferImageReader(::fwDataIO::reader::IObjectReader::Key key)
Constructor. Do nothing.
Key class used to restrict access to Object construction. See http://www.drdobbs.com/184402053.
Image Reader. Read file format .raw.gz.
Defines an helper to modify an fwData::Image by adding few medical fields and create in parallel the ...
#define SLM_ASSERT(message, cond)
work like &#39;assert&#39; from &#39;cassert&#39;, with in addition a message logged by spylog (with FATAL loglevel) ...
Definition: spyLog.hpp:308
virtual std::shared_ptr< DataType > getConcreteObject()
m_object getter.
Contains the representation of the data objects used in the framework.
FWDATATOOLS_API void * getBuffer()
Returns image buffer.
::fwData::location::ILocation::sptr m_location
Object location ( file path, directory path, url, etc )
FWDATAIO_API std::string extension() override
Defines extension supported by this reader ".raw.gz".