• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

load.hh

00001 // Copyright (C) 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_IO_DICOM_LOAD_HH
00027 # define MLN_IO_DICOM_LOAD_HH
00028 
00031 
00032 # include <mln/core/image/image2d.hh>
00033 # include <mln/core/image/image3d.hh>
00034 
00035 # include <mln/algebra/vec.hh>
00036 
00037 # include <gdcm-2.0/gdcmReader.h>
00038 # include <gdcm-2.0/gdcmImageReader.h>
00039 # include <gdcm-2.0/gdcmWriter.h>
00040 # include <gdcm-2.0/gdcmDataSet.h>
00041 # include <gdcm-2.0/gdcmAttribute.h>
00042 
00043 
00044 namespace mln
00045 {
00046 
00047   namespace io
00048   {
00049 
00050     namespace dicom
00051     {
00052 
00058       template <typename I>
00059       void load(Image<I>& ima,
00060                 const std::string& filename);
00061 
00062 
00063 # ifndef MLN_INCLUDE_ONLY
00064 
00065       template <typename V>
00066       inline
00067       image2d<V> load(const std::string& filename)
00068       {
00069         trace::entering("mln::io::gdcm::load");
00070         image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
00071         trace::exiting("mln::io::gdcm::load");
00072         return ima;
00073       }
00074 
00075       template <typename V>
00076       inline
00077       image3d<V> load(const std::string& filename)
00078       {
00079         trace::entering("mln::io::gdcm::load");
00080         image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
00081         trace::exiting("mln::io::gdcm::load");
00082         return ima;
00083       }
00084 
00085 
00086       template <typename I>
00087       inline
00088       void load(Image<I>& ima_,
00089                 const std::string& filename)
00090       {
00091         trace::entering("mln::io::dicom::load");
00092 
00093         I& ima = exact(ima_);
00094 
00095         gdcm::ImageReader r;
00096         r.SetFileName(filename.c_str());
00097         if (!r.Read())
00098         {
00099           std::cerr << "error: cannot open file '" << filename << "'!";
00100           abort();
00101         }
00102 
00103         gdcm::File &file = r.GetFile();
00104         gdcm::DataSet& ds = file.GetDataSet();
00105 
00106         gdcm::Image& image = r.GetImage();
00107 
00108         char* dataBuffer = new char[image.GetBufferLength()];
00109         image.GetBuffer(dataBuffer);
00110 
00111         int ndims = image.GetNumberOfDimensions();
00112         const unsigned int* dims = image.GetDimensions();
00113 
00114         unsigned short bits_allocated = image.GetPixelFormat().GetBitsAllocated();
00115         unsigned short bytes_allocated = bits_allocated / 8;
00116         unsigned short bits_stored = image.GetPixelFormat().GetBitsStored();
00117         unsigned short samples_per_pixel = image.GetPixelFormat().GetSamplesPerPixel();
00118 
00119         unsigned int offset = 8 - (bits_allocated - bits_stored);
00120         unsigned int off_pow = 1;
00121         for (int i = 0; i < offset; ++i)
00122         {
00123           off_pow *= 2;
00124         }
00125 
00126         if (mln_site_(I)::dim != ndims)
00127         {
00128           std::cerr << "error: dimension mismatch" << std::endl;
00129           abort();
00130         }
00131 
00132         algebra::vec<mln_site_(I)::dim, unsigned int> vmin;
00133         algebra::vec<mln_site_(I)::dim, unsigned int> vmax;
00134         algebra::vec<mln_site_(I)::dim, unsigned int> vdims;
00135         for (int i = 0; i < ndims; ++i)
00136         {
00137           vmin[i] = 0;
00138           vmax[i] = dims[i] - 1;
00139           if (i == 0)
00140             vdims[i] = 1;
00141           else
00142             vdims[i] = dims[i - 1] * vdims[i - 1];
00143         }
00144 
00145         mln_site(I) pmin(vmin);
00146         mln_site(I) pmax(vmax);
00147         mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
00148         initialize(ima, result);
00149         mln_piter(I) p(ima.domain());
00150         unsigned int index = 0;
00151         for_all(p)
00152         {
00153           index = 0;
00154           for (int i = 0; i < ndims; ++i)
00155           {
00156             index += p.to_site().to_vec()[i] * vdims[i];
00157           }
00158 
00159           ima(p) = (unsigned char) dataBuffer[(index * bytes_allocated) * samples_per_pixel];
00160           // FIXME: RGB support, HighBit if HB == 1
00161           for (int j = 0; j < bytes_allocated; ++j)
00162           {
00163             ima(p) += ((unsigned char) dataBuffer[(index * bytes_allocated + j) * samples_per_pixel]) * 256 * j;
00164           }
00165           /*std::cout << "[ x = " << p.to_site().to_vec()[0]
00166                     << " | y = " << p.to_site().to_vec()[1]
00167                     << " | z = " << p.to_site().to_vec()[2]
00168                     << " ] => " << ima(p)
00169                     << std::endl;*/
00170         }
00171 
00172         delete(dataBuffer);
00173 
00174         trace::exiting("mln::io::dicom::load");
00175       }
00176 
00177 # endif // ! MLN_INCLUDE_ONLY
00178 
00179     } // end of namespace mln::io::dicom
00180 
00181   } // end of namespace mln::io
00182 
00183 } // end of namespace mln
00184 
00185 
00186 #endif // ! MLN_IO_DICOM_LOAD_HH

Generated on Thu Sep 8 2011 18:32:04 for Milena (Olena) by  doxygen 1.7.1