]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkImageSeriesReader.cxx
Feature #1774
[bbtk.git] / packages / itk / src / bbitkImageSeriesReader.cxx
1 /*
2  # ---------------------------------------------------------------------
3  #
4  # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5  #                        pour la SantÈ)
6  # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
7  # Previous Authors : Laurent Guigues, Jean-Pierre Roux
8  # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9  #
10  #  This software is governed by the CeCILL-B license under French law and
11  #  abiding by the rules of distribution of free software. You can  use,
12  #  modify and/ or redistribute the software under the terms of the CeCILL-B
13  #  license as circulated by CEA, CNRS and INRIA at the following URL
14  #  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
15  #  or in the file LICENSE.txt.
16  #
17  #  As a counterpart to the access to the source code and  rights to copy,
18  #  modify and redistribute granted by the license, users are provided only
19  #  with a limited warranty  and the software's author,  the holder of the
20  #  economic rights,  and the successive licensors  have only  limited
21  #  liability.
22  #
23  #  The fact that you are presently reading this means that you have had
24  #  knowledge of the CeCILL-B license and that you accept its terms.
25  # ------------------------------------------------------------------------ */
26
27
28 /*=========================================================================
29   Program:   bbtk
30   Module:    $RCSfile: bbitkImageSeriesReader.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:50:39 $
33   Version:   $Revision: 1.12 $
34 =========================================================================*/
35
36
37
38 /**
39  *  \file 
40  *  \brief 
41  */
42 #ifdef _USE_ITK_
43
44 #include "bbitkImageSeriesReader.h"
45 #include "bbitkPackage.h"
46 #include "itkImageSeriesReader.h"
47
48 namespace bbitk 
49 {
50   BBTK_BLACK_BOX_IMPLEMENTATION(ImageSeriesReader,bbtk::AtomicBlackBox);
51   
52   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itk,ImageSeriesReader);
53
54     
55         //-----------------------------------------------------------------     
56         void ImageSeriesReader::bbUserSetDefaultValues()
57         {
58                 bbSetInputXSpacing(-1.0);
59                 bbSetInputYSpacing(-1.0);
60                 bbSetInputZSpacing(-1.0);               
61         }
62         
63         //-----------------------------------------------------------------     
64         void ImageSeriesReader::bbUserInitializeProcessing()
65         {
66         }
67         
68         //-----------------------------------------------------------------     
69         void ImageSeriesReader::bbUserFinalizeProcessing()
70         {
71         }       
72         
73         
74         
75   void ImageSeriesReader::Read()
76   {
77 std::cout << "--------------------------------------------------- ImageSeriesReader::Read( NO template)" << std::endl;
78     const std::vector<std::string>& filenames = bbGetInputFileNames();
79     if (!filenames.size()) 
80       {
81         typedef Image_uint8_t_2_ptr itype;
82         Image_uint8_t_2::Pointer p = Image_uint8_t_2::New();
83         Image_uint8_t_2::IndexType i;
84         i.Fill(0);
85         Image_uint8_t_2::SizeType s;
86         s.Fill(1);
87         Image_uint8_t_2::RegionType r(i,s);
88         p->SetRegions(r);
89         p->Allocate();
90         p->Register();
91         bbSetOutputOut(p.GetPointer());
92 std::cout << "--------------------------------------------------- ImageSeriesReader::Read( NO image found)" << std::endl;
93         return;
94         //bbtkError("Void vector passed to "<<bbGetFullName());
95       }
96
97     std::string filename = filenames[0];
98     std::cout << "--------------------------------------------------- size " << filenames.size() << std::endl;
99 std::cout << "--------------------------------------------------- filenames[0] : " << filenames[0]<< std::endl; 
100 std::cout << "--------------------------------------------------- filename.c_str : " << filename.c_str()<< std::endl; 
101    
102     itk::ImageIOBase::Pointer genericReader = 
103       itk::ImageIOFactory::CreateImageIO(filename.c_str(), 
104                                          itk::ImageIOFactory::ReadMode);
105                                          
106     if (!genericReader)
107       {
108         bbtkError(bbGetFullName()<<" : File format unknown (while reading [" << filename << "])");
109       }
110     genericReader->SetFileName(filename.c_str());
111     genericReader->ReadImageInformation();
112     
113     // a stack of 2D images will result as a 3D image // JPRx
114     int plusUn;
115     if (filenames.size()>1)
116        plusUn = 1;
117     else
118        plusUn = 0;
119     
120     bbtk::TypeInfo typ = GetITKImagePtrTypeInfoFromPixelTypeInfoAndDimension(genericReader->GetComponentTypeInfo(), genericReader->GetNumberOfDimensions() + plusUn);
121       
122     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(typ,Read);
123     
124   }
125
126   /** 
127       Template Processing 
128   */
129   template<class T>
130   void ImageSeriesReader::Read()
131   {
132 std::cout << "--------------------------------------------------- ImageSeriesReader::Read()" << std::endl;
133     bbtkDebugMessageInc("Core",9,"bbitk::ImageSeriesReader<"
134                         <<bbtk::TypeName<T>()
135                         <<">::Read()"<<std::endl);
136
137     typedef T itkImageType;
138     typedef itk::ImageSeriesReader< itkImageType > itkReaderType;
139
140     typename itkReaderType::Pointer reader = itkReaderType::New();
141     const std::vector<std::string>& filenames = bbGetInputFileNames();
142     
143 std::cout << "--------------------------------------------------- size " << filenames.size() << std::endl;
144         for (int i = 0; i< (int)(filenames.size()); i++)
145         std::cout << "-- [" <<filenames[i] << "]" << std::endl;
146         
147     reader->SetFileNames(filenames);    
148
149     try { reader->Update(); }
150     catch( std::exception& e ) 
151       {
152         bbtkError("could not read image series : "<<e.what());
153       }
154
155     // JPRx     
156     typename itkImageType::SpacingType spacingJPRx;
157         
158     bool spacingToBeSet = false;
159     if (bbGetInputXSpacing() > 0.)
160     {
161        spacingToBeSet = true;
162        spacingJPRx[0]= bbGetInputXSpacing();
163     }
164
165     if (bbGetInputYSpacing() > 0.)
166     {
167        spacingToBeSet = true;
168        spacingJPRx[1]= bbGetInputYSpacing();
169     }
170
171     if (bbGetInputZSpacing() > 0.)
172     {
173        spacingToBeSet = true;
174        spacingJPRx[2]= bbGetInputZSpacing();
175     }
176
177     if (spacingToBeSet)
178        reader->GetOutput()->SetSpacing(spacingJPRx);   
179     // JPRx 
180
181     reader->GetOutput()->Register();
182     //    if (bbGetOutputOut()) 
183     bbSetOutputOut ( reader->GetOutput() );
184
185     bbtkDebugDecTab("Core",9);
186   }
187
188 }
189 // eo namespace bbtk
190
191 #endif