]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkUnMosaic.cxx
49c617ea587792beed30ad61a878c206874b230e
[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    imageIn->Update();
117      
118    unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
119    outputdims[0] = inputdims[0] / div;
120    outputdims[1] = inputdims[1] / div;
121    outputdims[2] = numberOfImagesInMosaic;
122  
123     vtkImageData *vtkImageOut;
124     vtkImageOut = vtkImageData::New();
125     vtkImageOut->SetDimensions( outputdims );
126     vtkImageOut->SetExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
127     vtkImageOut->SetWholeExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
128     vtkImageOut->SetNumberOfScalarComponents(1);
129 //vtkImageOut->SetSpacing( blabla );
130     vtkImageOut->SetScalarType( VTK_UNSIGNED_SHORT );
131     vtkImageOut->AllocateScalars();
132     vtkImageOut->Update();
133     
134     unsigned short *output =(unsigned short *)(vtkImageOut->GetScalarPointer());
135
136     unsigned short *dest = output;
137     int dimXImageElem = outputdims[0];
138     int dimYImageElem = outputdims[1];
139     int lgrImage = dimXImageElem*dimYImageElem;
140     int debImage;
141     for (int i=0; i<numberOfImagesInMosaic; i++)
142     {
143        debImage=(i/nbImagesPerRow) * lgrImage*nbImagesPerRow + (i%nbImagesPerRow)*dimXImageElem;
144        for(int j=0; j<dimYImageElem; j++)
145        {
146           memcpy(dest, input+debImage, dimXImageElem*sizeof(unsigned short));
147           debImage += dimXImageElem*nbImagesPerRow;
148           dest += dimXImageElem;
149        }
150     }
151     return  vtkImageOut;    
152 }
153
154 }//namespace bbtk
155
156 #endif // _USE_VTK_