VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grcolourspacetypes.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 grcolourspacetypes.h
19 ///
20 
21 #ifndef GR_COLOURSPACE_TYPES__H
22 #define GR_COLOURSPACE_TYPES__H
23 
24 #include "gdef.h"
25 
26 namespace Gr
27 {
28 namespace ColourSpace
29 {
30 
31 /// \class Gr::ColourSpace::fp_1000
32 /// A simple fixed-point number class with a scale factor of 1000.
33 ///
34 struct fp_1000
35 {
36  typedef int int_t ;
37  static g__constexpr int scale__ = 1000 ;
38  explicit fp_1000( int i ) g__noexcept : m_i(int_t(i)*scale__) {}
39  fp_1000( int i_scaled , int /*private*/ ) g__noexcept : m_i(i_scaled) {}
40  static fp_1000 value__( int i_scaled ) g__noexcept { return fp_1000(i_scaled,0) ; }
41  int to_builtin() const g__noexcept { return int( m_i / scale__ ) ; }
42  int_t m_i ;
43 } ;
44 inline fp_1000 operator*( const fp_1000 & a , const fp_1000 & b ) g__noexcept
45 {
46  return fp_1000::value__( ( a.m_i * b.m_i ) / fp_1000::scale__ ) ;
47 }
48 inline fp_1000 operator/( const fp_1000 & a , const fp_1000 & b ) g__noexcept
49 {
50  return fp_1000::value__( ( a.m_i * fp_1000::scale__ ) / b.m_i ) ;
51 }
52 inline fp_1000 operator+( const fp_1000 & a , const fp_1000 & b ) g__noexcept
53 {
54  return fp_1000::value__( a.m_i + b.m_i ) ;
55 }
56 inline fp_1000 operator-( const fp_1000 & a , const fp_1000 & b ) g__noexcept
57 {
58  return fp_1000::value__( a.m_i - b.m_i ) ;
59 }
60 inline bool operator<( const fp_1000 & a , const fp_1000 & b ) g__noexcept
61 {
62  return a.m_i < b.m_i ;
63 }
64 inline bool operator>( const fp_1000 & a , const fp_1000 & b ) g__noexcept
65 {
66  return a.m_i > b.m_i ;
67 }
68 
69 
70 /// \class Gr::ColourSpace::fp_256
71 /// A simple fixed-point number class with a scale factor of 256
72 ///
73 struct fp_256
74 {
75  typedef int int_t ;
76  static g__constexpr int scale__ = 256 ;
77  explicit fp_256( int i ) g__noexcept : m_i(i>0?(int_t(i)<<8):-(int_t(-i)<<8)) {}
78  fp_256( int i_scaled , int /*private*/ ) g__noexcept : m_i(i_scaled) {}
79  static fp_256 value__( int i_scaled ) g__noexcept { return fp_256(i_scaled,0) ; }
80  int to_builtin() const g__noexcept { return int(m_i>0?(m_i>>8):-((-m_i)>>8)) ; }
81  int_t m_i ;
82 } ;
83 inline fp_256 operator*( fp_256 a , fp_256 b ) g__noexcept
84 {
85  return fp_256::value__( ( a.m_i * b.m_i ) >> 8 ) ;
86 }
87 inline fp_256 operator/( fp_256 a , fp_256 b ) g__noexcept
88 {
89  return fp_256::value__( ( a.m_i << 8 ) / b.m_i ) ;
90 }
91 inline fp_256 operator+( fp_256 a , fp_256 b ) g__noexcept
92 {
93  return fp_256::value__( a.m_i + b.m_i ) ;
94 }
95 inline fp_256 operator-( fp_256 a , fp_256 b ) g__noexcept
96 {
97  return fp_256::value__( a.m_i - b.m_i ) ;
98 }
99 inline bool operator<( fp_256 a , fp_256 b ) g__noexcept
100 {
101  return a.m_i < b.m_i ;
102 }
103 inline bool operator>( fp_256 a , fp_256 b ) g__noexcept
104 {
105  return a.m_i > b.m_i ;
106 }
107 
108 /// \class Gr::ColourSpace::converter_double
109 /// An adaptor for double.
110 ///
112 {
113  typedef double fp_type ;
114  static double make_fp( int i , int s ) g__noexcept
115  {
116  return double(i) / double(s) ;
117  }
118  static double make_half() g__noexcept
119  {
120  return 0.5 ;
121  }
122  static unsigned char to_range( double d ) g__noexcept
123  {
124  return static_cast<unsigned char>(d) ;
125  }
126 } ;
127 
128 /// \class Gr::ColourSpace::converter_fp
129 /// An adaptor for fp_#.
130 ///
131 template <typename T>
133 {
134  typedef T fp_type ;
135  static fp_type make_fp( int i , int ) g__noexcept
136  {
137  return fp_type::value__( i ) ; // assume scale factors match
138  }
139  static g__constexpr fp_type make_half() g__noexcept
140  {
141  return fp_type::value__( fp_type::scale__ >> 1 ) ;
142  }
143  static unsigned char to_range( fp_type n ) g__noexcept
144  {
145  return static_cast<unsigned char>( n.to_builtin() ) ;
146  }
147 } ;
148 
149 }
150 }
151 
152 #endif
A simple fixed-point number class with a scale factor of 256.
A simple fixed-point number class with a scale factor of 1000.