Twitch SDK (Internal)
ditherers.h
Go to the documentation of this file.
1 /********************************************************************************************
2  * Twitch Broadcasting SDK
3  *
4  * This software is supplied under the terms of a license agreement with Twitch Interactive, Inc. and
5  * may not be copied or used except in accordance with the terms of that agreement
6  * Copyright (c) 2012-2017 Twitch Interactive, Inc.
7  *
8  *********************************************************************************************/
9 
10 #pragma once
11 
12 #include <cmath>
13 #include <type_traits>
14 
15 namespace ttv
16 {
21  {
22  template <typename FloatingPointType>
23  static FloatingPointType DitherFloatValue(FloatingPointType value)
24  {
25  static_assert(std::is_floating_point<FloatingPointType>::value, "Can only dither floating point values.");
26 
27  return std::round(value);
28  }
29 
30 
31  template <typename IntegerType>
32  static IntegerType DitherFractionalValue(IntegerType numerator, IntegerType denominator)
33  {
34  static_assert(std::is_integral<IntegerType>::value, "Numerator and denominator must be integral types.");
35  assert(denominator > 0);
36 
37  IntegerType quotient = numerator / denominator;
38  IntegerType remainder = numerator % denominator;
39 
40  if (remainder >= (denominator / 2))
41  {
42  quotient++;
43  }
44  else if (std::is_signed<IntegerType>::value && (remainder <= -(denominator / 2)))
45  {
46  quotient--;
47  }
48 
49  return quotient;
50  }
51  };
52 
53 
58  {
59  template <typename FloatingPointType>
60  static FloatingPointType DitherFloatValue(FloatingPointType value)
61  {
62  static_assert(std::is_floating_point<FloatingPointType>::value, "Can only dither floating point values.");
63 
64  return (std::rand() % 2 == 0) ? std::floor(value) : std::ceil(value);
65  }
66 
67 
68  template <typename IntegerType>
69  static IntegerType DitherFractionalValue(IntegerType numerator, IntegerType denominator)
70  {
71  static_assert(std::is_integral<IntegerType>::value, "Numerator and denominator must be integral types.");
72  assert(denominator > 0);
73 
74 
75  IntegerType quotient = numerator / denominator;
76  IntegerType remainder = numerator % denominator;
77 
78  if (remainder > 0)
79  {
80  if (std::rand() % 2 == 0)
81  {
82  quotient++;
83  }
84  }
85  else if (remainder < 0)
86  {
87  if (std::rand() % 2 == 0)
88  {
89  quotient--;
90  }
91  }
92 
93  return quotient;
94  }
95  };
96 
97 
102  {
103  template <typename FloatingPointType>
104  static FloatingPointType DitherFloatValue(FloatingPointType value)
105  {
106  static_assert(std::is_floating_point<FloatingPointType>::value, "Can only dither floating point values.");
107 
108  // Generate noise between -0.5 and 0.5.
109  FloatingPointType noise = static_cast<double>(std::rand()) / static_cast<double>(RAND_MAX);
110  noise -= 0.5;
111 
112  return std::round(value + noise);
113  }
114 
115 
116  template <typename IntegerType>
117  static IntegerType DitherFractionalValue(IntegerType numerator, IntegerType denominator)
118  {
119  static_assert(std::is_integral<IntegerType>::value, "Numerator and denominator must be integral types.");
120  assert(denominator > 0);
121 
122 
123  IntegerType quotient = numerator / denominator;
124  IntegerType remainder = numerator % denominator;
125 
126  if (remainder > 0)
127  {
128  IntegerType noise = std::rand() % denominator;
129  if (remainder + noise >= denominator)
130  {
131  quotient++;
132  }
133  }
134  else if (remainder < 0)
135  {
136  IntegerType noise = std::rand() % denominator;
137  if (remainder - noise < -denominator)
138  {
139  quotient--;
140  }
141  }
142 
143  return quotient;
144  }
145  };
146 }
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