1
//---------------------------------------------------------------------------
3
// Project: OpenWalnut ( http://www.openwalnut.org )
5
// Copyright 2009 OpenWalnut Community, BSV-Leipzig and CNCF-CBS
6
// For more information see http://www.openwalnut.org/copying
8
// This file is part of OpenWalnut.
10
// OpenWalnut is free software: you can redistribute it and/or modify
11
// it under the terms of the GNU Lesser General Public License as published by
12
// the Free Software Foundation, either version 3 of the License, or
13
// (at your option) any later version.
15
// OpenWalnut is distributed in the hope that it will be useful,
16
// but WITHOUT ANY WARRANTY; without even the implied warranty of
17
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
// GNU Lesser General Public License for more details.
20
// You should have received a copy of the GNU Lesser General Public License
21
// along with OpenWalnut. If not, see <http://www.gnu.org/licenses/>.
23
//---------------------------------------------------------------------------
30
#include "core/kernel/WKernel.h"
31
#include "core/dataHandler/WDataHandler.h"
32
#include "core/dataHandler/WDataTexture3D.h"
33
#include "core/graphicsEngine/WGEColormapping.h"
34
#include "core/common/WPropertyHelper.h"
36
#include "WMImageExtractor.xpm"
37
#include "WMImageExtractor.h"
39
// This line is needed by the module loader to actually find your module.
40
W_LOADABLE_MODULE( WMImageExtractor )
42
WMImageExtractor::WMImageExtractor():
47
WMImageExtractor::~WMImageExtractor()
51
boost::shared_ptr< WModule > WMImageExtractor::factory() const
53
return boost::shared_ptr< WModule >( new WMImageExtractor() );
56
const std::string WMImageExtractor::getName() const
58
return "Image Extractor";
61
const std::string WMImageExtractor::getDescription() const
63
return "This module allows extracting of single images from a dataset.";
66
const char** WMImageExtractor::getXPMIcon() const
68
return imageExtractor_xpm;
71
void WMImageExtractor::connectors()
73
m_input = boost::shared_ptr< WModuleInputData < WDataSetSingle > >(
74
new WModuleInputData< WDataSetSingle >( shared_from_this(), "in", "The input dataset." ) );
75
addConnector( m_input );
77
m_output = boost::shared_ptr< WModuleOutputData < WDataSetScalar > >(
78
new WModuleOutputData< WDataSetScalar >( shared_from_this(), "out", "The extracted image." ) );
79
addConnector( m_output );
81
WModule::connectors();
84
void WMImageExtractor::properties()
86
m_propCondition = boost::shared_ptr< WCondition >( new WCondition() );
88
m_selectedImage = m_properties->addProperty( "Image", "The image to be extracted.", 0, m_propCondition );
89
m_selectedImage->setMin( 0 );
90
m_selectedImage->setMax( 0 );
92
m_minValuePct = m_properties->addProperty( "Min %", "The data value percent that maps to 0.0 in the texture.", 0.0, m_propCondition );
93
m_minValuePct->setMin( 0.0 );
94
m_minValuePct->setMax( 100.0 );
96
m_maxValuePct = m_properties->addProperty( "Max %", "The data value percent that maps to 1.0 in the texture.", 100.0, m_propCondition );
97
m_maxValuePct->setMin( 0.0 );
98
m_maxValuePct->setMax( 100.0 );
100
WModule::properties();
103
void WMImageExtractor::activate()
107
m_outData->getTexture()->active()->set( m_active->get() );
112
void WMImageExtractor::moduleMain()
114
m_moduleState.setResetable( true, true );
115
m_moduleState.add( m_input->getDataChangedCondition() );
116
m_moduleState.add( m_propCondition );
120
while( !m_shutdownFlag() )
122
m_moduleState.wait();
124
if( m_shutdownFlag() )
129
boost::shared_ptr< WDataSetSingle > newDataSet = m_input->getData();
130
bool dataChanged = ( m_dataSet != newDataSet );
131
bool dataValid = ( newDataSet );
135
if( dataChanged || m_selectedImage->changed() || m_minValuePct->changed() || m_maxValuePct->changed() )
137
m_dataSet = newDataSet;
138
WAssert( m_dataSet, "" );
139
WAssert( m_dataSet->getValueSet(), "" );
143
m_selectedImage->setMax( m_dataSet->getValueSet()->dimension() - 1 );
144
m_selectedImage->ensureValidity( 0 );
147
// remove the old dataset's properties and de-register from colormapper
150
m_properties->removeProperty( m_outData->getTexture()->getProperties() );
151
m_infoProperties->removeProperty( m_outData->getTexture()->getInformationProperties() );
154
std::size_t i = static_cast< std::size_t >( m_selectedImage->get( true ) );
156
boost::shared_ptr< WDataSetScalar > oldOut = m_outData;
157
m_outData = extract( i );
162
m_outData->setFilename( makeImageName( i ) );
163
// provide the texture's properties as own properties
164
m_properties->addProperty( m_outData->getTexture()->getProperties() );
165
m_infoProperties->addProperty( m_outData->getTexture()->getInformationProperties() );
168
// update colormapper
169
// 1: there was some texture and there is a new texture:
170
if( oldOut && m_outData )
172
// according to WGEColormapper::replaceTexture, an non-existing old texture causes the new one to be inserted at the end.
173
WGEColormapping::replaceTexture( oldOut->getTexture(), m_outData->getTexture(), makeImageName( i ) );
175
// 2: there is no new texture. Remove old one.
178
// no new texture. Remove old one.
179
WGEColormapping::deregisterTexture( oldOut->getTexture() );
181
// 3: there was no texture. Add new one.
184
// no new texture. Remove old one.
185
WGEColormapping::registerTexture( m_outData->getTexture(), makeImageName( i ) );
188
m_output->updateData( m_outData );
198
else // case !dataValid
202
m_properties->removeProperty( m_outData->getTexture()->getProperties() );
203
m_infoProperties->removeProperty( m_outData->getTexture()->getInformationProperties() );
204
WGEColormapping::deregisterTexture( m_outData->getTexture() );
206
m_outData = boost::shared_ptr< WDataSetScalar >();
207
m_output->updateData( m_outData );
211
debugLog() << "Shutting down...";
215
m_properties->removeProperty( m_outData->getTexture()->getProperties() );
216
m_infoProperties->removeProperty( m_outData->getTexture()->getInformationProperties() );
217
WGEColormapping::deregisterTexture( m_outData->getTexture() );
220
debugLog() << "Finished! Good Bye!";
223
boost::shared_ptr< WDataSetScalar > WMImageExtractor::extract( std::size_t i ) const
225
WAssert( m_dataSet, "" );
226
WAssert( m_dataSet->getValueSet(), "" );
227
WAssert( m_dataSet->getGrid(), "" );
229
if( m_dataSet->getValueSet()->order() > 1 || i >= m_dataSet->getValueSet()->dimension() )
231
return boost::shared_ptr< WDataSetScalar >();
234
boost::shared_ptr< WGridRegular3D > grid = boost::shared_dynamic_cast< WGridRegular3D >( m_dataSet->getGrid() );
237
std::size_t dim = m_dataSet->getValueSet()->dimension();
239
// TODO(reichenbach): use the new valueset function mechanism
240
switch( m_dataSet->getValueSet()->getDataType() )
244
boost::shared_ptr< std::vector< float > > values = boost::shared_ptr< std::vector< float > >(
245
new std::vector< float >( m_dataSet->getGrid()->size() ) );
246
boost::shared_ptr< WValueSet< float > > v = boost::shared_dynamic_cast< WValueSet< float > >( m_dataSet->getValueSet() );
248
for( std::size_t k = 0; k < grid->size(); ++k )
250
( *values )[k] = v->rawData()[ dim * k + i ];
253
boost::shared_ptr< WValueSet< float > > vs =
254
boost::shared_ptr< WValueSet< float > >( new WValueSet< float >( 0, 1, values, W_DT_FLOAT ) );
256
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
260
boost::shared_ptr< std::vector< double > > values = boost::shared_ptr< std::vector< double > >(
261
new std::vector< double >( m_dataSet->getGrid()->size() ) );
262
boost::shared_ptr< WValueSet< double > > v = boost::shared_dynamic_cast< WValueSet< double > >( m_dataSet->getValueSet() );
264
for( std::size_t k = 0; k < grid->size(); ++k )
266
( *values )[k] = v->rawData()[ dim * k + i ];
269
boost::shared_ptr< WValueSet< double > > vs =
270
boost::shared_ptr< WValueSet< double > >( new WValueSet< double >( 0, 1, values, W_DT_DOUBLE ) );
272
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
276
boost::shared_ptr< std::vector< uint8_t > > values = boost::shared_ptr< std::vector< uint8_t > >(
277
new std::vector< uint8_t >( m_dataSet->getGrid()->size() ) );
278
boost::shared_ptr< WValueSet< uint8_t > > v = boost::shared_dynamic_cast< WValueSet< uint8_t > >( m_dataSet->getValueSet() );
280
for( std::size_t k = 0; k < grid->size(); ++k )
282
( *values )[k] = v->rawData()[ dim * k + i ];
285
boost::shared_ptr< WValueSet< uint8_t > > vs =
286
boost::shared_ptr< WValueSet< uint8_t > >( new WValueSet< uint8_t >( 0, 1, values, W_DT_UINT8 ) );
288
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
292
boost::shared_ptr< std::vector< uint16_t > > values = boost::shared_ptr< std::vector< uint16_t > >(
293
new std::vector< uint16_t >( m_dataSet->getGrid()->size() ) );
294
boost::shared_ptr< WValueSet< uint16_t > > v = boost::shared_dynamic_cast< WValueSet< uint16_t > >( m_dataSet->getValueSet() );
296
for( std::size_t k = 0; k < grid->size(); ++k )
298
( *values )[k] = v->rawData()[ dim * k + i ];
301
boost::shared_ptr< WValueSet< uint16_t > > vs =
302
boost::shared_ptr< WValueSet< uint16_t > >( new WValueSet< uint16_t >( 0, 1, values, W_DT_UINT16 ) );
304
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
308
boost::shared_ptr< std::vector< uint32_t > > values = boost::shared_ptr< std::vector< uint32_t > >(
309
new std::vector< uint32_t >( m_dataSet->getGrid()->size() ) );
310
boost::shared_ptr< WValueSet< uint32_t > > v = boost::shared_dynamic_cast< WValueSet< uint32_t > >( m_dataSet->getValueSet() );
312
for( std::size_t k = 0; k < grid->size(); ++k )
314
( *values )[k] = v->rawData()[ dim * k + i ];
317
boost::shared_ptr< WValueSet< uint32_t > > vs =
318
boost::shared_ptr< WValueSet< uint32_t > >( new WValueSet< uint32_t >( 0, 1, values, W_DT_UINT32 ) );
320
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
324
boost::shared_ptr< std::vector< int8_t > > values = boost::shared_ptr< std::vector< int8_t > >(
325
new std::vector< int8_t >( m_dataSet->getGrid()->size() ) );
326
boost::shared_ptr< WValueSet< int8_t > > v = boost::shared_dynamic_cast< WValueSet< int8_t > >( m_dataSet->getValueSet() );
328
for( std::size_t k = 0; k < grid->size(); ++k )
330
( *values )[k] = v->rawData()[ dim * k + i ];
333
boost::shared_ptr< WValueSet< int8_t > > vs =
334
boost::shared_ptr< WValueSet< int8_t > >( new WValueSet< int8_t >( 0, 1, values, W_DT_INT8 ) );
336
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
340
boost::shared_ptr< std::vector< int16_t > > values = boost::shared_ptr< std::vector< int16_t > >(
341
new std::vector< int16_t >( m_dataSet->getGrid()->size() ) );
342
boost::shared_ptr< WValueSet< int16_t > > v = boost::shared_dynamic_cast< WValueSet< int16_t > >( m_dataSet->getValueSet() );
344
for( std::size_t k = 0; k < grid->size(); ++k )
346
( *values )[k] = v->rawData()[ dim * k + i ];
349
boost::shared_ptr< WValueSet< int16_t > > vs =
350
boost::shared_ptr< WValueSet< int16_t > >( new WValueSet< int16_t >( 0, 1, values, W_DT_INT16 ) );
352
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
354
case W_DT_SIGNED_INT:
356
boost::shared_ptr< std::vector< int32_t > > values = boost::shared_ptr< std::vector< int32_t > >(
357
new std::vector< int32_t >( m_dataSet->getGrid()->size() ) );
358
boost::shared_ptr< WValueSet< int32_t > > v = boost::shared_dynamic_cast< WValueSet< int32_t > >( m_dataSet->getValueSet() );
360
for( std::size_t k = 0; k < grid->size(); ++k )
362
( *values )[k] = v->rawData()[ dim * k + i ];
365
boost::shared_ptr< WValueSet< int32_t > > vs =
366
boost::shared_ptr< WValueSet< int32_t > >( new WValueSet< int32_t >( 0, 1, values, W_DT_SIGNED_INT ) );
368
return boost::shared_ptr< WDataSetScalar >( new WDataSetScalar( vs, grid ) );
371
warnLog() << "Encountered unsupported data format in ImageExtractor!";
372
return boost::shared_ptr< WDataSetScalar >();
376
const std::string WMImageExtractor::makeImageName( std::size_t i )
379
WAssert( m_dataSet, "" );
380
WAssert( m_dataSet->getValueSet(), "" );
382
s << m_dataSet->getFilename() << " (" << i << " of " << m_dataSet->getValueSet()->dimension() << ")";
386
void WMImageExtractor::setOutputProps()
390
double min = m_outData->getMin();
391
double max = m_outData->getMax();
392
float fmin = static_cast< float >( min + ( max - min ) * m_minValuePct->get( true ) * 0.01 );
393
float fmax = static_cast< float >( min + ( max - min ) * m_maxValuePct->get( true ) * 0.01 );
395
m_outData->getTexture()->minimum()->set( fmin );
396
m_outData->getTexture()->scale()->set( std::max( fmax, fmin + 1.0f ) - fmin );