]> Creatis software - creaVtk.git/blob - lib/creaVtk/creaVtkUnMosaicVectorVtkImageData.cpp
2319 creaVtk Feature New Normal Change in box and lib UnMosaicVectorVtkImageData...
[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 #include <math.h>
30 creaVtkUnMosaicVectorVtkImageData::creaVtkUnMosaicVectorVtkImageData()
31 {
32 }
33
34 creaVtkUnMosaicVectorVtkImageData::~creaVtkUnMosaicVectorVtkImageData()
35 {
36 }
37
38 std::vector<vtkImageData*> creaVtkUnMosaicVectorVtkImageData::unMosaicVectorVtkImageData (std::vector<vtkImageData*> imageInput, std::vector<int> NbImagesInMosaicVector)
39 {
40         std::vector<int> nbImagesPerRow;
41         std::vector<int> nbImagesInMosaic;
42
43         for(int i = 0; i < NbImagesInMosaicVector.size(); i++){
44                 nbImagesInMosaic.push_back(NbImagesInMosaicVector[i]);
45                 nbImagesPerRow.push_back( ceil(sqrt(NbImagesInMosaicVector[i])) );
46         }
47
48         if(imageInput.size() != NbImagesInMosaicVector.size()){
49                 for(int j = NbImagesInMosaicVector.size(); j < imageInput.size(); j++)  {       
50                         nbImagesInMosaic.push_back( NbImagesInMosaicVector[NbImagesInMosaicVector.size()-1] );
51                         nbImagesPerRow.push_back( ceil(sqrt(NbImagesInMosaicVector[NbImagesInMosaicVector.size()-1])) );                
52                 }
53         }
54         
55         if (nbImagesPerRow.size() == 0 || nbImagesInMosaic.size() == 0) 
56         {
57                 std::cout << "VtkUnMosaicVectorVtkImageData ERROR: The number of Images by mosaic is not set " << std::endl; 
58         }
59
60         std::vector<vtkImageData*> imageIn = imageInput;
61
62         std::vector<vtkImageData*> mImageOut;
63         for (int i = 0 ; i < (unsigned int)imageIn.size() ; i++) {
64                         mImageOut.push_back( unMosaic(imageIn[i], nbImagesPerRow[i], nbImagesInMosaic[i]) );
65         }
66
67         return mImageOut;       
68 }
69
70 vtkImageData * creaVtkUnMosaicVectorVtkImageData::unMosaic(vtkImageData *imageIn, int nbImagesPerRow, int numberOfImagesInMosaic)
71 {   
72    int inputdims[3];
73    int outputdims[3];
74    imageIn->GetDimensions (inputdims);
75    unsigned short *input = (unsigned short *)(imageIn->GetScalarPointer());
76    imageIn->Update();
77      
78    unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
79    outputdims[0] = inputdims[0] / div;
80    outputdims[1] = inputdims[1] / div;
81    outputdims[2] = numberOfImagesInMosaic;
82  
83     vtkImageData *vtkImageOut;
84     vtkImageOut = vtkImageData::New();
85     vtkImageOut->SetDimensions( outputdims );
86     vtkImageOut->SetExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
87     vtkImageOut->SetWholeExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
88     vtkImageOut->SetNumberOfScalarComponents(1);
89 //vtkImageOut->SetSpacing( blabla );
90     vtkImageOut->SetScalarType( VTK_UNSIGNED_SHORT );
91     vtkImageOut->AllocateScalars();
92     vtkImageOut->Update();
93     
94     unsigned short *output =(unsigned short *)(vtkImageOut->GetScalarPointer());
95
96     unsigned short *dest = output;
97     int dimXImageElem = outputdims[0];
98     int dimYImageElem = outputdims[1];
99     int lgrImage = dimXImageElem*dimYImageElem;
100     int debImage;
101     int i,j;
102 //    for (i=0; i<numberOfImagesInMosaic; i++)
103       for (i=numberOfImagesInMosaic-1; i>=0; i--)
104     {
105        debImage=(i/nbImagesPerRow) * lgrImage*nbImagesPerRow + (i%nbImagesPerRow)*dimXImageElem;
106        for(j=0; j<dimYImageElem; j++)
107 //       for(int j=dimYImageElem-1; j>=0; j--)
108        {
109           memcpy(dest, input+debImage, dimXImageElem*sizeof(unsigned short));
110           debImage += dimXImageElem*nbImagesPerRow;
111           dest += dimXImageElem;
112        }
113     }
114     return  vtkImageOut;    
115 }
116
117 //---------------------------------------------
118 //Method template
119 //---------------------------------------------
120 /*
121 void creaVtkUnMosaicVectorVtkImageData::FunctionName(int& parameterA)
122 {
123   parameterA = 2 * parameterA;
124   return;
125 }
126 */