]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkUnMosaic.cxx
b449f144623ebf411a0d06d133fd4e625e494116
[bbtk.git] / packages / vtk / src / bbvtkUnMosaic.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkUnMosaic.cxx,v $
4   Language:  C++
5   Date:      $Date: 2011/05/31 09:39:11 $
6   Version:   $Revision: 1.3 $
7 =========================================================================*/
8
9 /* ---------------------------------------------------------------------
10
11 * Copyright (c) CREATIS-LRMN (Centre de Recherche en Imagerie Medicale)
12 * Authors : Eduardo Davila, Laurent Guigues, Jean-Pierre Roux
13 *
14 *  This software is governed by the CeCILL-B license under French law and 
15 *  abiding by the rules of distribution of free software. You can  use, 
16 *  modify and/ or redistribute the software under the terms of the CeCILL-B 
17 *  license as circulated by CEA, CNRS and INRIA at the following URL 
18 *  http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html 
19 *  or in the file LICENSE.txt.
20 *
21 *  As a counterpart to the access to the source code and  rights to copy,
22 *  modify and redistribute granted by the license, users are provided only
23 *  with a limited warranty  and the software's author,  the holder of the
24 *  economic rights,  and the successive licensors  have only  limited
25 *  liability. 
26 *
27 *  The fact that you are presently reading this means that you have had
28 *  knowledge of the CeCILL-B license and that you accept its terms.
29 * ------------------------------------------------------------------------ */                                                                         
30
31 /**
32  *  \file 
33  *  \brief 
34  */
35
36 #ifdef _USE_VTK_
37 #include "bbvtkUnMosaic.h"
38 #include "bbvtkPackage.h"
39
40 namespace bbvtk
41 {
42    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,UnMosaic)
43    BBTK_BLACK_BOX_IMPLEMENTATION(UnMosaic,bbtk::AtomicBlackBox);
44
45 //---------------------------------------------------------------------
46
47    void UnMosaic::bbUserSetDefaultValues() 
48    { 
49       //std::cout << "-------- entree ds UnMosaic::bbUserSetDefaultValues()\n" << std::endl;
50       
51    // bbSetInputIn(NULL);  /// \TODO fix it! IN is a std::vector<vtkImageData> // JPR
52       mImageOut = NULL;
53       bbSetOutputOut(NULL);
54    }
55
56 //---------------------------------------------------------------------
57
58    void UnMosaic::bbUserInitializeProcessing() 
59    {
60        //std::cout << "-------- entree ds UnMosaic::bbUserInitalizeProcessing()\n" << std::endl;
61       //bbUserFinalizeProcessing();
62       mImageOut = vtkImageData::New();  // Alloc depends on  bbGetInputIn().size()  
63    }
64   
65 //---------------------------------------------------------------------
66
67    void UnMosaic::bbUserFinalizeProcessing() 
68    {
69             //std::cout << "-------- entree ds UnMosaic::bbUserFinalizeProcessing()\n" << std::endl;
70    // WTF? we never enter here // JPR  bbUserFinalizeProcessing()  JPR  
71       if (mImageOut!=NULL)
72       {
73         // mImageOut->Delete();
74         // mImageOut=NULL;
75       }
76       bbSetOutputOut(mImageOut);          
77    }
78
79 //---------------------------------------------------------------------
80  
81  ///  :
82  ///  - receives a vtkImageData*imageIn, int nbImagesPerRow, int numberOfImagesInMosaic 
83  ///  - exports a vtkImageData* as a3D image
84  ///  - supposes the input image is a 2D Siemens Mosaic
85  ///
86    void UnMosaic::Process()
87    {
88         int nbImagesPerRow =   (unsigned int)bbGetInputNbImagesPerRow();
89         int nbImagesInMosaic = (unsigned int)bbGetInputNbImagesInMosaic();      
90
91         if (nbImagesPerRow == 0 || nbImagesInMosaic == 0) {
92            std::cout << "Go away!" << std::endl; // ??? JPR
93            // exit(0);
94         }
95         
96         vtkImageData* imageIn = bbGetInputIn();
97         
98         vtkImageData * mImageOut = unMosaic(imageIn, nbImagesPerRow, nbImagesInMosaic);
99       
100    // Devrait etre dans bbUserFinalizeProcessing() ? // JPR     
101         bbSetOutputOut(mImageOut);      
102    }
103
104
105 vtkImageData * UnMosaic::unMosaic(vtkImageData *imageIn, int nbImagesPerRow, int numberOfImagesInMosaic)
106 {   
107    int inputdims[3];
108    int outputdims[3];
109    imageIn->GetDimensions (inputdims);
110    unsigned short *input = (unsigned short *)(imageIn->GetScalarPointer());
111    imageIn->Update();
112      
113    unsigned int div = (unsigned int)ceil(sqrt( (double)numberOfImagesInMosaic ) );
114    outputdims[0] = inputdims[0] / div;
115    outputdims[1] = inputdims[1] / div;
116    outputdims[2] = numberOfImagesInMosaic;
117  
118     vtkImageData *vtkImageOut;
119     vtkImageOut = vtkImageData::New();
120     vtkImageOut->SetDimensions( outputdims );
121     vtkImageOut->SetExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
122     vtkImageOut->SetWholeExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
123     vtkImageOut->SetNumberOfScalarComponents(1);
124 //vtkImageOut->SetSpacing( blabla );
125     vtkImageOut->SetScalarType( VTK_UNSIGNED_SHORT );
126     vtkImageOut->AllocateScalars();
127     vtkImageOut->Update();
128     
129     unsigned short *output =(unsigned short *)(vtkImageOut->GetScalarPointer());
130
131     unsigned short *dest = output;
132     int dimXImageElem = outputdims[0];
133     int dimYImageElem = outputdims[1];
134     int lgrImage = dimXImageElem*dimYImageElem;
135     int debImage;
136     for (int i=0; i<numberOfImagesInMosaic; i++)
137     {
138        debImage=(i/nbImagesPerRow) * lgrImage*nbImagesPerRow + (i%nbImagesPerRow)*dimXImageElem;
139        for(int j=0; j<dimYImageElem; j++)
140        {
141           memcpy(dest, input+debImage, dimXImageElem*sizeof(unsigned short));
142           debImage += dimXImageElem*nbImagesPerRow;
143           dest += dimXImageElem;
144        }
145     }
146     return  vtkImageOut;    
147 }
148
149 }//namespace bbtk
150
151 #endif // _USE_VTK_