Embedded Template Library 1.0
Loading...
Searching...
No Matches
gamma.h
Go to the documentation of this file.
1
2
3/******************************************************************************
4The MIT License(MIT)
5
6Embedded Template Library.
7https://github.com/ETLCPP/etl
8https://www.etlcpp.com
9
10Copyright(c) 2021 John Wellbelove
11
12Permission is hereby granted, free of charge, to any person obtaining a copy
13of this software and associated documentation files(the "Software"), to deal
14in the Software without restriction, including without limitation the rights
15to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
16copies of the Software, and to permit persons to whom the Software is
17furnished to do so, subject to the following conditions :
18
19The above copyright notice and this permission notice shall be included in all
20copies or substantial portions of the Software.
21
22THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
25AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28SOFTWARE.
29******************************************************************************/
30
31#ifndef ETL_GAMMA_INCLUDED
32#define ETL_GAMMA_INCLUDED
33
34#include "platform.h"
35#include "functional.h"
36#include "type_traits.h"
37
38#include <math.h>
39#include <stdint.h>
40
41namespace etl
42{
43 //***************************************************************************
45 //***************************************************************************
46 template <typename TInput>
47 class gamma_encode : public etl::unary_function<TInput, TInput>
48 {
49 public:
50
51 //*********************************
53 //*********************************
54 gamma_encode(double gamma_, TInput maximum_)
55 : one_over_gamma(1.0 / gamma_)
56 , maximum(maximum_)
57 {
58 }
59
60 //*********************************
63 //*********************************
64 TInput operator ()(TInput value) const
65 {
66 // Calculate intermediate result because rounding + optimization
67 // lead to wrong values when returning directly (test on i386)
68 const double result = maximum * pow(double(value) / maximum, one_over_gamma);
69
70 return TInput(result);
71 }
72
73 private:
74
75 const double one_over_gamma;
76 const double maximum;
77 };
78
79 //***************************************************************************
81 //***************************************************************************
82 template <typename TInput>
83 class gamma_decode : public etl::unary_function<TInput, TInput>
84 {
85 public:
86
87 //*********************************
89 //*********************************
90 gamma_decode(double gamma_, TInput maximum_)
91 : gamma(gamma_)
92 , maximum(maximum_)
93 {
94 }
95
96 //*********************************
99 //*********************************
100 TInput operator ()(TInput value) const
101 {
102 // Calculate intermediate result because rounding + optimization
103 // lead to wrong values when returning directly (test on i386)
104 const double result = maximum * pow(double(value) / maximum, gamma);
105 return TInput(result);
106 }
107
108 private:
109
110 const double gamma;
111 const double maximum;
112 };
113}
114
115#endif
gamma_decode(double gamma_, TInput maximum_)
Constructor.
Definition gamma.h:90
TInput operator()(TInput value) const
Definition gamma.h:100
TInput operator()(TInput value) const
Definition gamma.h:64
gamma_encode(double gamma_, TInput maximum_)
Constructor.
Definition gamma.h:54
bitset_ext
Definition absolute.h:39
unary_function
Definition functional.h:151