implement mapping table
Stefan Schuermans

Stefan Schuermans commited on 2017-06-11 13:35:25
Showing 2 changed files, with 45 additions and 0 deletions.

... ...
@@ -17,6 +17,9 @@
17 17
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 18
  */
19 19
 
20
+#include <math.h>
21
+#include <stdint.h>
22
+
20 23
 #include "mapping.h"
21 24
 
22 25
 /// default constructor
... ...
@@ -25,6 +28,7 @@ Mapping::Mapping():
25 28
   m_factor(0.0),
26 29
   m_gamma(1.0)
27 30
 {
31
+  update();
28 32
 }
29 33
 
30 34
 /// constructor based on parameters
... ...
@@ -33,5 +37,31 @@ Mapping::Mapping(double base, double factor, double gamma):
33 37
   m_factor(factor),
34 38
   m_gamma(gamma)
35 39
 {
40
+  update();
41
+}
42
+
43
+/// update internal mapping table based on parameters
44
+void Mapping::update()
45
+{
46
+  /* mapping formula:
47
+   *   display := base + factor * video ** (1.0 / gamma)
48
+   * display values are coming in via IPv4/UDP
49
+   * simulator needs to show video values
50
+   *   video := ((display - base) / factor) ** gamma
51
+   */
52
+  for (unsigned int display_i = 0; display_i < 256; ++display_i) {
53
+    double display = (double)display_i / 255.0;
54
+    double video = pow((display - m_base) / m_factor, m_gamma);
55
+    uint8_t video_u8;
56
+    if (video < 0.0) {
57
+      video_u8 = 0;
58
+    } else if (video > 1.0) {
59
+      video_u8 = 255;
60
+    } else {
61
+      video_u8 = (uint8_t)round(255.0 * video);
62
+    }
63
+    m_display2video[display_i] = video_u8;
36 64
   }
65
+}
66
+
37 67
 
... ...
@@ -20,6 +20,8 @@
20 20
 #ifndef MAPPING_H
21 21
 #define MAPPING_H
22 22
 
23
+#include <stdint.h>
24
+
23 25
 /// mapping of input values to LED output values for a color channel
24 26
 class Mapping
25 27
 {
... ...
@@ -39,6 +41,16 @@ public:
39 41
   /// constructor based on parameters
40 42
   Mapping(double base, double factor, double gamma);
41 43
 
44
+  /// map display value to video value
45
+  uint8_t display2video(uint8_t display) const
46
+  {
47
+    return m_display2video[display];
48
+  }
49
+
50
+protected:
51
+  /// update internal mapping table based on parameters
52
+  void update();
53
+
42 54
 protected:
43 55
    /// parameters of mapping: display := base + factor * video ** (1.0 / gamma)
44 56
    //@{
... ...
@@ -46,6 +58,9 @@ protected:
46 58
    double m_factor;
47 59
    double m_gamma;
48 60
    //@}
61
+   /** mapping table from display values (from IPv4/UDP)
62
+                     to video values (to show on screen) */
63
+   uint8_t m_display2video[256];
49 64
 };
50 65
 
51 66
 #endif // #ifndef MAPPING_H
52 67