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 vil_print_value(std::ostream & os, const bool & value, unsigned)
25 {
26   os << int(value);
27 }
28 
29 //: Explicit overload for byte
30 template <>
31 void
vil_print_value(std::ostream & os,const vxl_byte & value,unsigned)32 vil_print_value(std::ostream & os, const vxl_byte & value, unsigned)
33 {
34   os.width(3);
35   os << int(value);
36 }
37 
38 //: Explicit overload for signed byte
39 template <>
40 void
vil_print_value(std::ostream & os,const vxl_sbyte & value,unsigned)41 vil_print_value(std::ostream & os, const vxl_sbyte & value, unsigned)
42 {
43   os.width(3);
44   os << int(value);
45 }
46 
47 //: Explicit overload for short
48 template <>
49 void
vil_print_value(std::ostream & os,const vxl_int_16 & value,unsigned width)50 vil_print_value(std::ostream & os, const vxl_int_16 & value, unsigned width /*=0*/)
51 {
52   if (width == 0)
53     width = 5;
54   int v = value;
55   if (v < 0)
56   {
57     v = -v;
58     os << '-';
59   }
60   else
61     os << ' ';
62   if (v < 10 && width > 1)
63     os << '0';
64   if (v < 100 && width > 2)
65     os << '0';
66   if (v < 1000 && width > 3)
67     os << '0';
68   if (v < 10000 && width > 4)
69     os << '0';
70   os << v;
71 }
72 
73 //: Explicit overload for unsigned short
74 template <>
75 void
vil_print_value(std::ostream & os,const vxl_uint_16 & value,unsigned width)76 vil_print_value(std::ostream & os, const vxl_uint_16 & value, unsigned width /*=0*/)
77 {
78   if (width == 0)
79     width = 5;
80   if (value < 10 && width > 1)
81     os << '0';
82   if (value < 100 && width > 2)
83     os << '0';
84   if (value < 1000 && width > 3)
85     os << '0';
86   if (value < 10000 && width > 4)
87     os << '0';
88   os << value;
89 }
90 
91 //: Explicit overload for int
92 template <>
93 void
vil_print_value(std::ostream & os,const vxl_int_32 & value,unsigned width)94 vil_print_value(std::ostream & os, const vxl_int_32 & value, unsigned width /*=0*/)
95 {
96   if (width == 0)
97     width = 8;
98   int v = value;
99   if (v < 0)
100   {
101     v = -v;
102     os << '-';
103   }
104   else
105     os << ' ';
106   if (v < 10 && width > 1)
107     os << '0';
108   if (v < 100 && width > 2)
109     os << '0';
110   if (v < 1000 && width > 3)
111     os << '0';
112   if (v < 10000 && width > 4)
113     os << '0';
114   if (v < 100000 && width > 5)
115     os << '0';
116   if (v < 1000000 && width > 6)
117     os << '0';
118   if (v < 10000000 && width > 7)
119     os << '0';
120   os << v;
121 }
122 
123 //: Explicit overload for unsigned int
124 template <>
125 void
vil_print_value(std::ostream & os,const vxl_uint_32 & value,unsigned width)126 vil_print_value(std::ostream & os, const vxl_uint_32 & value, unsigned width /*=0*/)
127 {
128   if (width == 0)
129     width = 8;
130   if (value < 10 && width > 1)
131     os << '0';
132   if (value < 100 && width > 2)
133     os << '0';
134   if (value < 1000 && width > 3)
135     os << '0';
136   if (value < 10000 && width > 4)
137     os << '0';
138   if (value < 100000 && width > 5)
139     os << '0';
140   if (value < 1000000 && width > 6)
141     os << '0';
142   if (value < 10000000 && width > 7)
143     os << '0';
144   os << value;
145 }
146 
147 #if VXL_HAS_INT_64
148 
149 //: Explicit overload for unsigned long
150 template <>
151 void
vil_print_value(std::ostream & os,const vxl_uint_64 & value,unsigned width)152 vil_print_value(std::ostream & os, const vxl_uint_64 & value, unsigned width /*=0*/)
153 {
154   if (width == 0)
155     width = 8;
156   if (value < 10 && width > 1)
157     os << '0';
158   if (value < 100 && width > 2)
159     os << '0';
160   if (value < 1000 && width > 3)
161     os << '0';
162   if (value < 10000 && width > 4)
163     os << '0';
164   if (value < 100000 && width > 5)
165     os << '0';
166   if (value < 1000000 && width > 6)
167     os << '0';
168   if (value < 10000000 && width > 7)
169     os << '0';
170   os << value;
171 }
172 
173 //: Explicit overload for long
174 template <>
175 void
vil_print_value(std::ostream & os,const vxl_int_64 & value,unsigned width)176 vil_print_value(std::ostream & os, const vxl_int_64 & value, unsigned width /*=0*/)
177 {
178   if (width == 0)
179     width = 8;
180   vxl_int_64 v = value;
181   if (v < 0)
182   {
183     v = -v;
184     os << '-';
185   }
186   else
187     os << ' ';
188   if (v < 10 && width > 1)
189     os << '0';
190   if (v < 100 && width > 2)
191     os << '0';
192   if (v < 1000 && width > 3)
193     os << '0';
194   if (v < 10000 && width > 4)
195     os << '0';
196   if (v < 100000 && width > 5)
197     os << '0';
198   if (v < 1000000 && width > 6)
199     os << '0';
200   if (v < 10000000 && width > 7)
201     os << '0';
202   os << v;
203 }
204 
205 #endif
206 
207 //: Explicit overload for float
208 template <>
209 void
vil_print_value(std::ostream & os,const float & value,unsigned)210 vil_print_value(std::ostream & os, const float & value, unsigned)
211 {
212   os << value;
213 }
214 
215 //: Explicit overload for double
216 template <>
217 void
vil_print_value(std::ostream & os,const double & value,unsigned)218 vil_print_value(std::ostream & os, const double & value, unsigned)
219 {
220   os << value;
221 }
222 
223 //: Explicit overload for complex float
224 template <>
225 void
vil_print_value(std::ostream & os,const std::complex<float> & value,unsigned)226 vil_print_value(std::ostream & os, const std::complex<float> & value, unsigned)
227 {
228   os << value;
229 }
230 
231 //: Explicit overload for complex double
232 template <>
233 void
vil_print_value(std::ostream & os,const std::complex<double> & value,unsigned)234 vil_print_value(std::ostream & os, const std::complex<double> & value, unsigned)
235 {
236   os << value;
237 }
238 
239 //: Explicit overload of print for rgb<byte>
240 template <>
241 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_byte> & value,unsigned)242 vil_print_value(std::ostream & os, const vil_rgb<vxl_byte> & value, unsigned)
243 {
244   int r = int(value.r);
245   if (r < 10)
246     os << '0';
247   if (r < 100)
248     os << '0';
249   os << r << '/';
250   int g = int(value.g);
251   if (g < 10)
252     os << '0';
253   if (g < 100)
254     os << '0';
255   os << g << '/';
256   int b = int(value.b);
257   if (b < 10)
258     os << '0';
259   if (b < 100)
260     os << '0';
261   os << b;
262 }
263 
264 //: Explicit overload of print for rgb<sbyte>
265 template <>
266 void
vil_print_value(std::ostream & os,vil_rgb<vxl_sbyte> const & value,unsigned)267 vil_print_value(std::ostream & os, vil_rgb<vxl_sbyte> const & value, unsigned)
268 {
269   int r = int(value.r);
270   if (r < 0)
271     r = -r, os << '-';
272   else
273     os << '+';
274   if (r < 10)
275     os << '0';
276   if (r < 100)
277     os << '0';
278   os << r << '/';
279   int g = int(value.g);
280   if (g < 0)
281     g = -g, os << '-';
282   else
283     os << '+';
284   if (g < 10)
285     os << '0';
286   if (g < 100)
287     os << '0';
288   os << g << '/';
289   int b = int(value.b);
290   if (b < 0)
291     b = -b, os << '-';
292   else
293     os << '+';
294   if (b < 10)
295     os << '0';
296   if (b < 100)
297     os << '0';
298   os << b;
299 }
300 
301 //: Explicit overload of print for rgb<short>
302 template <>
303 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_int_16> & value,unsigned width)304 vil_print_value(std::ostream & os, const vil_rgb<vxl_int_16> & value, unsigned width)
305 {
306   vil_print_value(os, value.r, width);
307   os << '/';
308   vil_print_value(os, value.g, width);
309   os << '/';
310   vil_print_value(os, value.b, width);
311 }
312 
313 //: Explicit overload of print for rgb<unsigned short>
314 template <>
315 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_uint_16> & value,unsigned width)316 vil_print_value(std::ostream & os, const vil_rgb<vxl_uint_16> & value, unsigned width)
317 {
318   vil_print_value(os, value.r, width);
319   os << '/';
320   vil_print_value(os, value.g, width);
321   os << '/';
322   vil_print_value(os, value.b, width);
323 }
324 
325 //: Explicit overload of print for rgb<int>
326 template <>
327 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_int_32> & value,unsigned width)328 vil_print_value(std::ostream & os, const vil_rgb<vxl_int_32> & value, unsigned width)
329 {
330   vil_print_value(os, value.r, width);
331   os << '/';
332   vil_print_value(os, value.g, width);
333   os << '/';
334   vil_print_value(os, value.b, width);
335 }
336 
337 //: Explicit overload of print for rgb<unsigned int>
338 template <>
339 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_uint_32> & value,unsigned width)340 vil_print_value(std::ostream & os, const vil_rgb<vxl_uint_32> & 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 }
348 
349 #if VXL_HAS_INT_64
350 
351 //: Explicit overload of print for rgb<long>
352 template <>
353 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_int_64> & value,unsigned width)354 vil_print_value(std::ostream & os, const vil_rgb<vxl_int_64> & value, unsigned width)
355 {
356   vil_print_value(os, value.r, width);
357   os << '/';
358   vil_print_value(os, value.g, width);
359   os << '/';
360   vil_print_value(os, value.b, width);
361 }
362 
363 //: Explicit overload of print for rgb<unsigned long>
364 template <>
365 void
vil_print_value(std::ostream & os,const vil_rgb<vxl_uint_64> & value,unsigned width)366 vil_print_value(std::ostream & os, const vil_rgb<vxl_uint_64> & 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 }
374 
375 #endif
376 
377 //: Explicit overload of print for rgb<float>
378 template <>
379 void
vil_print_value(std::ostream & os,const vil_rgb<float> & value,unsigned)380 vil_print_value(std::ostream & os, const vil_rgb<float> & value, unsigned)
381 {
382   os << value.r << '/' << value.g << '/' << value.b;
383 }
384 
385 
386 //: Explicit overload of print for rgb<double>
387 template <>
388 void
vil_print_value(std::ostream & os,const vil_rgb<double> & value,unsigned)389 vil_print_value(std::ostream & os, const vil_rgb<double> & value, unsigned)
390 {
391   os << value.r << '/' << value.g << '/' << value.b;
392 }
393 
394 //: Explicit overload of print for rgba<byte>
395 template <>
396 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_byte> & value,unsigned)397 vil_print_value(std::ostream & os, const vil_rgba<vxl_byte> & value, unsigned)
398 {
399   int r = int(value.r);
400   if (r < 10)
401     os << '0';
402   if (r < 100)
403     os << '0';
404   os << r << '/';
405   int g = int(value.g);
406   if (g < 10)
407     os << '0';
408   if (g < 100)
409     os << '0';
410   os << g << '/';
411   int b = int(value.b);
412   if (b < 10)
413     os << '0';
414   if (b < 100)
415     os << '0';
416   os << b << '/';
417   int a = int(value.a);
418   if (a < 10)
419     os << '0';
420   if (a < 100)
421     os << '0';
422   os << a;
423 }
424 
425 //: Explicit overload of print for rgba<sbyte>
426 template <>
427 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_sbyte> & value,unsigned)428 vil_print_value(std::ostream & os, const vil_rgba<vxl_sbyte> & value, unsigned)
429 {
430   int r = int(value.r);
431   if (r < 0)
432     r = -r, os << '-';
433   else
434     os << '+';
435   if (r < 10)
436     os << '0';
437   if (r < 100)
438     os << '0';
439   os << r << '/';
440   int g = int(value.g);
441   if (g < 0)
442     g = -g, os << '-';
443   else
444     os << '+';
445   if (g < 10)
446     os << '0';
447   if (g < 100)
448     os << '0';
449   os << g << '/';
450   int b = int(value.b);
451   if (b < 0)
452     b = -b, os << '-';
453   else
454     os << '+';
455   if (b < 10)
456     os << '0';
457   if (b < 100)
458     os << '0';
459   os << b << '/';
460   int a = int(value.a);
461   if (a < 0)
462     a = -a, os << '-';
463   else
464     os << '+';
465   if (a < 10)
466     os << '0';
467   if (a < 100)
468     os << '0';
469   os << a;
470 }
471 
472 //: Explicit overload of print for rgba<short>
473 template <>
474 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_int_16> & value,unsigned width)475 vil_print_value(std::ostream & os, const vil_rgba<vxl_int_16> & value, unsigned width)
476 {
477   vil_print_value(os, value.r, width);
478   os << '/';
479   vil_print_value(os, value.g, width);
480   os << '/';
481   vil_print_value(os, value.b, width);
482   os << '/';
483   vil_print_value(os, value.a, width);
484 }
485 
486 //: Explicit overload of print for rgba<unsigned short>
487 template <>
488 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_uint_16> & value,unsigned width)489 vil_print_value(std::ostream & os, const vil_rgba<vxl_uint_16> & value, unsigned width)
490 {
491   vil_print_value(os, value.r, width);
492   os << '/';
493   vil_print_value(os, value.g, width);
494   os << '/';
495   vil_print_value(os, value.b, width);
496   os << '/';
497   vil_print_value(os, value.a, width);
498 }
499 
500 //: Explicit overload of print for rgba<int>
501 template <>
502 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_int_32> & value,unsigned width)503 vil_print_value(std::ostream & os, const vil_rgba<vxl_int_32> & value, unsigned width)
504 {
505   vil_print_value(os, value.r, width);
506   os << '/';
507   vil_print_value(os, value.g, width);
508   os << '/';
509   vil_print_value(os, value.b, width);
510   os << '/';
511   vil_print_value(os, value.a, width);
512 }
513 
514 //: Explicit overload of print for rgba<unsigned int>
515 template <>
516 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_uint_32> & value,unsigned width)517 vil_print_value(std::ostream & os, const vil_rgba<vxl_uint_32> & value, unsigned width)
518 {
519   vil_print_value(os, value.r, width);
520   os << '/';
521   vil_print_value(os, value.g, width);
522   os << '/';
523   vil_print_value(os, value.b, width);
524   os << '/';
525   vil_print_value(os, value.a, width);
526 }
527 
528 #if VXL_HAS_INT_64
529 
530 //: Explicit overload of print for rgba<long>
531 template <>
532 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_int_64> & value,unsigned width)533 vil_print_value(std::ostream & os, const vil_rgba<vxl_int_64> & value, unsigned width)
534 {
535   vil_print_value(os, value.r, width);
536   os << '/';
537   vil_print_value(os, value.g, width);
538   os << '/';
539   vil_print_value(os, value.b, width);
540   os << '/';
541   vil_print_value(os, value.a, width);
542 }
543 
544 //: Explicit overload of print for rgba<unsigned long>
545 template <>
546 void
vil_print_value(std::ostream & os,const vil_rgba<vxl_uint_64> & value,unsigned width)547 vil_print_value(std::ostream & os, const vil_rgba<vxl_uint_64> & value, unsigned width)
548 {
549   vil_print_value(os, value.r, width);
550   os << '/';
551   vil_print_value(os, value.g, width);
552   os << '/';
553   vil_print_value(os, value.b, width);
554   os << '/';
555   vil_print_value(os, value.a, width);
556 }
557 
558 #endif
559 
560 //: Explicit overload of print for rgba<float>
561 template <>
562 void
vil_print_value(std::ostream & os,const vil_rgba<float> & value,unsigned)563 vil_print_value(std::ostream & os, const vil_rgba<float> & value, unsigned)
564 {
565   os << value.r << '/' << value.g << '/' << value.b << '/' << value.a;
566 }
567 
568 //: Explicit overload of print for rgba<double>
569 template <>
570 void
vil_print_value(std::ostream & os,const vil_rgba<double> & value,unsigned)571 vil_print_value(std::ostream & os, const vil_rgba<double> & value, unsigned)
572 {
573   os << value.r << '/' << value.g << '/' << value.b << '/' << value.a;
574 }
575 
576 void
vil_print_all(std::ostream & os,vil_image_view_base_sptr const & view)577 vil_print_all(std::ostream & os, vil_image_view_base_sptr const & view)
578 {
579 #define docase(T)                                                                                                      \
580   case T:                                                                                                              \
581     vil_print_all(os, static_cast<vil_image_view<vil_pixel_format_type_of<T>::type>>(view));                           \
582     break
583 
584   switch (view->pixel_format())
585   {
586 #if VXL_HAS_INT_64
587     docase(VIL_PIXEL_FORMAT_UINT_64);
588     docase(VIL_PIXEL_FORMAT_INT_64);
589 #endif
590     docase(VIL_PIXEL_FORMAT_UINT_32);
591     docase(VIL_PIXEL_FORMAT_INT_32);
592     docase(VIL_PIXEL_FORMAT_UINT_16);
593     docase(VIL_PIXEL_FORMAT_INT_16);
594     docase(VIL_PIXEL_FORMAT_BYTE);
595     docase(VIL_PIXEL_FORMAT_SBYTE);
596     docase(VIL_PIXEL_FORMAT_FLOAT);
597     docase(VIL_PIXEL_FORMAT_DOUBLE);
598     docase(VIL_PIXEL_FORMAT_BOOL);
599 
600 #if VXL_HAS_INT_64
601     docase(VIL_PIXEL_FORMAT_RGB_UINT_64);
602     docase(VIL_PIXEL_FORMAT_RGB_INT_64);
603 #endif
604     docase(VIL_PIXEL_FORMAT_RGB_UINT_32);
605     docase(VIL_PIXEL_FORMAT_RGB_INT_32);
606     docase(VIL_PIXEL_FORMAT_RGB_UINT_16);
607     docase(VIL_PIXEL_FORMAT_RGB_INT_16);
608     docase(VIL_PIXEL_FORMAT_RGB_BYTE);
609     docase(VIL_PIXEL_FORMAT_RGB_SBYTE);
610     docase(VIL_PIXEL_FORMAT_RGB_FLOAT);
611     docase(VIL_PIXEL_FORMAT_RGB_DOUBLE);
612 
613 #if VXL_HAS_INT_64
614     docase(VIL_PIXEL_FORMAT_RGBA_UINT_64);
615     docase(VIL_PIXEL_FORMAT_RGBA_INT_64);
616 #endif
617     docase(VIL_PIXEL_FORMAT_RGBA_UINT_32);
618     docase(VIL_PIXEL_FORMAT_RGBA_INT_32);
619     docase(VIL_PIXEL_FORMAT_RGBA_UINT_16);
620     docase(VIL_PIXEL_FORMAT_RGBA_INT_16);
621     docase(VIL_PIXEL_FORMAT_RGBA_BYTE);
622     docase(VIL_PIXEL_FORMAT_RGBA_SBYTE);
623     docase(VIL_PIXEL_FORMAT_RGBA_FLOAT);
624     docase(VIL_PIXEL_FORMAT_RGBA_DOUBLE);
625 
626     docase(VIL_PIXEL_FORMAT_COMPLEX_FLOAT);
627     docase(VIL_PIXEL_FORMAT_COMPLEX_DOUBLE);
628 
629     default:;
630   }
631 #undef docase
632 }
633