VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grcolourspacemap.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2017 Graeme Walker
3 //
4 // This program is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 // ===
17 ///
18 /// \file grcolourspacemap.h
19 ///
20 
21 #ifndef GR_COLOURSPACE_MAP__H
22 #define GR_COLOURSPACE_MAP__H
23 
24 #include "gdef.h"
25 
26 namespace Gr
27 {
28 namespace ColourSpace
29 {
30 
31 /// Does min/max clamping.
32 ///
33 template <typename T>
34 g__constexpr
35 T clamp( T min , T value , T max )
36 {
37  return value < min ? min : ( value > max ? max : value ) ;
38 }
39 
40 /// Does min/max clamping to a range.
41 ///
42 template <typename Trange, typename Tout, typename Tin>
43 g__constexpr
44 Tout clamp( Tin value )
45 {
46  return static_cast<Tout>(clamp(static_cast<Tin>(Trange::min),value,static_cast<Tin>(Trange::max))) ;
47 }
48 
49 /// Scales an unsigned fp value to another fp value in the relevant range.
50 ///
51 template <typename Trange, typename Tconverter>
52 g__constexpr
53 typename Tconverter::fp_type map_to_range_from_unsigned( typename Tconverter::fp_type fp ) g__noexcept
54 {
55  typedef typename Tconverter::fp_type fp_t ;
56  return ( fp * fp_t(Trange::max-Trange::min) + fp_t(Trange::min) ) ;
57 }
58 
59 /// Scales a signed fp value to another fp value in the relevant range.
60 ///
61 template <typename Trange, typename Tconverter>
62 g__constexpr
63 typename Tconverter::fp_type map_to_range_from_signed( typename Tconverter::fp_type fp ) g__noexcept
64 {
65  return map_to_range_from_unsigned<Trange,Tconverter>( fp + Tconverter::make_half() ) ;
66 }
67 
68 /// Scales an fp value to another fp value in the relevant range.
69 ///
70 template <typename Trange, typename Tconverter>
71 g__constexpr
72 typename Tconverter::fp_type map_to_range_imp( typename Tconverter::fp_type fp ) g__noexcept
73 {
74  return
75  Trange::is_signed ?
76  map_to_range_from_signed<Trange,Tconverter>(fp) :
77  map_to_range_from_unsigned<Trange,Tconverter>(fp) ;
78 }
79 
80 /// Maps an "analogue" space value to a "digial" range value.
81 ///
82 template <typename Trange, typename Tconverter>
83 g__constexpr
84 typename Trange::value_type map_to_range( typename Tconverter::fp_type fp ) g__noexcept
85 {
86  typedef typename Tconverter::fp_type fp_t ;
87  return Tconverter::to_range(
88  clamp( fp_t(Trange::min) , map_to_range_imp<Trange,Tconverter>(fp) , fp_t(Trange::max) ) ) ;
89 }
90 
91 
92 /// Maps a "digital" range value to a signed "analogue" space value.
93 ///
94 template <typename Trange, typename Tconverter>
95 g__constexpr
96 typename Tconverter::fp_type map_to_space_signed( typename Trange::value_type n ) g__noexcept
97 {
98  typedef typename Tconverter::fp_type fp_t ;
99  return fp_t(n-Trange::min) / fp_t(Trange::max-Trange::min) - Tconverter::make_half() ;
100 }
101 
102 /// Maps a "digital" range value to an unsigned "analogue" space value.
103 ///
104 template <typename Trange, typename Tconverter>
105 g__constexpr
106 typename Tconverter::fp_type map_to_space_unsigned( typename Trange::value_type n ) g__noexcept
107 {
108  typedef typename Tconverter::fp_type fp_t ;
109  return fp_t(n-Trange::min) / fp_t(Trange::max-Trange::min) ;
110 }
111 
112 /// Maps a "digital" range value to an "analogue" space value.
113 ///
114 template <typename Trange, typename Tconverter>
115 g__constexpr
116 typename Tconverter::fp_type map_to_space_imp( typename Trange::value_type n ) g__noexcept
117 {
118  return
119  Trange::is_signed ?
120  map_to_space_signed<Trange,Tconverter>( n ) :
121  map_to_space_unsigned<Trange,Tconverter>( n ) ;
122 }
123 
124 /// Maps a "digital" range triple to an "analogue" space triple.
125 ///
126 template <typename Tconverter, typename Trange1, typename Trange2, typename Trange3>
127 g__constexpr
129 {
130  typedef typename Tconverter::fp_type fp_t ;
131  return triple<fp_t>(
132  map_to_space_imp<Trange1,Tconverter>(t.m_1) ,
133  map_to_space_imp<Trange2,Tconverter>(t.m_2) ,
134  map_to_space_imp<Trange3,Tconverter>(t.m_3) ) ;
135 }
136 
137 }
138 }
139 
140 #endif
g__constexpr T clamp(T min, T value, T max)
Does min/max clamping.
g__constexpr Tconverter::fp_type map_to_range_imp(typename Tconverter::fp_type fp) g__noexcept
Scales an fp value to another fp value in the relevant range.
g__constexpr Trange::value_type map_to_range(typename Tconverter::fp_type fp) g__noexcept
Maps an "analogue" space value to a "digial" range value.
A 3-tuple for holding rgb or yuv triples.
Definition: grcolourspace.h:85
g__constexpr Tconverter::fp_type map_to_space_imp(typename Trange::value_type n) g__noexcept
Maps a "digital" range value to an "analogue" space value.
g__constexpr Tconverter::fp_type map_to_range_from_signed(typename Tconverter::fp_type fp) g__noexcept
Scales a signed fp value to another fp value in the relevant range.
g__constexpr Tconverter::fp_type map_to_range_from_unsigned(typename Tconverter::fp_type fp) g__noexcept
Scales an unsigned fp value to another fp value in the relevant range.
g__constexpr Tconverter::fp_type map_to_space_signed(typename Trange::value_type n) g__noexcept
Maps a "digital" range value to a signed "analogue" space value.
g__constexpr Tconverter::fp_type map_to_space_unsigned(typename Trange::value_type n) g__noexcept
Maps a "digital" range value to an unsigned "analogue" space value.
g__constexpr triple< typename Tconverter::fp_type > map_to_space(triple< typename Trange1::value_type > t) g__noexcept
Maps a "digital" range triple to an "analogue" space triple.