Saturate cast

group NVCV_CPP_CUDATOOLS_SATURATECAST

Functions

template<typename T, typename U, class = Require<(NumComponents<T> == NumComponents<U>) || (NumComponents<T> == 0 && HasTypeTraits<U>)>> __host__ __device__ auto SaturateCast (U u)

Metafunction to saturate cast all elements to a target type.

This function saturate casts (clamping with potential rounding) all elements to the range defined by the template argument type T. For instance, a float4 with any values (can be below 0 and above 255) can be casted to an uchar4 rounding-then-saturating each value to be in between 0 and 255 (see example below). It is a requirement of SaturateCast that both types have the same number of components or T is a regular C type.

using DataType = MakeType<uchar, 4>;
using FloatDataType = ConvertBaseTypeTo<float, DataType>;
FloatDataType res = ...; // res component values are in [0, 1]
DataType pix = SaturateCast<DataType>(res); // pix are in [0, 255]
Template Parameters:
  • T – Type that defines the target range to cast.

  • U – Type of the source value (with 1 to 4 elements) passed as argument.

Parameters:

u[in] Source value to cast all elements to range of base type of T

Returns:

The value with all elements clamped and potentially rounded.