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