]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkImageReader.cxx
023ccd5ac13c834e3ce49c7760a0dc7d5f8d553e
[bbtk.git] / packages / itk / src / bbitkImageReader.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbitkImageReader.cxx,v $
4   Language:  C++
5   Date:      $Date: 2010/04/08 14:37:58 $
6   Version:   $Revision: 1.8 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief 
34  */
35 #ifdef _USE_ITK_
36
37 #include "bbitkImageReader.h"
38 #include "bbitkPackage.h"
39 #include "itkImageFileReader.h"
40
41 namespace bbitk 
42 {
43   BBTK_BLACK_BOX_IMPLEMENTATION(ImageReader,bbtk::AtomicBlackBox);
44   
45   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itk,ImageReader);
46
47         //-----------------------------------------------------------------     
48         void ImageReader::bbUserSetDefaultValues()
49         {
50         }
51         
52         //-----------------------------------------------------------------     
53         void ImageReader::bbUserInitializeProcessing()
54         {
55         }
56         
57         //-----------------------------------------------------------------     
58         void ImageReader::bbUserFinalizeProcessing()
59         {
60         }       
61         
62   void ImageReader::Read()
63   {
64     std::string filename = bbGetInputIn();
65
66     itk::ImageIOBase::Pointer genericReader = 
67       itk::ImageIOFactory::CreateImageIO(filename.c_str(), 
68                                          itk::ImageIOFactory::ReadMode);
69     if (!genericReader)
70       {
71         bbtkError("File format unknown (while reading [" << filename << "])");
72       }
73     genericReader->SetFileName(filename.c_str());
74     genericReader->ReadImageInformation();  
75     
76     bbtk::TypeInfo typ = GetITKImagePtrTypeInfoFromPixelTypeInfoAndDimension(genericReader->GetComponentTypeInfo(), genericReader->GetNumberOfDimensions());
77
78     BBTK_TEMPLATE_ITK_IMAGE_SWITCH( typ, Read);
79   }
80
81
82   /** 
83       Template Processing 
84   */
85   template<class itkImageType>
86   void ImageReader::Read()
87   {
88     bbtkDebugMessageInc("Core",9,"itkImageReader<"<<
89                         bbtk::TypeName<itkImageType>()
90                         <<">::Read()"<<std::endl);
91
92     typedef itk::ImageFileReader< itkImageType > itkReaderType;
93
94     typename itkReaderType::Pointer reader = itkReaderType::New();
95     std::string filename = bbGetInputIn();
96     reader->SetFileName(filename.c_str());
97     try { reader->Update(); }
98     catch( std::exception& e ) 
99       {
100         bbtkError("could not read image \""<< filename << "\" : "<<e.what());
101       }
102
103     reader->GetOutput()->Register();
104     bbSetOutputOut( reader->GetOutput() );
105
106     bbtkDebugDecTab("Core",9);
107   }
108
109 }
110 // eo namespace bbtk
111
112 #endif