33 #ifndef __INCLUDE_MIST_RAW__ 
   34 #define __INCLUDE_MIST_RAW__ 
   37 #ifndef __INCLUDE_MIST_H__ 
   42 #ifndef __INCLUDE_MIST_COLOR_H__ 
   43 #include "../config/color.h" 
   46 #ifndef __INCLUDE_MIST_ENDIAN__ 
   47 #include "../config/endian.h" 
   60 namespace __raw_controller__
 
   65         template < 
class T, 
class Allocator >
 
   66         static bool resize( array1< T, Allocator > &image, 
typename array1< T, Allocator >::size_type num_elements )
 
   68             typedef typename array1< T, Allocator >::size_type size_type;
 
   70             if( num_elements == 0 )
 
   74             else if( image.size( ) == num_elements )
 
   80                 array1< T, Allocator > tmp( image );
 
   81                 image.resize( num_elements );
 
   82                 for( size_type i = 0 ; i < image.size( ) ; i++ )
 
   84                     image[ i ] = tmp[ i ];
 
   90         template < 
class T, 
class Allocator >
 
   91         static bool resize( array2< T, Allocator > &image, 
typename array2< T, Allocator >::size_type num_elements )
 
   93             typedef typename array2< T, Allocator >::size_type size_type;
 
   95             size_type num_lines = num_elements / image.width( );
 
  100             else if( image.height( ) == num_lines )
 
  106                 array2< T, Allocator > tmp( image );
 
  107                 image.resize( image.width( ), num_lines );
 
  108                 for( size_type i = 0 ; i < image.size( ) ; i++ )
 
  116         template < 
class T, 
class Allocator >
 
  117         static bool resize( array3< T, Allocator > &image, 
typename array3< T, Allocator >::size_type num_elements )
 
  119             typedef typename array3< T, Allocator >::size_type size_type;
 
  121             size_type num_slices = num_elements / ( image.width( ) * image.height( ) );
 
  122             if( num_slices == 0 )
 
  126             else if( image.depth( ) == num_slices )
 
  132                 array3< T, Allocator > tmp( image );
 
  133                 image.resize( image.width( ), image.height( ), num_slices );
 
  134                 for( size_type i = 0 ; i < image.size( ) ; i++ )
 
  136                     image[ i ] = tmp[ i ];
 
  143     struct raw_controller
 
  145         template < 
class Array, 
class Functor, 
class ValueType >
 
  146         static bool read( Array &image,
 
  147                             const std::string &filename,
 
  148                             typename Array::size_type w,
 
  149                             typename Array::size_type h,
 
  150                             typename Array::size_type d,
 
  151                             typename Array::value_type offset,
 
  152                             bool from_little_endian,
 
  153                             typename Array::size_type skip_num_bytes,
 
  158             typedef typename Array::value_type value_type;
 
  159             typedef typename Array::size_type size_type;
 
  163             if( ( fp = gzopen( filename.c_str( ), 
"rb" ) ) == NULL )
 
  165                 std::cerr << 
"Error occured while opening RAW file format in [" << filename << 
"]" << std::endl;
 
  169             image.resize( w, h, d );
 
  171             size_type byte = 
sizeof( ValueType );
 
  172             unsigned char tmparray[ 
sizeof( ValueType ) * 4096 ];
 
  173             byte_array< ValueType > data;
 
  178             unsigned int pre_progress = 0;
 
  179             while( !gzeof( fp ) )
 
  181                 size_type num = gzread( fp, ( 
void * )tmparray, 
sizeof( ValueType ) * 4096 );
 
  183                 if( num <= skip_num_bytes )
 
  185                     skip_num_bytes -= num;
 
  189                 size_type j = skip_num_bytes;
 
  192                 for( ; i < image.size( ) && j < num ; j += byte )
 
  194                     for( size_type l = 0 ; l < byte ; l++ )
 
  196                         data[ l ] = tmparray[ j + l ];
 
  198                     image[ i++ ] = 
static_cast< value_type 
>( 
to_current_endian( data, from_little_endian ).get_value( ) ) + offset;
 
  201                 if( i >= image.size( ) )
 
  208                 unsigned int progress = 
static_cast< unsigned int >( 
static_cast< double >( i + 1 ) / static_cast< double >( image.size( ) ) * 100.0 );
 
  209                 if( progress != pre_progress )
 
  211                     pre_progress = progress;
 
  224             if( !resize_image::resize( image, i ) )
 
  238         template < 
class Array, 
class Functor, 
class ValueType >
 
  239         static bool write( 
const Array &image, 
const std::string &filename, 
typename Array::value_type offset, 
bool to_little_endian, Functor f, ValueType v )
 
  241             typedef typename Array::value_type value_type;
 
  242             typedef typename Array::size_type size_type;
 
  246             if( ( fp = fopen( filename.c_str( ), 
"wb" ) ) == NULL )
 
  248                 std::cerr << 
"Error occured while opening RAW file format in [" << filename << 
"]" << std::endl;
 
  252             size_type w = image.width( );
 
  253             size_type h = image.height( );
 
  254             size_type d = image.depth( );
 
  255             unsigned char tmparray[ 
sizeof( ValueType ) * 4096 ];
 
  256             byte_array< ValueType > data;
 
  260             size_type i = 0, n, l, pre_progress = 0;
 
  261             while( i < image.size( ) )
 
  263                 for( n = 0 ; i < image.size( ) && n < 
sizeof( ValueType ) * 4096 ; i++, n += 
sizeof( ValueType ) )
 
  265                     data.set_value( static_cast< ValueType >( image[ i ] + offset ) );
 
  267                     for( l = 0 ; l < 
sizeof( ValueType ) ; l++ )
 
  269                         tmparray[ n + l ] = data[ l ];
 
  272                 fwrite( tmparray, 1, n, fp );
 
  276                 size_type progress = 
static_cast< size_type 
>( 
static_cast< double >( i ) / static_cast< double >( image.size( ) ) * 100.0 );
 
  277                 if( progress != pre_progress )
 
  279                     pre_progress = progress;
 
  280                     if( !f( static_cast< double >( progress ) ) )
 
  295         template < 
class Array, 
class Functor, 
class ValueType >
 
  296         static bool write_gz( 
const Array &image, 
const std::string &filename, 
typename Array::value_type offset, 
bool to_little_endian, Functor f, ValueType v )
 
  298             typedef typename Array::value_type value_type;
 
  299             typedef typename Array::size_type size_type;
 
  303             if( ( fp = gzopen( filename.c_str( ), 
"wb" ) ) == NULL )
 
  305                 std::cerr << 
"Error occured while opening RAW + GZ file format in [" << filename << 
"]" << std::endl;
 
  309             unsigned char tmparray[ 
sizeof( ValueType ) * 4096 ];
 
  310             byte_array< ValueType > data;
 
  314             size_type i = 0, n, l, pre_progress = 0;
 
  315             while( i < image.size( ) )
 
  317                 for( n = 0 ; i < image.size( ) && n < 
sizeof( ValueType ) * 4096 ; i++, n += 
sizeof( ValueType ) )
 
  319                     data.set_value( static_cast< ValueType >( image[ i ] + offset ) );
 
  321                     for( l = 0 ; l < 
sizeof( ValueType ) ; l++ )
 
  323                         tmparray[ n + l ] = data[ l ];
 
  326                 gzwrite( fp, tmparray, static_cast< unsigned int >( n ) );
 
  330                 size_type progress = 
static_cast< size_type 
>( 
static_cast< double >( i ) / static_cast< double >( image.size( ) ) * 100.0 );
 
  331                 if( progress != pre_progress )
 
  333                     pre_progress = progress;
 
  334                     if( !f( static_cast< double >( progress ) ) )
 
  382 template < 
class T, 
class Allocator, 
class ValueType, 
class Functor >
 
  386     return( __raw_controller__::raw_controller::read( image, filename, w, 1, 1, offset, from_little_endian, 0, callback, __dmy__ ) );
 
  403 template < 
class T, 
class Allocator, 
class ValueType, 
class Functor >
 
  406     return( __raw_controller__::raw_controller::write( image, filename, offset, to_little_endian, callback, __dmy__ ) );
 
  423 template < 
class T, 
class Allocator, 
class ValueType, 
class Functor >
 
  426     return( __raw_controller__::raw_controller::write_gz( image, filename, offset, to_little_endian, callback, __dmy__ ) );
 
  443 template < 
class T, 
class Allocator, 
class Functor >
 
  448     return( 
read_raw( image, filename, w, offset, from_little_endian, v, callback ) );
 
  464 template < 
class T, 
class Allocator, 
class Functor >
 
  468     return( 
write_raw( image, filename, offset, to_little_endian, v, callback ) );
 
  483 template < 
class T, 
class Allocator, 
class Functor >
 
  487     return( 
write_raw_gz( image, filename, offset, to_little_endian, v, callback ) );
 
  502 template < 
class T, 
class Allocator >
 
  519 template < 
class T, 
class Allocator >
 
  536 template < 
class T, 
class Allocator >
 
  561 template < 
class T, 
class Allocator, 
class ValueType, 
class Functor >
 
  566     return( __raw_controller__::raw_controller::read( image, filename, w, 1, 1, offset, from_little_endian, 0, callback, __dmy__ ) );
 
  583 template < 
class T, 
class Allocator, 
class Functor >
 
  588     return( 
read_raw( image, filename, w, x, offset, from_little_endian, v, callback ) );
 
  604 template < 
class T, 
class Allocator >
 
  630 template < 
class T, 
class Allocator, 
class ValueType, 
class Functor >
 
  637     return( __raw_controller__::raw_controller::read( image, filename, w, h, 1, offset, from_little_endian, 0, callback, __dmy__ ) );
 
  656 template < 
class T, 
class Allocator, 
class Functor >
 
  662     return( 
read_raw( image, filename, w, h, x, y, offset, from_little_endian, v, callback ) );
 
  681 template < 
class T, 
class Allocator >
 
  710 template < 
class T, 
class Allocator, 
class ValueType, 
class Functor >
 
  718     return( __raw_controller__::raw_controller::read( image, filename, w, h, d, offset, from_little_endian, 0, callback, __dmy__ ) );
 
  741 template < 
class T, 
class Allocator, 
class Functor >
 
  747     return( 
read_raw( image, filename, w, h, d, x, y, z, offset, from_little_endian, v, callback ) );
 
  768 template < 
class T, 
class Allocator >
 
  773     return( 
read_raw( image, filename, w, h, d, x, y, z, offset, from_little_endian, 
__mist_dmy_callback__( ) ) );
 
  790 #endif // __INCLUDE_MIST_RAW__