GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
itkMegaCaptureReader.cxx
Go to the documentation of this file.
1 /*=========================================================================
2  Authors: The GoFigure Dev. Team.
3  at Megason Lab, Systems biology, Harvard Medical school, 2009-11
4 
5  Copyright (c) 2009-11, President and Fellows of Harvard College.
6  All rights reserved.
7 
8  Redistribution and use in source and binary forms, with or without
9  modification, are permitted provided that the following conditions are met:
10 
11  Redistributions of source code must retain the above copyright notice,
12  this list of conditions and the following disclaimer.
13  Redistributions in binary form must reproduce the above copyright notice,
14  this list of conditions and the following disclaimer in the documentation
15  and/or other materials provided with the distribution.
16  Neither the name of the President and Fellows of Harvard College
17  nor the names of its contributors may be used to endorse or promote
18  products derived from this software without specific prior written
19  permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
27  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
28  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 
33 =========================================================================*/
34 
35 #include "itkMegaCaptureReader.h"
36 #include "itkNumericTraits.h"
37 
38 #include "vtkImageData.h"
39 #include "vtkImageAppend.h"
40 
41 #include "vtkJPEGReader.h"
42 #include "vtkBMPReader.h"
43 #include "vtkPNGReader.h"
44 #include "vtkTIFFReader.h"
45 #include "vtkMetaImageReader.h"
46 
47 #include "VolumeBuilderHelper.h"
48 
49 namespace itk
50 {
51 //--------------------------------------------------------------------------
53  m_FileType(GoFigure::PNG),
54  m_TimeBased(true),
55  m_Modified(true)
56 {
58  unsigned int max_uint = itk::NumericTraits< unsigned int >::max();
59  m_MinTimePoint = max_uint;
60  m_MaxTimePoint = max_uint;
61  m_UpdateTimePoint = max_uint;
62  m_TimeInterval = max_uint;
63  m_MinZSlice = max_uint;
64  m_MaxZSlice = max_uint;
65  m_UpdateZSlice = max_uint;
66  m_MinChannel = max_uint;
67  m_MaxChannel = max_uint;
68 }
69 
70 //--------------------------------------------------------------------------
71 
72 //--------------------------------------------------------------------------
75 {
76  delete m_HeaderReader;
77 }
78 
79 //--------------------------------------------------------------------------
80 
81 //--------------------------------------------------------------------------
82 void
83 MegaCaptureReader::SetTimePoint(const unsigned int & iT)
84 {
85  if ( iT < m_MinTimePoint )
86  {
89  }
90  else
91  {
92  if ( iT > m_MaxTimePoint )
93  {
96  }
97  else
98  {
99  m_Modified = ( m_UpdateTimePoint != iT );
100  m_UpdateTimePoint = iT;
101  }
102  }
103 }
104 
105 //--------------------------------------------------------------------------
106 
107 //--------------------------------------------------------------------------
108 void
109 MegaCaptureReader::SetZSlice(const unsigned int & iZs)
110 {
111  if ( iZs < m_MinZSlice )
112  {
115  }
116  else
117  {
118  if ( iZs > m_MaxZSlice )
119  {
122  }
123  else
124  {
125  m_Modified = ( m_UpdateZSlice != iZs );
126  m_UpdateZSlice = iZs;
127  }
128  }
129 }
130 
131 //--------------------------------------------------------------------------
132 
133 //--------------------------------------------------------------------------
134 void
135 MegaCaptureReader::SetMegaCaptureHeader(const std::string & iHeader)
136 {
137  m_HeaderReader->SetFileName(iHeader);
138  m_HeaderReader->Read();
139 
141 
142  m_Modified = true;
143 }
144 
145 //--------------------------------------------------------------------------
146 
147 //--------------------------------------------------------------------------
148 void
150 {
151  if ( iUserFileList.size() == 0 )
152  {
153  std::cerr << "iUserFileList.empty()" << std::endl;
154  return;
155  }
156 
157  m_FileList = iUserFileList;
158  m_Modified = true;
159 
160  ComputeBounds();
161 }
162 
163 //--------------------------------------------------------------------------
164 
165 //--------------------------------------------------------------------------
166 void
168 {
169  // get min time point
170  GoFigureFileInfoHelperMultiIndexContainer::index< m_TCoord >::type::iterator
171  tm_it = m_FileList.get< m_TCoord >().begin();
172 
173  m_MinTimePoint = ( *tm_it ).m_TCoord;
174 
175  // get max time point
176  GoFigureFileInfoHelperMultiIndexContainer::index< m_TCoord >::type::reverse_iterator
177  r_tm_it = m_FileList.get< m_TCoord >().rbegin();
178 
179  m_MaxTimePoint = ( *r_tm_it ).m_TCoord;
180 
181  // get min Z slices
182  GoFigureFileInfoHelperMultiIndexContainer::index< m_ZCoord >::type::iterator
183  zs_it = m_FileList.get< m_ZCoord >().begin();
184 
185  m_MinZSlice = ( *zs_it ).m_ZCoord;
186 
187  // get max Z slices
188  GoFigureFileInfoHelperMultiIndexContainer::index< m_ZCoord >::type::reverse_iterator
189  r_zs_it = m_FileList.get< m_ZCoord >().rbegin();
190 
191  m_MaxZSlice = ( *r_zs_it ).m_ZCoord;
192 
193  // get min channel
194  GoFigureFileInfoHelperMultiIndexContainer::index< m_Channel >::type::iterator
195  ch_it = m_FileList.get< m_Channel >().begin();
196 
197  m_MinChannel = ( *ch_it ).m_Channel;
198 
199  // (note that the real one could be different)
200  GoFigureFileInfoHelperMultiIndexContainer::index< m_Channel >::type::reverse_iterator
201  r_ch_it = m_FileList.get< m_Channel >().rbegin();
202 
203  m_MaxChannel = ( *r_ch_it ).m_Channel;
204 }
205 
206 //--------------------------------------------------------------------------
207 //--------------------------------------------------------------------------
208 void
210  const std::string & iFileName,
211  vtkImageAppend *iBuilder)
212 {
213  switch ( this->m_FileType )
214  {
215  case GoFigure::JPEG:
216  {
217  AddToVolumeBuilder< vtkJPEGReader >(iCounter, iFileName, iBuilder);
218  break;
219  }
220  case GoFigure::BMP:
221  {
222  AddToVolumeBuilder< vtkBMPReader >(iCounter, iFileName, iBuilder);
223  break;
224  }
225  case GoFigure::PNG:
226  {
227  AddToVolumeBuilder< vtkPNGReader >(iCounter, iFileName, iBuilder);
228  break;
229  }
230  case GoFigure::TIFF:
231  {
232  AddToVolumeBuilder< vtkTIFFReader >(iCounter, iFileName, iBuilder);
233  break;
234  }
235  case GoFigure::MHA:
236  {
237  AddToVolumeBuilder< vtkMetaImageReader >(iCounter, iFileName,
238  iBuilder);
239  break;
240  }
241  case GoFigure::LSM:
242  {
243  itkGenericExceptionMacro(<< "stacks of 2D LSM are not supported at this time.");
244  break;
245  }
246  default:
247  {
248  itkGenericExceptionMacro(<< "unsupported type: " << m_FileType << ".");
249  break;
250  }
251  }
252 }
253 
254 //--------------------------------------------------------------------------
255 
256 //--------------------------------------------------------------------------
257 void
259 {
260  if ( m_Modified )
261  {
262  this->InvokeEvent( StartEvent() );
263 
264  std::map< unsigned int, std::list< std::string > > filelistperchannel;
265 
266  if ( m_TimeBased )
267  {
268  filelistperchannel =
271  }
272  else
273  {
274  filelistperchannel =
277  }
278 
279  // prepare the final output
280  std::map< unsigned int, std::list< std::string > >::iterator
281  fch_it = filelistperchannel.begin();
282  std::map< unsigned int, std::list< std::string > >::iterator
283  fch_end = filelistperchannel.end();
284 
285  size_t kk = 0;
286  size_t size = filelistperchannel.size() * fch_it->second.size();
287 
288  while ( fch_it != fch_end )
289  {
290  int counter = 0;
291 
292  vtkSmartPointer< vtkImageAppend > volumeBuilder =
293  vtkSmartPointer< vtkImageAppend >::New();
294  volumeBuilder->SetNumberOfThreads(VTK_MAX_THREADS);
295  volumeBuilder->SetAppendAxis(2);
296 
297  std::list< std::string >::iterator f_it = fch_it->second.begin();
298  std::list< std::string >::iterator f_end = fch_it->second.end();
299 
300  while ( f_it != f_end )
301  {
302  AddToVTKVolumeBuilder(counter, *f_it, volumeBuilder);
303 
304  this->SetProgress( static_cast< float >( kk ) / static_cast< float >( size ) );
305 
306  ++kk;
307  ++f_it;
308  ++counter;
309  }
310 
311  volumeBuilder->Update();
312  vtkImageData *temp_output = volumeBuilder->GetOutput();
313 
314  double zspacing = 1.;
315 
317  // false.
319  zspacing = m_HeaderReader->m_VoxelSizeZ;
320 
321  // if( m_TimeBased )
322  // {
323  // zspacing = m_HeaderReader.m_VoxelSizeZ;
324  // }
325  // else
326  // {
327  // zspacing = m_HeaderReader.m_TimeInterval;
328  // }
329 
330  temp_output->SetSpacing(m_HeaderReader->m_VoxelSizeX,
332  zspacing);
333 
334  if ( m_OutputImageMap.find(fch_it->first) == m_OutputImageMap.end() )
335  {
336  m_OutputImageMap[fch_it->first] = vtkImageData::New();
337  }
338  m_OutputImageMap[fch_it->first]->ShallowCopy(temp_output);
339 
340  ++fch_it;
341  }
343 
344  this->InvokeEvent( EndEvent() );
345  }
346 }
347 
348 //--------------------------------------------------------------------------
349 
350 //--------------------------------------------------------------------------
351 vtkSmartPointer< vtkImageData >
352 MegaCaptureReader::GetOutput(const unsigned int & iChannel)
353 {
354  std::map< unsigned int, vtkImageData * >::iterator
355  it = m_OutputImageMap.find(iChannel);
356 
357  if ( it != m_OutputImageMap.end() )
358  {
359  //vtkSmartPointer< vtkImageData > output =
360  // vtkSmartPointer<vtkImageData>::New();
361  //output->ShallowCopy(it->second);
362  return it->second;
363  }
364  else
365  {
366  return NULL;
367  }
368 }
369 
370 //--------------------------------------------------------------------------
371 
372 //--------------------------------------------------------------------------
373 vtkSmartPointer< vtkImageData >
374 MegaCaptureReader::GetImage(const unsigned int & iCh,
375  const unsigned int & iT)
376 {
377  std::list< std::string > filenames =
379 
380  if ( filenames.empty() )
381  {
382  return NULL;
383  }
384  else
385  {
386  vtkSmartPointer< vtkImageAppend > volumeBuilder =
387  vtkSmartPointer< vtkImageAppend >::New();
388  volumeBuilder->SetNumberOfThreads(VTK_MAX_THREADS);
389  volumeBuilder->SetAppendAxis(2);
390 
391  std::list< std::string >::iterator f_it = filenames.begin();
392  std::list< std::string >::iterator f_end = filenames.end();
393  int counter = 0;
394 
395  while ( f_it != f_end )
396  {
397  this->AddToVTKVolumeBuilder(counter, *f_it, volumeBuilder);
398  ++counter;
399  ++f_it;
400  }
401  volumeBuilder->Update();
402  vtkSmartPointer< vtkImageData > temp_output = vtkSmartPointer< vtkImageData >::New();
403  temp_output->ShallowCopy( volumeBuilder->GetOutput() );
404  temp_output->SetSpacing(m_HeaderReader->m_VoxelSizeX,
407  return temp_output;
408  }
409 }
410 
411 //--------------------------------------------------------------------------
412 
413 //--------------------------------------------------------------------------
414 std::map< unsigned int, vtkImageData * >
416 {
417  return m_OutputImageMap;
418 }
419 //--------------------------------------------------------------------------
420 
421 //--------------------------------------------------------------------------
422 std::vector< std::vector< int > >
425 {
426  return m_ChannelColor;
427 }
428 //--------------------------------------------------------------------------
429 }//end of namespace
std::map< unsigned int, vtkImageData * > GetOutputs()
std::vector< std::vector< int > > m_ChannelColor
vtkSmartPointer< vtkImageData > GetImage(const unsigned int &iChannel, const unsigned int &iT)
void SetZSlice(const unsigned int &iZs)
vtkSmartPointer< vtkImageData > GetOutput(const unsigned int &iChannel)
void SetMegaCaptureHeader(const std::string &iHeader)
void SetTimePoint(const unsigned int &iTm)
std::map< unsigned int, std::list< std::string > > GetAllFileNamesForGivenTCoord(const GoFigureFileInfoHelperMultiIndexContainer &iContainer, const unsigned int &iT, const unsigned int &iMinCh, const unsigned int &iMaxCh)
MegaCaptureHeaderReader * m_HeaderReader
std::map< unsigned int, vtkImageData * > m_OutputImageMap
std::vector< std::vector< int > > GetChannelColor()
std::map< unsigned int, std::list< std::string > > GetAllFileNamesForGivenZCoord(const GoFigureFileInfoHelperMultiIndexContainer &iContainer, const unsigned int &iZ, const unsigned int &iMinCh, const unsigned int &iMaxCh)
void SetFileName(const std::string &iFileName)
void AddToVTKVolumeBuilder(const int &iCounter, const std::string &iFileName, vtkImageAppend *iBuilder)
std::list< std::string > GetAllFileNamesForGivenTCoordAndChannel(const GoFigureFileInfoHelperMultiIndexContainer &iContainer, const unsigned int &iT, const unsigned int &iCh)
boost::multi_index::multi_index_container< GoFigureFileInfoHelper, boost::multi_index::indexed_by< boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_PCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_RCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_CCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_XTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_YTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_ZTileCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_ZCoord >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_Channel >, >, boost::multi_index::ordered_non_unique< boost::multi_index::tag< m_TCoord >, > >> GoFigureFileInfoHelperMultiIndexContainer
GoFigureFileInfoHelperMultiIndexContainer m_FileList
std::vector< std::vector< int > > m_ChannelColor
GoFigure::FileType m_FileType
void SetInput(const GoFigureFileInfoHelperMultiIndexContainer &UserFileList)
set the input as a GoFigure format file list