CIF文件转换为QCIF

#include <iostream>
#include <cstdio>
#include <fstream>

using namespace std;
const char* inCIF = "miss.cif";
const char* outQCIF = "miss.qcif";
int main()
{
	ifstream in(inCIF, ios::binary);
	ofstream out(outQCIF, ios::binary);
	if (!(in.is_open() & in.is_open()))
	{
		cout << "open failed." << endl;
		exit(1);
	}

	int height = 288, width = 352;
	int newHeight = 144, newWidth = 176;
	unsigned char* yBuffer, * uBuffer, * vBuffer;
	unsigned char* newYBuffer, * newUBuffer, * newVBuffer;

	yBuffer = new unsigned char[height * width];
	for (auto i : { &uBuffer, &vBuffer })
		*i = new unsigned char[height * width / 4];
	
	newYBuffer = new unsigned char[newWidth * newHeight];
	for( auto i : {&newUBuffer, &newVBuffer})
		*i = new unsigned char[newHeight * newWidth / 4];

	while( !in.eof() )
	{
		bool flag = 0;
		if (!in.read((char*)yBuffer, width * height))
			break;
		for (auto i : { &uBuffer, &vBuffer })
			if (!in.read((char*)(*i), height * width / 4))
			{
				break; flag = 1;
			}
	    
		int pos = 0;
		for (int i = 0; i < width * height; ++i)
		{
			int h = i / width, w = i % width;
			if ((h & 1) || (w & 1)) continue;
			newYBuffer[pos++] = yBuffer[i];
		}
		pos = 0;
		for (int i = 0; i < width * height / 4; ++i)
		{
			int h = i * 2/ width, w = i % ( width / 2);
			if ((h & 1) || (w & 1)) continue;
			newUBuffer[pos] = uBuffer[i];
			newVBuffer[pos++] = vBuffer[i];
		}

		if (flag) break;
		out.write((char*)newYBuffer, newWidth * newHeight);
		for (auto& i : { &newUBuffer, &newVBuffer })
			out.write((char*)(*i), newWidth * newHeight / 4 );
	}
	in.close(); out.close();
	for (auto& i : { &yBuffer, &uBuffer, &vBuffer, &newYBuffer, &newUBuffer, &newVBuffer })
		delete[] * i;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值