13 #include <type_traits> 22 template <
typename FloatingPo
intType>
25 static_assert(std::is_floating_point<FloatingPointType>::value,
"Can only dither floating point values.");
27 return std::round(value);
31 template <
typename IntegerType>
34 static_assert(std::is_integral<IntegerType>::value,
"Numerator and denominator must be integral types.");
37 IntegerType quotient = numerator / denominator;
38 IntegerType remainder = numerator % denominator;
40 if (remainder >= (denominator / 2))
44 else if (std::is_signed<IntegerType>::value && (remainder <= -(denominator / 2)))
59 template <
typename FloatingPo
intType>
62 static_assert(std::is_floating_point<FloatingPointType>::value,
"Can only dither floating point values.");
64 return (std::rand() % 2 == 0) ? std::floor(value) : std::ceil(value);
68 template <
typename IntegerType>
71 static_assert(std::is_integral<IntegerType>::value,
"Numerator and denominator must be integral types.");
75 IntegerType quotient = numerator / denominator;
76 IntegerType remainder = numerator % denominator;
80 if (std::rand() % 2 == 0)
85 else if (remainder < 0)
87 if (std::rand() % 2 == 0)
103 template <
typename FloatingPo
intType>
106 static_assert(std::is_floating_point<FloatingPointType>::value,
"Can only dither floating point values.");
109 FloatingPointType noise =
static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX);
112 return std::round(value + noise);
116 template <
typename IntegerType>
119 static_assert(std::is_integral<IntegerType>::value,
"Numerator and denominator must be integral types.");
123 IntegerType quotient = numerator / denominator;
124 IntegerType remainder = numerator % denominator;
128 IntegerType noise = std::rand() % denominator;
129 if (remainder + noise >= denominator)
134 else if (remainder < 0)
136 IntegerType noise = std::rand() % denominator;
137 if (remainder - noise < -denominator)
Definition: ditherers.h:101
static IntegerType DitherFractionalValue(IntegerType numerator, IntegerType denominator)
Definition: ditherers.h:69
Definition: ditherers.h:57
static IntegerType DitherFractionalValue(IntegerType numerator, IntegerType denominator)
Definition: ditherers.h:32
JSON (JavaScript Object Notation).
Definition: adsapi.h:16
#define assert(expr)
Definition: assertion.h:47
Definition: ditherers.h:20
static FloatingPointType DitherFloatValue(FloatingPointType value)
Definition: ditherers.h:23
static IntegerType DitherFractionalValue(IntegerType numerator, IntegerType denominator)
Definition: ditherers.h:117
static FloatingPointType DitherFloatValue(FloatingPointType value)
Definition: ditherers.h:60
static FloatingPointType DitherFloatValue(FloatingPointType value)
Definition: ditherers.h:104