ITK  4.1.0
Insight Segmentation and Registration Toolkit
itkIPLCommonImageIO.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  *=========================================================================*/
00032 #ifndef __itkIPLCommonImageIO_h
00033 #define __itkIPLCommonImageIO_h
00034 
00035 #include "itkImageIOBase.h"
00036 #include "itkIPLFileNameList.h"
00037 #include "itkGEImageHeader.h"
00038 
00039 namespace itk
00040 {
00049 class ITK_EXPORT IPLCommonImageIO:public ImageIOBase
00050 {
00051 public:
00053   typedef IPLCommonImageIO     Self;
00054   typedef ImageIOBase          Superclass;
00055   typedef SmartPointer< Self > Pointer;
00056 
00057   typedef unsigned char  U8;
00058   typedef signed char    S8;
00059   typedef unsigned short U16;
00060   typedef signed short   S16;
00061   typedef unsigned int   U32;
00062   typedef signed int     S32;
00063   typedef uint64_t       U64;
00064   typedef int64_t        S64;
00065   typedef float          F32;
00066   typedef double         F64;
00067 
00069   itkNewMacro(Self);
00070 
00072   itkTypeMacro(IPLCommonImageIO, Superclass);
00073 
00074   /*-------- This part of the interfaces deals with reading data. ----- */
00075 
00082   virtual bool CanReadFile(const char *FileNameToRead);
00083 
00085   virtual void ReadImageInformation();
00086 
00088   virtual void ModifyImageInformation() {}
00089 
00091   virtual void Read(void *buffer);
00092 
00096   virtual unsigned int GetComponentSize() const;
00097 
00098   /*-------- This part of the interfaces deals with writing data. ----- */
00099 
00106   virtual bool CanWriteFile(const char *FileNameToWrite);
00107 
00109   virtual void WriteImageInformation();
00110 
00113   virtual void Write(const void *buffer);
00114 
00116   virtual void SortImageListByNameAscend();
00117 
00119   virtual void SortImageListByNameDescend();
00120 
00121 protected:
00122   IPLCommonImageIO();
00123   ~IPLCommonImageIO();
00124   void PrintSelf(std::ostream & os, Indent indent) const;
00125 
00126   int AddElementToList(char const *const filename,
00127                        const float sliceLocation,
00128                        const int offset,
00129                        const int XDim,
00130                        const int YDim,
00131                        const float XRes,
00132                        const float YRes,
00133                        const int Key1,
00134                        const int Key2);
00135 
00136   void sortImageListAscend();
00137 
00138   void sortImageListDescend();
00139 
00140   int statTimeToAscii(void *clock, char *timeString, int len);
00141 
00142   virtual GEImageHeader * ReadHeader(const char *FileNameToRead);
00143 
00144   //
00145   // data members
00146   GEImageHeader         *m_ImageHeader;
00147   ImageIOBase::ByteOrder m_SystemByteOrder;
00148   IPLFileNameList       *m_FilenameList;
00149   //
00150   // return 0 on success, -1 on failure
00151   int GetStringAt(std::ifstream & f, std::streamoff Offset, char *buf,
00152                   size_t amount, bool throw_exception = true);
00153 
00154   int GetIntAt(std::ifstream & f, std::streamoff Offset, int *ip,
00155                bool throw_exception = true);
00156 
00157   int GetShortAt(std::ifstream & f, std::streamoff Offset, short *ip,
00158                  bool throw_exception = true);
00159 
00160   int GetFloatAt(std::ifstream & f, std::streamoff Offset, float *ip,
00161                  bool throw_exception = true);
00162 
00163   int GetDoubleAt(std::ifstream & f, std::streamoff Offset, double *ip,
00164                   bool throw_exception = true);
00165 
00166   short hdr2Short(char *hdr);
00167 
00168   int hdr2Int(char *hdr);
00169 
00170   float hdr2Float(char *hdr);
00171 
00172   double hdr2Double(char *hdr);
00173 
00174 private:
00175   IPLCommonImageIO(const Self &); //purposely not implemented
00176   void operator=(const Self &);   //purposely not implemented
00177 };
00178 } // end namespace itk
00179 #define RAISE_EXCEPTION()                                    \
00180             { ExceptionObject exception(__FILE__, __LINE__); \
00181             exception.SetDescription("File cannot be read"); \
00182             throw exception; }
00183 
00184 #define IOCHECK()      \
00185   if ( f.fail() )      \
00186     {                  \
00187     if ( f.is_open() ) \
00188       {                \
00189       f.close();       \
00190       }                \
00191     RAISE_EXCEPTION(); \
00192     }
00193 
00194 #endif // __itkAnalyzeImageIO_h
00195