1 /* ***** BEGIN LICENSE BLOCK *****
7 * Copyright (c) 2008 BBC Research
9 * Permission is hereby granted, free of charge, to any person obtaining a copy
10 * of this software and associated documentation files (the "Software"), to deal
11 * in the Software without restriction, including without limitation the rights
12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 * copies of the Software, and to permit persons to whom the Software is
14 * furnished to do so, subject to the following conditions:
16 * The above copyright notice and this permission notice shall be included in
17 * all copies or substantial portions of the Software.
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27 * ***** END LICENSE BLOCK ***** */
29 #ifndef __COLOURSPACE_H
30 #define __COLOURSPACE_H
35 // THERE ARE TWO COLOUR SYSTEMS WE ARE INTERESTED IN, 'SDTV' and 'HDTV', WHICH
36 // COVER ALL REAL APPLICATIONS
40 //ITU-R BT 470, PAL system B/G
42 //SMPTE 293M, 720x483 59.94P
44 //Ey = 0.299Er + 0.587Eg + 0.114Eb
47 //ITU-R BT 709 (1125/60/2:1 only)
48 //ITU-R BT 1361 Worldwide Unified HDTV
49 //SMPTE 274M 1920x1080 HDTV
50 //SMPTE 296M 1280x720 HDTV
52 //Ey = 0.2126Er + 0.7152Eg + 0.0722Eb
54 //For converting RGB to YCbCr
56 // Ey = Kg.Eg + Kb.Eb + Kr.Er
60 // Ecb = (Eb-Ey) / (2(1-Kb))
61 // Ecr = (Er-Ey) / (2(1-Kr))
63 //For converting YCbCr to RGB
65 // Er = Ey + 2(1-Kr)Ecr
66 // Eg = Ey - (2(1-Kb)Kb.Ecb)/Kg - (2(1-Kr)Kr.Ecr)/Kg
67 // Eb = Ey + 2(1-Kb)Ecb
69 //This class builds the colour matrix using the equations above from Kr, Kg, Kb
74 enum MatrixType
{SDTVtoYUV
, SDTVtoRGB
, HDTVtoYUV
, HDTVtoRGB
};
76 ColourSpace(int inWidth
, int inHeight
, RawFrame::Chroma destChroma
) :
77 _width(inWidth
), _height(inHeight
), _converted(inWidth
, inHeight
,
81 buildYUVMatrix(matrices
[SDTVtoYUV
], (float)0.299, (float)0.587,
83 buildRGBMatrix(matrices
[SDTVtoRGB
], (float)0.299, (float)0.587,
85 buildYUVMatrix(matrices
[HDTVtoYUV
], (float)0.2126, (float)0.7152,
87 buildRGBMatrix(matrices
[HDTVtoRGB
], (float)0.2126, (float)0.7152,
92 //reference matrix coefficients generated from Kr, Kg, Kb
93 typedef struct _ColourMatrix
{
99 //integer matrix coefficients used for the actual processing
100 //these may have been scaled to adjust number ranges during conversion
101 typedef struct _ColourMatrixInt
{
107 const RawFrame
& process(const RawFrame
&src
, const MatrixType type
,
108 const bool scaledRGB
, const bool YOnly
,
110 void process_ext(const RawFrame
&src
, RawFrame
& dest
,
111 const MatrixType type
, const bool scaledRGB
,
112 const bool YOnly
, const int depth
);
115 void buildRGBMatrix(ColourMatrix
&matrix
, const float Kr
, const float Kg
,
117 void buildYUVMatrix(ColourMatrix
&matrix
, const float Kr
, const float Kg
,
124 /* colour matrices */
125 ColourMatrix matrices
[4];