examples: Add RGB<->YUV color conversion shader

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2021-11-25 10:02:45 +01:00
parent b366cf055c
commit 1d4bc9377f

View file

@ -0,0 +1,74 @@
// Copyright 2021 Michael Fabian Dirks <info@xaymar.com>
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
// 3. Neither the name of the copyright holder nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
// ================================================================================ //
// Matrices
// ================================================================================ //
#define RGB_YUV_709 float3x3( 0.21260, 0.71520, 0.07220,\
-0.11457, -0.38543, 0.50000,\
0.50000, -0.45415, -0.04585)
#define YUV_709_RGB float3x3( 1.00000, 0.00000, 1.57480,\
1.00000, -0.18732, -0.46812,\
1.00000, 1.85560, 0.00000)
// ================================================================================ //
// Functions: Balanced Color Conversion
// ================================================================================ //
float3 RGBtoYUV(float3 rgb, float3x3 m) {
return mul(m, rgb) + float3(0, .5, .5);
}
float4 RGBAtoYUVA(float4 rgba, float3x3 m) {
return float4(RGBtoYUV(rgba.rgb, m), rgba.a);
}
float3 YUVtoRGB(float3 yuv, float3x3 m) {
return mul(m, yuv - float3(0, .5, .5));
}
float4 YUVAtoRGBA(float4 yuva, float3x3 m) {
return float4(YUVtoRGB(yuva.rgb, m), yuva.a);
}
// ================================================================================ //
// Function: Unbalanced Color Conversion
// ================================================================================ //
float3 RGBtoYUVf(float3 rgb, float3x3 m) {
return mul(m, rgb);
}
float4 RGBAtoYUVAf(float4 rgba, float3x3 m) {
return float4(RGBtoYUVf(rgba.rgb, m), rgba.a);
}
float3 YUVtoRGBf(float3 yuv, float3x3 m) {
return mul(m, yuv);
}
float4 YUVAtoRGBAf(float4 yuva, float3x3 m) {
return float4(YUVtoRGBf(yuva.rgb, m), yuva.a);
}