]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkConcatImages.cxx
Concat 2D vtkImageData into a single 3D vtkImageData
[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/08 14:39:45 $
6   Version:   $Revision: 1.1 $
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 nb = (unsigned int)bbGetInputIn().size();
91         if (nb == 0) {} // ??? JPR
92         
93         bbGetInputIn()[0]->GetDimensions(dim);
94         if (dim[2] > 1  || nb == 1) // dim[2] > 1 : Hopeless for vtk : the first file contains already a 3D object
95                                     // nb == 1 : only one image, nothing to do.
96         {
97            mConcat->Delete();
98            mConcat = bbGetInputIn()[0];
99            //mConcat->PrintSelf(std::cout, vtkIndent(2));
100         }
101         else
102         {
103            // We suppose *all* the images within the directory are consistent (same sizeS, same pixel Type?...)
104            vtkImageData * firstImage = bbGetInputIn()[0];
105            
106                   //std::cout << "--------PrintSelf firstImage " << std::endl;
107                   // firstImage->PrintSelf(std::cout, vtkIndent(2));       
108            
109            int nbComponents = firstImage->GetNumberOfScalarComponents();
110            mConcat->SetNumberOfScalarComponents(nbComponents);
111            mConcat->SetScalarType(firstImage->GetScalarType( ));        
112            firstImage->GetDimensions(dim);
113            dim[2]=nb;
114            mConcat->SetDimensions(dim);
115            mConcat->SetExtent(0, dim[0]-1, 0, dim[1]-1, 0, dim[2]-1);
116            mConcat->SetSpacing(firstImage->GetSpacing());
117            
118            mConcat->AllocateScalars();
119            mConcat->Update();      
120                      
121            int index_image, index_lignes, index_colonnes, index_components;
122            
123            for(index_image=0; index_image<nb; index_image++)
124            {
125               for(index_lignes=0; index_lignes<dim[0];index_lignes++)
126               {
127                  for(index_colonnes=0; index_colonnes<dim[1];index_colonnes++)
128                  {
129                     for(index_components=0; index_components<nbComponents;index_components++)
130                     { 
131                       mConcat->SetScalarComponentFromDouble(index_lignes, index_colonnes, index_image, index_components,(bbGetInputIn()[index_image])->GetScalarComponentAsDouble(index_colonnes,index_lignes,0,index_components));
132                     }
133                  }
134               }
135            }
136         }
137    // Devrait etre dans bbUserFinalizeProcessing() ? On n'y entre jamais // JPR 
138    bbSetOutputOut(mConcat);     
139    }               
140
141 }//namespace bbtk
142
143 #endif // _USE_VTK_