]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkUnMosaicVectorVtkImageData.cpp
#3110 creaVtk Bug New Normal - branch vtk7itk4 compilation with vtk7
[creaVtk.git] / lib / creaVtk / creaVtkUnMosaicVectorVtkImageData.cpp
1 /*
2 # ---------------------------------------------------------------------
3 #
4 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 #                        pour la Sante)
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 #include "creaVtkUnMosaicVectorVtkImageData.h"
29
30 #define _USE_MATH_DEFINES // for C
31 #include <math.h>
32
33 creaVtkUnMosaicVectorVtkImageData::creaVtkUnMosaicVectorVtkImageData()
34 {
35 }
36
37 creaVtkUnMosaicVectorVtkImageData::~creaVtkUnMosaicVectorVtkImageData()
38 {
39 }
40
41 std::vector<vtkImageData*> creaVtkUnMosaicVectorVtkImageData::unMosaicVectorVtkImageData (std::vector<vtkImageData*> imageInput, std::vector<int> NbImagesInMosaicVector)
42 {
43         std::vector<int> nbImagesPerRow;
44         std::vector<int> nbImagesInMosaic;
45         double imagesInMosaic;
46         for(int i = 0; i < NbImagesInMosaicVector.size(); i++)
47         {
48                 imagesInMosaic = NbImagesInMosaicVector[i];
49                 nbImagesPerRow.push_back( ceil(sqrt(imagesInMosaic)) );
50                 nbImagesInMosaic.push_back(NbImagesInMosaicVector[i]);
51         } // for i
52         if(imageInput.size() != NbImagesInMosaicVector.size())
53         {
54                 for(int j = NbImagesInMosaicVector.size(); j < imageInput.size(); j++)  
55                 {
56                         imagesInMosaic = NbImagesInMosaicVector[NbImagesInMosaicVector.size()-1];
57                         nbImagesPerRow.push_back( ceil(sqrt(imagesInMosaic)) );         
58                         nbImagesInMosaic.push_back( NbImagesInMosaicVector[NbImagesInMosaicVector.size()-1] );
59                 } // for j
60         } // for imageInput size
61         if (nbImagesPerRow.size() == 0 || nbImagesInMosaic.size() == 0) 
62     {
63                 std::cout << "VtkUnMosaicVectorVtkImageData ERROR: The number of Images by mosaic is not set " << std::endl; 
64         } // if nbImagesPerRow size
65         std::vector<vtkImageData*> imageIn = imageInput;
66         std::vector<vtkImageData*> mImageOut;
67         for (int i = 0 ; i < (unsigned int)imageIn.size() ; i++) 
68         {
69                 mImageOut.push_back( unMosaic(imageIn[i], nbImagesPerRow[i], nbImagesInMosaic[i]) );
70         } // for i
71         return mImageOut;       
72 }
73
74 //------------------------------------------------------------------------------
75 vtkImageData * creaVtkUnMosaicVectorVtkImageData::unMosaic(vtkImageData *imageIn, int nbImagesPerRow, int numberOfImagesInMosaic)
76 {   
77         int inputdims[3];
78         int outputdims[3];
79         imageIn->GetDimensions (inputdims);
80         unsigned short *input = (unsigned short *)(imageIn->GetScalarPointer());
81 //EED 2017-01-01 Migration VTK7
82 #if VTK_MAJOR_VERSION <= 5
83         imageIn->Update();
84 #else
85         // ..
86 #endif 
87         unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
88         outputdims[0] = inputdims[0] / div;
89         outputdims[1] = inputdims[1] / div;
90         outputdims[2] = numberOfImagesInMosaic;
91  
92     vtkImageData *vtkImageOut;
93     vtkImageOut = vtkImageData::New();
94     vtkImageOut->SetDimensions( outputdims );
95     vtkImageOut->SetExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
96     
97 //EED 2017-01-01 Migration VTK7
98 #if VTK_MAJOR_VERSION <= 5
99     vtkImageOut->SetWholeExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
100     vtkImageOut->SetNumberOfScalarComponents(1);
101 //vtkImageOut->SetSpacing( blabla );
102     vtkImageOut->SetScalarType( VTK_UNSIGNED_SHORT );
103     vtkImageOut->AllocateScalars();
104     vtkImageOut->Update();
105 #else
106     vtkImageOut->AllocateScalars(VTK_UNSIGNED_SHORT,1);
107 #endif
108     unsigned short *output =(unsigned short *)(vtkImageOut->GetScalarPointer());
109     unsigned short *dest = output;
110     int dimXImageElem = outputdims[0];
111     int dimYImageElem = outputdims[1];
112     int lgrImage = dimXImageElem*dimYImageElem;
113     int debImage;
114     int i,j;
115 //    for (i=0; i<numberOfImagesInMosaic; i++)
116       for (i=numberOfImagesInMosaic-1; i>=0; i--)
117     {
118        debImage=(i/nbImagesPerRow) * lgrImage*nbImagesPerRow + (i%nbImagesPerRow)*dimXImageElem;
119        for(j=0; j<dimYImageElem; j++)
120 //       for(int j=dimYImageElem-1; j>=0; j--)
121        {
122           memcpy(dest, input+debImage, dimXImageElem*sizeof(unsigned short));
123           debImage += dimXImageElem*nbImagesPerRow;
124           dest += dimXImageElem;
125        } // for j
126     } // for i
127     return  vtkImageOut;    
128 }
129