Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
dicom/load.hh
1 // Copyright (C) 2009, 2011 EPITA Research and Development Laboratory
2 // (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_IO_DICOM_LOAD_HH
28 # define MLN_IO_DICOM_LOAD_HH
29 
32 
33 # include <mln/core/image/image2d.hh>
34 # include <mln/core/image/image3d.hh>
35 
36 # include <mln/algebra/vec.hh>
37 
38 # include <gdcm-2.0/gdcmReader.h>
39 # include <gdcm-2.0/gdcmImageReader.h>
40 # include <gdcm-2.0/gdcmWriter.h>
41 # include <gdcm-2.0/gdcmDataSet.h>
42 # include <gdcm-2.0/gdcmAttribute.h>
43 
44 
45 namespace mln
46 {
47 
48  namespace io
49  {
50 
51  namespace dicom
52  {
53 
66  template <typename I>
67  void load(Image<I>& ima,
68  const std::string& filename);
69 
70 
71 # ifndef MLN_INCLUDE_ONLY
72 
73  template <typename V>
74  inline
75  image2d<V> load(const std::string& filename)
76  {
77  trace::entering("mln::io::gdcm::load");
78  image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
79  trace::exiting("mln::io::gdcm::load");
80  return ima;
81  }
82 
83  template <typename V>
84  inline
85  image3d<V> load(const std::string& filename)
86  {
87  trace::entering("mln::io::gdcm::load");
88  image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
89  trace::exiting("mln::io::gdcm::load");
90  return ima;
91  }
92 
93 
94  template <typename I>
95  inline
96  void load(Image<I>& ima_,
97  const std::string& filename)
98  {
99  trace::entering("mln::io::dicom::load");
100 
101  I& ima = exact(ima_);
102 
103  gdcm::ImageReader r;
104  r.SetFileName(filename.c_str());
105  if (!r.Read())
106  {
107  std::cerr << "error: cannot open file '" << filename << "'!";
108  abort();
109  }
110 
111  //gdcm::File &file = r.GetFile();
112  //gdcm::DataSet& ds = file.GetDataSet();
113 
114  gdcm::Image& image = r.GetImage();
115 
116  char* dataBuffer = new char[image.GetBufferLength()];
117  image.GetBuffer(dataBuffer);
118 
119  int ndims = image.GetNumberOfDimensions();
120  const unsigned int* dims = image.GetDimensions();
121 
122  unsigned short bits_allocated = image.GetPixelFormat().GetBitsAllocated();
123  unsigned short bytes_allocated = bits_allocated / 8;
124  unsigned short bits_stored = image.GetPixelFormat().GetBitsStored();
125  unsigned short samples_per_pixel = image.GetPixelFormat().GetSamplesPerPixel();
126 
127  unsigned int offset = 8 - (bits_allocated - bits_stored);
128  unsigned int off_pow = 1;
129  for (unsigned int i = 0; i < offset; ++i)
130  {
131  off_pow *= 2;
132  }
133 
134  if (mln_site_(I)::dim != ndims)
135  {
136  std::cerr << "error: dimension mismatch" << std::endl;
137  abort();
138  }
139 
140  algebra::vec<mln_site_(I)::dim, unsigned int> vmin;
141  algebra::vec<mln_site_(I)::dim, unsigned int> vmax;
142  algebra::vec<mln_site_(I)::dim, unsigned int> vdims;
143  int j = ndims - 1;
144  for (int i = 0; i < ndims; ++i, --j)
145  {
146  vmin[i] = 0;
147  vmax[i] = dims[i] - 1;
148  if (i == 0)
149  vdims[j] = 1;
150  else
151  vdims[j] = dims[i - 1] * vdims[j + 1];
152  }
153 
154  if (ndims > 1)
155  {
156  std::swap(vmin[0], vmin[1]);
157  std::swap(vmax[0], vmax[1]);
158  }
159 
160  mln_site(I) pmin(vmin);
161  mln_site(I) pmax(vmax);
162  mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
163  initialize(ima, result);
164  mln_piter(I) p(ima.domain());
165  unsigned int index = 0;
166 
167  for_all(p)
168  {
169  index = 0;
170  for (int i = 0; i < ndims; ++i)
171  {
172  index += p[i] * vdims[i];
173  }
174 
175  mln_value(I) v = (unsigned char) dataBuffer[(index * bytes_allocated) * samples_per_pixel];
176  // FIXME: RGB support, HighBit if HB == 1
177  for (unsigned int j = 0; j < bytes_allocated; ++j)
178  {
179  v += ((unsigned char) dataBuffer[(index * bytes_allocated + j) * samples_per_pixel]) * 256 * j;
180  }
181 
182  ima(p) = v;
183  }
184 
185  delete(dataBuffer);
186 
187  trace::exiting("mln::io::dicom::load");
188  }
189 
190 # endif // ! MLN_INCLUDE_ONLY
191 
192  } // end of namespace mln::io::dicom
193 
194  } // end of namespace mln::io
195 
196 } // end of namespace mln
197 
198 
199 #endif // ! MLN_IO_DICOM_LOAD_HH