Add facility to read frames in RGB & RGBA format
[imageviewer.git] / colourSpace.h
blob683864f401d3f38f63644e8befe1f2a118693e39
1 /* ***** BEGIN LICENSE BLOCK *****
3 * $Id$
5 * The MIT License
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
25 * THE SOFTWARE.
27 * ***** END LICENSE BLOCK ***** */
29 #ifndef __COLOURSPACE_H
30 #define __COLOURSPACE_H
32 #include "rawFrame.h"
34 // USEFUL INFORMATION
35 // THERE ARE TWO COLOUR SYSTEMS WE ARE INTERESTED IN, 'SDTV' and 'HDTV', WHICH
36 // COVER ALL REAL APPLICATIONS
38 //SDTV :-
39 //ITU-R BT 601
40 //ITU-R BT 470, PAL system B/G
41 //SMPTE 170M, NTSC
42 //SMPTE 293M, 720x483 59.94P
44 //Ey = 0.299Er + 0.587Eg + 0.114Eb
46 //HDTV :-
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
57 // and
58 // Kg + Kb + Kr = 1
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
71 class ColourSpace {
72 public:
74 enum MatrixType {SDTVtoYUV, SDTVtoRGB, HDTVtoYUV, HDTVtoRGB};
76 ColourSpace(int inWidth, int inHeight, RawFrame::Chroma destChroma) :
77 _width(inWidth), _height(inHeight), _converted(inWidth, inHeight,
78 destChroma)
80 //construct matrices
81 buildYUVMatrix(matrices[SDTVtoYUV], (float)0.299, (float)0.587,
82 (float)0.114);
83 buildRGBMatrix(matrices[SDTVtoRGB], (float)0.299, (float)0.587,
84 (float)0.114);
85 buildYUVMatrix(matrices[HDTVtoYUV], (float)0.2126, (float)0.7152,
86 (float)0.0722);
87 buildRGBMatrix(matrices[HDTVtoRGB], (float)0.2126, (float)0.7152,
88 (float)0.0722);
92 //reference matrix coefficients generated from Kr, Kg, Kb
93 typedef struct _ColourMatrix {
94 double a, b, c;
95 double d, e, f;
96 double g, h, i;
97 } 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 {
102 int a, b, c;
103 int d, e, f;
104 int g, h, i;
105 } ColourMatrixInt;
107 const RawFrame& process(const RawFrame &src, const MatrixType type,
108 const bool scaledRGB, const bool YOnly,
109 const int depth);
110 void process_ext(const RawFrame &src, RawFrame& dest,
111 const MatrixType type, const bool scaledRGB,
112 const bool YOnly, const int depth);
114 protected:
115 void buildRGBMatrix(ColourMatrix &matrix, const float Kr, const float Kg,
116 const float Kb);
117 void buildYUVMatrix(ColourMatrix &matrix, const float Kr, const float Kg,
118 const float Kb);
120 /* input size */
121 const int _width;
122 const int _height;
124 /* colour matrices */
125 ColourMatrix matrices[4];
127 /* output */
128 RawFrame _converted;
131 #endif