如果不使用规定的YUV2RGB 转换矩阵来转换可能会造成overflkow
#include <stdio.h> static const float matrix_rgb2yuv_709[] = { 0.2126, 0.7152, 0.0722, - 0.1146, -0.3854, 0.5000, 0.5000, -0.4542, -0.0458 }; static const float matrix_rgb2yuv_709_invert[] = { 1, 0, 1.5747, 1, -0.1873, -0.4682, 1, 1.8556, 0 }; static const float matrix_rgb2yuv_ntsc[] = { 0.2989, 0.5866, 0.1145, -0.1688, -0.3312, 0.5000, 0.5000, -0.4184, -0.0816 }; static const float matrix_rgb2yuv_ntsc_invert[] = { 1, 0, 1.4022, 1, -0.3456, -0.7145, 1, 1.7710, 0 }; void mul32f_3x3(const float* matrix, float* in, float* out) { out[0] = matrix[0] * in[0] + matrix[1] * in[1] + matrix[2] * in[2]; out[1] = matrix[3] * in[0] + matrix[4] * in[1] + matrix[5] * in[2]; out[2] = matrix[6] * in[0] + matrix[7] * in[1] + matrix[8] * in[2]; } int check_if_overflow() { for (int i = 0; i < 256; i+=255) { for (int j = 0; j < 256; j+=255) { for (int k = 0; k < 256; k+=255) { float src[] = { i/255.0,j/255.0,k/255.0 }; //float src[] = { 0/255.0,255/255.0,0/255.0 }; float srcyuv[3], dstrgb[3]; mul32f_3x3(matrix_rgb2yuv_709, src, srcyuv); mul32f_3x3(matrix_rgb2yuv_ntsc_invert, srcyuv, dstrgb); if (dstrgb[0] < -0.1 || dstrgb[1] < -0.1 || dstrgb[2] < -0.1) { printf("srcyuv:%.4f %.4f %.4f dstrgb:%.4f %.4f %.4f\n", srcyuv[0], srcyuv[1], srcyuv[2], dstrgb[0], dstrgb[1], dstrgb[2]); //return -1; } } } } return 0; } int main() { return check_if_overflow(); }
output
srcyuv:0.2126 -0.1146 0.5000 dstrgb:0.9137 -0.1050 0.0096
srcyuv:0.2848 0.3854 0.4542 dstrgb:0.9217 -0.1729 0.9673