ITK  4.4.0
Insight Segmentation and Registration Toolkit
itkMRCImageIO.h
Go to the documentation of this file.
1 /*=========================================================================
2  *
3  * Copyright Insight Software Consortium
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0.txt
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  *=========================================================================*/
18 #ifndef __itkMRCImageIO_h
19 #define __itkMRCImageIO_h
20 
22 #include "itkMRCHeaderObject.h"
23 #include "itkGenericUtilities.h"
24 #include <numeric>
25 
26 namespace itk
27 {
59 class ITK_EXPORT MRCImageIO:
61 {
62 public:
64  typedef MRCImageIO Self;
67 
69  itkNewMacro(Self);
70 
72  itkTypeMacro(MRCImageIO, StreamingImageIOBase);
73 
74  // we don't use this method
75  virtual void WriteImageInformation(void) {}
76 
77  //-------- This part of the interface deals with reading data. ------
78 
79  // See super class for documentation
80  virtual bool CanReadFile(const char *);
81 
82  // See super class for documentation
83  virtual void ReadImageInformation();
84 
85  // See super class for documentation
86  virtual void Read(void *buffer);
87 
88  // -------- This part of the interfaces deals with writing data. -----
89 
96  virtual bool CanWriteFile(const char *);
97 
98  // see super class for documentation
99  virtual void Write(const void *buffer);
100 
105  static const char *m_MetaDataHeaderName;
106 
107 protected:
108  MRCImageIO();
109  // ~MRCImageIO(); // default works
110  void PrintSelf(std::ostream & os, Indent indent) const;
111 
116  virtual SizeType GetHeaderSize(void) const;
117 
118 private:
119 
120  MRCImageIO(const Self &); //purposely not implemented
121  void operator=(const Self &); //purposely not implemented
122 
123  // internal methods to update the min and max in the header based on
124  // the data, in the image buffer to be written
125  template< typename TPixelType >
126  void UpdateHeaderWithMinMaxMean(const TPixelType *bufferBegin)
127  {
128  typedef const TPixelType *ConstPixelPointer;
129 
130  ConstPixelPointer bufferEnd = bufferBegin + m_IORegion.GetNumberOfPixels();
131 
132  // this could be replaced with std::min_element and
133  // std::max_element, but that is slighlty less efficient
134  std::pair< ConstPixelPointer, ConstPixelPointer > mm =
135  itk::min_max_element(bufferBegin, bufferEnd);
136 
137  double mean = std::accumulate( bufferBegin, bufferEnd, double(0.0) )
138  / std::distance(bufferBegin, bufferEnd);
139 
140  m_MRCHeader->m_Header.amin = float(*mm.first);
141  m_MRCHeader->m_Header.amax = float(*mm.second);
142  m_MRCHeader->m_Header.amean = float(mean);
143  }
144 
145  void UpdateHeaderWithMinMaxMean(const void *bufferBegin);
146 
147  // internal methods to update the header object from the ImageIO's
148  // set member variables
149  void UpdateHeaderFromImageIO(void);
150 
151  // reimplemented
152  void InternalReadImageInformation(std::ifstream & is);
153 
154  virtual void WriteImageInformation(const void *bufferBegin);
155 
157 };
158 } // namespace itk
159 
160 #endif
161