83 #include <vcl_compiler_detection.h> 85 # include <vcl_msvc_warnings.h> 95 template <
class In,
class Out>
102 #ifndef DOXYGEN_SHOULD_SKIP_THIS 104 #define macro( in , out )\ 106 inline void vil_convert_cast_pixel<in, vil_rgb<out > >::operator () (in v, vil_rgb<out >& d) const \ 107 { d.r = d.g = d.b = (out)v; } \ 109 inline void vil_convert_cast_pixel<vil_rgb<out >, in >::operator () (vil_rgb<out > v, in& d) const \ 110 { d = (in)(0.2125*v.r+0.7154*v.g+0.0721*v.b); } \ 112 inline void vil_convert_cast_pixel<in, vil_rgba<out > >::operator () (in v, vil_rgba<out >& d) const \ 113 { d.r = d.g = d.b = (out)v; d.a=1; } \ 115 inline void vil_convert_cast_pixel<vil_rgba<out >, in >::operator () (vil_rgba<out > v, in& d) const \ 116 { d = (in)(0.2125*v.r+0.7154*v.g+0.0721*v.b); } 117 macro( vxl_byte , vxl_byte )
118 macro( vxl_int_16 , vxl_byte )
119 macro( vxl_uint_16 , vxl_byte )
120 macro( vxl_int_32 , vxl_byte )
121 macro( vxl_uint_32 , vxl_byte )
122 macro(
float , vxl_byte )
123 macro(
double , vxl_byte )
124 macro( vxl_byte , vxl_sbyte )
125 macro( vxl_int_16 , vxl_sbyte )
126 macro( vxl_uint_16 , vxl_sbyte )
127 macro( vxl_int_32 , vxl_sbyte )
128 macro( vxl_uint_32 , vxl_sbyte )
129 macro(
float , vxl_sbyte )
130 macro(
double , vxl_sbyte )
131 macro( vxl_int_16 , vxl_int_16 )
132 macro(
float , vxl_int_16 )
133 macro(
double , vxl_int_16 )
134 macro( vxl_uint_16 , vxl_uint_16 )
135 macro(
float , vxl_uint_16 )
136 macro(
double , vxl_uint_16 )
137 macro( vxl_int_32 , vxl_int_32 )
138 macro(
float , vxl_int_32 )
139 macro(
double , vxl_int_32 )
140 macro( vxl_uint_32 , vxl_uint_32 )
141 macro(
float , vxl_uint_32 )
142 macro(
double , vxl_uint_32 )
143 macro(
double ,
float )
145 macro( vxl_int_64 , vxl_byte )
146 macro( vxl_uint_64 , vxl_byte )
147 macro( vxl_int_64 , vxl_sbyte )
148 macro( vxl_uint_64 , vxl_sbyte )
149 macro( vxl_int_64 , vxl_int_64 )
150 macro(
float , vxl_int_64 )
151 macro(
double , vxl_int_64 )
152 macro( vxl_uint_64 , vxl_uint_64 )
153 macro(
float , vxl_uint_64 )
154 macro(
double , vxl_uint_64 )
157 #define macro( inout )\ 159 inline void vil_convert_cast_pixel<inout, inout >::operator () ( \ 160 inout v, inout& d) const { d=v; } 182 inline void vil_convert_cast_pixel<in,std::complex<double> >::operator () (in v, std::complex<double>& d) const \ 183 { d = std::complex<double>(double(v),0.0); } \ 185 inline void vil_convert_cast_pixel<in,std::complex<float> >::operator () (in v, std::complex<float>& d) const \ 186 { d = std::complex<float>(float(v),0.0f); } 198 #define macro( out )\ 200 inline void vil_convert_cast_pixel<std::complex<double>,out >::operator () (std::complex<double> d, out& v) const \ 201 { v = (out)(d.real()); } \ 203 inline void vil_convert_cast_pixel<std::complex<float>,out >::operator () (std::complex<float> d, out& v) const \ 204 { v = (out)(d.real()); } 216 #endif // DOXYGEN_SHOULD_SKIP_THIS 219 #if defined(_MSC_VER) 223 # pragma warning( push ) 224 # pragma warning( disable : 4800 ) 227 template <
class In,
class Out>
230 d = static_cast<Out>(
v);
233 #if defined(_MSC_VER) 234 # pragma warning( pop ) 244 template <
class inP,
class outP>
266 template <
class outP>
271 vil_convert_cast( vil_image_view< typename vil_pixel_format_type_of<T >::component_type >(src), dest );\ 274 switch ( src->pixel_format() )
316 template <
class In,
class Out>
323 #ifndef DOXYGEN_SHOULD_SKIP_THIS 325 #define macro( in , out )\ 327 inline void vil_convert_round_pixel<in, out >::operator () ( \ 328 in v, out& d) const { \ 329 d.r = (out::value_type)(v.r+0.5); \ 330 d.g = (out::value_type)(v.g+0.5); \ 331 d.b = (out::value_type)(v.b+0.5); } 352 #define macro( in , out )\ 354 inline void vil_convert_round_pixel<in, out >::operator () (in v, out& d) \ 356 d.r = (out::value_type)(v.r); \ 357 d.g = (out::value_type)(v.g); \ 358 d.b = (out::value_type)(v.b); } 362 #define macro( in , out )\ 364 inline void vil_convert_round_pixel<in, out >::operator () (in v, out& d) \ 366 d.r = (out::value_type)(v.r+0.5); \ 367 d.g = (out::value_type)(v.g+0.5); \ 368 d.b = (out::value_type)(v.b+0.5); \ 369 d.a = (out::value_type)(v.a+0.5); } 389 #define macro( in , out )\ 391 inline void vil_convert_round_pixel<in, out >::operator () (in v, out& d) \ 393 d.r = (out::value_type)(v.r); \ 394 d.g = (out::value_type)(v.g); \ 395 d.b = (out::value_type)(v.b); \ 396 d.a = (out::value_type)(v.a); } 401 #define macro( in , out )\ 403 inline void vil_convert_round_pixel<in, out >::operator () (in v, out& d) \ 404 const { d = (out)(v > 0.0 ? v + 0.5 : v - 0.5); } 405 macro(
float , vxl_byte )
406 macro(
double , vxl_byte )
407 macro(
float , vxl_sbyte )
408 macro(
double , vxl_sbyte )
409 macro(
float , vxl_int_16 )
410 macro(
double , vxl_int_16 )
411 macro(
float , vxl_uint_16 )
412 macro(
double , vxl_uint_16 )
413 macro(
float , vxl_int_32 )
414 macro(
double , vxl_int_32 )
415 macro(
float , vxl_uint_32 )
416 macro(
double , vxl_uint_32 )
418 macro(
float , vxl_int_64 )
419 macro(
double , vxl_int_64 )
420 macro(
float , vxl_uint_64 )
421 macro(
double , vxl_uint_64 )
424 #endif // DOXYGEN_SHOULD_SKIP_THIS 427 template <
class In,
class Out>
442 template <
class inP,
class outP>
454 template <
class inP,
class outP>
469 template <
class inP,
class outP>
472 const unsigned nplanes)
477 assert((nplanes == 2) || (nplanes == 4));
481 for (
unsigned j = 0; j < src.
nj(); ++j)
482 for (
unsigned i = 0; i < src.
ni(); ++i)
483 for (
unsigned k = 0; k < nplanes-1; ++k)
493 template <
class rgbP,
class outP>
496 double rw=0.2125,
double gw=0.7154,
double bw=0.0721)
511 template <
class inP,
class outP>
514 double rw=0.2125,
double gw=0.7154,
double bw=0.0721)
520 for (
unsigned j = 0; j < src.
nj(); ++j)
521 for (
unsigned i = 0; i < src.
ni(); ++i)
523 src(i,j,0)*rw + src(i,j,1)*gw + src(i,j,2)*bw, dest(i,j));
535 double a = -1.0*double(min_b);
537 if (max_b-min_b >0) b = 255.0/(max_b-min_b);
539 for (
unsigned p = 0; p < src.
nplanes(); ++p)
540 for (
unsigned j = 0; j < src.
nj(); ++j)
541 for (
unsigned i = 0; i < src.
ni(); ++i)
542 dest(i,j,p) = static_cast<vxl_byte>( b*( src(i,j,p)+ a ) );
557 double dest_lo,
double dest_hi)
559 inP min_b=0, max_b=0;
563 b = static_cast<double>(dest_hi-dest_lo)/static_cast<double>(max_b-min_b);
564 double a = -1.0*min_b*b + dest_lo;
566 for (
unsigned p = 0; p < src.
nplanes(); ++p)
567 for (
unsigned j = 0; j < src.
nj(); ++j)
568 for (
unsigned i = 0; i < src.
ni(); ++i)
569 dest(i,j,p) = b*src(i,j,p) + a;
576 float dest_lo,
float dest_hi)
578 inP min_b=0, max_b=0;
582 b = (dest_hi-dest_lo)/static_cast<float>(max_b-min_b);
583 float a = -1.0f*min_b*b + dest_lo;
585 for (
unsigned p = 0; p < src.
nplanes(); ++p)
586 for (
unsigned j = 0; j < src.
nj(); ++j)
587 for (
unsigned i = 0; i < src.
ni(); ++i)
588 dest(i,j,p) = b*src(i,j,p) + a;
599 const double dest_lo,
600 const double dest_hi)
602 double ddest = dest_hi - dest_lo;
603 double dsrc = static_cast<double>(src_hi - src_lo);
604 double dds = ddest / dsrc;
607 for (
unsigned p = 0; p < src.
nplanes(); ++p)
608 for (
unsigned j = 0; j < src.
nj(); ++j)
609 for (
unsigned i = 0; i < src.
ni(); ++i)
612 dest(i,j,p) = s<=src_lo ? dest_lo :
613 s>=src_hi ? dest_hi :
614 dest_lo + dds*static_cast<double>(s-src_lo);
628 float ddest = dest_hi - dest_lo;
629 float dsrc = static_cast<float>(src_hi - src_lo);
630 float dds = ddest / dsrc;
633 for (
unsigned p = 0; p < src.
nplanes(); ++p)
634 for (
unsigned j = 0; j < src.
nj(); ++j)
635 for (
unsigned i = 0; i < src.
ni(); ++i)
638 dest(i,j,p) = s<=src_lo ? dest_lo :
639 s>=src_hi ? dest_hi :
640 dest_lo + dds*static_cast<float>(s-src_lo);
651 const unsigned short dest_lo,
652 const unsigned short dest_hi)
654 const double ddest = dest_hi - dest_lo;
655 const double dsrc = static_cast<double>(src_hi - src_lo);
656 const double dds = ddest / dsrc;
659 for (
unsigned p = 0; p < src.
nplanes(); ++p)
660 for (
unsigned j = 0; j < src.
nj(); ++j)
661 for (
unsigned i = 0; i < src.
ni(); ++i)
664 dest(i,j,p) = s<=src_lo ? dest_lo :
665 s>=src_hi ? dest_hi :
666 static_cast<unsigned short>(dest_lo + dds*(s-src_lo)+0.5);
677 const vxl_byte dest_lo,
678 const vxl_byte dest_hi)
680 const double ddest = dest_hi - dest_lo;
681 const double dsrc = static_cast<double>(src_hi - src_lo);
682 const double dds = ddest / dsrc;
685 for (
unsigned p = 0; p < src.
nplanes(); ++p)
686 for (
unsigned j = 0; j < src.
nj(); ++j)
687 for (
unsigned i = 0; i < src.
ni(); ++i)
690 dest(i,j,p) = s<=src_lo ? dest_lo :
691 s>=src_hi ? dest_hi :
692 static_cast<vxl_byte>(dest_lo + dds*(s-src_lo)+0.5);
704 const double dsrc = static_cast<double>(src_hi - src_lo);
705 const double dds = 255.0 / dsrc;
708 for (
unsigned p = 0; p < src.
nplanes(); ++p)
709 for (
unsigned j = 0; j < src.
nj(); ++j)
710 for (
unsigned i = 0; i < src.
ni(); ++i)
713 dest(i,j,p) = s<=src_lo ? 0 :
714 static_cast<vxl_byte>( s>=src_hi ? 255 : (dds*(s-src_lo)+0.5) );
727 template <
class outP>
738 #ifndef DOXYGEN_SHOULD_SKIP_THIS 739 #define macro(F , T) \ 740 case F: vil_convert_cast( vil_image_view<T >(*src), dest_ref ); break 743 macro( VIL_PIXEL_FORMAT_UINT_64, vxl_uint_64 );
744 macro( VIL_PIXEL_FORMAT_INT_64, vxl_int_64 );
756 #endif // DOXYGEN_SHOULD_SKIP_THIS 759 src->pixel_format(),
"vil_convert_cast") );
776 template <
class outP>
780 VXL_DEPRECATED_MACRO(
"void vil_convert_cast(const vil_image_view_base_sptr&," 781 " vil_image_view<outP>&)" );
783 switch ( src->pixel_format() )
785 #ifndef DOXYGEN_SHOULD_SKIP_THIS 786 #define macro(F , T) \ 787 case F: vil_convert_cast( vil_image_view<T >(src), dest ); break; 790 macro( VIL_PIXEL_FORMAT_UINT_64, vxl_uint_64 )
791 macro( VIL_PIXEL_FORMAT_INT_64, vxl_int_64 )
803 #endif // DOXYGEN_SHOULD_SKIP_THIS 826 template <
class outP>
843 #ifndef DOXYGEN_SHOULD_SKIP_THIS 844 #define macro( F , T ) \ 846 vil_image_view<T > src1 = src; \ 847 vil_transform2(src1, dest_ref, vil_convert_round_pixel<T , outP>()); \ 852 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
853 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
862 #endif // DOXYGEN_SHOULD_SKIP_THIS 865 src->pixel_format(),
"vil_convert_round") );
887 #ifndef DOXYGEN_SHOULD_SKIP_THIS 888 #define macro( F , T )\ 890 vil_image_view<T > src_ref(src); \ 891 if (!src_ref) return vil_image_view_base_sptr(); \ 892 if (src_ref.planestep()==1) return src; \ 893 const unsigned ni=src->ni(), nj=src->nj(), nplanes=src->nplanes(); \ 894 vil_memory_chunk_sptr chunk = new vil_memory_chunk(ni*nj*nplanes*sizeof(T), \ 895 vil_pixel_format_component_format(F)); \ 896 dest = new vil_image_view<T >(chunk, reinterpret_cast<T*>(chunk->data()), \ 897 ni, nj, nplanes, nplanes, nplanes*ni, 1); \ 898 vil_image_view<T > & dest_ref = static_cast<vil_image_view<T >&>(*dest); \ 899 vil_copy_reformat(src_ref, dest_ref); \ 904 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
905 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
914 #endif // DOXYGEN_SHOULD_SKIP_THIS 918 src->pixel_format(),
"vil_convert_to_compound_order") );
937 template <
class outP>
941 VXL_DEPRECATED_MACRO(
"vil_convert_to_grey_using_average<outP>(" 942 "const vil_image_view_base_sptr &, outP)" );
959 #ifndef DOXYGEN_SHOULD_SKIP_THIS 960 #define macro( F , T ) \ 962 vil_image_view<T > src1 = *src; \ 963 vil_math_mean_over_planes(src1, dest, double()); \ 968 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
969 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
978 #endif // DOXYGEN_SHOULD_SKIP_THIS 1004 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1005 #define macro( F , T ) \ 1008 if (src->nplanes() == 1 && \ 1009 vil_pixel_format_component_format(src->pixel_format())==1) \ 1012 vil_image_view<T > dest; \ 1013 vil_image_view<T > src1 = *src; \ 1014 vil_math_mean_over_planes(src1, dest, double()); \ 1015 return vil_image_view_base_sptr(new vil_image_view<T >(dest)); } 1019 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
1020 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
1029 #endif // DOXYGEN_SHOULD_SKIP_THIS 1032 src->pixel_format(),
"vil_convert_to_grey_using_average") );
1053 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1054 #define macro( F , T ) \ 1057 if (src->nplanes() == 1 && \ 1058 vil_pixel_format_num_components(src->pixel_format()) == 1) \ 1059 return vil_image_view_base_sptr(src); \ 1060 vil_image_view<T > src1 = src; \ 1061 vil_image_view<double> dest1; \ 1062 vil_convert_planes_to_grey(src1, dest1, rw, gw, bw); \ 1063 vil_image_view<T > dest; \ 1064 vil_convert_round(dest1,dest); \ 1065 return vil_image_view_base_sptr(new vil_image_view<T >(dest)); } 1069 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
1070 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
1079 #endif // DOXYGEN_SHOULD_SKIP_THIS 1082 src->pixel_format(),
"vil_convert_to_grey_using_rgb_weighting") );
1104 #if 0 // deprecated version of this function now commented out 1115 template <
class outP>
1123 VXL_DEPRECATED_MACRO(
"vil_convert_to_grey_using_rgb_weighting<outP>(" 1124 "const vil_image_view_base_sptr &, outP)" );
1141 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1142 #define macro( F , T ) \ 1144 vil_image_view<T > src1 = src; \ 1145 vil_image_view<double> dest1; \ 1146 vil_convert_planes_to_grey(src1, dest1, rw, gw, bw); \ 1147 vil_convert_round(dest1,dest); \ 1152 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
1153 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
1163 #endif // DOXYGEN_SHOULD_SKIP_THIS 1199 if (!src || n_planes == 0)
1204 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1205 #define macro( F, T ) \ 1207 vil_image_view<T > src_ref = src; \ 1208 if (!src_ref) return vil_image_view_base_sptr(); \ 1210 if (src_ref.nplanes() >= n_planes) \ 1211 return vil_image_view_base_sptr( new vil_image_view<T >( \ 1212 vil_planes(vil_image_view<T > (src),0,1,n_planes) )); \ 1214 vil_image_view_base_sptr dest = new vil_image_view<T >(src_ref.ni(), src_ref.nj(), n_planes); \ 1215 vil_image_view<T > & dest_ref = static_cast<vil_image_view<T >&>(*dest); \ 1216 vil_image_view<T > dest_slices = vil_planes(dest_ref, 0, 1, src_ref.nplanes()); \ 1217 vil_copy_reformat(src_ref, dest_slices); \ 1218 vil_image_view<T > src_slice(vil_plane(src_ref, 0)); \ 1219 for (unsigned i=src_ref.nplanes(); i<n_planes; ++i) { \ 1220 dest_slices = vil_plane(dest_ref, i); \ 1221 vil_copy_reformat(src_slice, dest_slices); } \ 1226 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
1227 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
1236 #endif // DOXYGEN_SHOULD_SKIP_THIS 1239 src->pixel_format(),
"vil_convert_to_n_planes") );
1255 template <
class outP>
1267 if (std::numeric_limits<outP>::is_integer)
1269 hi = std::numeric_limits<outP>::max()+0.999;
1270 lo = std::numeric_limits<outP>::min();
1283 #ifndef DOXYGEN_SHOULD_SKIP_THIS 1284 #define macro( F , T ) \ 1286 vil_image_view<T> src_ref = src; \ 1287 if (!src_ref) return vil_image_view_base_sptr(); \ 1288 vil_convert_stretch_range(src_ref, inter, lo, hi); \ 1289 vil_convert_cast(inter, dest_ref); \ 1294 macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
1295 macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
1304 #endif // DOXYGEN_SHOULD_SKIP_THIS 1307 src->pixel_format(),
"vil_convert_stretch_range") );
1315 #endif // vil_convert_h_
vil_image_view_base_sptr vil_convert_to_n_planes(unsigned n_planes, const vil_image_view_base_sptr &src)
Create an n plane image from any image src.
vil_smart_ptr< vil_image_view_base > vil_image_view_base_sptr
Performs conversion between different pixel types.
A templated smart pointer class.
Various mathematical manipulations of 2D images.
void vil_convert_stretch_range_limited(const vil_image_view< inP > &src, vil_image_view< double > &dest, const inP src_lo, const inP src_hi, const double dest_lo, const double dest_hi)
Convert src image<inP> to dest image<double> by stretching input range [src_lo, src_hi] to output ran...
Concrete view of image data of type T held in memory.
vil_image_view_base_sptr vil_convert_to_grey_using_rgb_weighting(double rw, double gw, double bw, const vil_image_view_base_sptr &src)
Create a greyscale image from any image src.
void vil_convert_rgb_to_grey(const vil_image_view< rgbP > &src, vil_image_view< outP > &dest, double rw=0.2125, double gw=0.7154, double bw=0.0721)
Convert single plane rgb (or rgba) images to greyscale.
Convert various rgb types to greyscale, using given weights.
void set_size(unsigned ni, unsigned nj) override
resize current planes to ni x nj.
Exceptions thrown by vil, and a mechanism for turning them off.
void operator()(In v, Out &d) const
void vil_transform2(const vil_image_view< inP > &src, vil_image_view< outP > &dest, Op functor)
Apply a binary function to each pixel in src and dest that modifies dest.
unsigned ni() const
Width.
unsigned nj() const
Height.
Performs rounding between different pixel types.
vil_image_view_base_sptr vil_convert_to_grey_using_average(const vil_image_view_base_sptr &src)
Create a greyscale image of specified pixel type from any image src.
Various image copying functions.
void vil_convert_stretch_range(const vil_image_view< T > &src, vil_image_view< vxl_byte > &dest)
Convert src to byte image dest by stretching to range [0,255].
void vil_convert_cast(const vil_image_view< inP > &src, vil_image_view< outP > &dest)
Cast one pixel type to another.
void operator()(vil_rgb< inP > v, outP &d) const
This is the appropriate pixel type for RGBA colour images.
vil_convert_rgb_to_grey_pixel(double rw, double gw, double bw)
unsigned nplanes() const
Number of planes.
void vil_convert_round(const vil_image_view< inP > &src, vil_image_view< outP > &dest)
Convert one pixel type to another with rounding.
vil_pixel_format pixel_format() const override
Return a description of the concrete data pixel type.
void vil_convert_planes_to_grey(const vil_image_view< inP > &src, vil_image_view< outP > &dest, double rw=0.2125, double gw=0.7154, double bw=0.0721)
Convert first three planes of src image to grey, assuming rgb.
vil_image_view_base_sptr vil_convert_to_component_order(const vil_image_view_base_sptr &src)
Force data to be suitable for viewing as multi component view.
void vil_exception_warning(T exception)
Throw an exception indicating a potential problem.
void operator()(In v, Out &d) const
void vil_math_value_range(const vil_image_view< T > &view, T &min_value, T &max_value)
Compute minimum and maximum values over view.
This is the appropriate pixel type for 24-bit colour images.
void vil_convert_merge_alpha(const vil_image_view< inP > &src, vil_image_view< outP > &dest, const unsigned nplanes)
Convert images with alpha plane (variable or binary) to images without alpha plane.