Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoGammaTableMethod.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoGammaTableMethod class is used to correct RGB colors for human eye gamma levels equally
3 across all color channels
4 
5 Written by Michael C. Miller.
6 
7 I invest time and resources providing this open source code,
8 please support me by dontating (see https://github.com/Makuna)
9 
10 -------------------------------------------------------------------------
11 This file is part of the LUMITRONIX_iFlex_Workshop library.
12 
13 LumitronixIFlexBus is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as
15 published by the Free Software Foundation, either version 3 of
16 the License, or (at your option) any later version.
17 
18 LumitronixIFlexBus is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public
24 License along with LumitronixIFlex. If not, see
25 <http://www.gnu.org/licenses/>.
26 -------------------------------------------------------------------------*/
27 #pragma once
28 
29 // NeoGammaTableMethod uses 256 bytes of memory, but is significantly faster
31 {
32 protected:
34  {
35  uint8_t pos;
36  uint8_t count;
37  };
38 
39 public:
40  static uint8_t Correct(uint8_t value)
41  {
42  return _table[value];
43  }
44 
45  static uint16_t Correct(uint16_t value)
46  {
47  // since a single monolithic table would be an unreasonable memory usage
48  // this will use a hybrid of two tables, the base 255 table for the hibyte
49  // and a smaller table with hints on how to use the table for certain values
50  // and the left over values some simple calculations to approximate the final
51  // 16 bit value as compared to the equation
52  static const NeoGamma16LowHint _hints[] = {
53  {0,16}, {1,16}, {2,16}, {3,16}, {4,16}, {5,16}, {6,16}, {7,16}, {8,16}, {9,16}, {10,16}, {11,16}, {12,16}, {13,16}, {14,16}, {15,16},
54  {0,10}, {1,10}, {2,10}, {3,10}, {4,10}, {5,10}, {6,10}, {7,10}, {8,10}, {9,10}, {0,6}, {1,6}, {2,6}, {3,6}, {4,6}, {5,6},
55  {0,6}, {1,6}, {2,6}, {3,6}, {4,6}, {5,6}, {0,4}, {1,4}, {2,4}, {3,4}, {0,4}, {1,4}, {2,4}, {3,4}, {0,3}, {1,3},
56  {2,3}, {0,4}, {1,4}, {2,4}, {3,4}, {0,3}, {1,3}, {2,3}, {0,3}, {1,3}, {2,3}, {0,2}, {1,2}, {0,3}, {1,3}, {2,3},
57  {0,2}, {1,2}, {0,2}, {1,2}, {0,3}, {1,3}, {2,3}, {0,2}, {1,2}
58  };
59 
60  uint8_t hi = (value >> 8);
61  uint16_t lo = (value & 0x00ff);
62  uint8_t hiResult = _table[hi];
63  uint16_t lowResult = 0;
64 
65  if (hi < countof(_hints))
66  {
67  // use _hints table to calculate a reasonable lowbyte
68  lowResult = (lo + _hints[hi].pos * 256) / _hints[hi].count;
69  }
70  else if (hi == 255)
71  {
72  // last entry is always linear
73  lowResult = lo;
74  }
75  else
76  {
77  // check the _table for duplicate or jumps to adjust the range of lowbyte
78  if (hiResult == _table[hi - 1])
79  {
80  // this result is an upper duplicate
81  lowResult = (lo >> 1) | 0x80; // lo / 2 + 128
82  }
83  else
84  {
85  uint8_t delta = _table[hi + 1] - hiResult;
86 
87  if (delta == 0)
88  {
89  // this result is a lower duplicate
90  lowResult = (lo >> 1); // lo / 2
91  }
92  else if (delta == 1)
93  {
94  // this result is incremental and thus linear
95  lowResult = lo;
96  }
97  else
98  {
99  // this result jumps by more than one, so need to spread across
100  lowResult = delta * lo;
101  }
102  }
103 
104  }
105 
106  return (static_cast<uint16_t>(hiResult) << 8) + lowResult;
107  }
108 
109 
110 private:
111  static const uint8_t _table[256];
112 
113 };
#define countof(array)
Definition: NeoUtil.h:46
Definition: NeoGammaTableMethod.h:31
static uint16_t Correct(uint16_t value)
Definition: NeoGammaTableMethod.h:45
static uint8_t Correct(uint8_t value)
Definition: NeoGammaTableMethod.h:40
Definition: NeoGammaTableMethod.h:34
uint8_t pos
Definition: NeoGammaTableMethod.h:35
uint8_t count
Definition: NeoGammaTableMethod.h:36