Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_RAW_READ_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_RAW_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #ifdef WIN32
00013 # include <io.h>
00014 #endif
00015
00016 #include <OpenTissue/core/containers/grid/grid.h>
00017
00018 #include <iostream>
00019 #include <iomanip>
00020 #include <sstream>
00021 #include <list>
00022 #include <cstdio>
00023 #include <cmath>
00024 #include <string>
00025 #include <cassert>
00026
00027 namespace OpenTissue
00028 {
00029 namespace grid
00030 {
00031
00040 template <typename grid_type>
00041 inline bool raw_read(std::string const & filename, grid_type & grid)
00042 {
00043 using std::min;
00044 using std::max;
00045
00046 assert(grid.data() || !"raw_read(): invalid data pointer");
00047 assert(grid.size()>0 || !"raw_read(): no space for reading data");
00048 typedef typename grid_type::value_type value_type;
00049
00050 FILE *stream;
00051 if((stream = fopen( filename.c_str(), "rb" )) == NULL ){
00052 std::cerr << "raw_read(): Unable to open file" << filename << std::endl;
00053 return false;
00054 }
00055 fread( grid.data(), sizeof( value_type ), grid.size(), stream );
00056 fclose( stream );
00057
00058
00059 std::cout << "raw_read(): Completed reading grid file: "
00060 << filename
00061 << ", min = "
00062 << static_cast<double>( OpenTissue::grid::min_element(grid) )
00063 << " max = "
00064 << static_cast<double>( OpenTissue::grid::max_element(grid) )
00065 << std::endl;
00066 return true;
00067 }
00068
00072 template <typename grid_type>
00073 inline void raw_read_8bit(std::string const & filename, grid_type & target)
00074 {
00075 typedef OpenTissue::grid::Grid<unsigned char, typename grid_type::math_types > grid_8bit_type;
00076 raw_read( filename, target );
00077 }
00078
00082 template <typename grid_type>
00083 inline void raw_read_8bit_to_16bit(std::string const & filename, grid_type & target)
00084 {
00085 typedef OpenTissue::grid::Grid<unsigned char, typename grid_type::math_types > grid_8bit_type;
00086
00087 grid_8bit_type grid8bit;
00088
00089 grid8bit.create( target.min_coord(), target.max_coord(), target.I(), target.J(), target.K() );
00090 raw_read( filename, grid8bit );
00091
00092 unsigned char* cval = grid8bit.data();
00093 unsigned short* usval = target.data();
00094 for (size_t i=0; i<grid8bit.size(); i++, cval++, usval++)
00095 *usval = static_cast< unsigned short>(*cval)*16;
00096 }
00097
00101 template <typename grid_type>
00102 inline void raw_read_16bit(std::string const & filename, grid_type & target)
00103 {
00104 typedef OpenTissue::grid::Grid<unsigned short, typename grid_type::math_types> grid_16bit_type;
00105 raw_read( filename, target );
00106 }
00107
00108 }
00109 }
00110
00111
00112 #endif