• 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 
00065       template <typename I>
00066       void load(Image<I>& ima,
00067                 const std::string& filename);
00068 
00069 
00070 # ifndef MLN_INCLUDE_ONLY
00071 
00072       template <typename V>
00073       inline
00074       image2d<V> load(const std::string& filename)
00075       {
00076         trace::entering("mln::io::gdcm::load");
00077         image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
00078         trace::exiting("mln::io::gdcm::load");
00079         return ima;
00080       }
00081 
00082       template <typename V>
00083       inline
00084       image3d<V> load(const std::string& filename)
00085       {
00086         trace::entering("mln::io::gdcm::load");
00087         image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
00088         trace::exiting("mln::io::gdcm::load");
00089         return ima;
00090       }
00091 
00092 
00093       template <typename I>
00094       inline
00095       void load(Image<I>& ima_,
00096                 const std::string& filename)
00097       {
00098         trace::entering("mln::io::dicom::load");
00099 
00100         I& ima = exact(ima_);
00101 
00102         gdcm::ImageReader r;
00103         r.SetFileName(filename.c_str());
00104         if (!r.Read())
00105         {
00106           std::cerr << "error: cannot open file '" << filename << "'!";
00107           abort();
00108         }
00109 
00110         //gdcm::File &file = r.GetFile();
00111         //gdcm::DataSet& ds = file.GetDataSet();
00112 
00113         gdcm::Image& image = r.GetImage();
00114 
00115         char* dataBuffer = new char[image.GetBufferLength()];
00116         image.GetBuffer(dataBuffer);
00117 
00118         int ndims = image.GetNumberOfDimensions();
00119         const unsigned int* dims = image.GetDimensions();
00120 
00121         unsigned short bits_allocated = image.GetPixelFormat().GetBitsAllocated();
00122         unsigned short bytes_allocated = bits_allocated / 8;
00123         unsigned short bits_stored = image.GetPixelFormat().GetBitsStored();
00124         unsigned short samples_per_pixel = image.GetPixelFormat().GetSamplesPerPixel();
00125 
00126         unsigned int offset = 8 - (bits_allocated - bits_stored);
00127         unsigned int off_pow = 1;
00128         for (unsigned int i = 0; i < offset; ++i)
00129         {
00130           off_pow *= 2;
00131         }
00132 
00133         if (mln_site_(I)::dim != ndims)
00134         {
00135           std::cerr << "error: dimension mismatch" << std::endl;
00136           abort();
00137         }
00138 
00139         algebra::vec<mln_site_(I)::dim, unsigned int> vmin;
00140         algebra::vec<mln_site_(I)::dim, unsigned int> vmax;
00141         algebra::vec<mln_site_(I)::dim, unsigned int> vdims;
00142         for (int i = 0; i < ndims; ++i)
00143         {
00144           vmin[i] = 0;
00145           vmax[i] = dims[i] - 1;
00146           if (i == 0)
00147             vdims[i] = 1;
00148           else
00149             vdims[i] = dims[i - 1] * vdims[i - 1];
00150         }
00151 
00152         mln_site(I) pmin(vmin);
00153         mln_site(I) pmax(vmax);
00154         mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
00155         initialize(ima, result);
00156         mln_piter(I) p(ima.domain());
00157         unsigned int index = 0;
00158         for_all(p)
00159         {
00160           index = 0;
00161           for (int i = 0; i < ndims; ++i)
00162           {
00163             index += p.to_site().to_vec()[i] * vdims[i];
00164           }
00165 
00166           ima(p) = (unsigned char) dataBuffer[(index * bytes_allocated) * samples_per_pixel];
00167           // FIXME: RGB support, HighBit if HB == 1
00168           for (int j = 0; j < bytes_allocated; ++j)
00169           {
00170             ima(p) += ((unsigned char) dataBuffer[(index * bytes_allocated + j) * samples_per_pixel]) * 256 * j;
00171           }
00172           /*std::cout << "[ x = " << p.to_site().to_vec()[0]
00173                     << " | y = " << p.to_site().to_vec()[1]
00174                     << " | z = " << p.to_site().to_vec()[2]
00175                     << " ] => " << ima(p)
00176                     << std::endl;*/
00177         }
00178 
00179         delete(dataBuffer);
00180 
00181         trace::exiting("mln::io::dicom::load");
00182       }
00183 
00184 # endif // ! MLN_INCLUDE_ONLY
00185 
00186     } // end of namespace mln::io::dicom
00187 
00188   } // end of namespace mln::io
00189 
00190 } // end of namespace mln
00191 
00192 
00193 #endif // ! MLN_IO_DICOM_LOAD_HH

Generated on Fri Oct 19 2012 04:15:58 for Milena (Olena) by  doxygen 1.7.1