]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkUnMosaic.cxx
#3107 BBTK Bug New Normal - branch vtk7itk4 compilation with vtk7
[bbtk.git] / packages / vtk / src / bbvtkUnMosaic.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: bbvtkUnMosaic.cxx,v $
31   Language:  C++
32   Date:      $Date: 2012/11/16 08:51:58 $
33   Version:   $Revision: 1.4 $
34 =========================================================================*/
35
36 /**
37  *  \file 
38  *  \brief 
39  */
40
41 #ifdef _USE_VTK_
42 #include "bbvtkUnMosaic.h"
43 #include "bbvtkPackage.h"
44
45 namespace bbvtk
46 {
47    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,UnMosaic)
48    BBTK_BLACK_BOX_IMPLEMENTATION(UnMosaic,bbtk::AtomicBlackBox);
49
50 //---------------------------------------------------------------------
51
52    void UnMosaic::bbUserSetDefaultValues() 
53    { 
54       //std::cout << "-------- entree ds UnMosaic::bbUserSetDefaultValues()\n" << std::endl;
55       
56    // bbSetInputIn(NULL);  /// \TODO fix it! IN is a std::vector<vtkImageData> // JPR
57       mImageOut = NULL;
58       bbSetOutputOut(NULL);
59    }
60
61 //---------------------------------------------------------------------
62
63    void UnMosaic::bbUserInitializeProcessing() 
64    {
65        //std::cout << "-------- entree ds UnMosaic::bbUserInitalizeProcessing()\n" << std::endl;
66       //bbUserFinalizeProcessing();
67       mImageOut = vtkImageData::New();  // Alloc depends on  bbGetInputIn().size()  
68    }
69   
70 //---------------------------------------------------------------------
71
72    void UnMosaic::bbUserFinalizeProcessing() 
73    {
74             //std::cout << "-------- entree ds UnMosaic::bbUserFinalizeProcessing()\n" << std::endl;
75    // WTF? we never enter here // JPR  bbUserFinalizeProcessing()  JPR  
76       if (mImageOut!=NULL)
77       {
78         // mImageOut->Delete();
79         // mImageOut=NULL;
80       }
81       bbSetOutputOut(mImageOut);          
82    }
83
84 //---------------------------------------------------------------------
85  
86  ///  :
87  ///  - receives a vtkImageData*imageIn, int nbImagesPerRow, int numberOfImagesInMosaic 
88  ///  - exports a vtkImageData* as a3D image
89  ///  - supposes the input image is a 2D Siemens Mosaic
90  ///
91    void UnMosaic::Process()
92    {
93         int nbImagesPerRow =   (unsigned int)bbGetInputNbImagesPerRow();
94         int nbImagesInMosaic = (unsigned int)bbGetInputNbImagesInMosaic();      
95
96         if (nbImagesPerRow == 0 || nbImagesInMosaic == 0) {
97            std::cout << "Go away!" << std::endl; // ??? JPR
98            // exit(0);
99         }
100         
101         vtkImageData* imageIn = bbGetInputIn();
102         
103         vtkImageData * mImageOut = unMosaic(imageIn, nbImagesPerRow, nbImagesInMosaic);
104       
105    // Devrait etre dans bbUserFinalizeProcessing() ? // JPR     
106         bbSetOutputOut(mImageOut);      
107    }
108
109
110 vtkImageData * UnMosaic::unMosaic(vtkImageData *imageIn, int nbImagesPerRow, int numberOfImagesInMosaic)
111 {   
112    int inputdims[3];
113    int outputdims[3];
114    imageIn->GetDimensions (inputdims);
115    unsigned short *input = (unsigned short *)(imageIn->GetScalarPointer());
116
117 //EED 2017-01-01 Migration VTK7
118 #if VTK_MAJOR_VERSION <= 5
119    imageIn->Update();
120 #else
121         // .. 
122 #endif
123      
124    unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
125    outputdims[0] = inputdims[0] / div;
126    outputdims[1] = inputdims[1] / div;
127    outputdims[2] = numberOfImagesInMosaic;
128  
129     vtkImageData *vtkImageOut;
130     vtkImageOut = vtkImageData::New();
131     vtkImageOut->SetDimensions( outputdims );
132     vtkImageOut->SetExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
133  
134 //EED 2017-01-01 Migration VTK7
135 #if VTK_MAJOR_VERSION <= 5
136    vtkImageOut->SetWholeExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
137     vtkImageOut->SetNumberOfScalarComponents(1);
138 //vtkImageOut->SetSpacing( blabla );
139     vtkImageOut->SetScalarType( VTK_UNSIGNED_SHORT );
140     vtkImageOut->AllocateScalars();
141     vtkImageOut->Update();
142 #else
143     vtkImageOut->AllocateScalars(VTK_UNSIGNED_SHORT,1);
144 #endif
145
146
147
148     
149     unsigned short *output =(unsigned short *)(vtkImageOut->GetScalarPointer());
150
151     unsigned short *dest = output;
152     int dimXImageElem = outputdims[0];
153     int dimYImageElem = outputdims[1];
154     int lgrImage = dimXImageElem*dimYImageElem;
155     int debImage;
156     for (int i=0; i<numberOfImagesInMosaic; i++)
157     {
158        debImage=(i/nbImagesPerRow) * lgrImage*nbImagesPerRow + (i%nbImagesPerRow)*dimXImageElem;
159        for(int j=0; j<dimYImageElem; j++)
160        {
161           memcpy(dest, input+debImage, dimXImageElem*sizeof(unsigned short));
162           debImage += dimXImageElem*nbImagesPerRow;
163           dest += dimXImageElem;
164        }
165     }
166     return  vtkImageOut;    
167 }
168
169 }//namespace bbtk
170
171 #endif // _USE_VTK_