1b167d5e7Smrg/************************************************************************** 2b167d5e7Smrg * 3b167d5e7Smrg * Copyright 2009 Younes Manton. 4b167d5e7Smrg * All Rights Reserved. 5b167d5e7Smrg * 6b167d5e7Smrg * Permission is hereby granted, free of charge, to any person obtaining a 7b167d5e7Smrg * copy of this software and associated documentation files (the 8b167d5e7Smrg * "Software"), to deal in the Software without restriction, including 9b167d5e7Smrg * without limitation the rights to use, copy, modify, merge, publish, 10b167d5e7Smrg * distribute, sub license, and/or sell copies of the Software, and to 11b167d5e7Smrg * permit persons to whom the Software is furnished to do so, subject to 12b167d5e7Smrg * the following conditions: 13b167d5e7Smrg * 14b167d5e7Smrg * The above copyright notice and this permission notice (including the 15b167d5e7Smrg * next paragraph) shall be included in all copies or substantial portions 16b167d5e7Smrg * of the Software. 17b167d5e7Smrg * 18b167d5e7Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 19b167d5e7Smrg * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 20b167d5e7Smrg * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. 21b167d5e7Smrg * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR 22b167d5e7Smrg * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 23b167d5e7Smrg * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 24b167d5e7Smrg * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 25b167d5e7Smrg * 26b167d5e7Smrg **************************************************************************/ 27b167d5e7Smrg 28b167d5e7Smrg#include "util/u_math.h" 29b167d5e7Smrg#include "util/u_debug.h" 30b167d5e7Smrg 31b167d5e7Smrg#include "vl_csc.h" 32b167d5e7Smrg 33b167d5e7Smrg/* 34b167d5e7Smrg * Color space conversion formulas 35b167d5e7Smrg * 36b167d5e7Smrg * To convert YCbCr to RGB, 37b167d5e7Smrg * vec4 ycbcr, rgb 38b167d5e7Smrg * mat44 csc 39b167d5e7Smrg * rgb = csc * ycbcr 40b167d5e7Smrg * 41b167d5e7Smrg * To calculate the color space conversion matrix csc with ProcAmp adjustments, 42b167d5e7Smrg * mat44 csc, cstd, procamp, bias 43b167d5e7Smrg * csc = cstd * (procamp * bias) 44b167d5e7Smrg * 45b167d5e7Smrg * Where cstd is a matrix corresponding to one of the color standards (BT.601, BT.709, etc) 46b167d5e7Smrg * adjusted for the kind of YCbCr -> RGB mapping wanted (1:1, full), 47b167d5e7Smrg * bias is a matrix corresponding to the kind of YCbCr -> RGB mapping wanted (1:1, full) 48b167d5e7Smrg * 49b167d5e7Smrg * To calculate procamp, 50b167d5e7Smrg * mat44 procamp, hue, saturation, brightness, contrast 51b167d5e7Smrg * procamp = brightness * (saturation * (contrast * hue)) 52b167d5e7Smrg * Alternatively, 53b167d5e7Smrg * procamp = saturation * (brightness * (contrast * hue)) 54b167d5e7Smrg * 55b167d5e7Smrg * contrast 56b167d5e7Smrg * [ c, 0, 0, 0] 57b167d5e7Smrg * [ 0, c, 0, 0] 58b167d5e7Smrg * [ 0, 0, c, 0] 59b167d5e7Smrg * [ 0, 0, 0, 1] 60b167d5e7Smrg * 61b167d5e7Smrg * brightness 627e995a2eSmrg * [ 1, 0, 0, b/c] 637e995a2eSmrg * [ 0, 1, 0, 0] 647e995a2eSmrg * [ 0, 0, 1, 0] 657e995a2eSmrg * [ 0, 0, 0, 1] 66b167d5e7Smrg * 67b167d5e7Smrg * saturation 68b167d5e7Smrg * [ 1, 0, 0, 0] 69b167d5e7Smrg * [ 0, s, 0, 0] 70b167d5e7Smrg * [ 0, 0, s, 0] 71b167d5e7Smrg * [ 0, 0, 0, 1] 72b167d5e7Smrg * 73b167d5e7Smrg * hue 74b167d5e7Smrg * [ 1, 0, 0, 0] 75b167d5e7Smrg * [ 0, cos(h), sin(h), 0] 76b167d5e7Smrg * [ 0, -sin(h), cos(h), 0] 77b167d5e7Smrg * [ 0, 0, 0, 1] 78b167d5e7Smrg * 79b167d5e7Smrg * procamp 80b167d5e7Smrg * [ c, 0, 0, b] 81b167d5e7Smrg * [ 0, c*s*cos(h), c*s*sin(h), 0] 82b167d5e7Smrg * [ 0, -c*s*sin(h), c*s*cos(h), 0] 83b167d5e7Smrg * [ 0, 0, 0, 1] 84b167d5e7Smrg * 85b167d5e7Smrg * bias 86b167d5e7Smrg * [ 1, 0, 0, ybias] 87b167d5e7Smrg * [ 0, 1, 0, cbbias] 88b167d5e7Smrg * [ 0, 0, 1, crbias] 89b167d5e7Smrg * [ 0, 0, 0, 1] 90b167d5e7Smrg * 91b167d5e7Smrg * csc 92b167d5e7Smrg * [ c*cstd[ 0], c*cstd[ 1]*s*cos(h) - c*cstd[ 2]*s*sin(h), c*cstd[ 2]*s*cos(h) + c*cstd[ 1]*s*sin(h), cstd[ 3] + cstd[ 0]*(b + c*ybias) + cstd[ 1]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[ 2]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 93b167d5e7Smrg * [ c*cstd[ 4], c*cstd[ 5]*s*cos(h) - c*cstd[ 6]*s*sin(h), c*cstd[ 6]*s*cos(h) + c*cstd[ 5]*s*sin(h), cstd[ 7] + cstd[ 4]*(b + c*ybias) + cstd[ 5]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[ 6]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 94b167d5e7Smrg * [ c*cstd[ 8], c*cstd[ 9]*s*cos(h) - c*cstd[10]*s*sin(h), c*cstd[10]*s*cos(h) + c*cstd[ 9]*s*sin(h), cstd[11] + cstd[ 8]*(b + c*ybias) + cstd[ 9]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[10]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 95b167d5e7Smrg * [ c*cstd[12], c*cstd[13]*s*cos(h) - c*cstd[14]*s*sin(h), c*cstd[14]*s*cos(h) + c*cstd[13]*s*sin(h), cstd[15] + cstd[12]*(b + c*ybias) + cstd[13]*(c*cbbias*s*cos(h) + c*crbias*s*sin(h)) + cstd[14]*(c*crbias*s*cos(h) - c*cbbias*s*sin(h))] 96b167d5e7Smrg */ 97b167d5e7Smrg 98b167d5e7Smrg/* 99b167d5e7Smrg * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where: 100b167d5e7Smrg * Y is in [16,235], Cb and Cr are in [16,240] 101b167d5e7Smrg * R, G, and B are in [16,235] 102b167d5e7Smrg */ 103b167d5e7Smrgstatic const vl_csc_matrix bt_601 = 104b167d5e7Smrg{ 105b167d5e7Smrg { 1.0f, 0.0f, 1.371f, 0.0f, }, 106b167d5e7Smrg { 1.0f, -0.336f, -0.698f, 0.0f, }, 107b167d5e7Smrg { 1.0f, 1.732f, 0.0f, 0.0f, } 108b167d5e7Smrg}; 109b167d5e7Smrg 110b167d5e7Smrg/* 111b167d5e7Smrg * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where: 112b167d5e7Smrg * Y is in [16,235], Cb and Cr are in [16,240] 113b167d5e7Smrg * R, G, and B are in [16,235] 114b167d5e7Smrg */ 115b167d5e7Smrgstatic const vl_csc_matrix bt_709 = 116b167d5e7Smrg{ 117b167d5e7Smrg { 1.0f, 0.0f, 1.540f, 0.0f, }, 118b167d5e7Smrg { 1.0f, -0.183f, -0.459f, 0.0f, }, 119b167d5e7Smrg { 1.0f, 1.816f, 0.0f, 0.0f, } 120b167d5e7Smrg}; 121b167d5e7Smrg 122b167d5e7Smrg/* 1237e995a2eSmrg * Converts SMPTE 240M YCbCr pixels to RGB pixels where: 124b167d5e7Smrg * Y is in [16,235], Cb and Cr are in [16,240] 1257e995a2eSmrg * R, G, and B are in [16,235] 126b167d5e7Smrg */ 127b167d5e7Smrgstatic const vl_csc_matrix smpte240m = 128b167d5e7Smrg{ 1297e995a2eSmrg { 1.0f, 0.0f, 1.541f, 0.0f, }, 1307e995a2eSmrg { 1.0f, -0.221f, -0.466f, 0.0f, }, 1317e995a2eSmrg { 1.0f, 1.785f, 0.0f, 0.0f, } 132b167d5e7Smrg}; 133b167d5e7Smrg 1347e995a2eSmrgstatic const vl_csc_matrix bt_709_rev = { 1357e995a2eSmrg { 0.183f, 0.614f, 0.062f, 0.0625f}, 1367e995a2eSmrg {-0.101f, -0.338f, 0.439f, 0.5f }, 1377e995a2eSmrg { 0.439f, -0.399f, -0.040f, 0.5f } 138b167d5e7Smrg}; 139b167d5e7Smrg 140b167d5e7Smrgstatic const vl_csc_matrix identity = 141b167d5e7Smrg{ 142b167d5e7Smrg { 1.0f, 0.0f, 0.0f, 0.0f, }, 143b167d5e7Smrg { 0.0f, 1.0f, 0.0f, 0.0f, }, 144b167d5e7Smrg { 0.0f, 0.0f, 1.0f, 0.0f, } 145b167d5e7Smrg}; 146b167d5e7Smrg 147b167d5e7Smrgconst struct vl_procamp vl_default_procamp = { 148b167d5e7Smrg 0.0f, /* brightness */ 149b167d5e7Smrg 1.0f, /* contrast */ 150b167d5e7Smrg 1.0f, /* saturation */ 151b167d5e7Smrg 0.0f /* hue */ 152b167d5e7Smrg}; 153b167d5e7Smrg 154b167d5e7Smrgvoid vl_csc_get_matrix(enum VL_CSC_COLOR_STANDARD cs, 155b167d5e7Smrg struct vl_procamp *procamp, 156b167d5e7Smrg bool full_range, 157b167d5e7Smrg vl_csc_matrix *matrix) 158b167d5e7Smrg{ 159b167d5e7Smrg float cbbias = -128.0f/255.0f; 160b167d5e7Smrg float crbias = -128.0f/255.0f; 161b167d5e7Smrg 162b167d5e7Smrg const struct vl_procamp *p = procamp ? procamp : &vl_default_procamp; 163b167d5e7Smrg float c = p->contrast; 164b167d5e7Smrg float s = p->saturation; 165b167d5e7Smrg float b = p->brightness; 166b167d5e7Smrg float h = p->hue; 1677e995a2eSmrg float x, y; 168b167d5e7Smrg 169b167d5e7Smrg const vl_csc_matrix *cstd; 170b167d5e7Smrg 1717e995a2eSmrg if (full_range) { 1727e995a2eSmrg c *= 1.164f; /* Adjust for the y range */ 1737e995a2eSmrg b *= 1.164f; /* Adjust for the y range */ 1747e995a2eSmrg b -= c * 16.0f / 255.0f; /* Adjust for the y bias */ 1757e995a2eSmrg } 1767e995a2eSmrg 1777e995a2eSmrg /* Parameter substitutions */ 1787e995a2eSmrg x = c * s * cosf(h); 1797e995a2eSmrg y = c * s * sinf(h); 1807e995a2eSmrg 181b167d5e7Smrg assert(matrix); 182b167d5e7Smrg 183b167d5e7Smrg switch (cs) { 184b167d5e7Smrg case VL_CSC_COLOR_STANDARD_BT_601: 1857e995a2eSmrg cstd = &bt_601; 186b167d5e7Smrg break; 187b167d5e7Smrg case VL_CSC_COLOR_STANDARD_BT_709: 1887e995a2eSmrg cstd = &bt_709; 189b167d5e7Smrg break; 190b167d5e7Smrg case VL_CSC_COLOR_STANDARD_SMPTE_240M: 1917e995a2eSmrg cstd = &smpte240m; 192b167d5e7Smrg break; 1937e995a2eSmrg case VL_CSC_COLOR_STANDARD_BT_709_REV: 1947e995a2eSmrg memcpy(matrix, bt_709_rev, sizeof(vl_csc_matrix)); 1957e995a2eSmrg return; 196b167d5e7Smrg case VL_CSC_COLOR_STANDARD_IDENTITY: 197b167d5e7Smrg default: 198b167d5e7Smrg assert(cs == VL_CSC_COLOR_STANDARD_IDENTITY); 199b167d5e7Smrg memcpy(matrix, identity, sizeof(vl_csc_matrix)); 200b167d5e7Smrg return; 201b167d5e7Smrg } 202b167d5e7Smrg 203b167d5e7Smrg (*matrix)[0][0] = c * (*cstd)[0][0]; 2047e995a2eSmrg (*matrix)[0][1] = (*cstd)[0][1] * x - (*cstd)[0][2] * y; 2057e995a2eSmrg (*matrix)[0][2] = (*cstd)[0][2] * x + (*cstd)[0][1] * y; 2067e995a2eSmrg (*matrix)[0][3] = (*cstd)[0][3] + (*cstd)[0][0] * b + 2077e995a2eSmrg (*cstd)[0][1] * (x * cbbias + y * crbias) + 2087e995a2eSmrg (*cstd)[0][2] * (x * crbias - y * cbbias); 209b167d5e7Smrg 210b167d5e7Smrg (*matrix)[1][0] = c * (*cstd)[1][0]; 2117e995a2eSmrg (*matrix)[1][1] = (*cstd)[1][1] * x - (*cstd)[1][2] * y; 2127e995a2eSmrg (*matrix)[1][2] = (*cstd)[1][2] * x + (*cstd)[1][1] * y; 2137e995a2eSmrg (*matrix)[1][3] = (*cstd)[1][3] + (*cstd)[1][0] * b + 2147e995a2eSmrg (*cstd)[1][1] * (x * cbbias + y * crbias) + 2157e995a2eSmrg (*cstd)[1][2] * (x * crbias - y * cbbias); 216b167d5e7Smrg 217b167d5e7Smrg (*matrix)[2][0] = c * (*cstd)[2][0]; 2187e995a2eSmrg (*matrix)[2][1] = (*cstd)[2][1] * x - (*cstd)[2][2] * y; 2197e995a2eSmrg (*matrix)[2][2] = (*cstd)[2][2] * x + (*cstd)[2][1] * y; 2207e995a2eSmrg (*matrix)[2][3] = (*cstd)[2][3] + (*cstd)[2][0] * b + 2217e995a2eSmrg (*cstd)[2][1] * (x * cbbias + y * crbias) + 2227e995a2eSmrg (*cstd)[2][2] * (x * crbias - y * cbbias); 223b167d5e7Smrg} 224