I420转RGB24,YUY2转RGB24,RGB24垂直翻转,RGB24水平翻转

I420转RGB24

bool I420ToRGB24(unsigned char* out, const unsigned char* src, int width, int height)
{
    if (src == NULL || width <=0 || height <= 0)
        return false;
	const int R = 0;
	const int G = 1;
	const int B = 2;

	int numOfPixel = width * height;
	int positionOfU = numOfPixel;
	int positionOfV = numOfPixel / 4 + numOfPixel;
	int temp = 0;
	for (int i = 0; i<height; i++) {
		int startY = i*width;
		int step = (i / 2)*(width / 2);
		int startV = positionOfV + step;
		int startU = positionOfU + step;
		for (int j = 0; j < width; j++) {
			int Y = startY + j;
			int V = startV + j / 2;
			int U = startU + j / 2;
			int index = Y * 3;

			temp = (int)((src[Y] & 0xff) + 1.4075 * ((src[V] & 0xff) - 128));
			out[index + B] = temp<0 ? 0 : (temp > 255 ? 255 : temp);

			temp = (int)((src[Y] & 0xff) - 0.3455* ((src[U] & 0xff) - 128) - 0.7169*((src[V] & 0xff) - 128));
			out[index + G] = temp<0 ? 0 : (temp > 255 ? 255 : temp);

			temp = (int)((src[Y] & 0xff) + 1.779* ((src[U] & 0xff) - 128));
			out[index + R] = temp<0 ? 0 : (temp > 255 ? 255 : temp);
		}
	}
	return true;
}

YUY2转RGB24

bool ConvertYUY2toRGB24(unsigned char* pOutputRgb,
    unsigned char* pInputBuffer,
    int tInputWidth,
    int tInputHeight)
{
	if (pInputBuffer == NULL || tInputWidth <= 0 || tInputHeight <= 0)
		return false;
	unsigned char* yuyv = pInputBuffer;
	int z = 0, i = 0, j = 0;
	for (i = 0; i < tInputHeight; i++) {
		for (j = 0; j < tInputWidth; j++) {
			int r, g, b;
			int y, u, v;

			if (!z)
				y = yuyv[0] << 8;
			else
				y = yuyv[2] << 8;
			u = yuyv[1] - 128;
			v = yuyv[3] - 128;

			r = (y + (359 * v)) >> 8;
			g = (y - (88 * u) - (183 * v)) >> 8;
			b = (y + (454 * u)) >> 8;

			r = (r > 255) ? 255 : ((r < 0) ? 0 : r);
			g = (g > 255) ? 255 : ((g < 0) ? 0 : g);
			b = (b > 255) ? 255 : ((b < 0) ? 0 : b);

			*pOutputRgb++ = b;
			*pOutputRgb++ = g;
			*pOutputRgb++ = r;

			if (z++) {
				z = 0;
				yuyv += 4;
			}
		}
	}
	return true;
}

RGB24垂直翻转

bool HorizontalFlipRGB24(unsigned char* rgb24, int iWidth, int iHeight)
{
	// 输入参数合法性判断
	if (rgb24 == NULL || iWidth <= 0 || iHeight <= 0) return false;

	// 每行图像数据的字节数
	int iLBytes = (iWidth * 24 + 23) / 24 * 3;

	// 临时RGB图像指针
	unsigned char *prgbtmp = (unsigned char*)malloc(iLBytes*iHeight);
	if (prgbtmp == NULL) {
		return false;
	}
	else {
		std::memset(prgbtmp, 0, iLBytes*iHeight);
	}

	// 每行
	for (int i = 0; i < iHeight; i++)
	{
		// 每列
		for (int j = 0; j < iWidth; j++)
		{
			std::memcpy((prgbtmp + iLBytes * (iHeight - 1 - i) + 3 * (iWidth - 1 - j)), (rgb24 + iLBytes * (iHeight - 1 - i) + 3 * j), 3);
		}
	}
	std::memcpy(rgb24, prgbtmp, iLBytes*iHeight);
	free(prgbtmp);
	return true;
}

RGB24水平翻转

bool VerticalRotateRGB24(unsigned char* rgb24, int width, int height)
{
	if (rgb24 == NULL || width == 0 || height == 0)
		return false;
	unsigned char* rgb = new unsigned char[width * height * 3]; //3表示位24位的视频流
	if (rgb == NULL)
		return false;
	for (int x = 0; x < height; x++)
	{
		std::memcpy(rgb + width * 3 * x, rgb24 + (height - 1 - x) * width * 3, width * 3);
	}
	memcpy(rgb24, rgb, width * height * 3);
	delete[] rgb;
	return true;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

简单前行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值