X-Git-Url: https://git.libre-soc.org/?a=blobdiff_plain;f=src%2Fgallium%2Fauxiliary%2Fvl%2Fvl_csc.c;h=e4395d0624d20e56d431d42bea1ea6fdebc71095;hb=e6469ec43b25898e99766a30aa8f54cc64c3bc04;hp=75159be80dfd0c4f6983e28ae4df39c1ae700a36;hpb=5ed848129cb57269e80caf2c1ca522ae41f4500b;p=mesa.git diff --git a/src/gallium/auxiliary/vl/vl_csc.c b/src/gallium/auxiliary/vl/vl_csc.c index 75159be80df..e4395d0624d 100644 --- a/src/gallium/auxiliary/vl/vl_csc.c +++ b/src/gallium/auxiliary/vl/vl_csc.c @@ -18,15 +18,15 @@ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. - * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR + * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * **************************************************************************/ -#include -#include +#include "util/u_math.h" +#include "util/u_debug.h" #include "vl_csc.h" @@ -59,10 +59,10 @@ * [ 0, 0, 0, 1] * * brightness - * [ 1, 0, 0, b] - * [ 0, 1, 0, 0] - * [ 0, 0, 1, 0] - * [ 0, 0, 0, 1] + * [ 1, 0, 0, b/c] + * [ 0, 1, 0, 0] + * [ 0, 0, 1, 0] + * [ 0, 0, 0, 1] * * saturation * [ 1, 0, 0, 0] @@ -100,108 +100,115 @@ * Y is in [16,235], Cb and Cr are in [16,240] * R, G, and B are in [16,235] */ -static const float bt_601[16] = +static const vl_csc_matrix bt_601 = { - 1.0f, 0.0f, 1.371f, 0.0f, - 1.0f, -0.336f, -0.698f, 0.0f, - 1.0f, 1.732f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + { 1.0f, 0.0f, 1.371f, 0.0f, }, + { 1.0f, -0.336f, -0.698f, 0.0f, }, + { 1.0f, 1.732f, 0.0f, 0.0f, } }; /* - * Converts ITU-R BT.601 YCbCr pixels to RGB pixels where: + * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where: * Y is in [16,235], Cb and Cr are in [16,240] - * R, G, and B are in [0,255] + * R, G, and B are in [16,235] */ -static const float bt_601_full[16] = +static const vl_csc_matrix bt_709 = { - 1.164f, 0.0f, 1.596f, 0.0f, - 1.164f, -0.391f, -0.813f, 0.0f, - 1.164f, 2.018f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + { 1.0f, 0.0f, 1.540f, 0.0f, }, + { 1.0f, -0.183f, -0.459f, 0.0f, }, + { 1.0f, 1.816f, 0.0f, 0.0f, } }; /* - * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where: + * Converts SMPTE 240M YCbCr pixels to RGB pixels where: * Y is in [16,235], Cb and Cr are in [16,240] * R, G, and B are in [16,235] */ -static const float bt_709[16] = +static const vl_csc_matrix smpte240m = { - 1.0f, 0.0f, 1.540f, 0.0f, - 1.0f, -0.183f, -0.459f, 0.0f, - 1.0f, 1.816f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + { 1.0f, 0.0f, 1.541f, 0.0f, }, + { 1.0f, -0.221f, -0.466f, 0.0f, }, + { 1.0f, 1.785f, 0.0f, 0.0f, } }; -/* - * Converts ITU-R BT.709 YCbCr pixels to RGB pixels where: - * Y is in [16,235], Cb and Cr are in [16,240] - * R, G, and B are in [0,255] - */ -static const float bt_709_full[16] = +static const vl_csc_matrix identity = { - 1.164f, 0.0f, 1.793f, 0.0f, - 1.164f, -0.213f, -0.534f, 0.0f, - 1.164f, 2.115f, 0.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f + { 1.0f, 0.0f, 0.0f, 0.0f, }, + { 0.0f, 1.0f, 0.0f, 0.0f, }, + { 0.0f, 0.0f, 1.0f, 0.0f, } }; -static const float identity[16] = -{ - 1.0f, 0.0f, 0.0f, 0.0f, - 0.0f, 1.0f, 0.0f, 0.0f, - 0.0f, 0.0f, 1.0f, 0.0f, - 0.0f, 0.0f, 0.0f, 1.0f +const struct vl_procamp vl_default_procamp = { + 0.0f, /* brightness */ + 1.0f, /* contrast */ + 1.0f, /* saturation */ + 0.0f /* hue */ }; void vl_csc_get_matrix(enum VL_CSC_COLOR_STANDARD cs, struct vl_procamp *procamp, bool full_range, - float *matrix) + vl_csc_matrix *matrix) { - float ybias = full_range ? -16.0f/255.0f : 0.0f; float cbbias = -128.0f/255.0f; float crbias = -128.0f/255.0f; - float c = procamp ? procamp->contrast : 1.0f; - float s = procamp ? procamp->saturation : 1.0f; - float b = procamp ? procamp->brightness : 0.0f; - float h = procamp ? procamp->hue : 0.0f; - const float *cstd; + + const struct vl_procamp *p = procamp ? procamp : &vl_default_procamp; + float c = p->contrast; + float s = p->saturation; + float b = p->brightness; + float h = p->hue; + float x, y; + + const vl_csc_matrix *cstd; + + if (full_range) { + c *= 1.164f; /* Adjust for the y range */ + b *= 1.164f; /* Adjust for the y range */ + b -= c * 16.0f / 255.0f; /* Adjust for the y bias */ + } + + /* Parameter substitutions */ + x = c * s * cosf(h); + y = c * s * sinf(h); assert(matrix); switch (cs) { case VL_CSC_COLOR_STANDARD_BT_601: - cstd = full_range ? &bt_601_full[0] : &bt_601[0]; + cstd = &bt_601; break; case VL_CSC_COLOR_STANDARD_BT_709: - cstd = full_range ? &bt_709_full[0] : &bt_709[0]; + cstd = &bt_709; + break; + case VL_CSC_COLOR_STANDARD_SMPTE_240M: + cstd = &smpte240m; break; case VL_CSC_COLOR_STANDARD_IDENTITY: default: assert(cs == VL_CSC_COLOR_STANDARD_IDENTITY); - memcpy(matrix, &identity[0], sizeof(float) * 16); + memcpy(matrix, identity, sizeof(vl_csc_matrix)); return; } - matrix[ 0] = c*cstd[ 0]; - matrix[ 1] = c*cstd[ 1]*s*cosf(h) - c*cstd[ 2]*s*sinf(h); - matrix[ 2] = c*cstd[ 2]*s*cosf(h) + c*cstd[ 1]*s*sinf(h); - matrix[ 3] = cstd[ 3] + cstd[ 0]*(b + c*ybias) + cstd[ 1]*(c*cbbias*s*cosf(h) + c*crbias*s*sinf(h)) + cstd[ 2]*(c*crbias*s*cosf(h) - c*cbbias*s*sinf(h)); - - matrix[ 4] = c*cstd[ 4]; - matrix[ 5] = c*cstd[ 5]*s*cosf(h) - c*cstd[ 6]*s*sinf(h); - matrix[ 6] = c*cstd[ 6]*s*cosf(h) + c*cstd[ 5]*s*sinf(h); - matrix[ 7] = cstd[ 7] + cstd[ 4]*(b + c*ybias) + cstd[ 5]*(c*cbbias*s*cosf(h) + c*crbias*s*sinf(h)) + cstd[ 6]*(c*crbias*s*cosf(h) - c*cbbias*s*sinf(h)); - - matrix[ 8] = c*cstd[ 8]; - matrix[ 9] = c*cstd[ 9]*s*cosf(h) - c*cstd[10]*s*sinf(h); - matrix[10] = c*cstd[10]*s*cosf(h) + c*cstd[ 9]*s*sinf(h); - matrix[11] = cstd[11] + cstd[ 8]*(b + c*ybias) + cstd[ 9]*(c*cbbias*s*cosf(h) + c*crbias*s*sinf(h)) + cstd[10]*(c*crbias*s*cosf(h) - c*cbbias*s*sinf(h)); - - matrix[12] = c*cstd[12]; - matrix[13] = c*cstd[13]*s*cos(h) - c*cstd[14]*s*sin(h); - matrix[14] = c*cstd[14]*s*cos(h) + c*cstd[13]*s*sin(h); - matrix[15] = 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)); + (*matrix)[0][0] = c * (*cstd)[0][0]; + (*matrix)[0][1] = (*cstd)[0][1] * x - (*cstd)[0][2] * y; + (*matrix)[0][2] = (*cstd)[0][2] * x + (*cstd)[0][1] * y; + (*matrix)[0][3] = (*cstd)[0][3] + (*cstd)[0][0] * b + + (*cstd)[0][1] * (x * cbbias + y * crbias) + + (*cstd)[0][2] * (x * crbias - y * cbbias); + + (*matrix)[1][0] = c * (*cstd)[1][0]; + (*matrix)[1][1] = (*cstd)[1][1] * x - (*cstd)[1][2] * y; + (*matrix)[1][2] = (*cstd)[1][2] * x + (*cstd)[1][1] * y; + (*matrix)[1][3] = (*cstd)[1][3] + (*cstd)[1][0] * b + + (*cstd)[1][1] * (x * cbbias + y * crbias) + + (*cstd)[1][2] * (x * crbias - y * cbbias); + + (*matrix)[2][0] = c * (*cstd)[2][0]; + (*matrix)[2][1] = (*cstd)[2][1] * x - (*cstd)[2][2] * y; + (*matrix)[2][2] = (*cstd)[2][2] * x + (*cstd)[2][1] * y; + (*matrix)[2][3] = (*cstd)[2][3] + (*cstd)[2][0] * b + + (*cstd)[2][1] * (x * cbbias + y * crbias) + + (*cstd)[2][2] * (x * crbias - y * cbbias); }