00001 /*========================================================================= 00002 00003 Program: Insight Segmentation & Registration Toolkit 00004 Module: $RCSfile: Image2.cxx,v $ 00005 Language: C++ 00006 Date: $Date: 2005-02-08 03:51:52 $ 00007 Version: $Revision: 1.19 $ 00008 00009 Copyright (c) Insight Software Consortium. All rights reserved. 00010 See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details. 00011 00012 This software is distributed WITHOUT ANY WARRANTY; without even 00013 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00014 PURPOSE. See the above copyright notices for more information. 00015 00016 =========================================================================*/ 00017 #if defined(_MSC_VER) 00018 #pragma warning ( disable : 4786 ) 00019 #endif 00020 00021 #include "itkImage.h" 00022 00023 // Software Guide : BeginLatex 00024 // 00025 // The first thing required to read an image from a file is to include 00026 // the header file of the \doxygen{ImageFileReader} class. 00027 // 00028 // Software Guide : EndLatex 00029 00030 // Software Guide : BeginCodeSnippet 00031 #include "itkImageFileReader.h" 00032 // Software Guide : EndCodeSnippet 00033 00034 int main( int , char * argv[]) 00035 { 00036 // Software Guide : BeginLatex 00037 // 00038 // Then, the image type should be defined by specifying the 00039 // type used to represent pixels and the dimensions of the image. 00040 // 00041 // Software Guide : EndLatex 00042 00043 // Software Guide : BeginCodeSnippet 00044 typedef unsigned char PixelType; 00045 const unsigned int Dimension = 3; 00046 00047 typedef itk::Image< PixelType, Dimension > ImageType; 00048 // Software Guide : EndCodeSnippet 00049 00050 00051 // Software Guide : BeginLatex 00052 // 00053 // Using the image type, it is now possible to instantiate the image reader 00054 // class. The image type is used as a template parameter to define how the 00055 // data will be represented once it is loaded into memory. This type does 00056 // not have to correspond exactly to the type stored in the file. However, 00057 // a conversion based on C-style type casting is used, so the type chosen 00058 // to represent the data on disk must be sufficient to characterize it 00059 // accurately. Readers do not apply any transformation to the pixel data 00060 // other than casting from the pixel type of the file to the pixel type of 00061 // the ImageFileReader. The following illustrates a typical 00062 // instantiation of the ImageFileReader type. 00063 // 00064 // \index{itk::ImageFileReader!Instantiation} 00065 // \index{itk::Image!read} 00066 // 00067 // Software Guide : EndLatex 00068 00069 // Software Guide : BeginCodeSnippet 00070 typedef itk::ImageFileReader< ImageType > ReaderType; 00071 // Software Guide : EndCodeSnippet 00072 00073 00074 // Software Guide : BeginLatex 00075 // 00076 // The reader type can now be used to create one reader object. A 00077 // \doxygen{SmartPointer} (defined by the \code{::Pointer} notation) is used 00078 // to receive the reference to the newly created reader. The \code{New()} 00079 // method is invoked to create an instance of the image reader. 00080 // 00081 // \index{itk::ImageFileReader!New()} 00082 // \index{itk::ImageFileReader!Pointer} 00083 // 00084 // Software Guide : EndLatex 00085 00086 // Software Guide : BeginCodeSnippet 00087 ReaderType::Pointer reader = ReaderType::New(); 00088 // Software Guide : EndCodeSnippet 00089 00090 00091 // Software Guide : BeginLatex 00092 // 00093 // The minimum information required by the reader is the filename 00094 // of the image to be loaded in memory. This is provided through 00095 // the \code{SetFileName()} method. The file format here is inferred 00096 // from the filename extension. The user may also explicitly specify the 00097 // data format explicitly using the \doxygen{ImageIO} (See 00098 // Chapter~\ref{sec:ImagReadWrite} \pageref{sec:ImagReadWrite} for more 00099 // information 00100 // 00101 // \index{itk::ImageFileReader!SetFileName()} 00102 // 00103 // Software Guide : EndLatex 00104 00105 // Software Guide : BeginCodeSnippet 00106 const char * filename = argv[1]; 00107 reader->SetFileName( filename ); 00108 // Software Guide : EndCodeSnippet 00109 00110 00111 // Software Guide : BeginLatex 00112 // 00113 // Reader objects are referred to as pipeline source objects; they 00114 // respond to pipeline update requests and initiate the data flow in the 00115 // pipeline. The pipeline update mechanism ensures that the reader only 00116 // executes when a data request is made to the reader and the reader has 00117 // not read any data. In the current example we explicitly invoke the 00118 // \code{Update()} method because the output of the reader is not connected 00119 // to other filters. In normal application the reader's output is connected 00120 // to the input of an image filter and the update invocation on the filter 00121 // triggers an update of the reader. The following line illustrates how an 00122 // explicit update is invoked on the reader. 00123 // 00124 // \index{itk::ImageFileReader!Update()} 00125 // 00126 // Software Guide : EndLatex 00127 00128 // Software Guide : BeginCodeSnippet 00129 reader->Update(); 00130 // Software Guide : EndCodeSnippet 00131 00132 00133 // Software Guide : BeginLatex 00134 // 00135 // Access to the newly read image can be gained by calling the 00136 // \code{GetOutput()} method on the reader. This method can also be called 00137 // before the update request is sent to the reader. The reference to the 00138 // image will be valid even though the image will be empty until the reader 00139 // actually executes. 00140 // 00141 // \index{itk::ImageFileReader!GetOutput()} 00142 // 00143 // Software Guide : EndLatex 00144 00145 // Software Guide : BeginCodeSnippet 00146 ImageType::Pointer image = reader->GetOutput(); 00147 // Software Guide : EndCodeSnippet 00148 00149 // Software Guide : BeginLatex 00150 // 00151 // Any attempt to access image data before the reader executes will yield 00152 // an image with no pixel data. It is likely that a program crash will 00153 // result since the image will not have been properly initialized. 00154 // 00155 // Software Guide : EndLatex 00156 00157 return 0; 00158 } 00159