GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
ConversionLsmToMegaThread.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 =========================================================================*/
35 
36 #include "LSMToMegaCapture.h"
37 #include "vtkLSMReader.h"
38 
39 #include <fstream>
40 #include <sstream>
41 #include <time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <stdio.h>
45 
46 #include "vtkImageWriterHelper.h"
47 #include "vtkExtractVOI.h"
48 #include "vtkImageData.h"
49 #include "vtkLSMReader.h"
50 #include "vtkSmartPointer.h"
51 
52 #include "vtkPNGWriter.h"
53 #include "vtkTIFFWriter.h"
54 
55 #include <iostream>
56 
57 //-------------------------------------------------------------------------
58 
59 //-------------------------------------------------------------------------
60 ConversionLsmToMegaThread::ConversionLsmToMegaThread () : m_BaseName(""), m_LsmPath(""), m_MegaPath(""),
61  m_FileType(GoFigure::PNG), m_LSMReaders(0), m_Plaque (0), m_Row(0), m_Column (0),
62  m_XTile(0), m_YTile(0), m_ZTile (0), m_XOverlap(0), m_YOverlap(0), m_ZOverlap(0),
63  m_NumberOfChannels(0), m_NumberOfTimePoints(0), m_Dim(0)
64 {
65 }
66 
67 //-------------------------------------------------------------------------
68 
69 //-------------------------------------------------------------------------
71 {
72  if ( !m_LSMReaders.empty() )
73  {
74  for ( unsigned int i = 0; i < m_LSMReaders.size(); i++ )
75  {
76  m_LSMReaders[i]->Delete();
77  }
78  }
79 }
80 
81 //-------------------------------------------------------------------------
82 
83 //-------------------------------------------------------------------------
84 void
86 {
87  // Allocate memory to avoid automatic suppression at the end of this function
92  LSMToMegaCapture *converter = new LSMToMegaCapture;
93 
94  converter->SetFileName(m_LsmPath);
95  converter->SetOutputFileType(m_FileType);
96 
97  m_LSMReaders = converter->GetLSMReaders();
98  m_NumberOfChannels = m_LSMReaders[0]->GetNumberOfChannels();
99  m_NumberOfTimePoints = m_LSMReaders[0]->GetNumberOfTimePoints();
100  int dim[5];
101  m_LSMReaders[0]->GetDimensions(dim);
102  m_Dim = dim[2];
103 
104  // Start conversion
106 
107  //converter.Export( m_MegaPath );
109 
110  // send message "conversion terminated"
112 }
113 
114 void
116 {
117  m_BaseName = iBaseName;
118 }
119 
120 //-------------------------------------------------------------------------
121 
122 //-------------------------------------------------------------------------
123 void
125 {
126  m_LsmPath = iLsmPath;
127 }
128 
129 //-------------------------------------------------------------------------
130 
131 //-------------------------------------------------------------------------
132 void
134 {
135  m_MegaPath = iMegaPath;
136 }
137 
138 //-------------------------------------------------------------------------
139 
140 //-------------------------------------------------------------------------
141 void
143 {
144  m_FileType = iFileType;
145 }
146 
147 //-------------------------------------------------------------------------
148 
149 //-------------------------------------------------------------------------
150 void
152 {
153  double spacing[3];
154 
155  m_LSMReaders[0]->GetVoxelSizes(spacing);
156 
157  int dim[5];
158  m_LSMReaders[0]->GetDimensions(dim);
159 
160  std::string headerfilename = iMegaPath;
161  headerfilename += m_BaseName;
162  headerfilename += ".meg";
163 
164  std::ofstream file( headerfilename.c_str() );
165  file << "MegaCapture" << std::endl;
166  file << "<ImageSessionData>" << std::endl;
167  file << "Version 3.0" << std::endl;
168  file << "ExperimentTitle " << std::endl;
169  file << "ExperimentDescription ";
170  if ( m_LSMReaders[0]->GetDescription() )
171  {
172  file << m_LSMReaders[0]->GetDescription();
173  }
174  file << std::endl;
175  file << "TimeInterval " << m_LSMReaders[0]->GetTimeInterval() << std::endl;
176  file << "Objective " << m_LSMReaders[0]->GetObjective() << std::endl;
177  file << "VoxelSizeX " << spacing[0] * 1000000 << std::endl;
178  file << "VoxelSizeY " << spacing[1] * 1000000 << std::endl;
179  file << "VoxelSizeZ " << spacing[2] * 1000000 << std::endl;
180  file << "DimensionX " << dim[0] << std::endl;
181  file << "DimensionY " << dim[1] << std::endl;
182  file << "DimensionPL " << m_Plaque << std::endl;
183  file << "DimensionCO " << m_Column << std::endl;
184  file << "DimensionRO " << m_Row << std::endl;
185  file << "DimensionZT " << m_ZTile << std::endl;
186  file << "DimensionYT " << m_YTile << std::endl;
187  file << "DimensionXT " << m_XTile << std::endl;
188  file << "DimensionTM " << m_NumberOfTimePoints << std::endl;
189  file << "DimensionZS " << dim[2] << std::endl;
190  file << "DimensionCH " << m_NumberOfChannels << std::endl;
191 
192  unsigned int i, j, k;
193  for ( i = 0; i < m_NumberOfChannels; i++ )
194  {
195  int r = m_LSMReaders[0]->GetChannelColorComponent(i, 0);
196  int g = m_LSMReaders[0]->GetChannelColorComponent(i, 1);
197  int b = m_LSMReaders[0]->GetChannelColorComponent(i, 2);
198  std::ostringstream channelColor;
199  channelColor << "ChannelColor" << std::setw(2) << std::setfill('0') << i;
200  file << channelColor.str() << " " << r * 256 * 256 + g * 256 + b << std::endl;
201  //send signal for progress bar
202  emit ProgressSent();
203  }
204 
205  file << "ChannelDepth 8" << std::endl;
206  file << "FileType PNG" << std::endl;
207  file << "</ImageSessionData>" << std::endl;
208 
209  if ( m_NumberOfChannels > 1 )
210  {
211  for ( i = 1; i < m_NumberOfChannels; i++ )
212  {
213  m_LSMReaders.push_back( vtkLSMReader::New() );
214  m_LSMReaders[i]->SetFileName( m_LsmPath.c_str() );
215  m_LSMReaders[i]->SetUpdateChannel(i);
216  //send signal for progress bar
217  emit ProgressSent();
218  }
219  }
220 
221  int extent[6];
222 
223  char timeStr[100] = "";
224  struct stat buf;
225 
226  if ( !stat(m_LsmPath.c_str(), &buf) )
227  {
228  strftime( timeStr, 100, "%Y-%m-%d %H:%M:%S", localtime(&buf.st_mtime) );
229  }
230 
231  for ( i = 0; i < m_NumberOfTimePoints; i++ )
232  {
233  for ( k = 0; k < m_NumberOfChannels; k++ )
234  {
235  m_LSMReaders[k]->SetUpdateTimePoint(i);
236  //send signal for progress bar
237  emit ProgressSent();
238  }
239  for ( j = 0; j < m_NumberOfChannels; j++ )
240  {
241  m_LSMReaders[j]->Update();
242  vtkImageData *image3d = m_LSMReaders[j]->GetOutput();
243  image3d->GetExtent(extent);
244 
245  for ( k = 0; k < static_cast< unsigned int >( dim[2] ); k++ )
246  {
247  std::stringstream filename;
248  filename << m_BaseName << "-PL" << setfill('0') << setw(2) << m_Plaque;
249  filename << "-CO" << setfill('0') << setw(2) << m_Column;
250  filename << "-RO" << setfill('0') << setw(2) << m_Row;
251  filename << "-ZT" << setfill('0') << setw(2) << m_ZTile;
252  filename << "-YT" << setfill('0') << setw(2) << m_YTile;
253  filename << "-XT" << setfill('0') << setw(2) << m_XTile;
254  filename << "-TM" << setfill('0') << setw(4) << i;
255  filename << "-ch" << setfill('0') << setw(2) << j;
256  filename << "-zs" << setfill('0') << setw(4) << k;
257 
258  switch ( m_FileType )
259  {
260  default:
261  case GoFigure::PNG:
262  {
263  filename << ".png";
264  break;
265  }
266  case GoFigure::TIFF:
267  {
268  filename << ".tiff";
269  break;
270  }
271  }
272 
273  file << "<Image>" << std::endl;
274  file << "Filename " << filename.str() << std::endl;
275  file << "DateTime " << timeStr << std::endl;
276  file << "StageX 1000" << std::endl;
277  file << "StageY -1000" << std::endl;
278  file << "Pinhole 44.216" << std::endl;
279  file << "</Image>" << std::endl;
280 
281  vtkSmartPointer< vtkExtractVOI > extract =
282  vtkSmartPointer< vtkExtractVOI >::New();
283  extract->SetSampleRate(1, 1, 1);
284  extract->SetInput(image3d);
285  extract->SetVOI(extent[0], extent[1], extent[2], extent[3], k, k);
286  extract->Update();
287 
288  vtkImageData *image2d = extract->GetOutput();
289 
290  std::string final_filename = iMegaPath;
291  final_filename += filename.str();
292 
293  switch ( m_FileType )
294  {
295  default:
296  case GoFigure::PNG:
297  {
298  vtkWriteImage< vtkPNGWriter >(image2d, final_filename);
299  break;
300  }
301  case GoFigure::TIFF:
302  {
303  vtkWriteImage< vtkTIFFWriter >(image2d, final_filename);
304  break;
305  }
306  }
307  //send signal for progress bar
308  emit ProgressSent();
309  }
310  }
311  }
312  file.close();
313 }
314 
315 //-------------------------------------------------------------------------
316 
317 //-------------------------------------------------------------------------
318 int
320 {
324 
325  return total;
326 }
327 
328 //-------------------------------------------------------------------------
std::vector< vtkLSMReader * > m_LSMReaders
std::vector< vtkLSMReader * > GetLSMReaders()
void SetFileName(const std::string &iFileName)
Set input lsm file. (Extract m_BaseName)
Convert 1 LSM (5D file) into megacapture files.
void SetMegaPath(std::string iMegaPath)
Set the path of the MegaCapture file to create.
int GetNumberOfPoints()
Returns the number of signals to be sent for the progress bar.
void run()
Start multithread process (call when parameters are set up properly)
void SetOutputFileType(const GoFigure::FileType &iFileType)
Set the output file type.
virtual ~ConversionLsmToMegaThread()
Destructor.
void ExportWithReimplemented(std::string iMegaPath)
Start the conversion to MegaCapture.
static vtkLSMReader * New()
void SetOutputFileType(const GoFigure::FileType &iFileType)
Set the output format: PNG (default) or TIFF.
void SetBaseName(std::string iBaseName)
Set the base name of the LSM file to convert.
void SetLsmPath(std::string iLsmPath)
Set the path to the LSM file to convert and initialise LSM reader.