$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
io.hh
1 // Copyright (C) 2008, 2009, 2011 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 
29 
30 // FIXME: Refactor and move into the library.
31 
32 #include <fstream>
33 #include <sstream>
34 #include <iomanip>
35 
36 #include <string>
37 
38 #include <mln/core/image/complex_image.hh>
39 #include <mln/core/alias/complex_geometry.hh>
40 
41 #include <mln/core/image/complex_neighborhoods.hh>
42 #include <mln/core/image/complex_neighborhood_piter.hh>
43 
44 #include <mln/value/int_u8.hh>
45 
46 
47 namespace mln
48 {
49 
50  namespace io
51  {
52 
53  namespace neato
54  {
55 
56  /* FIXME: This is just the gray-level version. Handle other value
57  types as well. */
58  // FIXME: Use an alias instead of the long type name.
59  void
61  value::int_u8>& ima,
62  const std::string& filename,
63  const std::string& bgcolor = "#0000C0",
64  const std::string& fontcolor = "#0000C0",
65  bool empty_vertex_label = true)
66  {
67  typedef value::int_u8 V;
69  const unsigned D = 1;
71 
72  std::ofstream g(filename.c_str());
73  g << "graph wst" << std::endl
74  << "{" << std::endl
75  << " graph [bgcolor = \"" << bgcolor << "\"]" << std::endl
76  << " edge [color = \"#FFFFFF\"]" << std::endl
77  << " node [color = \"#FFFFFF\", height=\"5\", width=\"5\","
78  << " fontsize=\"100\", fontcolor = \"" << fontcolor << "\"]"
79  << std::endl;
80 
81  // Vertices.
82  p_n_faces_fwd_piter<D, G> v(ima.domain(), 0);
83  typedef complex_higher_neighborhood<D, G> e_nbh_t;
84  e_nbh_t e_nbh;
85  for_all(v)
86  {
87  V vertex_color = ima(v);
88  std::ostringstream vertex_color_str;
89  // FIXME: Only valid for gray-level images.
90  vertex_color_str << '#'
91  << std::hex
92  << std::setfill('0')
93  << std::setw(2) << vertex_color
94  << std::setw(2) << vertex_color
95  << std::setw(2) << vertex_color
96  << std::dec;
97 
98  g << " v" << v.unproxy_().face_id()
99  << " [pos = \""
100  << std::fixed << std::setprecision(1)
101  << (float)v.to_site().front()[1] << ", "
102  << -(float)v.to_site().front()[0]
103  << "\", color = \"" << vertex_color_str.str()
104  << "\", fillcolor = \"" << vertex_color_str.str()
105  << "\", pin = \"true\", style=\"filled,setlinewidth(3)\"";
106  if (empty_vertex_label)
107  g << ", label = \"\"";
108  g << "];"
109  << std::endl;
110  }
111 
112  // Edges.
113  p_n_faces_fwd_piter<D, G> e(ima.domain(), 1);
114  typedef complex_lower_neighborhood<D, G> v_nbh_t;
115  v_nbh_t v_nbh;
116  mln_niter_(v_nbh_t) adj_v(v_nbh, e);
117  for_all(e)
118  {
119  V edge_color = ima(e);
120  std::ostringstream edge_color_str;
121  edge_color_str << '#'
122  << std::hex
123  << std::setfill('0')
124  << std::setw(2) << edge_color
125  << std::setw(2) << edge_color
126  << std::setw(2) << edge_color
127  << std::dec;
128 
129  // Adjacent vertices.
130  adj_v.start();
131  topo::face<1> v1 = adj_v.unproxy_().face();
132  adj_v.next();
133  topo::face<1> v2 = adj_v.unproxy_().face();
134  adj_v.next();
135  mln_invariant(!adj_v.is_valid());
136 
137  g << " v" << v1.face_id() << " -- v" << v2.face_id() << " ";
138  g << "[color = \"" << edge_color_str.str()
139  << "\", style=\"setlinewidth(10)\"];" << std::endl;
140  }
141 
142  g << "}" << std::endl;
143  g.close();
144  }
145 
146  // FIXME: Factor with the previous version.
147  void
149  value::rgb8>& ima,
150  const std::string& filename,
151  const std::string& bgcolor = "#0000C0",
152  const std::string& fontcolor = "#0000C0",
153  bool empty_vertex_label = true)
154  {
155  typedef value::rgb8 V;
157  const unsigned D = 1;
159 
160  std::ofstream g(filename.c_str());
161  g << "graph wst" << std::endl
162  << "{" << std::endl
163  << " graph [bgcolor = \"" << bgcolor << "\"]" << std::endl
164  << " edge [color = \"#FFFFFF\"]" << std::endl
165  << " node [color = \"#FFFFFF\", height=\"5\", width=\"5\","
166  << " fontsize=\"100\", fontcolor = \"" << fontcolor << "\"]"
167  << std::endl;
168 
169  // Vertices.
170  p_n_faces_fwd_piter<D, G> v(ima.domain(), 0);
171  typedef complex_higher_neighborhood<D, G> e_nbh_t;
172  e_nbh_t e_nbh;
173  for_all(v)
174  {
175  V vertex_color = ima(v);
176  std::ostringstream vertex_color_str;
177  // FIXME: Only valid for gray-level images.
178  vertex_color_str << '#'
179  << std::hex
180  << std::setfill('0')
181  << std::setw(2) << vertex_color.red()
182  << std::setw(2) << vertex_color.green()
183  << std::setw(2) << vertex_color.blue()
184  << std::dec;
185 
186  g << " v" << v.unproxy_().face_id()
187  << " [pos = \""
188  << std::fixed << std::setprecision(1)
189  << (float)v.to_site().front()[1] << ", "
190  << -(float)v.to_site().front()[0]
191  << "\", color = \"" << vertex_color_str.str()
192  << "\", fillcolor = \"" << vertex_color_str.str()
193  << "\", pin = \"true\", style=\"filled,setlinewidth(3)\"";
194  if (empty_vertex_label)
195  g << ", label = \"\"";
196  g << "];"
197  << std::endl;
198  }
199 
200  // Edges.
201  p_n_faces_fwd_piter<D, G> e(ima.domain(), 1);
202  typedef complex_lower_neighborhood<D, G> v_nbh_t;
203  v_nbh_t v_nbh;
204  mln_niter_(v_nbh_t) adj_v(v_nbh, e);
205  for_all(e)
206  {
207  V edge_color = ima(e);
208  std::ostringstream edge_color_str;
209  edge_color_str << '#'
210  << std::hex
211  << std::setfill('0')
212  << std::setw(2) << edge_color.red()
213  << std::setw(2) << edge_color.green()
214  << std::setw(2) << edge_color.blue()
215  << std::dec;
216 
217  // Adjacent vertices.
218  adj_v.start();
219  topo::face<1> v1 = adj_v.unproxy_().face();
220  adj_v.next();
221  topo::face<1> v2 = adj_v.unproxy_().face();
222  adj_v.next();
223  mln_invariant(!adj_v.is_valid());
224 
225  g << " v" << v1.face_id() << " -- v" << v2.face_id() << " ";
226  g << "[color = \"" << edge_color_str.str()
227  << "\", style=\"setlinewidth(10)\"];" << std::endl;
228  }
229 
230  g << "}" << std::endl;
231  g.close();
232  }
233 
234  } // end of namespace mln::io::neato
235 
236  } // end of namespace mln::io
237 
238 } // end of namespace mln