34 #ifndef CARTODATA_IO_VOLUMEFORMATREADER_D_H
35 #define CARTODATA_IO_VOLUMEFORMATREADER_D_H
61 #define localMsg( message ) cartoCondMsg( 4, message, "VOLUMEFORMATREADER" )
92 template <
typename T>
void
99 localMsg(
"Reading existing object ( " + dsi->url() +
" )" );
100 std::string uri = dsi->list().dataSource()->url();
101 std::string url = FileUtil::uriFilename( uri );
103 if( urioptions.
get() ) {
106 options->copyProperties( urioptions );
115 typename std::set<std::string>::iterator p;
116 typename std::set<std::string>::iterator plast = prop.end();
117 for( p = prop.begin(); p != prop.end(); ++p )
119 if( options->hasProperty( *p ) )
121 localMsg(
"use VolumeUtilIO::read()" );
123 bool convert =
false;
124 options->getProperty(
"convert_to_aims", convert );
137 localMsg(
"Fallback to FormatReader<Volume<T> >::setupAndRead()" );
145 template <
typename T> Volume<T>*
150 localMsg(
"Creating and reading object ( " + dsi->url() +
" )" );
153 std::string uri = dsi->list().dataSource()->url();
154 std::string url = FileUtil::uriFilename( uri );
159 options = options->clone();
160 if( urioptions.
get() ) {
161 options->copyProperties( urioptions );
169 typename std::set<std::string>::iterator p;
170 typename std::set<std::string>::iterator plast = prop.end();
171 Volume<T> *volume = 0;
172 for( p = prop.begin(); p != plast; ++p )
174 if( options->hasProperty( *p ) )
176 localMsg(
"using VolumeUtilIO<T>::read ( " + dsi->url() +
" )" );
184 localMsg(
"using classical Volume reading ( " + dsi->url() +
" )" );
186 dsi, context, options );
190 bool convert =
false;
191 options->getProperty(
"convert_to_aims", convert );
215 template <
typename T>
221 localMsg(
"Reading object ( " + dsi->url() +
" )" );
224 std::string otype = dsi->header()->getProperty(
"object_type" )->getString();
225 std::string dtype = dsi->header()->getProperty(
"data_type" )->getString();
226 std::string ttype = DataTypeCode<T>::dataType();
227 if( otype !=
"Volume" )
228 throw datatype_format_error(
"unsupported data type - " + otype +
" != Volume", dsi->url() );
231 if( dsi->header()->hasProperty(
"possible_data_types" ) )
235 i<dsi->header()->getProperty(
"possible_data_types" )->size();
238 if( dsi->header()->getProperty(
"possible_data_types" )
239 ->getArrayItem( i )->getString()
243 if( i == dsi->header()->getProperty(
"possible_data_types" )->size() )
244 throw datatype_format_error( dsi->url() );
246 throw datatype_format_error(
"unsupported data type - " + dtype +
" != " + ttype, dsi->url() );
250 localMsg(
"checking for memory mapping..." );
254 localMsg(
" -> Memory Mapping : nothing to read." );
259 localMsg(
"checking if object is a view..." );
266 localMsg(
" -> Unallocated Volume : nothing to read." );
269 localMsg( std::string(
" -> object ") + ( parent1 ?
"is" :
"isn't" )
275 localMsg( std::string(
" -> parent exists and ")
281 localMsg( std::string(
" -> grandparent exists and ")
288 std::vector<int> viewsize = obj.
getSize();
296 localMsg(
"reading multiresolution level..." );
298 if( options->hasProperty(
"resolution_level" ) ) {
299 options->getProperty(
"resolution_level", level );
305 level += dsi->header()->getProperty(
"resolutions_dimension" )
315 localMsg(
"reading full volume size..." );
316 std::vector<int> imagesize( 4, 0 );
321 = dsi->header()->getProperty(
"resolutions_dimension" )->getArrayItem(
323 int i, ndim = dim->size();
324 imagesize.resize(
std::max(
size_t( 4 ), dim->size() ), 1 );
325 for( i=0; i<ndim; ++i )
326 imagesize[ i ] = (
int) rint( dim->getArrayItem( i )->getScalar() );
327 localMsg(
" -> found \"resolutions_dimension\"." );
334 carto::Object dim = dsi->header()->getProperty(
"volume_dimension" );
335 int i, ndim = dim->size();
336 imagesize.resize(
std::max(
size_t( 4 ), dim->size() ), 1 );
337 for( i=0; i<ndim; ++i )
338 imagesize[ i ] = (
int) rint( dim->getArrayItem( i )->getScalar() );
339 localMsg(
" -> found \"volume_dimension\"." );
345 imagesize = parent1->
getSize();
346 localMsg(
" -> found unallocated parent." );
347 }
else if( parent2 ) {
348 imagesize = parent2->
getSize();
349 localMsg(
" -> found grandparent." );
351 imagesize = viewsize;
352 localMsg(
" -> full volume is self." );
364 localMsg(
"reading allocated size..." );
365 std::vector<int> allocsize( 4, 0 );
367 allocsize = viewsize;
368 localMsg(
" -> allocated volume is self (full volume)." );
370 allocsize = viewsize;
371 localMsg(
" -> allocated volume is self (partial volume)." );
373 allocsize = parent1->
getSize();
374 localMsg(
" -> allocated volume is parent "
375 "(borders or partially loading in full volume)." );
377 localMsg(
" -> allocated volume size ( "
384 size_t i, ndim = allocsize.size();
385 std::vector<long> strides( ndim );
386 std::vector<int> stride_pos;
387 for( i=0; i<ndim; ++i )
389 stride_pos = std::vector<int>( ndim, 0 );
391 strides[i] = &obj( stride_pos ) - &obj( 0,0,0,0 );
395 localMsg(
"reading view position in reference to full volume..." );
396 std::vector<int> pos ( ndim, 0 );
398 localMsg(
"parent is not allocated" );
400 }
else if( parent2 ) {
408 for(i=0; i<ndim; ++i)
411 viewsize[i] += pos[i];
422 localMsg(
"reading view size in reference to full volume..." );
425 for( i=0; i<ndim; ++i )
426 if (pos[i] + viewsize[i] > imagesize[i])
427 viewsize[i] = (pos[i] < imagesize[i] ? imagesize[i] - pos[i] : 0);
436 bool withborders =
false;
437 bool partialreading =
false;
438 for( i=0; i<ndim; ++i )
440 if( allocsize[i] > viewsize[i] )
442 if( imagesize[i] != viewsize[i] )
443 partialreading =
true;
446 + std::string( ( withborders ?
"yes" :
"no" ) ) );
448 + std::string( ( partialreading ?
"yes" :
"no" ) ) );
456 if( !withborders || dsi->capabilities().canHandleStrides() )
458 localMsg(
"reading volume using strides..." );
460 _imr->read( ( T * ) &obj(0,0,0,0), *dsi, pos,
461 viewsize, strides, options );
465 localMsg(
"reading volume without strides..." );
468 std::vector<int> posline ( pos );
469 std::vector<int> sizeline ( ndim, 1 );
470 std::vector<int> volpos( ndim, 0 );
472 sizeline[ 0 ] = viewsize[ 0 ];
474 bool nextrow =
false, ended =
false;
476 bool was_open = _imr->isOpen( *dsi );
478 if( !was_open && !_imr->open( *dsi ) )
484 for( dim=1; dim<ndim; ++dim )
489 if( volpos[dim] == viewsize[dim] )
491 if( dim == ( ndim - 1 ) )
498 posline[dim] = pos[dim] + volpos[dim];
502 posline[1] = pos[1] + volpos[1];
503 _imr->read( ( T * ) &obj( volpos ), *dsi, posline,
504 sizeline, strides, options );
533 template <
typename T>
544 template <
typename T>
551 template <
typename T>
557 return _imr->formatID();
567 template <
typename T>
578 template <
typename T>
void
585 std::string uri = dsi->list().dataSource()->url();
586 std::string url = FileUtil::uriFilename( uri );
588 if( urioptions.
get() ) {
591 options->copyProperties( urioptions );
595 if( !options.
get() ) {
603 typename std::set<std::string>::iterator p;
604 typename std::set<std::string>::iterator plast = prop.end();
605 for( p = prop.begin(); p != plast; ++p )
607 if( options->hasProperty( *p ) )
612 bool convert =
false;
613 options->getProperty(
"convert_to_aims", convert );
630 template <
typename T> VolumeRef<T>*
637 return new VolumeRef<T>( vrf.
createAndRead( dsi, context, options ) );
648 template <
typename T>
656 vrf.
read( *obj, dsi, context, options );
662 template <
typename T>
669 template <
typename T>
679 template <
typename T>
685 return _imr->formatID();
Object reference(Object &value)
std::vector< int > getSize() const
get the 4 dimensions in a vector
Convenient handle for a Volume - this is normally the entry point for all volumes handling.
const AllocatorContext & allocatorContext() const
returns volume's AllocatorContext
const Position & posInRefVolume() const
Get position in parent volume.
rc_ptr< Volume< T > > refVolume() const
Get parent volume.
GenericObject * get() const
MappingMode allocatorType() const
static std::set< std::string > listReadProperties()
list of properties triggering partial reading and/or borders
static carto::Volume< T > * read(carto::Volume< T > *obj, carto::rc_ptr< DataSourceInfo > dsi, carto::Object options)
Manages all the volumes necessary and returns the final Volume.
std::string toString(const T &object)
T max(const Volume< T > &vol)
Returns the maximum value of the volume.