GOFIGURE2  0.9.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
MegaCaptureHeaderReader.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 
36 
37 #include <iostream>
38 #include <fstream>
39 #include <iomanip>
40 #include <sstream>
41 
42 /*****************************************************************************/
43 MegaCaptureHeaderReader::MegaCaptureHeaderReader(const std::string & iFileName) :
44  m_FileName(iFileName)
45 {
46  m_TimeInterval = 0.;
47  m_VoxelSizeX = 0.;
48  m_VoxelSizeY = 0.;
49  m_VoxelSizeZ = 0.;
50  m_DimensionX = 0;
51  m_DimensionY = 0;
53  m_ChannelDepth = 0;
54  m_ChannelColor.resize(0);
55 }
56 /*****************************************************************************/
57 
58 /*****************************************************************************/
61 {
62 }
63 /*****************************************************************************/
64 
65 /*****************************************************************************/
66 void MegaCaptureHeaderReader::SetFileName(const std::string & iFileName)
67 {
68  if ( iFileName.empty() )
69  {
70  std::cerr << "empty filename" << std::endl;
71  return;
72  }
73  else
74  {
75  m_FileName = iFileName;
76  }
77 }
78 /*****************************************************************************/
79 
80 /*****************************************************************************/
82 {
83  std::string line;
84  std::ifstream ifs(m_FileName.c_str(), std::ifstream::in);
85 
86  if ( ifs.is_open() )
87  {
91  // Skip the 5 first lines, i.e.
92  // MegaCapture
93  // <ImageSessionData>
94  // Version 3.0
95  // ExperimentTitle
96  // ExperimentDescription
97  int lineNumber(0);
98  for ( int i = 0; i < 5; i++ )
99  {
100  getline(ifs, line);
101  ++lineNumber;
102  }
103 
104  std::string word;
105  ifs >> word >> m_TimeInterval;
106  CheckKeyWord(word, "TimeInterval", lineNumber);
107 
111  getline(ifs, line); // to get '\n'
112  getline(ifs, line); // Objective Plan-Apochromat 20x/0.8 M27
113  ++lineNumber;
114 
115  ifs >> word >> m_VoxelSizeX;
116  CheckKeyWord(word, "VoxelSizeX", lineNumber);
117  ifs >> word >> m_VoxelSizeY;
118  CheckKeyWord(word, "VoxelSizeY", lineNumber);
119  ifs >> word >> m_VoxelSizeZ;
120  CheckKeyWord(word, "VoxelSizeZ", lineNumber);
121  ifs >> word >> m_DimensionX;
122  CheckKeyWord(word, "DimensionX", lineNumber);
123  ifs >> word >> m_DimensionY;
124  CheckKeyWord(word, "DimensionY", lineNumber);
125 
129  // DimensionPL 1
130  // DimensionCO 2
131  // DimensionRO 2
132  // DimensionZT 1
133  // DimensionYT 2
134  // DimensionXT 2
135  // DimensionTM 2
136  // DimensionZS 4
137 
138  getline(ifs, line); // to get '\n'
139  for ( int i = 0; i < 8; i++ )
140  {
141  getline(ifs, line);
142  ++lineNumber;
143  }
144 
145  ifs >> word >> m_NumberOfChannels;
146  CheckKeyWord(word, "DimensionCH", lineNumber);
147 
148  m_ChannelColor.resize(m_NumberOfChannels);
149  unsigned long color;
150  for ( unsigned int i = 0; i < m_NumberOfChannels; i++ )
151  {
152  ifs >> word >> color;
153  std::ostringstream channelColor1;
154  channelColor1 << "ChannelColor" << std::setw(2) << std::setfill('0') << i;
155  std::ostringstream channelColor2;
156  channelColor2 << "ChannelColor" << i;
157  CheckKeyWord(word, channelColor1.str(), lineNumber, channelColor2.str());
159  }
160  ifs >> word >> m_ChannelDepth;
161  CheckKeyWord(word, "ChannelDepth", lineNumber);
162 
166  // FileType TIF
167  // </ImageSessionData>
168  // <Image>
169  // Filename
170  //
171  // D:/megacapture/image-PL00-CO00-RO00-ZT00-YT00-XT00-TM0000-ch00-zs0000.png
172  for ( int i = 0; i < 5; i++ )
173  {
174  getline(ifs, line);
175  }
176  std::string date, hours;
177  ifs >> word >> date >> hours;
178  m_CreationDate = date;
179  m_CreationDate += " ";
180  m_CreationDate += hours;
181 
182  //std::cout << "** " << m_CreationDate << std::endl;
183  }
184  else
185  {
186  std::cerr << "Unable to open file" << std::endl;
187  }
188 }
189 
190 std::vector< int > MegaCaptureHeaderReader::ConvertUnsignedLongColorToRGBIntColor(const unsigned long & iColor)
191 {
192  std::vector< int > oRGB(3);
193  oRGB[0] = static_cast< int >( ( iColor / ( 256 * 256 ) ) % 256 );
194  oRGB[1] = static_cast< int >( ( iColor / 256 ) % 256 );
195  oRGB[2] = static_cast< int >( iColor % 256 );
196  return oRGB;
197 }
198 
199 // std::string MegaCaptureHeaderReader::
200 // ConvertDayStringFormat( std::string iDate )
201 // {
202 // std::string oDate;
203 // oDate = iDate.substr( );
204 // }
205 /*****************************************************************************/
206 
207 /*****************************************************************************/
208 bool
210 CheckKeyWord(std::string iWord, std::string iCompare, int& iLineNumber,
211  std::string iExtraKeyWord)
212 {
213  ++iLineNumber;
214  if( (iWord.compare(iCompare) != 0) && (iWord.compare(iExtraKeyWord) != 0) )
215  {
216  std::cerr << ">> ERROR: *" << iCompare << "* keyword should on the line *"
217  << iLineNumber << "* of your .meg file"<< std::endl;
218  throw std::string("Corrupted header file");
219 
220  return false;
221  }
222  return true;
223 }
std::vector< int > ConvertUnsignedLongColorToRGBIntColor(const unsigned long &iColor)
std::vector< std::vector< int > > m_ChannelColor
bool CheckKeyWord(std::string iWord, std::string iCompare, int &iLineNumber, std::string iExtraKeyWord="")
void SetFileName(const std::string &iFileName)
MegaCaptureHeaderReader(const std::string &iFileName="")