1 /*# ---------------------------------------------------------------------
3 # Copyright (c) CREATIS (Centre de Recherche en Acquisition et Traitement de l'Image
5 # Authors : Eduardo Davila, Frederic Cervenansky, Claire Mouton
6 # Previous Authors : Laurent Guigues, Jean-Pierre Roux
7 # CreaTools website : www.creatis.insa-lyon.fr/site/fr/creatools_accueil
9 # This software is governed by the CeCILL-B license under French law and
10 # abiding by the rules of distribution of free software. You can use,
11 # modify and/ or redistribute the software under the terms of the CeCILL-B
12 # license as circulated by CEA, CNRS and INRIA at the following URL
13 # http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
14 # or in the file LICENSE.txt.
16 # As a counterpart to the access to the source code and rights to copy,
17 # modify and redistribute granted by the license, users are provided only
18 # with a limited warranty and the software's author, the holder of the
19 # economic rights, and the successive licensors have only limited
22 # The fact that you are presently reading this means that you have had
23 # knowledge of the CeCILL-B license and that you accept its terms.
24 # ------------------------------------------------------------------------ */
26 /*=========================================================================
29 Module: $RCSfile: marDynData.cpp,v $
31 Date: $Date: 2012/11/15 14:16:12 $
32 Version: $Revision: 1.4 $
34 Copyright: (c) 2002, 2003
37 This software is distributed WITHOUT ANY WARRANTY; without even
38 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
39 PURPOSE. See the above copyright notice for more information.
41 =========================================================================*/
43 #include <vtkImageResample.h>
44 #include <vtkExtractVOI.h>
45 #include <vtkImageData.h>
46 #include <vtkImageChangeInformation.h>
47 #include <vtkImageShiftScale.h>
48 #include <vtkImageThreshold.h>
49 #include "marDynData.h"
51 // -------------------------------------------------------------------------
52 marDynData::marDynData( marParameters* p ) : marObject( p ), _volume( NULL )
54 _marimagedata = new marImageData(NULL);
57 // -------------------------------------------------------------------------
58 marDynData::~marDynData()
60 // _marimagedata->SetImageData(NULL);
64 // -------------------------------------------------------------------------
66 void marDynData::loadData( kVolume* vol, int* voi )
71 vtkExtractVOI *crop = vtkExtractVOI::New();
72 //EED 2017-01-01 Migration VTK7
73 #if VTK_MAJOR_VERSION <= 5
74 crop->SetInput( vol->castVtk( ) );
76 crop->SetInputData( vol->castVtk( ) );
80 //crop->SetSampleRate( getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ));
82 float RescalaSlope = getParameters()->getRescaleSlope();
83 float RescalaIntercept = getParameters()->getRescaleIntercept();
87 // RescalaIntercept = 0;
89 vtkImageShiftScale *scale = vtkImageShiftScale::New();
90 //EED 2017-01-01 Migration VTK7
91 #if VTK_MAJOR_VERSION <= 5
92 scale->SetInput( crop->GetOutput( ) );
94 scale->SetInputData( crop->GetOutput( ) );
96 scale->SetScale(RescalaSlope);
98 scale->SetOutputScalarTypeToShort();
100 vtkImageShiftScale *shift = vtkImageShiftScale::New();
101 //EED 2017-01-01 Migration VTK7
102 #if VTK_MAJOR_VERSION <= 5
103 shift->SetInput( scale->GetOutput( ) );
105 shift->SetInputData( scale->GetOutput( ) );
108 shift->SetShift(RescalaIntercept);
109 shift->SetOutputScalarTypeToShort();
113 vtkImageThreshold *threshold = vtkImageThreshold::New();
114 //EED 2017-01-01 Migration VTK7
115 #if VTK_MAJOR_VERSION <= 5
116 threshold->SetInput( shift->GetOutput( ) );
118 threshold->SetInputData( shift->GetOutput( ) );
120 threshold->ThresholdByUpper (-10000);
121 threshold->ThresholdByLower (-1);
122 threshold->SetInValue(0);
123 threshold->ReplaceOutOff ();
124 threshold->ReplaceInOn ();
125 threshold->SetOutputScalarTypeToUnsignedShort();
127 vtkImageResample* ir = vtkImageResample::New( );
128 //EED 2017-01-01 Migration VTK7
129 #if VTK_MAJOR_VERSION <= 5
130 ir->SetInput( threshold->GetOutput( ) );
132 ir->SetInputData( threshold->GetOutput( ) );
134 ir->SetDimensionality( 3 );
135 double voxelSize= getParameters( )->getVoxelSize( );
136 ir->SetAxisOutputSpacing( 0, voxelSize );
137 ir->SetAxisOutputSpacing( 1, voxelSize );
138 ir->SetAxisOutputSpacing( 2, voxelSize );
141 // ir->SetAxisMagnificationFactor(0,1/getParameters( )->getVoxelSize( ));
144 ir->InterpolateOn( );
145 // ir->InterpolateOff( );
148 ir->Update( ); //important
149 ir->GetOutput()->GetExtent( ext );
152 * Before we were using kgfoCrop which assume extent start at 0,0,0
154 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
155 //EED 2017-01-01 Migration VTK7
156 #if VTK_MAJOR_VERSION <= 5
157 change->SetInput( ir->GetOutput() );
159 change->SetInputData( ir->GetOutput() );
162 change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
163 change->SetOutputSpacing ( voxelSize , voxelSize , voxelSize );
164 change->Update(); //important
166 _volume = new kVolume( change->GetOutput( ) );
168 _marimagedata->AddImageData( _volume->castVtk() );
170 vol->castVtk()->GetSpacing(spc);
171 _marimagedata->SetSpcOriginal( spc );
172 _marimagedata->SetVoiOriginal(voi);
180 threshold-> Delete();
184 // -------------------------------------------------------------------------
185 void marDynData::reset( )
187 //Efface car _volume est juste un pointeur ver _volume de marGdcmDicom qui
188 //est detruit par le reset correspondant
189 // PS -> #ifndef DXMM
190 // PS -> if( _volume ) delete _volume;
195 // -------------------------------------------------------------------------
196 void marDynData::copyFrom( const marObject& from )
199 _volume = new kVolume( *( ( ( marDynData& )from )._volume ) );
202 // -------------------------------------------------------------------------
203 bool marDynData::save( std::ofstream& os )
205 void* data = _volume->getData1D( );
207 ulong dSize = _volume->getRawSizeInBytes( ); // How many bytes for a 'ulong'? // JPR
209 kVolume::Type type = _volume->getType( );
211 dims[ 0 ] = _volume->getXdim( );
212 dims[ 1 ] = _volume->getYdim( );
213 dims[ 2 ] = _volume->getZdim( );
214 sizes[ 0 ] = _volume->getXsize( );
215 sizes[ 1 ] = _volume->getYsize( );
216 sizes[ 2 ] = _volume->getZsize( );
218 os.write( ( const char* )dims, 3 * sizeof( uint32_t ) );
219 os.write( ( const char* )sizes, 3 * sizeof( double ) );
220 os.write( ( const char* )&type, sizeof( kVolume::Type ) );
221 os.write( ( const char* )&dSize, sizeof( ulong ) );
222 os.write( ( const char* )data, dSize );
227 // -------------------------------------------------------------------------
228 bool marDynData::load( std::ifstream& is )
238 is.read( ( char* )dims, 3 * sizeof( uint32_t ) );
239 is.read( ( char* )sizes, 3 * sizeof( double ) );
240 is.read( ( char* )&type, sizeof( kVolume::Type ) );
241 is.read( ( char* )&dSize, sizeof( ulong ) );
242 data = new uint8_t[ dSize ];
243 is.read( ( char* )data, dSize );
245 _volume = new kVolume( type, dims, sizes, ( void* )data );
253 // ----------------------------------------------------------------------------------------------
254 marImageData *marDynData::GetMarImageData()
256 return _marimagedata;
258 // ----------------------------------------------------------------------------------------------