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