]> Creatis software - bbtk.git/blob - packages/vtk/src/bbvtkFlip.cxx
Flip (Upside down) box
[bbtk.git] / packages / vtk / src / bbvtkFlip.cxx
1 /*=========================================================================                                                                               
2   Program:   bbtk
3   Module:    $RCSfile: bbvtkFlip.cxx,v $
4   Language:  C++
5   Date:      $Date: 2010/10/06 16:44:04 $
6   Version:   $Revision: 1.1 $
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 "bbvtkFlip.h"
38 #include "bbvtkPackage.h"
39
40 namespace bbvtk
41 {
42    BBTK_ADD_BLACK_BOX_TO_PACKAGE(vtk,Flip)
43    BBTK_BLACK_BOX_IMPLEMENTATION(Flip,bbtk::AtomicBlackBox);
44
45 //---------------------------------------------------------------------
46
47    void Flip::bbUserSetDefaultValues() 
48    { 
49       //std::cout << "-------- entree ds Flip::bbUserSetDefaultValues()\n" << std::endl;
50       
51       bbSetInputIn(NULL); 
52       mImageOut = NULL;
53       bbSetOutputOut(NULL);
54    }
55
56 //---------------------------------------------------------------------
57
58    void Flip::bbUserInitializeProcessing() 
59    {
60        //std::cout << "-------- entree ds Flip::bbUserInitalizeProcessing()\n" << std::endl;
61       //bbUserFinalizeProcessing();
62       mImageOut = vtkImageData::New();  // Alloc depends on  bbGetInputIn().size()  
63    }
64   
65 //---------------------------------------------------------------------
66
67    void Flip::bbUserFinalizeProcessing() 
68    {
69             //std::cout << "-------- entree ds Flip::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, flips it
83  ///  - exports a vtkImageData*  
84  ///  
85  ///
86 void Flip::Process()
87 {
88         vtkImageData* imageIn = bbGetInputIn();
89         vtkImageData * mImageOut = flip(imageIn);
90       
91    // Devrait etre dans bbUserFinalizeProcessing() ? // JPR     
92         bbSetOutputOut(mImageOut);      
93 }
94
95
96 vtkImageData * Flip::flip(vtkImageData *imageIn)
97 {   
98    int inputdims[3];
99 //   int outputdims[3];
100    imageIn->GetDimensions (inputdims);
101    imageIn->Update();
102    int nbScalComp = imageIn->GetNumberOfScalarComponents();
103    int scalarSize = imageIn->GetScalarSize();
104    int lineSize  = inputdims[0]*scalarSize*nbScalComp;     
105    int planeSize = inputdims[1]*lineSize;
106    char *pixelsIn = (char *)imageIn->GetScalarPointer();
107    
108 /*     
109    outputdims[0] = inputdims[1]; 
110    outputdims[1] = inputdims[0];
111    outputdims[2] = inputdims[2];
112  std::cout << " inputdims[0] " << inputdims[0] <<" inputdims[1] " <<inputdims[1] << " inputdims[2] "
113            << inputdims[2] << std::endl;
114 */
115
116  /*        
117     vtkImageData *vtkImageOut;
118     vtkImageOut = vtkImageData::New();
119     vtkImageOut->SetDimensions( outputdims );
120     vtkImageOut->SetExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
121     vtkImageOut->SetWholeExtent(0,outputdims[0]-1,0,outputdims[1]-1,0,outputdims[2]-1);
122     int nbScalComp = imageIn->GetNumberOfScalarComponents();
123     vtkImageOut->SetNumberOfScalarComponents(nbScalComp);
124     vtkImageOut->SetSpacing( imageIn->GetSpacing() );
125     vtkImageOut->SetScalarType(imageIn->GetScalarType()  );
126     vtkImageOut->AllocateScalars();
127     vtkImageOut->Update();    
128     char *pixelsOut = (char *)vtkImageOut->GetScalarPointer();    
129     unsigned short *output =(unsigned short *)(vtkImageOut->GetScalarPointer());
130    */
131    
132    char *temp = (char *)malloc(lineSize);
133    char *line1;
134    char *line2;
135    char *debPlan;     
136    for(int k=0; k<inputdims[2]; k++) {  // iterate on planes
137         debPlan = pixelsIn+k*planeSize;
138        for(int j=0; j<inputdims[1]/2; j++) { // iterates on rows
139           line1 = debPlan+j*lineSize;
140           line2 = debPlan+(inputdims[1]-j)*lineSize;
141           memcpy(temp,  line2, lineSize);
142           memcpy(line2, line1, lineSize);
143           memcpy(line1, temp,  lineSize);              
144        }
145    }
146    
147 // This was to transpose the image ...  
148 /*
149     for(int k=0; k<inputdims[2]; k++) {  // iterate on planes
150        for(int j=0; j<inputdims[1]; j++) { // iterates on rows
151           for(int i=0; i<inputdims[0]; i++) { // iterates on columns
152              for(int l=0; l<nbScalComp; l++) { // iterates on scalar components
153                 vtkImageOut->SetScalarComponentFromDouble(j,i,k,l,imageIn->GetScalarComponentAsDouble(i,j,k,l));
154              }
155           } 
156           
157        }
158     }
159     return  vtkImageOut;
160 */
161    free (temp);
162    return imageIn;        
163 }
164
165 }//namespace bbtk
166
167 #endif // _USE_VTK_