core/vil/vil_clamp.cxx
Go to the documentation of this file.
00001 // This is core/vil/vil_clamp.cxx
00002 #ifdef VCL_NEEDS_PRAGMA_INTERFACE
00003 #pragma implementation
00004 #endif
00005 //:
00006 // \file
00007 // \author Ian Scott.
00008 //
00009 // \verbatim
00010 //  Modifications
00011 //   23 Oct.2003 - Peter Vanroose - Added support for 64-bit int pixels
00012 // \endverbatim
00013 //
00014 //-----------------------------------------------------------------------------
00015 
00016 #include "vil_clamp.h"
00017 #include <vcl_cstring.h>
00018 #include <vcl_cassert.h>
00019 #include <vil/vil_property.h>
00020 #include <vil/vil_exception.h>
00021 
00022 
00023 vil_image_resource_sptr vil_clamp(const vil_image_resource_sptr &src, double lo, double hi)
00024 {
00025   return new vil_clamp_image_resource(src, lo, hi);
00026 }
00027 
00028 
00029 vil_clamp_image_resource::vil_clamp_image_resource(vil_image_resource_sptr const& src, double lo, double hi):
00030   src_(src),
00031   lo_(lo),
00032   hi_(hi)
00033 {
00034   assert (lo <= hi);
00035 }
00036 
00037   //: Extra property information
00038 bool vil_clamp_image_resource::get_property(char const* tag, void* property_value) const
00039 {
00040   if (0==vcl_strcmp(tag, vil_property_read_only))
00041     return property_value ? (*static_cast<bool*>(property_value)) = true : true;
00042 
00043   return src_->get_property(tag, property_value);
00044 }
00045 
00046 vil_image_view_base_sptr vil_clamp_image_resource::get_copy_view(unsigned i0, unsigned ni,
00047                                                                  unsigned j0, unsigned nj) const
00048 {
00049   vil_image_view_base_sptr vs = src_->get_copy_view(i0, ni, j0, nj);
00050   if (!vs) return 0;
00051 
00052   switch (vs->pixel_format())
00053   {
00054 #define macro( F , T ) \
00055   case F : \
00056     vil_clamp(static_cast<vil_image_view<T >&>(*vs), static_cast<vil_image_view<T >&>(*vs), \
00057               T (lo_), T (hi_)); \
00058     break;
00059 
00060       macro(VIL_PIXEL_FORMAT_BYTE , vxl_byte )
00061       macro(VIL_PIXEL_FORMAT_SBYTE , vxl_sbyte )
00062 #if VXL_HAS_INT_64
00063       macro(VIL_PIXEL_FORMAT_UINT_64 , vxl_uint_64 )
00064       macro(VIL_PIXEL_FORMAT_INT_64 , vxl_int_64 )
00065 #endif
00066       macro(VIL_PIXEL_FORMAT_UINT_32 , vxl_uint_32 )
00067       macro(VIL_PIXEL_FORMAT_INT_32 , vxl_int_32 )
00068       macro(VIL_PIXEL_FORMAT_UINT_16 , vxl_uint_16 )
00069       macro(VIL_PIXEL_FORMAT_INT_16 , vxl_int_16 )
00070       macro(VIL_PIXEL_FORMAT_FLOAT , float )
00071       macro(VIL_PIXEL_FORMAT_DOUBLE , double )
00072 // How might you clamp a vcl_complex image ?
00073 #undef macro
00074   default:
00075     vil_exception_warning(vil_exception_unsupported_pixel_format(
00076       vs->pixel_format(), "vil_clamp_image_resource::get_copy_view") );
00077     return 0;
00078   }
00079   return vs;
00080 }