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 crop->SetInput( vol->castVtk( ) );
75 //crop->SetSampleRate( getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ), getParameters( )->getVoxelSize( ));
77 float RescalaSlope = getParameters()->getRescaleSlope();
78 float RescalaIntercept = getParameters()->getRescaleIntercept();
82 // RescalaIntercept = 0;
84 vtkImageShiftScale *scale = vtkImageShiftScale::New();
85 scale->SetInput( crop->GetOutput( ) );
86 scale->SetScale(RescalaSlope);
88 scale->SetOutputScalarTypeToShort();
90 vtkImageShiftScale *shift = vtkImageShiftScale::New();
91 shift->SetInput( scale->GetOutput( ) );
93 shift->SetShift(RescalaIntercept);
94 shift->SetOutputScalarTypeToShort();
98 vtkImageThreshold *threshold = vtkImageThreshold::New();
99 threshold->SetInput( shift->GetOutput( ) );
100 threshold->ThresholdByUpper (-10000);
101 threshold->ThresholdByLower (-1);
102 threshold->SetInValue(0);
103 threshold->ReplaceOutOff ();
104 threshold->ReplaceInOn ();
105 threshold->SetOutputScalarTypeToUnsignedShort();
107 vtkImageResample* ir = vtkImageResample::New( );
108 ir->SetInput( threshold->GetOutput( ) );
109 ir->SetDimensionality( 3 );
110 double voxelSize= getParameters( )->getVoxelSize( );
111 ir->SetAxisOutputSpacing( 0, voxelSize );
112 ir->SetAxisOutputSpacing( 1, voxelSize );
113 ir->SetAxisOutputSpacing( 2, voxelSize );
116 // ir->SetAxisMagnificationFactor(0,1/getParameters( )->getVoxelSize( ));
119 ir->InterpolateOn( );
120 // ir->InterpolateOff( );
123 ir->Update( ); //important
124 ir->GetOutput()->GetExtent( ext );
127 * Before we were using kgfoCrop which assume extent start at 0,0,0
129 vtkImageChangeInformation* change = vtkImageChangeInformation::New();
130 change->SetInput( ir->GetOutput() );
131 change->SetExtentTranslation( -ext[0], -ext[2], -ext[4] );
132 change->SetOutputSpacing ( voxelSize , voxelSize , voxelSize );
133 change->Update(); //important
135 _volume = new kVolume( change->GetOutput( ) );
137 _marimagedata->AddImageData( _volume->castVtk() );
139 vol->castVtk()->GetSpacing(spc);
140 _marimagedata->SetSpcOriginal( spc );
141 _marimagedata->SetVoiOriginal(voi);
149 threshold-> Delete();
153 // -------------------------------------------------------------------------
154 void marDynData::reset( )
156 //Efface car _volume est juste un pointeur ver _volume de marGdcmDicom qui
157 //est detruit par le reset correspondant
158 // PS -> #ifndef DXMM
159 // PS -> if( _volume ) delete _volume;
164 // -------------------------------------------------------------------------
165 void marDynData::copyFrom( const marObject& from )
168 _volume = new kVolume( *( ( ( marDynData& )from )._volume ) );
171 // -------------------------------------------------------------------------
172 bool marDynData::save( std::ofstream& os )
174 void* data = _volume->getData1D( );
176 ulong dSize = _volume->getRawSizeInBytes( ); // How many bytes for a 'ulong'? // JPR
178 kVolume::Type type = _volume->getType( );
180 dims[ 0 ] = _volume->getXdim( );
181 dims[ 1 ] = _volume->getYdim( );
182 dims[ 2 ] = _volume->getZdim( );
183 sizes[ 0 ] = _volume->getXsize( );
184 sizes[ 1 ] = _volume->getYsize( );
185 sizes[ 2 ] = _volume->getZsize( );
187 os.write( ( const char* )dims, 3 * sizeof( uint32_t ) );
188 os.write( ( const char* )sizes, 3 * sizeof( double ) );
189 os.write( ( const char* )&type, sizeof( kVolume::Type ) );
190 os.write( ( const char* )&dSize, sizeof( ulong ) );
191 os.write( ( const char* )data, dSize );
196 // -------------------------------------------------------------------------
197 bool marDynData::load( std::ifstream& is )
207 is.read( ( char* )dims, 3 * sizeof( uint32_t ) );
208 is.read( ( char* )sizes, 3 * sizeof( double ) );
209 is.read( ( char* )&type, sizeof( kVolume::Type ) );
210 is.read( ( char* )&dSize, sizeof( ulong ) );
211 data = new uint8_t[ dSize ];
212 is.read( ( char* )data, dSize );
214 _volume = new kVolume( type, dims, sizes, ( void* )data );
222 // ----------------------------------------------------------------------------------------------
223 marImageData *marDynData::GetMarImageData()
225 return _marimagedata;
227 // ----------------------------------------------------------------------------------------------