core/vil1/vil1_clamp.h

Go to the documentation of this file.
00001 // This is core/vil1/vil1_clamp.h
00002 #ifndef vil1_clamp_h_
00003 #define vil1_clamp_h_
00004 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00005 #pragma interface
00006 #endif
00007 //:
00008 // \file
00009 // \brief Templated vil1_clamping functions
00010 //
00011 // To ensure value is in
00012 // a range appropriate for the data type (e.g. 0-255 for bytes).
00013 //
00014 // \author Andrew Fitzgibbon and David Capel
00015 
00016 #include <vil1/vil1_image.h>
00017 
00018 //: Adaptor which returns a vil1_image with pixel components clamped to given range.
00019 // Fits nicely into the functional composition and lazy evaluation scheme
00020 // provided by the other vil1 adaptors.
00021 // Old code can be implemented in the new style as
00022 //   vil1_image_as_byte(vil1_clamp(img, 0, 255));
00023 //   vil1_image_as_rgb_byte(vil1_clamp(img, 0, 255));
00024 vil1_image vil1_clamp(vil1_image src, double range_min, double range_max);
00025 
00026 //: Convenience templated functions for clamping of a single pixel.
00027 //    (vxl_byte)vil1_clamp_pixel(g, 0, 255);
00028 //    (vil1_rgb<vxl_byte>)vil1_clamp_pixel(rgb, 0, 255);
00029 template <class V>
00030 inline
00031 V vil1_clamp_pixel(V const& b, double range_min, double range_max)
00032 {
00033   return (b < V(range_min) ? V(range_min) : (b > V(range_max) ? V(range_max) : b));
00034 }
00035 
00036 // capes@robots : The functions below are deprecated.
00037 // Use the adaptor style or the vil1_clamp_pixel functions instead.
00038 #if 0
00039 //:
00040 // Default behaviour just returns value. Clamping of double to return byte is
00041 // also defined here. Other clamps, such as vil1_rgb<double> to vil1_rgb<byte>
00042 // may be defined in the appropriate places.
00043 //
00044 #if !defined(VCL_SGI_CC)
00045 template <class V, class U>
00046 inline
00047 U vil1_clamp(const V& b, U*)
00048 {
00049    return U(b);
00050 }
00051 #endif
00052 
00053 inline
00054 unsigned char vil1_clamp(const float& d, unsigned char *)
00055 {
00056   if (d > 255.0)
00057     return 255;
00058   else if (d < 0.0)
00059     return 0;
00060   else
00061     return (unsigned char)d;
00062 }
00063 
00064 inline
00065 unsigned char vil1_clamp(const double& d, unsigned char *)
00066 {
00067   if (d > 255.0)
00068     return 255;
00069   else if (d < 0.0)
00070     return 0;
00071   else
00072     return (unsigned char)d;
00073 }
00074 #endif
00075 
00076 #endif // vil1_clamp_h_

Generated on Sun Sep 7 05:08:27 2008 for core/vil1 by  doxygen 1.5.1