1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    vtkMINCImageReader.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 
17 Copyright (c) 2006 Atamai, Inc.
18 
19 Use, modification and redistribution of the software, in source or
20 binary forms, are permitted provided that the following terms and
21 conditions are met:
22 
23 1) Redistribution of the source code, in verbatim or modified
24    form, must retain the above copyright notice, this license,
25    the following disclaimer, and any notices that refer to this
26    license and/or the following disclaimer.
27 
28 2) Redistribution in binary form must include the above copyright
29    notice, a copy of this license and the following disclaimer
30    in the documentation or with other materials provided with the
31    distribution.
32 
33 3) Modified copies of the source code must be clearly marked as such,
34    and must not be misrepresented as verbatim copies of the source code.
35 
36 THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
37 WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
38 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 PURPOSE.  IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
40 MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
41 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
42 (INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
43 OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
44 THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
45 POSSIBILITY OF SUCH DAMAGES.
46 
47 =========================================================================*/
48 // .NAME vtkMINCImageReader - A reader for MINC files.
49 // .SECTION Description
50 // MINC is a NetCDF-based medical image file format that was developed
51 // at the Montreal Neurological Institute in 1992.
52 // This class will read a MINC file into VTK, rearranging the data to
53 // match the VTK x, y, and z dimensions, and optionally rescaling
54 // real-valued data to VTK_FLOAT if RescaleRealValuesOn() is set.
55 // If RescaleRealValues is off, then the data will be stored in its
56 // original data type and the GetRescaleSlope(), GetRescaleIntercept()
57 // method can be used to retrieve global rescaling parameters.
58 // If the original file had a time dimension, the SetTimeStep() method
59 // can be used to specify a time step to read.
60 // All of the original header information can be accessed though the
61 // GetImageAttributes() method.
62 // .SECTION See Also
63 // vtkMINCImageWriter vtkMINCImageAttributes
64 // .SECTION Thanks
65 // Thanks to David Gobbi for writing this class and Atamai Inc. for
66 // contributing it to VTK.
67 
68 #ifndef vtkMINCImageReader_h
69 #define vtkMINCImageReader_h
70 
71 #include "vtkIOMINCModule.h" // For export macro
72 #include "vtkImageReader2.h"
73 
74 class vtkStringArray;
75 class vtkIdTypeArray;
76 class vtkDoubleArray;
77 class vtkMatrix4x4;
78 
79 // A special class that holds the attributes
80 class vtkMINCImageAttributes;
81 
82 class VTKIOMINC_EXPORT vtkMINCImageReader : public vtkImageReader2
83 {
84 public:
85   vtkTypeMacro(vtkMINCImageReader,vtkImageReader2);
86 
87   static vtkMINCImageReader *New();
88   virtual void PrintSelf(ostream& os, vtkIndent indent);
89 
90   // Description:
91   // Set the file name.
92   virtual void SetFileName(const char *name);
93 
94   // Description:
95   // Get the entension for this file format.
GetFileExtensions()96   virtual const char* GetFileExtensions() {
97     return ".mnc"; }
98 
99   // Description:
100   // Get the name of this file format.
GetDescriptiveName()101   virtual const char* GetDescriptiveName() {
102     return "MINC"; }
103 
104   // Description:
105   // Test whether the specified file can be read.
106   virtual int CanReadFile(const char* name);
107 
108   // Description:
109   // Get a matrix that describes the orientation of the data.
110   // The three columns of the matrix are the direction cosines
111   // for the x, y and z dimensions respectively.
112   virtual vtkMatrix4x4 *GetDirectionCosines();
113 
114   // Description:
115   // Get the slope and intercept for rescaling the scalar values
116   // to real data values.  To convert scalar values to real values,
117   // use the equation y = x*RescaleSlope + RescaleIntercept.
118   virtual double GetRescaleSlope();
119   virtual double GetRescaleIntercept();
120 
121   // Description:
122   // Rescale real data values to float.  If this is done, the
123   // RescaleSlope and RescaleIntercept will be set to 1 and 0
124   // respectively.  This is off by default.
125   vtkSetMacro(RescaleRealValues, int);
126   vtkBooleanMacro(RescaleRealValues, int);
127   vtkGetMacro(RescaleRealValues, int);
128 
129   // Description:
130   // Get the scalar range of the output from the information in
131   // the file header.  This is more efficient that computing the
132   // scalar range, but in some cases the MINC file stores an
133   // incorrect valid_range and the DataRange will be incorrect.
134   virtual double *GetDataRange();
GetDataRange(double range[2])135   virtual void GetDataRange(double range[2]) {
136     double *r = this->GetDataRange();
137     range[0] = r[0]; range[1] = r[1]; };
138 
139   // Description:
140   // Get the number of time steps in the file.
141   virtual int GetNumberOfTimeSteps();
142 
143   // Description:
144   // Set the time step to read.
145   vtkSetMacro(TimeStep, int);
146   vtkGetMacro(TimeStep, int);
147 
148   // Description:
149   // Get the image attributes, which contain patient information and
150   // other useful metadata.
151   virtual vtkMINCImageAttributes *GetImageAttributes();
152 
153 protected:
154   vtkMINCImageReader();
155   ~vtkMINCImageReader();
156 
157   int MINCImageType;
158   int MINCImageTypeSigned;
159 
160   double ValidRange[2];
161   double ImageRange[2];
162   double DataRange[2];
163 
164   int NumberOfTimeSteps;
165   int TimeStep;
166   vtkMatrix4x4 *DirectionCosines;
167   double RescaleSlope;
168   double RescaleIntercept;
169   int RescaleRealValues;
170   vtkMINCImageAttributes *ImageAttributes;
171 
172   int FileNameHasChanged;
173 
174   virtual int OpenNetCDFFile(const char *filename, int& ncid);
175   virtual int CloseNetCDFFile(int ncid);
176   virtual int IndexFromDimensionName(const char *dimName);
177   virtual int ReadMINCFileAttributes();
178   virtual void FindRangeAndRescaleValues();
179   static int ConvertMINCTypeToVTKType(int minctype, int mincsigned);
180 
181   virtual void ExecuteInformation();
182   virtual void ExecuteDataWithInformation(vtkDataObject *out, vtkInformation *outInfo);
183 
184 private:
185   vtkMINCImageReader(const vtkMINCImageReader&); // Not implemented
186   void operator=(const vtkMINCImageReader&);  // Not implemented
187 
188 };
189 
190 #endif
191