Range cast

group NVCV_CPP_CUDATOOLS_RANGECAST

Functions

template<typename T, typename U, class = Require<HasTypeTraits<T, U> && !IsCompound<T>>> __host__ __device__ auto RangeCast (U u)

Metafunction to range cast (scale) all elements to a target range.

This function range casts (that is scales) all elements to the range defined by the template argument type T. For instance, a float4 with all elements between 0 and 1 can be casted to an uchar4 with scaling of each element to be in between 0 and 255 (see example below). It is a requirement of RangeCast that both types have type traits and type T must be a regular C type. Several examples of possible target range giving a source range, depending on the limits of regular C types, for the RangeCast function are as follows:

Source type U

Target type T

Source range

Target range

signed char

float

[-128, 127]

[-1, 1]

float

unsigned char

[0, 1]

[0, 255]

short

unsigned int

[-32768, 32767]

[0, 4294967295]

double

int

[-1, 1]

[-2147483648, 2147483647]

unsigned short

double

[0, 65535]

[0, 1]

using DataType = MakeType<uchar, 4>;
using FloatDataType = ConvertBaseTypeTo<float, DataType>;
FloatDataType res = ...; // res component values are in [0, 1]
DataType pix = RangeCast<BaseType<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 type T.

Returns:

The value with all elements scaled.