$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
pnm/save.hh
1 // Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
2 // 2010 EPITA Research and Development 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_PNM_SAVE_HH
28 # define MLN_IO_PNM_SAVE_HH
29 
34 
35 # include <iostream>
36 # include <fstream>
37 
38 # include <mln/core/concept/image.hh>
39 # include <mln/core/alias/point2d.hh>
40 # include <mln/value/concept/scalar.hh>
41 
42 # include <mln/value/rgb.hh>
43 # include <mln/value/rgb8.hh>
44 # include <mln/value/int_u8.hh>
45 
46 # include <mln/metal/templated_by.hh>
47 # include <mln/metal/not_equal.hh>
48 
49 # include <mln/io/pnm/save_header.hh>
50 # include <mln/io/pnm/macros.hh>
51 
52 # include <mln/geom/size2d.hh>
53 
54 
55 namespace mln
56 {
57 
58  namespace io
59  {
60 
61  namespace pnm
62  {
63 
71  template <typename I>
72  void save(char type, const Image<I>& ima_, const std::string& filename);
73 
74 
75 
76 # ifndef MLN_INCLUDE_ONLY
77 
78  namespace impl
79  {
80 
81  // write a rgb value into for uncontiguous datas
82  template <unsigned int n>
83  inline
84  void write_value(std::ofstream& file,
85  const value::rgb<n>& c)
86  {
87  typedef typename value::int_u<n>::enc E;
88 
89  E v = c.red().to_enc();
90  file.write((char*)&v, sizeof(E));
91  v = c.green().to_enc();
92  file.write((char*)&v, sizeof(E));
93  v = c.blue().to_enc();
94  file.write((char*)&v, sizeof(E));
95  }
96 
97  // write a scalar value into for uncontiguous datas
98  template <typename V>
99  inline
100  void write_value(std::ofstream& file,
101  const V& v)
102  {
103  mlc_not_equal(V,bool)::check();
104  file.write((char*)(&v), sizeof(V));
105  }
106 
107  // write a scalar value into for uncontiguous datas
108  template <typename S>
109  inline
110  void write_value(std::ofstream& file,
111  const value::Scalar<S>& s)
112  {
113  typedef typename S::enc E;
114 
115  E c = s.to_enc();
116  file.write((char*)(&c), sizeof(E));
117  }
118 
119  // save data for (sizeof(int_u8) != 1) and non fastest images
120  template <typename I>
121  inline
122  void save_data_uncontiguous(std::ofstream& file,
123  const I& ima)
124  {
125  const def::coord
126  min_row = geom::min_row(ima),
127  max_row = geom::max_row(ima),
128  min_col = geom::min_col(ima),
129  max_col = geom::max_col(ima);
130 
131  point2d p;
132  for (p.row() = min_row; p.row() <= max_row; ++p.row())
133  for (p.col() = min_col; p.col() <= max_col; ++p.col())
134  write_value(file, ima(p));
135  }
136 
137  // save data when (sizeof(int_u8) == 1) with fastest images
138  // (faster)
139  template <typename I>
140  inline
141  void save_data_contiguous(std::ofstream& file,
142  const I& ima_)
143  {
144  const I& ima = exact(ima_);
145  const def::coord
146  min_row = geom::min_row(ima),
147  max_row = geom::max_row(ima);
148  point2d p;
149  p.col() = geom::min_col(ima);
150  std::size_t len = geom::ncols(ima) * sizeof(mln_value(I));
151  for (p.row() = min_row; p.row() <= max_row; ++p.row())
152  file.write((char*)(& ima(p)), len);
153  }
154 
155 
156  // caller for fastest images
157  template <typename I>
158  inline
159  void save_data_(std::ofstream& file,
160  trait::image::speed::fastest, const I& ima)
161  {
162  // FIXME: GCC 4.1.1 crashes on that line.
163  if (sizeof(value::int_u8) == 1)
164  save_data_contiguous(file, ima);
165  else
166  save_data_uncontiguous(file, ima);
167  }
168 
169  // caller for non fastest images
170  template <typename I>
171  inline
172  void save_data_(std::ofstream& file,
173  trait::image::speed::any, const I& ima)
174  {
175  save_data_uncontiguous(file, ima);
176  }
177 
178  } // end of namespace mln::io::pnm::impl
179 
180 
181  // Facades.
182 
183  template <typename I>
184  inline
185  void save(char type, const Image<I>& ima_, const std::string& filename)
186  {
187  mln_trace("mln::io::pnm::save");
188  const I& ima = exact(ima_);
189  std::ofstream file(filename.c_str());
190  io::pnm::save_header(type, ima, filename, file);
191 
192  impl::save_data_(file,
193  mln_trait_image_speed(I)(), ima);
194  }
195 
196 # endif // ! MLN_INCLUDE_ONLY
197 
198  } // end of namespace mln::io::pnm
199 
200  } // end of namespace mln::io
201 
202 } // end of namespace mln
203 
204 
205 #endif // ! MLN_IO_PNM_SAVE_HH