vil_print.cxx
Go to the documentation of this file.
1 // This is core/vil/vil_print.cxx
2 #include <complex>
3 #include "vil_print.h"
4 //:
5 // \file
6 // \brief Various functions for manipulating image views
7 // \author Tim Cootes - Manchester
8 //
9 // \verbatim
10 // Modifications
11 // 23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
12 // \endverbatim
13 
14 #include <vxl_config.h> // for vxl_uint_32 etc.
15 #ifdef _MSC_VER
16 # include <vcl_msvc_warnings.h>
17 #endif
18 #include <vil/vil_rgb.h>
19 #include <vil/vil_rgba.h>
20 
21 //: Explicit overload for bool
22 template <>
23 void vil_print_value(std::ostream& os, const bool& value, unsigned)
24 {
25  os<<int(value);
26 }
27 
28 //: Explicit overload for byte
29 template <>
30 void vil_print_value(std::ostream& os, const vxl_byte& value, unsigned)
31 {
32  os.width(3);
33  os<<int(value);
34 }
35 
36 //: Explicit overload for signed byte
37 template <>
38 void vil_print_value(std::ostream& os, const vxl_sbyte& value, unsigned)
39 {
40  os.width(3);
41  os<<int(value);
42 }
43 
44 //: Explicit overload for short
45 template <>
46 void vil_print_value(std::ostream& os, const vxl_int_16& value, unsigned width/*=0*/)
47 {
48  if (width==0) width=5;
49  int v=value;
50  if (v<0) { v=-v; os<<'-'; } else os<<' ';
51  if (v<10 && width > 1) os<<'0';
52  if (v<100 && width > 2) os<<'0';
53  if (v<1000 && width > 3) os<<'0';
54  if (v<10000 && width > 4) os<<'0';
55  os<<v;
56 }
57 
58 //: Explicit overload for unsigned short
59 template <>
60 void vil_print_value(std::ostream& os, const vxl_uint_16& value, unsigned width/*=0*/)
61 {
62  if (width==0) width=5;
63  if (value<10 && width > 1) os<<'0';
64  if (value<100 && width > 2) os<<'0';
65  if (value<1000 && width > 3) os<<'0';
66  if (value<10000 && width > 4) os<<'0';
67  os<<value;
68 }
69 
70 //: Explicit overload for int
71 template <>
72 void vil_print_value(std::ostream& os, const vxl_int_32& value, unsigned width/*=0*/)
73 {
74  if (width==0) width=8;
75  int v=value;
76  if (v<0) { v=-v; os<<'-'; } else os<<' ';
77  if (v<10 && width > 1) os<<'0';
78  if (v<100 && width > 2) os<<'0';
79  if (v<1000 && width > 3) os<<'0';
80  if (v<10000 && width > 4) os<<'0';
81  if (v<100000 && width > 5) os<<'0';
82  if (v<1000000 && width > 6) os<<'0';
83  if (v<10000000 && width > 7) os<<'0';
84  os<<v;
85 }
86 
87 //: Explicit overload for unsigned int
88 template <>
89 void vil_print_value(std::ostream& os, const vxl_uint_32& value, unsigned width/*=0*/)
90 {
91  if (width==0) width=8;
92  if (value<10 && width > 1) os<<'0';
93  if (value<100 && width > 2) os<<'0';
94  if (value<1000 && width > 3) os<<'0';
95  if (value<10000 && width > 4) os<<'0';
96  if (value<100000 && width > 5) os<<'0';
97  if (value<1000000 && width > 6) os<<'0';
98  if (value<10000000 && width > 7) os<<'0';
99  os<<value;
100 }
101 
102 #if VXL_HAS_INT_64
103 
104 //: Explicit overload for unsigned long
105 template <>
106 void vil_print_value(std::ostream& os, const vxl_uint_64& value, unsigned width/*=0*/)
107 {
108  if (width==0) width=8;
109  if (value<10 && width > 1) os<<'0';
110  if (value<100 && width > 2) os<<'0';
111  if (value<1000 && width > 3) os<<'0';
112  if (value<10000 && width > 4) os<<'0';
113  if (value<100000 && width > 5) os<<'0';
114  if (value<1000000 && width > 6) os<<'0';
115  if (value<10000000 && width > 7) os<<'0';
116  os<<value;
117 }
118 
119 //: Explicit overload for long
120 template <>
121 void vil_print_value(std::ostream& os, const vxl_int_64& value, unsigned width/*=0*/)
122 {
123  if (width==0) width=8;
124  vxl_int_64 v=value;
125  if (v<0) { v=-v; os<<'-'; } else os<<' ';
126  if (v<10 && width > 1) os<<'0';
127  if (v<100 && width > 2) os<<'0';
128  if (v<1000 && width > 3) os<<'0';
129  if (v<10000 && width > 4) os<<'0';
130  if (v<100000 && width > 5) os<<'0';
131  if (v<1000000 && width > 6) os<<'0';
132  if (v<10000000 && width > 7) os<<'0';
133  os<<v;
134 }
135 
136 #endif
137 
138 //: Explicit overload for float
139 template <>
140 void vil_print_value(std::ostream& os, const float& value, unsigned)
141 {
142  os<<value;
143 }
144 
145 //: Explicit overload for double
146 template <>
147 void vil_print_value(std::ostream& os, const double& value, unsigned)
148 {
149  os<<value;
150 }
151 
152 //: Explicit overload for complex float
153 template <>
154 void vil_print_value(std::ostream& os, const std::complex<float>& value, unsigned)
155 {
156  os<<value;
157 }
158 
159 //: Explicit overload for complex double
160 template <>
161 void vil_print_value(std::ostream& os, const std::complex<double>& value, unsigned)
162 {
163  os<<value;
164 }
165 
166 //: Explicit overload of print for rgb<byte>
167 template <>
168 void vil_print_value(std::ostream& os, const vil_rgb<vxl_byte>& value, unsigned)
169 {
170  int r = int(value.r);
171  if (r<10) os<<'0';
172  if (r<100) os<<'0';
173  os<<r<<'/';
174  int g = int(value.g);
175  if (g<10) os<<'0';
176  if (g<100) os<<'0';
177  os<<g<<'/';
178  int b = int(value.b);
179  if (b<10) os<<'0';
180  if (b<100) os<<'0';
181  os<<b;
182 }
183 
184 //: Explicit overload of print for rgb<sbyte>
185 template <>
186 void vil_print_value(std::ostream& os, vil_rgb<vxl_sbyte> const& value, unsigned)
187 {
188  int r = int(value.r);
189  if (r<0) r=-r,os<<'-'; else os<<'+';
190  if (r<10) os<<'0';
191  if (r<100) os<<'0';
192  os<<r<<'/';
193  int g = int(value.g);
194  if (g<0) g=-g,os<<'-'; else os<<'+';
195  if (g<10) os<<'0';
196  if (g<100) os<<'0';
197  os<<g<<'/';
198  int b = int(value.b);
199  if (b<0) b=-b,os<<'-'; else os<<'+';
200  if (b<10) os<<'0';
201  if (b<100) os<<'0';
202  os<<b;
203 }
204 
205 //: Explicit overload of print for rgb<short>
206 template <>
207 void vil_print_value(std::ostream& os, const vil_rgb<vxl_int_16>& value, unsigned width)
208 {
209  vil_print_value(os, value.r, width);
210  os<<'/';
211  vil_print_value(os, value.g, width);
212  os<<'/';
213  vil_print_value(os, value.b, width);
214 }
215 
216 //: Explicit overload of print for rgb<unsigned short>
217 template <>
218 void vil_print_value(std::ostream& os, const vil_rgb<vxl_uint_16>& value, unsigned width)
219 {
220  vil_print_value(os, value.r, width);
221  os<<'/';
222  vil_print_value(os, value.g, width);
223  os<<'/';
224  vil_print_value(os, value.b, width);
225 }
226 
227 //: Explicit overload of print for rgb<int>
228 template <>
229 void vil_print_value(std::ostream& os, const vil_rgb<vxl_int_32>& value, unsigned width)
230 {
231  vil_print_value(os, value.r, width);
232  os<<'/';
233  vil_print_value(os, value.g, width);
234  os<<'/';
235  vil_print_value(os, value.b, width);
236 }
237 
238 //: Explicit overload of print for rgb<unsigned int>
239 template <>
240 void vil_print_value(std::ostream& os, const vil_rgb<vxl_uint_32>& value, unsigned width)
241 {
242  vil_print_value(os, value.r, width);
243  os<<'/';
244  vil_print_value(os, value.g, width);
245  os<<'/';
246  vil_print_value(os, value.b, width);
247 }
248 
249 #if VXL_HAS_INT_64
250 
251 //: Explicit overload of print for rgb<long>
252 template <>
253 void vil_print_value(std::ostream& os, const vil_rgb<vxl_int_64>& value, unsigned width)
254 {
255  vil_print_value(os, value.r, width);
256  os<<'/';
257  vil_print_value(os, value.g, width);
258  os<<'/';
259  vil_print_value(os, value.b, width);
260 }
261 
262 //: Explicit overload of print for rgb<unsigned long>
263 template <>
264 void vil_print_value(std::ostream& os, const vil_rgb<vxl_uint_64>& value, unsigned width)
265 {
266  vil_print_value(os, value.r, width);
267  os<<'/';
268  vil_print_value(os, value.g, width);
269  os<<'/';
270  vil_print_value(os, value.b, width);
271 }
272 
273 #endif
274 
275 //: Explicit overload of print for rgb<float>
276 template <>
277 void vil_print_value(std::ostream& os, const vil_rgb<float>& value, unsigned)
278 {
279  os<<value.r<<'/'<<value.g<<'/'<<value.b;
280 }
281 
282 
283 //: Explicit overload of print for rgb<double>
284 template <>
285 void vil_print_value(std::ostream& os, const vil_rgb<double>& value, unsigned)
286 {
287  os<<value.r<<'/'<<value.g<<'/'<<value.b;
288 }
289 
290 //: Explicit overload of print for rgba<byte>
291 template <>
292 void vil_print_value(std::ostream& os, const vil_rgba<vxl_byte>& value, unsigned)
293 {
294  int r = int(value.r);
295  if (r<10) os<<'0';
296  if (r<100) os<<'0';
297  os<<r<<'/';
298  int g = int(value.g);
299  if (g<10) os<<'0';
300  if (g<100) os<<'0';
301  os<<g<<'/';
302  int b = int(value.b);
303  if (b<10) os<<'0';
304  if (b<100) os<<'0';
305  os<<b<<'/';
306  int a = int(value.a);
307  if (a<10) os<<'0';
308  if (a<100) os<<'0';
309  os<<a;
310 }
311 
312 //: Explicit overload of print for rgba<sbyte>
313 template <>
314 void vil_print_value(std::ostream& os, const vil_rgba<vxl_sbyte>& value, unsigned)
315 {
316  int r = int(value.r);
317  if (r<0) r=-r,os<<'-'; else os<<'+';
318  if (r<10) os<<'0';
319  if (r<100) os<<'0';
320  os<<r<<'/';
321  int g = int(value.g);
322  if (g<0) g=-g,os<<'-'; else os<<'+';
323  if (g<10) os<<'0';
324  if (g<100) os<<'0';
325  os<<g<<'/';
326  int b = int(value.b);
327  if (b<0) b=-b,os<<'-'; else os<<'+';
328  if (b<10) os<<'0';
329  if (b<100) os<<'0';
330  os<<b<<'/';
331  int a = int(value.a);
332  if (a<0) a=-a,os<<'-'; else os<<'+';
333  if (a<10) os<<'0';
334  if (a<100) os<<'0';
335  os<<a;
336 }
337 
338 //: Explicit overload of print for rgba<short>
339 template <>
340 void vil_print_value(std::ostream& os, const vil_rgba<vxl_int_16>& value, unsigned width)
341 {
342  vil_print_value(os, value.r, width);
343  os<<'/';
344  vil_print_value(os, value.g, width);
345  os<<'/';
346  vil_print_value(os, value.b, width);
347  os<<'/';
348  vil_print_value(os, value.a, width);
349 }
350 
351 //: Explicit overload of print for rgba<unsigned short>
352 template <>
353 void vil_print_value(std::ostream& os, const vil_rgba<vxl_uint_16>& value, unsigned width)
354 {
355  vil_print_value(os, value.r, width);
356  os<<'/';
357  vil_print_value(os, value.g, width);
358  os<<'/';
359  vil_print_value(os, value.b, width);
360  os<<'/';
361  vil_print_value(os, value.a, width);
362 }
363 
364 //: Explicit overload of print for rgba<int>
365 template <>
366 void vil_print_value(std::ostream& os, const vil_rgba<vxl_int_32>& value, unsigned width)
367 {
368  vil_print_value(os, value.r, width);
369  os<<'/';
370  vil_print_value(os, value.g, width);
371  os<<'/';
372  vil_print_value(os, value.b, width);
373  os<<'/';
374  vil_print_value(os, value.a, width);
375 }
376 
377 //: Explicit overload of print for rgba<unsigned int>
378 template <>
379 void vil_print_value(std::ostream& os, const vil_rgba<vxl_uint_32>& value, unsigned width)
380 {
381  vil_print_value(os, value.r, width);
382  os<<'/';
383  vil_print_value(os, value.g, width);
384  os<<'/';
385  vil_print_value(os, value.b, width);
386  os<<'/';
387  vil_print_value(os, value.a, width);
388 }
389 
390 #if VXL_HAS_INT_64
391 
392 //: Explicit overload of print for rgba<long>
393 template <>
394 void vil_print_value(std::ostream& os, const vil_rgba<vxl_int_64>& value, unsigned width)
395 {
396  vil_print_value(os, value.r, width);
397  os<<'/';
398  vil_print_value(os, value.g, width);
399  os<<'/';
400  vil_print_value(os, value.b, width);
401  os<<'/';
402  vil_print_value(os, value.a, width);
403 }
404 
405 //: Explicit overload of print for rgba<unsigned long>
406 template <>
407 void vil_print_value(std::ostream& os, const vil_rgba<vxl_uint_64>& value, unsigned width)
408 {
409  vil_print_value(os, value.r, width);
410  os<<'/';
411  vil_print_value(os, value.g, width);
412  os<<'/';
413  vil_print_value(os, value.b, width);
414  os<<'/';
415  vil_print_value(os, value.a, width);
416 }
417 
418 #endif
419 
420 //: Explicit overload of print for rgba<float>
421 template <>
422 void vil_print_value(std::ostream& os, const vil_rgba<float>& value, unsigned)
423 {
424  os<<value.r<<'/'<<value.g<<'/'<<value.b<<'/'<<value.a;
425 }
426 
427 //: Explicit overload of print for rgba<double>
428 template <>
429 void vil_print_value(std::ostream& os, const vil_rgba<double>& value, unsigned)
430 {
431  os<<value.r<<'/'<<value.g<<'/'<<value.b<<'/'<<value.a;
432 }
433 
434 void vil_print_all(std::ostream& os, vil_image_view_base_sptr const& view)
435 {
436 #define docase(T) \
437  case T: \
438  vil_print_all(os, static_cast<vil_image_view< vil_pixel_format_type_of<T >::type > >(view) );\
439  break
440 
441  switch ( view->pixel_format() )
442  {
443 #if VXL_HAS_INT_64
444  docase( VIL_PIXEL_FORMAT_UINT_64 );
445  docase( VIL_PIXEL_FORMAT_INT_64 );
446 #endif
456 
457 #if VXL_HAS_INT_64
458  docase( VIL_PIXEL_FORMAT_RGB_UINT_64 );
459  docase( VIL_PIXEL_FORMAT_RGB_INT_64 );
460 #endif
469 
470 #if VXL_HAS_INT_64
471  docase( VIL_PIXEL_FORMAT_RGBA_UINT_64 );
472  docase( VIL_PIXEL_FORMAT_RGBA_INT_64 );
473 #endif
482 
485 
486  default: ;
487  }
488 #undef docase
489 }
std::complex<float> is a scalar for vil's purposes.
A templated smart pointer class.
Definition: vil_fwd.h:16
Pixel type for 24 bit images.
T g
Definition: vil_rgb.h:58
#define v
void vil_print_all(std::ostream &os, const vil_image_view< T > &view, unsigned width=0)
Print all image data to os in a grid (rounds output to int).
Definition: vil_print.h:77
T b
Definition: vil_rgb.h:58
void vil_print_value(std::ostream &s, const T &value, unsigned=0)
How to print value in vil_print_all(image_view).
#define docase(T)
This is the appropriate pixel type for RGBA colour images.
Definition: vil_fwd.h:15
Templated four-value colour cell.
T r
Definition: vil_rgb.h:58
std::complex<double> is a scalar for vil's purposes.
This is the appropriate pixel type for 24-bit colour images.
Definition: vil_fwd.h:14