fw4spl
ImageDicomStream.cpp
1 /* ***** BEGIN LICENSE BLOCK *****
2  * FW4SPL - Copyright (C) IRCAD, 2009-2015.
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 "vtkGdcmIO/helper/ImageDicomStream.hpp"
8 
9 #include <boost/filesystem/operations.hpp>
10 #include <boost/filesystem/path.hpp>
11 
12 #include <vtkStringArray.h>
13 
14 namespace vtkGdcmIO
15 {
16 namespace helper
17 {
18 
19 //------------------------------------------------------------------------------
20 
21 ImageDicomSource::ImageDicomSource( ImageDicomInfo::sptr dcmInfo ) :
22  m_dcmInfo ( dcmInfo ),
23  m_pos ( 0 ),
24  m_success ( false )
25 {
26  SLM_ASSERT( "ImageDicomSource needs dicom file to read a dciom image", dcmInfo->m_seriesFiles.size() > 0);
27 }
28 
29 //------------------------------------------------------------------------------
30 
31 bool filesStillExist( const ImageDicomInfo::SeriesFilesType & files )
32 {
33  ::boost::filesystem::path filePath;
34  bool allFilesExists = true;
35 
36  for( ImageDicomInfo::SeriesFilesType::const_iterator itFile = files.begin();
37  allFilesExists && ( itFile != files.end() );
38  ++itFile )
39  {
40  filePath = *itFile;
41  allFilesExists &= ::boost::filesystem::exists(filePath);
42  }
43  return allFilesExists;
44 }
45 
46 //------------------------------------------------------------------------------
47 
48 bool ImageDicomSource::readImage()
49 {
50  vtkSmartPointer< vtkStringArray > fileArray = vtkSmartPointer< vtkStringArray >::New();
51  fileArray->Initialize();
52  for( std::string file : m_dcmInfo->m_seriesFiles )
53  {
54  OSLM_TRACE("Add " << file << " to vtkGdcmReader");
55  fileArray->InsertNextValue( file.c_str() );
56  }
57 
58  m_reader = vtkSmartPointer< vtkGDCMImageReader >::New();
59  m_reader->FileLowerLeftOn();
60 
61  bool res = false;
62  if ( filesStillExist( m_dcmInfo->m_seriesFiles ) )
63  {
64  m_reader->SetFileNames( fileArray );
65  try
66  {
67  m_reader->Update();
68  m_inputReader = static_cast<char*>( m_reader->GetOutput()->GetScalarPointer() );
69  res = true;
70  }
71  catch (std::exception &e)
72  {
73  m_reader = 0;
74  m_inputReader = 0;
75  OSLM_ERROR ( "Error during conversion : " << e.what() );
76  }
77  catch (...)
78  {
79  m_reader = 0;
80  m_inputReader = 0;
81  OSLM_ERROR ( "Unexpected error during conversion" );
82  }
83  }
84 
85  return res;
86 }
87 
88 
89 //------------------------------------------------------------------------------
90 
91 std::streamsize ImageDicomSource::read(char* s, std::streamsize n)
92 {
93  if ( m_pos == 0 )
94  {
95  m_success = this->readImage();
96  OSLM_ERROR_IF( "Error while reading dicom files : " << m_dcmInfo->m_seriesFiles[0] << " ...", !m_success );
97  }
98 
99  using namespace std;
100  streamsize amt = static_cast<streamsize>( m_dcmInfo->m_buffSizeInBytes - m_pos );
101  streamsize result = std::min(n, amt);
102 
103  if (result != 0)
104  {
105  if( m_success )
106  {
107  std::copy( m_inputReader + m_pos, m_inputReader + m_pos + result, s );
108  }
109  m_pos += result;
110  return result;
111  }
112  else
113  {
114  if ( m_reader )
115  {
116  m_reader = 0;
117  }
118  return -1; // EOF
119  }
120 }
121 
122 
123 //------------------------------------------------------------------------------
124 
125 ImageDicomStream::ImageDicomStream( ImageDicomInfo::sptr dcmInfo ) : m_dcmInfo ( dcmInfo )
126 {
127 }
128 
129 //------------------------------------------------------------------------------
130 
131 SPTR(std::istream) ImageDicomStream::get()
132 {
133  SPTR(::boost::iostreams::stream<ImageDicomSource>) is
134  = std::make_shared< ::boost::iostreams::stream<ImageDicomSource> >( m_dcmInfo );
135 
136  return is;
137 }
138 
139 //------------------------------------------------------------------------------
140 
141 } // namespace helper
142 } // namespace vtkGdcmIO
#define SPTR(_cls_)
std::streamsize read(char *s, std::streamsize n)
Method to read n bytes in dicom buffer and write it in s. On the first call, readImage() method is ca...
ImageDicomStream(ImageDicomInfo::sptr dcmInfo)
Builds the fwMemory::stream::in::IFactory with few dicom information.
STL namespace.
std::shared_ptr< std::istream > get()
Returns the istream on dicom image buffer.
Class to perform a lazy reading on dicom image with fw4spl system.
#define OSLM_TRACE(message)
Definition: spyLog.hpp:230
vtkmGdcm reader/writer lib
Definition: GdcmHelper.hpp:15
#define OSLM_ERROR(message)
Definition: spyLog.hpp:274
ImageDicomInfo::sptr m_dcmInfo
To conserve dicom information.
#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
ImageDicomSource(ImageDicomInfo::sptr dcmInfo)
Constructor.
#define OSLM_ERROR_IF(message, cond)
Definition: spyLog.hpp:278