|
发表于 2023-10-24 16:10:47
|
显示全部楼层
广西壮族自治区钦州市
using System;
public class YuvToRgbConverter
{
public static void Convert(byte[] yuvData, byte[] rgbData, int width, int height)
{
int frameSize = width * height;
int uvOffset = frameSize;
for (int y = 0; y < height; y++)
{
int yIndex = y * width;
int uvIndex = uvOffset + (y / 2) * width;
for (int x = 0; x < width; x++)
{
int yValue = yuvData[yIndex + x];
int uValue = yuvData[uvIndex + (x / 2) * 2];
int vValue = yuvData[uvIndex + (x / 2) * 2 + 1];
int c = yValue - 16;
int d = uValue - 128;
int e = vValue - 128;
int r = (298 * c + 409 * e + 128) >> 8;
int g = (298 * c - 100 * d - 208 * e + 128) >> 8;
int b = (298 * c + 516 * d + 128) >> 8;
rgbData[yIndex * 3 + x * 3] = Clamp(r);
rgbData[yIndex * 3 + x * 3 + 1] = Clamp(g);
rgbData[yIndex * 3 + x * 3 + 2] = Clamp(b);
}
}
}
private static byte Clamp(int value)
{
return (byte)(value < 0 ? 0 : (value > 255 ? 255 : value));
}
} |
|