1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkMetaImageReader.h
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 /**
16  * @class   vtkMetaImageReader
17  * @brief   read binary UNC meta image data
18  *
19  * One of the formats for which a reader is already available in the toolkit is
20  * the MetaImage file format. This is a fairly simple yet powerful format
21  * consisting of a text header and a binary data section. The following
22  * instructions describe how you can write a MetaImage header for the data that
23  * you download from the BrainWeb page.
24  *
25  * The minimal structure of the MetaImage header is the following:
26  *
27  *    NDims = 3
28  *    DimSize = 181 217 181
29  *    ElementType = MET_UCHAR
30  *    ElementSpacing = 1.0 1.0 1.0
31  *    ElementByteOrderMSB = False
32  *    ElementDataFile = brainweb1.raw
33  *
34  *    * NDims indicate that this is a 3D image. ITK can handle images of
35  *      arbitrary dimension.
36  *    * DimSize indicates the size of the volume in pixels along each
37  *      direction.
38  *    * ElementType indicate the primitive type used for pixels. In this case
39  *      is "unsigned char", implying that the data is digitized in 8 bits /
40  *      pixel.
41  *    * ElementSpacing indicates the physical separation between the center of
42  *      one pixel and the center of the next pixel along each direction in space.
43  *      The units used are millimeters.
44  *    * ElementByteOrderMSB indicates is the data is encoded in little or big
45  *      endian order. You might want to play with this value when moving data
46  *      between different computer platforms.
47  *    * ElementDataFile is the name of the file containing the raw binary data
48  *      of the image. This file must be in the same directory as the header.
49  *
50  * MetaImage headers are expected to have extension: ".mha" or ".mhd"
51  *
52  * Once you write this header text file, it should be possible to read the
53  * image into your ITK based application using the itk::FileIOToImageFilter
54  * class.
55  *
56  *
57  *
58 */
59 
60 #ifndef vtkMetaImageReader_h
61 #define vtkMetaImageReader_h
62 
63 #include "vtkIOImageModule.h" // For export macro
64 #include "vtkImageReader2.h"
65 
66 namespace vtkmetaio { class MetaImage; } // forward declaration
67 
68 class VTKIOIMAGE_EXPORT vtkMetaImageReader : public vtkImageReader2
69 {
70 public:
71   vtkTypeMacro(vtkMetaImageReader,vtkImageReader2);
72   void PrintSelf(ostream& os, vtkIndent indent) override;
73 
74   /**
75    * Construct object with FlipNormals turned off and Normals set to true.
76    */
77   static vtkMetaImageReader *New();
78 
GetFileExtensions()79   const char * GetFileExtensions() override
80     { return ".mhd .mha"; }
81 
GetDescriptiveName()82   const char * GetDescriptiveName() override
83     { return "MetaIO Library: MetaImage"; }
84 
85   // These duplicate functions in vtkImageReader2, vtkMedicalImageReader.
GetPixelSpacing()86   double * GetPixelSpacing()
87     { return this->GetDataSpacing(); }
GetWidth()88   int GetWidth()
89     { return (this->GetDataExtent()[1] - this->GetDataExtent()[0] + 1); }
GetHeight()90   int GetHeight()
91     { return (this->GetDataExtent()[3] - this->GetDataExtent()[2] + 1); }
GetImagePositionPatient()92   double * GetImagePositionPatient()
93     { return this->GetDataOrigin(); }
GetNumberOfComponents()94   int GetNumberOfComponents()
95     { return this->GetNumberOfScalarComponents(); }
GetPixelRepresentation()96   int GetPixelRepresentation()
97     { return this->GetDataScalarType(); }
98   int GetDataByteOrder(void) override;
99 
100   vtkGetMacro(RescaleSlope, double);
101   vtkGetMacro(RescaleOffset, double);
102   vtkGetMacro(BitsAllocated, int);
103   vtkGetStringMacro(DistanceUnits);
104   vtkGetStringMacro(AnatomicalOrientation);
105   vtkGetMacro(GantryAngle, double);
106   vtkGetStringMacro(PatientName);
107   vtkGetStringMacro(PatientID);
108   vtkGetStringMacro(Date);
109   vtkGetStringMacro(Series);
110   vtkGetStringMacro(ImageNumber);
111   vtkGetStringMacro(Modality);
112   vtkGetStringMacro(StudyID);
113   vtkGetStringMacro(StudyUID);
114   vtkGetStringMacro(TransferSyntaxUID);
115 
116   /**
117    * Test whether the file with the given name can be read by this
118    * reader.
119    */
120   int CanReadFile(const char* name) override;
121 
122 protected:
123   vtkMetaImageReader();
124   ~vtkMetaImageReader() override;
125 
126   // These functions make no sense for this (or most) file readers
127   // and should be hidden from the user...but then the getsettest fails.
128   /*virtual void SetFilePrefix(const char * arg)
129     { vtkImageReader2::SetFilePrefix(arg); }
130   virtual void SetFilePattern(const char * arg)
131     { vtkImageReader2::SetFilePattern(arg); }
132   virtual void SetDataScalarType(int type)
133     { vtkImageReader2::SetDataScalarType(type); }
134   virtual void SetDataScalarTypeToFloat()
135     { this->SetDataScalarType(VTK_FLOAT); }
136   virtual void SetDataScalarTypeToDouble()
137     { this->SetDataScalarType(VTK_DOUBLE); }
138   virtual void SetDataScalarTypeToInt()
139     { this->SetDataScalarType(VTK_INT); }
140   virtual void SetDataScalarTypeToShort()
141     { this->SetDataScalarType(VTK_SHORT); }
142   virtual void SetDataScalarTypeToUnsignedShort()
143     {this->SetDataScalarType(VTK_UNSIGNED_SHORT);}
144   virtual void SetDataScalarTypeToUnsignedChar()
145     {this->SetDataScalarType(VTK_UNSIGNED_CHAR);}
146   vtkSetMacro(NumberOfScalarComponents, int);
147   vtkSetVector6Macro(DataExtent, int);
148   vtkSetMacro(FileDimensionality, int);
149   vtkSetVector3Macro(DataSpacing, double);
150   vtkSetVector3Macro(DataOrigin, double);
151   vtkSetMacro(HeaderSize, unsigned long);
152   unsigned long GetHeaderSize(unsigned long)
153     { return 0; }
154   virtual void SetDataByteOrderToBigEndian()
155     { this->SetDataByteOrderToBigEndian(); }
156   virtual void SetDataByteOrderToLittleEndian()
157     { this->SetDataByteOrderToBigEndian(); }
158   virtual void SetDataByteOrder(int order)
159     { this->SetDataByteOrder(order); }
160   vtkSetMacro(FileNameSliceOffset,int);
161   vtkSetMacro(FileNameSliceSpacing,int);
162   vtkSetMacro(SwapBytes, int);
163   virtual int OpenFile()
164     { return vtkImageReader2::OpenFile(); }
165   virtual void SeekFile(int i, int j, int k)
166     { vtkImageReader2::SeekFile(i, j, k); }
167   vtkSetMacro(FileLowerLeft, int);
168   virtual void ComputeInternalFileName(int slice)
169     { vtkImageReader2::ComputeInternalFileName(slice); }
170   vtkGetStringMacro(InternalFileName)
171   const char * GetDataByteOrderAsString(void)
172     { return vtkImageReader2::GetDataByteOrderAsString(); }
173   unsigned long GetHeaderSize(void)
174     { return vtkImageReader2::GetHeaderSize(); }*/
175 
176   void ExecuteInformation() override;
177   void ExecuteDataWithInformation(vtkDataObject *out, vtkInformation *outInfo) override;
178   int RequestInformation(vtkInformation * request,
179                          vtkInformationVector ** inputVector,
180                          vtkInformationVector * outputVector) override;
181 
182 private:
183   vtkMetaImageReader(const vtkMetaImageReader&) = delete;
184   void operator=(const vtkMetaImageReader&) = delete;
185 
186   vtkmetaio::MetaImage *MetaImagePtr;
187 
188   double GantryAngle;
189   char PatientName[255];
190   char PatientID[255];
191   char Date[255];
192   char Series[255];
193   char Study[255];
194   char ImageNumber[255];
195   char Modality[255];
196   char StudyID[255];
197   char StudyUID[255];
198   char TransferSyntaxUID[255];
199 
200   double RescaleSlope;
201   double RescaleOffset;
202   int BitsAllocated;
203   char DistanceUnits[255];
204   char AnatomicalOrientation[255];
205 };
206 
207 #endif
208