Go to the documentation of this file.00001 #ifndef OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_IMAGE_READ_H
00002 #define OPENTISSUE_CORE_CONTAINERS_GRID_IO_GRID_IMAGE_READ_H
00003
00004
00005
00006
00007
00008
00009
00010 #include <OpenTissue/configuration.h>
00011
00012 #include <OpenTissue/gpu/image/image.h>
00013 #include <OpenTissue/gpu/image/io/image_read.h>
00014
00015 #include <iostream>
00016 #include <list>
00017 #include <cstdio>
00018 #include <cmath>
00019 #include <string>
00020
00021 #ifdef WIN32
00022 # include <io.h>
00023 #endif
00024
00025 namespace OpenTissue
00026 {
00027 namespace grid
00028 {
00045 template <typename grid_type>
00046 inline bool image_read(std::string const & directory, std::string const & filter, grid_type & grid)
00047 {
00048 typedef typename grid_type::value_type value_type;
00049 typedef typename grid_type::math_types math_types;
00050 typedef typename math_types::vector3_type vector3_type;
00051 typedef typename math_types::real_type real_type;
00052
00053 #ifdef WIN32
00054 std::string filter = directory + '/' + filetype;
00055
00056 std::list<std::string> filenames;
00057
00058 struct _finddata_t file;
00059 long hFile;
00060
00061
00062 if( (hFile = _findfirst( filter.c_str() , &file )) == -1L )
00063 {
00064 std::cerr << "-- Invalid filter " << filter << " No such files in that directory!" << std::endl;
00065 _findclose( hFile );
00066 return false;
00067 }
00068 else
00069 {
00070 size_t rdonly = file.attrib & _A_RDONLY;
00071 size_t system = file.attrib & _A_SYSTEM;
00072 size_t hidden = file.attrib & _A_HIDDEN;
00073 size_t archiv = file.attrib & _A_ARCH;
00074 std::string filename = file.name;
00075 filenames.push_back(filename);
00076
00077
00078 while( _findnext( hFile, &file ) == 0 )
00079 {
00080 rdonly = file.attrib & _A_RDONLY;
00081 system = file.attrib & _A_SYSTEM;
00082 hidden = file.attrib & _A_HIDDEN;
00083 archiv = file.attrib & _A_ARCH;
00084 filename = file.name;
00085 filenames.push_back(filename);
00086 }
00087 _findclose( hFile );
00088 }
00089
00090 filenames.sort();
00091 int K = (int)filenames.size();
00092 std::string image_filename = directory + '/' + (*filenames.begin());
00093
00094 image_type image;
00095 bool success = OpenTissue::image::read(image_filename,image);
00096 if(!success)
00097 {
00098 std::cout << "-- Could not read " << dibname1 << std::endl;
00099 }
00100 int I = image.width();
00101 int J = image.height();
00102
00103 vector3_type min_coord(-I/2.0,-J/2.,-K/2.);
00104 vector3_type max_coord(I/2.,J/2.,K/2.);
00105 grid.create(min_coord,max_coord,I,J,K);
00106
00107 int k=K-1;
00108 std::list<string>::iterator it = filenames.begin();
00109 for(;it!=filenames.end();++it,--k)
00110 {
00111 std::string filename = directory + '/' + (*it);
00112 std::cout << "image_read(): Reading " << filename << std::endl;
00113
00114 success = OpenTissue::image::read(filename,image);
00115 if(!success)
00116 {
00117 std::cout << "image_read(): could not read " << filename << std::endl;
00118 }
00119
00120 for(size_t j=0; j<grid.J(); ++j)
00121 {
00122 for(size_t i=0; i<grid.I(); ++i)
00123 {
00124 unsigned char red = image.get(i, j,0);
00125 unsigned char green = image.get(i, j,1);
00126 unsigned char blue = image.get(i, j,2);
00127 unsigned char alpha = image.get(i, j,3);
00128 value_type grid_value = static_cast<value_type>( red );
00129 long idx = (k*grid.I()*grid.J()) + (j*grid.I()) + i;
00130 grid(idx) = grid_value;
00131 }
00132 }
00133
00134 }
00135 std::cout << "image_read(): Read map of size (" << I << "x" << J << "x" << K << ")" << std::endl;
00136 #else
00137 std::cout<< "image_read(): Sorry windows only!" << std::endl;
00138 #endif // WIN32
00139 return true;
00140 }
00141
00142 }
00143 }
00144
00145
00146 #endif