saturate_cast.h
Go to the documentation of this file.
1 /*
2 * ECVL - European Computer Vision Library
3 * Version: 0.3.4
4 * copyright (c) 2021, Università degli Studi di Modena e Reggio Emilia (UNIMORE), AImageLab
5 * Authors:
6 * Costantino Grana (costantino.grana@unimore.it)
7 * Federico Bolelli (federico.bolelli@unimore.it)
8 * Michele Cancilla (michele.cancilla@unimore.it)
9 * Laura Canalini (laura.canalini@unimore.it)
10 * Stefano Allegretti (stefano.allegretti@unimore.it)
11 * All rights reserved.
12 */
13 
14 #ifndef ECVL_SATURATE_CAST_H_
15 #define ECVL_SATURATE_CAST_H_
16 
17 #include "datatype.h"
18 
19 namespace ecvl
20 {
21 
35 template<DataType ODT, typename IDT>
37 {
38  using basetype = typename TypeInfo<ODT>::basetype;
39 
40  if (v > std::numeric_limits<basetype>::max()) {
41  return std::numeric_limits<basetype>::max();
42  }
43  if (v < std::numeric_limits<basetype>::lowest()) {
44  return std::numeric_limits<basetype>::lowest();
45  }
46 
47  return static_cast<basetype>(v);
48 }
49 
63 template<typename ODT, typename IDT>
64 ODT saturate_cast(const IDT& v)
65 {
66  if (v > std::numeric_limits<ODT>::max()) {
67  return std::numeric_limits<ODT>::max();
68  }
69  if (v < std::numeric_limits<ODT>::lowest()) {
70  return std::numeric_limits<ODT>::lowest();
71  }
72 
73  return static_cast<ODT>(v);
74 }
75 } // namespace ecvl
76 #endif // ECVL_SATURATE_CAST_H_
void basetype
Definition: datatype.h:61
TypeInfo< ODT >::basetype saturate_cast(IDT v)
Saturate a value (of any type) to the specified type.
Definition: saturate_cast.h:36