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