]> Creatis software - bbtk.git/blob - packages/itk/src/bbitkImageSeriesReader.cxx
0d6b35eefcd687cc9aa1cc24f7db17b22b55af1f
[bbtk.git] / packages / itk / src / bbitkImageSeriesReader.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbitkImageSeriesReader.cxx,v $
4   Language:  C++
5   Date:      $Date: 2009/06/24 08:08:57 $
6   Version:   $Revision: 1.10 $
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 "bbitkImageSeriesReader.h"
38 #include "bbitkPackage.h"
39 #include "itkImageSeriesReader.h"
40
41 namespace bbitk 
42 {
43   BBTK_BLACK_BOX_IMPLEMENTATION(ImageSeriesReader,bbtk::AtomicBlackBox);
44   
45   BBTK_ADD_BLACK_BOX_TO_PACKAGE(itk,ImageSeriesReader);
46
47     
48         //-----------------------------------------------------------------     
49         void ImageSeriesReader::bbUserSetDefaultValues()
50         {
51                 bbSetInputXSpacing(-1.0);
52                 bbSetInputYSpacing(-1.0);
53                 bbSetInputZSpacing(-1.0);               
54         }
55         
56         //-----------------------------------------------------------------     
57         void ImageSeriesReader::bbUserInitializeProcessing()
58         {
59         }
60         
61         //-----------------------------------------------------------------     
62         void ImageSeriesReader::bbUserFinalizeProcessing()
63         {
64         }       
65         
66         
67         
68   void ImageSeriesReader::Read()
69   {
70 std::cout << "--------------------------------------------------- ImageSeriesReader::Read( NO template)" << std::endl;
71     const std::vector<std::string>& filenames = bbGetInputFileNames();
72     if (!filenames.size()) 
73       {
74         typedef Image_uint8_t_2_ptr itype;
75         Image_uint8_t_2::Pointer p = Image_uint8_t_2::New();
76         Image_uint8_t_2::IndexType i;
77         i.Fill(0);
78         Image_uint8_t_2::SizeType s;
79         s.Fill(1);
80         Image_uint8_t_2::RegionType r(i,s);
81         p->SetRegions(r);
82         p->Allocate();
83         p->Register();
84         bbSetOutputOut(p.GetPointer());
85 std::cout << "--------------------------------------------------- ImageSeriesReader::Read( NO image found)" << std::endl;
86         return;
87         //bbtkError("Void vector passed to "<<bbGetFullName());
88       }
89
90     std::string filename = filenames[0];
91     std::cout << "--------------------------------------------------- size " << filenames.size() << std::endl;
92 std::cout << "--------------------------------------------------- filenames[0] : " << filenames[0]<< std::endl; 
93 std::cout << "--------------------------------------------------- filename.c_str : " << filename.c_str()<< std::endl; 
94    
95     itk::ImageIOBase::Pointer genericReader = 
96       itk::ImageIOFactory::CreateImageIO(filename.c_str(), 
97                                          itk::ImageIOFactory::ReadMode);
98                                          
99     if (!genericReader)
100       {
101         bbtkError(bbGetFullName()<<" : File format unknown (while reading [" << filename << "])");
102       }
103     genericReader->SetFileName(filename.c_str());
104     genericReader->ReadImageInformation();
105     
106     // a stack of 2D images will result as a 3D image // JPRx
107     int plusUn;
108     if (filenames.size()>1)
109        plusUn = 1;
110     else
111        plusUn = 0;
112     
113     bbtk::TypeInfo typ = GetITKImagePtrTypeInfoFromPixelTypeInfoAndDimension(genericReader->GetComponentTypeInfo(), genericReader->GetNumberOfDimensions() + plusUn);
114       
115     BBTK_TEMPLATE_ITK_IMAGE_SWITCH(typ,Read);
116     
117   }
118
119   /** 
120       Template Processing 
121   */
122   template<class T>
123   void ImageSeriesReader::Read()
124   {
125 std::cout << "--------------------------------------------------- ImageSeriesReader::Read()" << std::endl;
126     bbtkDebugMessageInc("Core",9,"bbitk::ImageSeriesReader<"
127                         <<bbtk::TypeName<T>()
128                         <<">::Read()"<<std::endl);
129
130     typedef T itkImageType;
131     typedef itk::ImageSeriesReader< itkImageType > itkReaderType;
132
133     typename itkReaderType::Pointer reader = itkReaderType::New();
134     const std::vector<std::string>& filenames = bbGetInputFileNames();
135     
136 std::cout << "--------------------------------------------------- size " << filenames.size() << std::endl;
137         for (int i = 0; i< filenames.size(); i++)
138         std::cout << "-- [" <<filenames[i] << "]" << std::endl;
139         
140     reader->SetFileNames(filenames);    
141
142     try { reader->Update(); }
143     catch( std::exception& e ) 
144       {
145         bbtkError("could not read image series : "<<e.what());
146       }
147
148     // JPRx     
149     typename itkImageType::SpacingType spacingJPRx;
150         
151     bool spacingToBeSet = false;
152     if (bbGetInputXSpacing() > 0.)
153     {
154        spacingToBeSet = true;
155        spacingJPRx[0]= bbGetInputXSpacing();
156     }
157
158     if (bbGetInputYSpacing() > 0.)
159     {
160        spacingToBeSet = true;
161        spacingJPRx[1]= bbGetInputYSpacing();
162     }
163
164     if (bbGetInputZSpacing() > 0.)
165     {
166        spacingToBeSet = true;
167        spacingJPRx[2]= bbGetInputZSpacing();
168     }
169
170     if (spacingToBeSet)
171        reader->GetOutput()->SetSpacing(spacingJPRx);   
172     // JPRx 
173
174     reader->GetOutput()->Register();
175     //    if (bbGetOutputOut()) 
176     bbSetOutputOut ( reader->GetOutput() );
177
178     bbtkDebugDecTab("Core",9);
179   }
180
181 }
182 // eo namespace bbtk
183
184 #endif