$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
save.hh
1 // Copyright (C) 2011, 2012, 2013 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 SCRIBO_IO_IMG_SAVE_HH
28 # define SCRIBO_IO_IMG_SAVE_HH
29 
33 
34 # include <libgen.h>
35 # include <fstream>
36 # include <sstream>
37 
38 # include <map>
39 
40 # include <mln/core/image/image2d.hh>
41 # include <mln/value/rgb8.hh>
42 # include <mln/io/magick/save.hh>
43 # include <mln/data/transform.hh>
44 # include <mln/subsampling/antialiased.hh>
45 
46 # include <scribo/core/document.hh>
47 
48 # include <scribo/io/img/internal/text_img_visitor.hh>
49 # include <scribo/io/img/internal/non_text_img_visitor.hh>
50 # include <scribo/io/img/internal/full_img_visitor.hh>
51 # include <scribo/io/img/internal/debug_img_visitor.hh>
52 
53 
54 namespace scribo
55 {
56 
57  namespace io
58  {
59 
60  namespace img
61  {
62 
78  enum Format
79  {
80  Text,
81  NonText,
82  Full,
83  DebugWoImage,
84  DebugWithImage,
85  };
86 
87 
91  template <typename L>
92  void
93  save(const document<L>& doc, const std::string& output_name,
94  Format format);
95 
96 
97 # ifndef MLN_INCLUDE_ONLY
98 
99 
100  namespace internal
101  {
102 
103  // FIXME: should be moved as parameter of io::img::save. This
104  // requires to update some functors.
105  unsigned reduction_factor = 1;
106 
107  struct highlight_mask
108  : Function_v2v<highlight_mask>
109  {
110  typedef value::rgb8 result;
111 
112  highlight_mask(float ratio)
113  : ratio(ratio)
114  {
115  }
116 
117  result operator()(const result& v) const
118  {
119  result out = v;
120  out.red() = std::min(out.red() * ratio, 255.f);
121  out.green() = std::min(out.green() * ratio, 255.f);
122  out.blue() = std::min(out.blue() * ratio, 255.f);
123  return out;
124  }
125 
126  float ratio;
127  };
128 
129 
130  template <typename L>
132  save_text(const document<L>& doc)
133  {
134  mln_precondition(doc.is_valid());
135  mln::image2d<value::rgb8> output = duplicate(doc.image());
137  doc.accept(f);
138  return output;
139  }
140 
141  template <typename L>
143  save_non_text(const document<L>& doc)
144  {
145  mln_precondition(doc.is_valid());
146  mln::image2d<value::rgb8> output = duplicate(doc.image());
148  doc.accept(f);
149  return output;
150  }
151 
152  template <typename L>
154  save_full(const document<L>& doc)
155  {
156  mln_precondition(doc.is_valid());
157  mln::image2d<value::rgb8> output = duplicate(doc.image());
159  doc.accept(f);
160  return output;
161  }
162 
163  template <typename L>
165  save_debug_without_image(const document<L>& doc)
166  {
167  mln_precondition(doc.is_valid());
168 
169  const box2d& ima_domain = doc.image().domain();
170  box2d domain(ima_domain.pmin() / reduction_factor,
171  ima_domain.pmax() / reduction_factor);
172  mln::image2d<value::rgb8> output(domain);
173  data::fill(output, literal::black);
174 
176  reduction_factor);
177  doc.accept(f);
178  return output;
179  }
180 
181  template <typename L>
183  save_debug_with_image(const document<L>& doc)
184  {
185  mln_precondition(doc.is_valid());
186  internal::highlight_mask highlight(0.5f);
188  output = data::transform(doc.image(), highlight);
189  if (reduction_factor > 1)
190  output = mln::subsampling::antialiased(output, reduction_factor);
191 
193  reduction_factor);
194  doc.accept(f);
195  return output;
196  }
197 
198  } // end of namespace scribo::io::img::internal
199 
200 
201 
202  // FACADE
203 
204  template <typename L>
205  void
206  save(const document<L>& doc,
207  const std::string& output_name,
208  Format format)
209  {
210  mln_trace("scribo::io::img::save");
211 
212  mln_precondition(doc.is_open());
213 
215 
216  // Choose saving method.
217  switch (format)
218  {
219  case Text:
220  output = internal::save_text(doc);
221  break;
222 
223  case NonText:
224  output = internal::save_non_text(doc);
225  break;
226 
227  case Full:
228  output = internal::save_full(doc);
229  break;
230 
231  case DebugWoImage:
232  output = internal::save_debug_without_image(doc);
233  break;
234 
235  case DebugWithImage:
236  output = internal::save_debug_with_image(doc);
237  break;
238 
239  default:
240  mln_trace_warning("scribo::io::img::save - "
241  "Invalid image format! Skip saving...");
242  return;
243  }
244 
245  mln::io::magick::save(output, output_name.c_str());
246 
247  }
248 
249 
250 # endif // ! MLN_INCLUDE_ONLY
251 
252  } // end of namespace scribo::io::img
253 
254  } // end of namespace scribo::io
255 
256 } // end of namespace scribo
257 
258 
259 #endif // ! SCRIBO_IO_IMG_SAVE_HH