VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
grcolourspace.cpp
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 // grcolourspace.cpp
19 //
20 
21 #include "gdef.h"
22 #include "grcolourspace.h"
23 
24 static unsigned int to_int( double d ) ;
25 
26 // these are transliterations of the colourspace standards docs used
27 // to check the c++ implementation -- do not "improve" them without
28 // reference to the original specifications
29 
30 unsigned int g_colourspace_y( unsigned int r , unsigned int g , unsigned int b )
31 {
32  return to_int( 16. +
33  ((double)r)/255.*219.*0.299 + // 65.738
34  ((double)g)/255.*219.*0.587 + // 129.057
35  ((double)b)/255.*219.*0.114 ) ; // 25.064
36 }
37 
38 unsigned int g_colourspace_u( unsigned int r , unsigned int g , unsigned int b )
39 {
40  return to_int( 128. +
41  -((double)r)/255.*224.*0.169 // 37.945
42  -((double)g)/255.*224.*0.331 + // 74.494
43  ((double)b)/255.*224.*0.5 ) ; // 112.439
44 }
45 
46 unsigned int g_colourspace_v( unsigned int r , unsigned int g , unsigned int b )
47 {
48  return to_int( 128. +
49  ((double)r)/255.*224.*0.5 + // 112.439
50  -((double)g)/255.*224.*0.419 + // 94.154
51  -((double)b)/255.*224.*0.081 ) ; // 18.285
52 }
53 
54 unsigned int g_colourspace_r( unsigned int y , unsigned int , unsigned int v )
55 {
56  return to_int(
57  ((double)(y)-16.)*255./219. +
58  ((double)(v)-128.)*255./112.*0.701 ) ;
59 }
60 
61 unsigned int g_colourspace_g( unsigned int y , unsigned int u , unsigned int v )
62 {
63  return to_int(
64  ((double)(y)-16.)*255./219. +
65  -((double)(u)-128.)*255./112.*0.886*0.114/0.587 +
66  -((double)(v)-128.)*255./112.*0.701*0.299/0.587 ) ;
67 }
68 
69 unsigned int g_colourspace_b( unsigned int y , unsigned int u , unsigned int )
70 {
71  return to_int(
72  ((double)(y)-16.)*255./219. +
73  ((double)(u)-128.)*255./112.*0.886 ) ;
74 }
75 
76 static unsigned int to_int( double d )
77 {
78  int i = (int)(d+0.5) ; // moot rounding
79  return i < 0 ? 0U : ( i > 255 ? 255U : (unsigned int)i ) ;
80 }
81 
82 /// \file grcolourspace.cpp