]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkConcatImages.cxx
1367ec761bf1ad6fa23e6274539ac27d1cfde41c
[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            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            newSizeZ=0;
131            // brute way to perform an ultra-mini consistency check :
132            // First image is supposed to be the reference image,
133            // any unconsistent image is just discarted...
134            for(int i=0; i<nb; i++)
135            {
136
137 //EED 2013 oct 29 
138 //EED         if ( !CheckConsistency(firstImage, dim, nbComponents, scalarType))
139 //EED           consistentNb--;
140
141               if ( CheckConsistency(bbGetInputIn()[i], dim, nbComponents, scalarType))
142                 {
143                         bbGetInputIn()[i]->GetDimensions(curDim);
144                         newSizeZ = newSizeZ + curDim[2];
145                 } // if
146                 
147            }    // for
148
149            dim[2]=newSizeZ;
150
151
152            mConcat->SetDimensions(dim);
153            mConcat->SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
154                    
155            mConcat->AllocateScalars();
156            mConcat->Update();      
157                      
158            int index_image;
159
160            int blocksize;
161            int scalarSize=bbGetInputIn()[0]->GetScalarSize();
162            char *pImage = (char*)mConcat->GetScalarPointer();
163            char *sourcePointer;
164
165 //EED      int index_lignes, index_colonnes, index_components;
166         
167    
168            for(index_image=0; index_image<nb; index_image++)
169            {
170               if (!CheckConsistency(bbGetInputIn()[index_image], dim, nbComponents, scalarType))
171                 {
172                          continue;  // discard unconsistent image !
173                 }
174               bbGetInputIn()[index_image]->GetDimensions(curDim);
175               blocksize = curDim[0]*curDim[1]*curDim[2]*nbComponents*scalarSize;
176           sourcePointer = (char*)bbGetInputIn()[index_image]->GetScalarPointer();
177               memcpy(pImage, sourcePointer,blocksize);
178           pImage = pImage+blocksize;
179
180 //EED 2013 oct 29 
181 //EED         for(index_lignes=0; index_lignes<dim[0];index_lignes++)
182 //EED         {
183 //EED            for(index_colonnes=0; index_colonnes<dim[1];index_colonnes++)
184 //EED            {
185 //EED               for(index_components=0; index_components<nbComponents;index_components++)
186 //EED               { 
187 //EED                 mConcat->SetScalarComponentFromDouble(index_lignes, index_colonnes, index_image, index_components,(bbGetInputIn()[index_image])->GetScalarComponentAsDouble(index_colonnes,index_lignes,0,index_components));
188 //EED               } // components
189 //EED            } // colonnes
190 //EED         } // lignes
191
192
193            } // if CheckConsistency
194         } /// for index_image
195
196    // Devrait etre dans bbUserFinalizeProcessing() ? On n'y entre jamais // JPR 
197    bbSetOutputOut(mConcat);     
198    }               
199
200 bool ConcatImages::CheckConsistency( vtkImageData *curImage, int dim[], int nbComponents, int scalarType )
201 {
202    assert (curImage);
203    
204    if (scalarType != curImage->GetScalarType( ) )
205    {
206       return false;
207    }      
208
209    if (nbComponents != curImage->GetNumberOfScalarComponents() )
210    {
211       return false;
212    }
213      
214    int curDim[3];
215    curImage->GetDimensions(curDim);
216    if ( (curDim[0] =! dim[0]) || (curDim[1] =! dim[1]) )
217    {
218       return false;
219    }
220
221    return true;   
222 }
223
224
225 }//namespace bbtk
226
227 #endif // _USE_VTK_