ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkMRCImageIO.h
Go to the documentation of this file.
00001 /*=========================================================================
00002  *
00003  *  Copyright Insight Software Consortium
00004  *
00005  *  Licensed under the Apache License, Version 2.0 (the "License");
00006  *  you may not use this file except in compliance with the License.
00007  *  You may obtain a copy of the License at
00008  *
00009  *         http://www.apache.org/licenses/LICENSE-2.0.txt
00010  *
00011  *  Unless required by applicable law or agreed to in writing, software
00012  *  distributed under the License is distributed on an "AS IS" BASIS,
00013  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  *  See the License for the specific language governing permissions and
00015  *  limitations under the License.
00016  *
00017  *=========================================================================*/
00018 #ifndef __itkMRCImageIO_h
00019 #define __itkMRCImageIO_h
00020 
00021 #include "itkStreamingImageIOBase.h"
00022 #include "itkMRCHeaderObject.h"
00023 #include "itkGenericUtilities.h"
00024 #include <numeric>
00025 
00026 namespace itk
00027 {
00059 class ITK_EXPORT MRCImageIO:
00060   public StreamingImageIOBase
00061 {
00062 public:
00064   typedef MRCImageIO           Self;
00065   typedef StreamingImageIOBase Superclass;
00066   typedef SmartPointer< Self > Pointer;
00067 
00069   itkNewMacro(Self);
00070 
00072   itkTypeMacro(MRCImageIO, StreamingImageIOBase);
00073 
00074   // we don't use this method
00075   virtual void WriteImageInformation(void) {}
00076 
00077   //-------- This part of the interface deals with reading data. ------
00078 
00079   // See super class for documentation
00080   virtual bool CanReadFile(const char *);
00081 
00082   // See super class for documentation
00083   virtual void ReadImageInformation();
00084 
00085   // See super class for documentation
00086   virtual void Read(void *buffer);
00087 
00088   // -------- This part of the interfaces deals with writing data. -----
00089 
00096   virtual bool CanWriteFile(const char *);
00097 
00098   // see super class for documentation
00099   virtual void Write(const void *buffer);
00100 
00105   static const char *m_MetaDataHeaderName;
00106 protected:
00107   MRCImageIO();
00108   // ~MRCImageIO(); // default works
00109   void PrintSelf(std::ostream & os, Indent indent) const;
00111 
00116   virtual SizeType GetHeaderSize(void) const;
00117 
00118 private:
00119 
00120   MRCImageIO(const Self &);     //purposely not implemented
00121   void operator=(const Self &); //purposely not implemented
00122 
00123   // internal methods to update the min and max in the header based on
00124   // the data, in the image buffer to be written
00125   template< typename TPixelType >
00126   void UpdateHeaderWithMinMaxMean(const TPixelType *bufferBegin)
00127   {
00128     typedef const TPixelType *ConstPixelPointer;
00129 
00130     ConstPixelPointer bufferEnd = bufferBegin + m_IORegion.GetNumberOfPixels();
00131 
00132     // this could be replaced with std::min_element and
00133     // std::max_element, but that is slighlty less efficient
00134     std::pair< ConstPixelPointer, ConstPixelPointer > mm =
00135       itk::min_max_element(bufferBegin, bufferEnd);
00136 
00137     double mean = std::accumulate( bufferBegin, bufferEnd, double(0.0) )
00138                   / std::distance(bufferBegin, bufferEnd);
00139 
00140     m_MRCHeader->m_Header.amin = float(*mm.first);
00141     m_MRCHeader->m_Header.amax = float(*mm.second);
00142     m_MRCHeader->m_Header.amean = float(mean);
00143   }
00144 
00145   void UpdateHeaderWithMinMaxMean(const void *bufferBegin);
00146 
00147   // internal methods to update the header object from the ImageIO's
00148   // set member variables
00149   void UpdateHeaderFromImageIO(void);
00150 
00151   // reimplemented
00152   void InternalReadImageInformation(std::ifstream & is);
00153 
00154   virtual void WriteImageInformation(const void *bufferBegin);
00155 
00156   MRCHeaderObject::Pointer m_MRCHeader;
00157 };
00158 } // namespace itk
00159 
00160 #endif
00161