]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkConcatImages.cxx
39bc30555b07bcbbdf829b70eb06a7b375d7e96d
[bbtk.git] / packages / vtk / src / bbvtkConcatImages.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkConcatImages.cxx,v $
4   Language:  C++
5   Date:      $Date: 2010/04/28 22:10:07 $
6   Version:   $Revision: 1.2 $
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
36 #ifdef _USE_VTK_
37 #include "bbvtkConcatImages.h"
38 #include "bbvtkPackage.h"
39
40 namespace bbvtk
41 {
42    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,ConcatImages)
43    BBTK_BLACK_BOX_IMPLEMENTATION(ConcatImages,bbtk::AtomicBlackBox);
44
45 //---------------------------------------------------------------------
46
47    void ConcatImages::bbUserSetDefaultValues() 
48    { 
49       //std::cout << "-------- entree ds ConcatImages::bbUserSetDefaultValues()\n" << std::endl;
50       
51    // bbSetInputIn(NULL);  /// \TODO fix it! IN is a std::vector<vtkImageData> // JPR
52       mConcat = NULL;
53       bbSetOutputOut(NULL);
54    }
55
56 //---------------------------------------------------------------------
57
58    void ConcatImages::bbUserInitializeProcessing() 
59    {
60        //std::cout << "-------- entree ds ConcatImages::bbUserInitalizeProcessing()\n" << std::endl;
61       //bbUserFinalizeProcessing();
62       mConcat = vtkImageData::New();  // Alloc depends on  bbGetInputIn().size()  
63    }
64   
65 //---------------------------------------------------------------------
66
67    void ConcatImages::bbUserFinalizeProcessing() 
68    {
69             //std::cout << "-------- entree ds ConcatImages::bbUserFinalizeProcessing()\n" << std::endl;
70    // WTF? we never enter here // JPR  bbUserFinalizeProcessing()  JPR  
71       if (mConcat!=NULL)
72       {
73         // mConcat->Delete();
74         // mConcat=NULL;
75       }
76       bbSetOutputOut(mConcat);          
77    }
78
79 //---------------------------------------------------------------------
80  
81  /// \TODO :clean this dirty stuff :
82  ///  - receives a std::vector<vtkImageData*>
83  ///  - exports a vtkImageData*
84  ///  - supposes *all* the images are consistent (same type, same number of components, same pixel type)
85  ///  - if images in the vector are already 3D, exports only the first one. ?!?  // JPR
86  ///
87    void ConcatImages::Process()
88    {
89         int dim[3];
90         int nbComponents;
91         int scalarType;
92         int nb = (unsigned int)bbGetInputIn().size();
93         int consistentNb(nb);
94
95         if (nb == 0) {
96            // ??? JPR
97         } 
98         
99         bbGetInputIn()[0]->GetDimensions(dim);
100         if (dim[2] > 1  || nb == 1) // dim[2] > 1 : Hopeless for vtk : the first file contains already a 3D object
101                                     // nb == 1 : only one image, nothing to do.
102         {
103            mConcat->Delete();
104            mConcat = bbGetInputIn()[0];
105            //mConcat->PrintSelf(std::cout, vtkIndent(2));
106         }
107         else
108         {
109            // We suppose *all* the images within the directory are consistent (same sizeS, same pixel Type?...)
110            vtkImageData * firstImage = bbGetInputIn()[0];
111            
112                   //std::cout << "--------PrintSelf firstImage " << std::endl;
113                   // firstImage->PrintSelf(std::cout, vtkIndent(2));       
114            
115            nbComponents = firstImage->GetNumberOfScalarComponents();
116            mConcat->SetNumberOfScalarComponents(nbComponents);
117            
118            mConcat->SetScalarType(firstImage->GetScalarType( ));
119            scalarType = firstImage->GetScalarType( );
120            mConcat->SetScalarType(scalarType);
121            
122            mConcat->SetSpacing(firstImage->GetSpacing());
123            
124            firstImage->GetDimensions(dim);         
125            // brute way to perform an ultra-mini consistency check :
126            // First image is supposed to be the reference image,
127            // any unconsistent image is just discarted...
128            for(int i=0; i<nb; i++)
129            {
130               if ( !CheckConsistency(firstImage, dim, nbComponents, scalarType))
131                 consistentNb--;
132            }    
133
134            dim[2]=consistentNb;
135            mConcat->SetDimensions(dim);
136            mConcat->SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
137                    
138            mConcat->AllocateScalars();
139            mConcat->Update();      
140                      
141            int index_image, index_lignes, index_colonnes, index_components;
142            
143            for(index_image=0; index_image<nb; index_image++)
144            {
145               if (!CheckConsistency(bbGetInputIn()[index_image], dim, nbComponents, scalarType))
146                  continue;  // discard unconsistent image !
147               for(index_lignes=0; index_lignes<dim[0];index_lignes++)
148               {
149                  for(index_colonnes=0; index_colonnes<dim[1];index_colonnes++)
150                  {
151                     for(index_components=0; index_components<nbComponents;index_components++)
152                     { 
153                       mConcat->SetScalarComponentFromDouble(index_lignes, index_colonnes, index_image, index_components,(bbGetInputIn()[index_image])->GetScalarComponentAsDouble(index_colonnes,index_lignes,0,index_components));
154                     }
155                  }
156               }
157            }
158         }
159    // Devrait etre dans bbUserFinalizeProcessing() ? On n'y entre jamais // JPR 
160    bbSetOutputOut(mConcat);     
161    }               
162
163 bool ConcatImages::CheckConsistency( vtkImageData *curImage, int dim[], int nbComponents, int scalarType )
164 {
165    assert (curImage);
166    
167    if (scalarType != curImage->GetScalarType( ) )
168       return false;
169       
170    if (nbComponents != curImage->GetNumberOfScalarComponents() )
171       return false;
172       
173    int curDim[3];
174    curImage->GetDimensions(curDim);
175    if ( (curDim[0] =! dim[0]) || (curDim[1] =! dim[1]) )
176       return false;
177
178    return true;   
179 }
180
181
182 }//namespace bbtk
183
184 #endif // _USE_VTK_