]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkConcatImages.cxx
#3107 BBTK Bug New Normal - branch vtk7itk4 compilation with vtk7
[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 curDim[3];
96         int nbComponents;
97         int scalarType;
98         int nb = (unsigned int)bbGetInputIn().size();
99 //      int consistentNb(nb);
100         int newSizeZ;
101
102     if (nb == 0) {
103            // ??? JPR
104         } 
105         
106         bbGetInputIn()[0]->GetDimensions(dim);
107         if ( nb == 1) // dim[2] > 1 : Hopeless for vtk : the first file contains already a 3D object
108                                     // nb == 1 : only one image, nothing to do.
109         {
110            mConcat->Delete();
111            mConcat = bbGetInputIn()[0];
112            //mConcat->PrintSelf(std::cout, vtkIndent(2));
113         } else {
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                 scalarType = firstImage->GetScalarType( );
122            
123                 mConcat->SetSpacing(firstImage->GetSpacing());
124            
125                 firstImage->GetDimensions(dim);
126                 newSizeZ=0;
127                 // brute way to perform an ultra-mini consistency check :
128                 // First image is supposed to be the reference image,
129                 // any unconsistent image is just discarted...
130                 for(int i=0; i<nb; i++)
131                 {
132
133 //EED 2013 oct 29 
134 //EED         if ( !CheckConsistency(firstImage, dim, nbComponents, scalarType))
135 //EED           consistentNb--;
136
137               if ( CheckConsistency(bbGetInputIn()[i], dim, nbComponents, scalarType))
138                         {
139                                 bbGetInputIn()[i]->GetDimensions(curDim);
140                                 newSizeZ = newSizeZ + curDim[2];
141                         } // if
142                 
143                 }    // for
144
145            dim[2]=newSizeZ;
146
147
148            mConcat->SetDimensions(dim);
149            mConcat->SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
150                    
151
152 //EED 2017-01-01 Migration VTK7
153 #if (VTK_MAJOR_VERSION <= 5) 
154        mConcat->SetNumberOfScalarComponents(nbComponents);
155            mConcat->SetScalarType(scalarType);
156            mConcat->AllocateScalars();
157            mConcat->Update();      
158 #endif
159 #if (VTK_MAJOR_VERSION >= 6) 
160            mConcat->AllocateScalars(scalarType,nbComponents);
161 #endif
162                      
163            int index_image;
164
165            int blocksize;
166            int scalarSize=bbGetInputIn()[0]->GetScalarSize();
167            char *pImage = (char*)mConcat->GetScalarPointer();
168            char *sourcePointer;
169
170 //EED      int index_lignes, index_colonnes, index_components;
171         
172    
173            for(index_image=0; index_image<nb; index_image++)
174            {
175               if (!CheckConsistency(bbGetInputIn()[index_image], dim, nbComponents, scalarType))
176                 {
177                          continue;  // discard unconsistent image !
178                 }
179               bbGetInputIn()[index_image]->GetDimensions(curDim);
180               blocksize = curDim[0]*curDim[1]*curDim[2]*nbComponents*scalarSize;
181           sourcePointer = (char*)bbGetInputIn()[index_image]->GetScalarPointer();
182               memcpy(pImage, sourcePointer,blocksize);
183           pImage = pImage+blocksize;
184
185 //EED 2013 oct 29 
186 //EED         for(index_lignes=0; index_lignes<dim[0];index_lignes++)
187 //EED         {
188 //EED            for(index_colonnes=0; index_colonnes<dim[1];index_colonnes++)
189 //EED            {
190 //EED               for(index_components=0; index_components<nbComponents;index_components++)
191 //EED               { 
192 //EED                 mConcat->SetScalarComponentFromDouble(index_lignes, index_colonnes, index_image, index_components,(bbGetInputIn()[index_image])->GetScalarComponentAsDouble(index_colonnes,index_lignes,0,index_components));
193 //EED               } // components
194 //EED            } // colonnes
195 //EED         } // lignes
196
197
198            } // if CheckConsistency
199         } /// for index_image
200
201    // Devrait etre dans bbUserFinalizeProcessing() ? On n'y entre jamais // JPR 
202    bbSetOutputOut(mConcat);     
203 }                  
204
205 //---------------------------------------------------
206 bool ConcatImages::CheckConsistency( vtkImageData *curImage, int dim[], int nbComponents, int scalarType )
207 {
208    assert (curImage);
209    
210    if (scalarType != curImage->GetScalarType( ) )
211    {
212       return false;
213    }      
214
215    if (nbComponents != curImage->GetNumberOfScalarComponents() )
216    {
217       return false;
218    }
219      
220    int curDim[3];
221    curImage->GetDimensions(curDim);
222    if ( (curDim[0] =! dim[0]) || (curDim[1] =! dim[1]) )
223    {
224       return false;
225    }
226
227    return true;   
228 }
229
230
231 }//namespace bbtk
232
233 #endif // _USE_VTK_