GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
LSMToMegaCapture.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 "LSMToMegaCapture.h"
36 
37 #include <fstream>
38 #include <sstream>
39 #include <time.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <stdio.h>
43 
44 #include "vtkImageWriterHelper.h"
45 #include "vtkExtractVOI.h"
46 #include "vtkImageData.h"
47 #include "vtkLSMReader.h"
48 
49 #include "vtkPNGWriter.h"
50 #include "vtkTIFFWriter.h"
51 
55 LSMToMegaCapture::LSMToMegaCapture() : m_Plaque(0), m_Row(0),
56  m_Column(0), m_XTile(0), m_YTile(0), m_ZTile(0)
57 {
58 }
59 
65 {
66  if ( !m_LSMReaders.empty() )
67  {
68  for ( unsigned int i = 0; i < m_LSMReaders.size(); i++ )
69  {
70  m_LSMReaders[i]->Delete();
71  }
72  }
73 }
74 
75 void
76 LSMToMegaCapture::SetPlaque(const unsigned int & iPlaque)
77 {
78  m_Plaque = iPlaque;
79 }
80 
81 void
82 LSMToMegaCapture::SetRow(const unsigned int & iRow)
83 {
84  m_Row = iRow;
85 }
86 
87 void
88 LSMToMegaCapture::SetColumn(const unsigned int & iCol)
89 {
90  m_Column = iCol;
91 }
92 
93 void
94 LSMToMegaCapture::SetXTile(const unsigned int & iXt)
95 {
96  m_XTile = iXt;
97 }
98 
99 void
100 LSMToMegaCapture::SetYTile(const unsigned int & iYt)
101 {
102  m_YTile = iYt;
103 }
104 
105 void
106 LSMToMegaCapture::SetZTile(const unsigned int & iZt)
107 {
108  m_ZTile = iZt;
109 }
110 
111 void
113 {
114  m_FileType = iFileType;
115 }
116 
121 void
122 LSMToMegaCapture::SetFileName(const std::string & iFileName)
123 {
124  m_FileName = iFileName;
125  size_t point_idx = iFileName.rfind(".lsm");
126 
127  if ( point_idx != std::string::npos )
128  {
129  size_t slash_idx = iFileName.rfind('/');
130  if ( point_idx != std::string::npos )
131  {
132  m_BaseName = iFileName.substr(slash_idx, point_idx - slash_idx);
133  }
134  else
135  {
136  slash_idx = iFileName.rfind("\\");
137  if ( point_idx != std::string::npos )
138  {
139  m_BaseName = iFileName.substr(slash_idx, point_idx - slash_idx);
140  }
141  else
142  {
143  m_BaseName = iFileName.substr(0, point_idx);
144  }
145  }
146 
147  if ( !m_LSMReaders.empty() )
148  {
149  for ( unsigned int i = 0; i < m_LSMReaders.size(); i++ )
150  {
151  m_LSMReaders[i]->Delete();
152  }
153  }
154  m_LSMReaders.push_back( vtkLSMReader::New() );
155  m_LSMReaders.front()->SetFileName( iFileName.c_str() );
156  m_LSMReaders.front()->Update();
157  }
158 }
159 
160 std::vector< vtkLSMReader * >
162 {
163  return m_LSMReaders;
164 }
165 
170 void
171 LSMToMegaCapture::Export(const std::string & iDirectoryPath)
172 {
173  m_NumberOfChannels = m_LSMReaders[0]->GetNumberOfChannels();
174  m_NumberOfTimePoints = m_LSMReaders[0]->GetNumberOfTimePoints();
175 
176  std::cout << m_LSMReaders[0]->GetDescription() << std::endl;
177 
178  double spacing[3];
179  m_LSMReaders[0]->GetVoxelSizes(spacing);
180 
181  int dim[5];
182  m_LSMReaders[0]->GetDimensions(dim);
183 
184  std::string headerfilename = iDirectoryPath;
185  headerfilename += m_BaseName;
186  headerfilename += ".meg";
187 
188  std::ofstream file( headerfilename.c_str() );
189  file << "MegaCapture" << std::endl;
190  file << "<ImageSessionData>" << std::endl;
191  file << "Version 3.0" << std::endl;
192  file << "ExperimentTitle " << std::endl;
193  file << "ExperimentDescription ";
194  if ( m_LSMReaders[0]->GetDescription() )
195  {
196  file << m_LSMReaders[0]->GetDescription();
197  }
198  file << std::endl;
199  file << "TimeInterval " << m_LSMReaders[0]->GetTimeInterval() << std::endl;
200  file << "Objective " << m_LSMReaders[0]->GetObjective() << std::endl;
201  file << "VoxelSizeX " << spacing[0] * 1000000 << std::endl;
202  file << "VoxelSizeY " << spacing[1] * 1000000 << std::endl;
203  file << "VoxelSizeZ " << spacing[2] * 1000000 << std::endl;
204  file << "DimensionX " << dim[0] << std::endl;
205  file << "DimensionY " << dim[1] << std::endl;
206  file << "DimensionPL " << m_Plaque << std::endl;
207  file << "DimensionCO " << m_Column << std::endl;
208  file << "DimensionRO " << m_Row << std::endl;
209  file << "DimensionZT " << m_ZTile << std::endl;
210  file << "DimensionYT " << m_YTile << std::endl;
211  file << "DimensionXT " << m_XTile << std::endl;
212  file << "DimensionTM " << m_NumberOfTimePoints << std::endl;
213  file << "DimensionZS " << dim[2] << std::endl;
214  file << "DimensionCH " << m_NumberOfChannels << std::endl;
215 
216  unsigned int i, j, k;
217  for ( i = 0; i < m_NumberOfChannels; i++ )
218  {
219  int r = m_LSMReaders[0]->GetChannelColorComponent(i, 0);
220  int g = m_LSMReaders[0]->GetChannelColorComponent(i, 1);
221  int b = m_LSMReaders[0]->GetChannelColorComponent(i, 2);
222 
223  file << "ChannelColor" << i << " " << r * 256 * 256 + g * 256 + b << std::endl;
224  }
225 
226  file << "ChannelDepth 8" << std::endl;
227  file << "FileType PNG" << std::endl;
228  file << "</ImageSessionData>" << std::endl;
229 
230  if ( m_NumberOfChannels > 1 )
231  {
232  for ( i = 1; i < m_NumberOfChannels; i++ )
233  {
234  m_LSMReaders.push_back( vtkLSMReader::New() );
235  m_LSMReaders[i]->SetFileName( m_FileName.c_str() );
236  m_LSMReaders[i]->SetUpdateChannel(i);
237  }
238  }
239 
240  int extent[6];
241 
242  char timeStr[100] = "";
243  struct stat buf;
244 
245  if ( !stat(m_FileName.c_str(), &buf) )
246  {
247  strftime( timeStr, 100, "%Y-%m-%d %H:%M:%S", localtime(&buf.st_mtime) );
248  }
249 
250  for ( i = 0; i < m_NumberOfTimePoints; i++ )
251  {
252  for ( k = 0; k < m_NumberOfChannels; k++ )
253  {
254  m_LSMReaders[k]->SetUpdateTimePoint(i);
255  }
256 
257  for ( j = 0; j < m_NumberOfChannels; j++ )
258  {
259  m_LSMReaders[j]->Update();
260  vtkImageData *image3d = m_LSMReaders[j]->GetOutput();
261  image3d->GetExtent(extent);
262 
263  for ( k = 0; k < static_cast< unsigned int >( dim[2] ); k++ )
264  {
265  std::stringstream filename;
266  filename << m_BaseName << "-PL" << setfill('0') << setw(2) << m_Plaque;
267  filename << "-CO" << setfill('0') << setw(2) << m_Column;
268  filename << "-RO" << setfill('0') << setw(2) << m_Row;
269  filename << "-ZT" << setfill('0') << setw(2) << m_ZTile;
270  filename << "-YT" << setfill('0') << setw(2) << m_YTile;
271  filename << "-XT" << setfill('0') << setw(2) << m_XTile;
272  filename << "-TM" << setfill('0') << setw(4) << i;
273  filename << "-ch" << setfill('0') << setw(2) << j;
274  filename << "-zs" << setfill('0') << setw(4) << k;
275 
276  switch ( m_FileType )
277  {
278  default:
279  case GoFigure::PNG:
280  {
281  filename << ".png";
282  break;
283  }
284  case GoFigure::TIFF:
285  {
286  filename << ".tiff";
287  break;
288  }
289  }
290 
291  file << "<Image>" << std::endl;
292  file << "Filename " << filename.str() << std::endl;
293  file << "DateTime " << timeStr << std::endl;
294  file << "StageX 1000" << std::endl;
295  file << "StageY -1000" << std::endl;
296  file << "Pinhole 44.216" << std::endl;
297  file << "</Image>" << std::endl;
298 
299  vtkExtractVOI *extract = vtkExtractVOI::New();
300  extract->SetSampleRate(1, 1, 1);
301  extract->SetInput(image3d);
302 
303  extract->SetVOI(extent[0], extent[1], extent[2], extent[3], k, k);
304  extract->Update();
305 
306  vtkImageData *image2d = extract->GetOutput();
307 
308  std::string final_filename = iDirectoryPath;
309  final_filename += filename.str();
310 
311  switch ( m_FileType )
312  {
313  default:
314  case GoFigure::PNG:
315  {
316  vtkWriteImage< vtkPNGWriter >(image2d, final_filename);
317  break;
318  }
319  case GoFigure::TIFF:
320  {
321  vtkWriteImage< vtkTIFFWriter >(image2d, final_filename);
322  break;
323  }
324  }
325  extract->Delete();
326  }
327  }
328  }
329  file.close();
330 }
unsigned int m_Plaque
void SetPlaque(const unsigned int &iPlaque)
LSMToMegaCapture()
Constructor.
unsigned int m_ZTile
std::string m_FileName
unsigned int m_Row
unsigned int m_NumberOfTimePoints
std::vector< vtkLSMReader * > GetLSMReaders()
GoFigure::FileType m_FileType
unsigned int m_XTile
void SetColumn(const unsigned int &iCol)
void SetFileName(const std::string &iFileName)
Set input lsm file. (Extract m_BaseName)
void SetYTile(const unsigned int &iYt)
~LSMToMegaCapture()
Destructor.
unsigned int m_YTile
unsigned int m_Column
void Export(const std::string &iDirectoryPath)
Export as MegaCapture.
std::vector< vtkLSMReader * > m_LSMReaders
std::string m_BaseName
static vtkLSMReader * New()
unsigned int m_NumberOfChannels
void SetZTile(const unsigned int &iZt)
void SetOutputFileType(const GoFigure::FileType &iFileType)
Set the output format: PNG (default) or TIFF.
void SetXTile(const unsigned int &iXt)
void SetRow(const unsigned int &iRow)