$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
dicom/load.hh
1 // Copyright (C) 2009, 2011, 2012 EPITA Research and Development
2 // Laboratory (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 
67  template <typename I>
68  void load(Image<I>& ima,
69  const std::string& filename);
70 
71 
72 # ifndef MLN_INCLUDE_ONLY
73 
74  template <typename V>
75  inline
76  image2d<V> load(const std::string& filename)
77  {
78  mln_trace("mln::io::gdcm::load");
79  image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
80  return ima;
81  }
82 
83  template <typename V>
84  inline
85  image3d<V> load(const std::string& filename)
86  {
87  mln_trace("mln::io::gdcm::load");
88  image2d<V> ima;// = io::pnm::load<V>(MAGICK, filename);
89  return ima;
90  }
91 
92 
93  template <typename I>
94  inline
95  void load(Image<I>& ima_,
96  const std::string& filename)
97  {
98  mln_trace("mln::io::dicom::load");
99 
100  I& ima = exact(ima_);
101 
102  gdcm::ImageReader r;
103  r.SetFileName(filename.c_str());
104  if (!r.Read())
105  {
106  std::cerr << "error: cannot open file '" << filename << "'!";
107  abort();
108  }
109 
110  //gdcm::File &file = r.GetFile();
111  //gdcm::DataSet& ds = file.GetDataSet();
112 
113  gdcm::Image& image = r.GetImage();
114 
115  char* dataBuffer = new char[image.GetBufferLength()];
116  image.GetBuffer(dataBuffer);
117 
118  int ndims = image.GetNumberOfDimensions();
119  const unsigned int* dims = image.GetDimensions();
120 
121  unsigned short bits_allocated = image.GetPixelFormat().GetBitsAllocated();
122  unsigned short bytes_allocated = bits_allocated / 8;
123  unsigned short bits_stored = image.GetPixelFormat().GetBitsStored();
124  unsigned short samples_per_pixel = image.GetPixelFormat().GetSamplesPerPixel();
125 
126  unsigned int offset = 8 - (bits_allocated - bits_stored);
127  unsigned int off_pow = 1;
128  for (unsigned int i = 0; i < offset; ++i)
129  {
130  off_pow *= 2;
131  }
132 
133  if (mln_site_(I)::dim != ndims)
134  {
135  std::cerr << "error: dimension mismatch" << std::endl;
136  abort();
137  }
138 
139  algebra::vec<mln_site_(I)::dim, unsigned int> vmin;
140  algebra::vec<mln_site_(I)::dim, unsigned int> vmax;
141  algebra::vec<mln_site_(I)::dim, unsigned int> vdims;
142  int j = ndims - 1;
143  for (int i = 0; i < ndims; ++i, --j)
144  {
145  vmin[i] = 0;
146  vmax[i] = dims[i] - 1;
147  if (i == 0)
148  vdims[j] = 1;
149  else
150  vdims[j] = dims[i - 1] * vdims[j + 1];
151  }
152 
153  if (ndims > 1)
154  {
155  std::swap(vmin[0], vmin[1]);
156  std::swap(vmax[0], vmax[1]);
157  }
158 
159  mln_site(I) pmin(vmin);
160  mln_site(I) pmax(vmax);
161  mln_concrete(I) result(box<mln_site(I)>(pmin, pmax));
162  initialize(ima, result);
163  mln_piter(I) p(ima.domain());
164  unsigned int index = 0;
165 
166  for_all(p)
167  {
168  index = 0;
169  for (int i = 0; i < ndims; ++i)
170  {
171  index += p[i] * vdims[i];
172  }
173 
174  mln_value(I) v = (unsigned char) dataBuffer[(index * bytes_allocated) * samples_per_pixel];
175  // FIXME: RGB support, HighBit if HB == 1
176  for (unsigned int j = 0; j < bytes_allocated; ++j)
177  {
178  v += ((unsigned char) dataBuffer[(index * bytes_allocated + j) * samples_per_pixel]) * 256 * j;
179  }
180 
181  ima(p) = v;
182  }
183 
184  delete[] dataBuffer;
185 
186  }
187 
188 # endif // ! MLN_INCLUDE_ONLY
189 
190  } // end of namespace mln::io::dicom
191 
192  } // end of namespace mln::io
193 
194 } // end of namespace mln
195 
196 
197 #endif // ! MLN_IO_DICOM_LOAD_HH