⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 grayf.cpp

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 CPP
📖 第 1 页 / 共 4 页
字号:
Void CFloatImage::mutiplyAlpha (const CFloatImage& fi)
{
	CRct rctIntersect = m_rc;
	rctIntersect.clip (fi.where());
	if (!rctIntersect.valid () || rctIntersect.empty ()) return;

	PixelF* ppxlfRowStart = (PixelF*) pixels (rctIntersect.left, rctIntersect.top);
	const PixelF* ppxlfRowStartMask = fi.pixels (rctIntersect.left, rctIntersect.top);
	for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++)	  {
		PixelF* ppxlf = ppxlfRowStart;
		const PixelF* ppxlfMask = ppxlfRowStartMask;
		for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++)	{
			assert (*ppxlfMask == transpValueF || *ppxlfMask == opaqueValueF);
			if (*ppxlfMask == transpValueF)	{
				*ppxlf = transpValueF;
			}
			else
				*ppxlf = *ppxlf * *ppxlfMask / opaqueValueF; //normalize
			ppxlf++;
			ppxlfMask++;
		}
		ppxlfRowStart += where().width;
		ppxlfRowStartMask += fi.where().width;

	}
}

Void CFloatImage::cropOnAlpha ()
{
	CRct rctVisible = whereVisible ();
	where (rctVisible);				
}

own CFloatImage& CFloatImage::operator += (const CFloatImage& fiSrc)
{
	assert (valid() && fiSrc.valid());
	assert (where() == fiSrc.where());
	PixelF *ppxlfDst = (PixelF *) pixels();
	const PixelF *ppxlfSrc = fiSrc.pixels();
	Int i,area = where().area();
	
	for(i=0;i<area;i++)
		*ppxlfDst++ += *ppxlfSrc++;
	
	return *this;
}

own CFloatImage* CFloatImage::operator + (const CFloatImage& fi) const
{
	if (!valid () || !fi.valid ()) return NULL;
	assert (where () == fi.where ());
	CFloatImage* pfiSumRet = new CFloatImage (where ());
	PixelF* ppxlfRet = (PixelF*) pfiSumRet -> pixels ();
	const PixelF* ppxlfThis = pixels ();
	const PixelF* ppxlfFi = fi.pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++, ppxlfRet++, ppxlfThis++, ppxlfFi++)
		*ppxlfRet = *ppxlfThis + *ppxlfFi;
	return pfiSumRet;
}

own CFloatImage* CFloatImage::operator - (const CFloatImage& fi) const
{
	if (!valid () || !fi.valid ()) return NULL;
	assert (where () == fi.where ());
	CFloatImage* pfiSumRet = new CFloatImage (where ());
	PixelF* ppxlfRet = (PixelF*) pfiSumRet -> pixels ();
	const PixelF* ppxlfThis = pixels ();
	const PixelF* ppxlfFi = fi.pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++, ppxlfRet++, ppxlfThis++, ppxlfFi++)
		*ppxlfRet = *ppxlfThis - *ppxlfFi;
	return pfiSumRet;
}

own CFloatImage* CFloatImage::operator * (Float scale) const
{
	if (!valid ()) return NULL;
	CFloatImage* pfiSumRet = new CFloatImage (where ());
	PixelF* ppxlfRet = (PixelF*) pfiSumRet -> pixels ();
	const PixelF* ppxlfThis = pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++, ppxlfRet++, ppxlfThis++)
		*ppxlfRet = *ppxlfThis * scale;
	return pfiSumRet;
}

own CFloatImage* CFloatImage::operator * (const CTransform& tf) const
{
	CFloatImage* pfiRet = tf.apply (*this);
	return pfiRet; 
}

own CFloatImage* CFloatImage::operator / (Float scale) const
{
	if (!valid ()) return NULL;
	assert (scale != .0f);
	CFloatImage* pfiSumRet = new CFloatImage (where ());
	PixelF* ppxlfRet = (PixelF*) pfiSumRet -> pixels ();
	const PixelF* ppxlfThis = pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++, ppxlfRet++, ppxlfThis++)
		*ppxlfRet = *ppxlfThis / scale;
	return pfiSumRet;
}

Void CFloatImage::operator = (const CFloatImage& fi)
{
	delete [] m_ppxlf;
	copyConstruct (fi, fi.where ());
}

Bool CFloatImage::operator == (const CFloatImage& fi) const
{
	if (fi.where () != where ())
		return FALSE;
	UInt area = where ().area ();
	const PixelF* ppxlf = fi.pixels ();
	const PixelF* ppxlfThis = pixels ();
	for (UInt ip = 0; ip < area; ip++, ppxlf++, ppxlfThis++)
		if (*ppxlf != *ppxlfThis)
			return FALSE;
	return TRUE;
}

Double CFloatImage::mse (const CFloatImage& fiCompare) const
{
	assert (fiCompare.where () == where ());
	Double sqr = 0;
	const PixelF* ppxlfThis = pixels ();
	const PixelF* ppxlfCompare = fiCompare.pixels ();
	UInt area = where ().area ();
	UInt uiNonTransp = 0;
	for (UInt i = 0; i < area; i++) {
			Double diffR = (Double) (*ppxlfThis - *ppxlfCompare);
			sqr += diffR * diffR;
			uiNonTransp++;
		ppxlfThis++;
		ppxlfCompare++;
	}
	sqr /= (Double) uiNonTransp;
	return sqr;
}

Double CFloatImage::mse (const CFloatImage& fiCompare, const CFloatImage& fiMsk) const
{
	assert (fiCompare.where () == where () && fiMsk.where () == where ());
	Double sqr = 0;
	const PixelF* ppxlfThis = pixels ();
	const PixelF* ppxlfCompare = fiCompare.pixels ();
	const PixelF* ppxlfMsk = fiMsk.pixels ();
	UInt area = where ().area ();
	UInt uiNonTransp = 0;
	for (UInt i = 0; i < area; i++) {
		if (*ppxlfMsk != transpValue) {
			Double diffR = (Double) (*ppxlfThis - ((Int) *ppxlfCompare));
			sqr += diffR * diffR;
			uiNonTransp++;
		}
		ppxlfThis++;
		ppxlfCompare++;
		ppxlfMsk++;
	}
	if (uiNonTransp == 0) 
		return 0;
	sqr /= (Double) uiNonTransp;
	return sqr;
}

Double CFloatImage::snr (const CFloatImage& fiCompare) const
{
	Double msError = 0;
	msError = mse (fiCompare);
	if (msError == 0.0)
		return 1000000.0;
	else 
		return (log10 (255 * 255 / msError) * 10.0);
}

Double CFloatImage::snr (const CFloatImage& fiCompare, const CFloatImage& fiMsk) const
{
	CFloatImage* pfiMskOp = NULL;
	Double msError = 0;
	if (&fiMsk == NULL) {
		pfiMskOp = new CFloatImage (where (), (PixelF) opaqueValue);		
		msError = mse (fiCompare, *pfiMskOp);
		delete pfiMskOp;
	}
	else
		msError = mse (fiCompare, fiMsk);
	if (msError == 0.0)
		return 1000000.0;
	else 
		return (log10 (255 * 255 / msError) * 10.0);
}


Void CFloatImage::vdlDump (const Char* fileName) const
{
	CVideoObjectPlane vop (where (), opaquePixel);
	CPixel* ppxl = (CPixel*) vop.pixels ();
	const PixelF* ppxlf = pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++, ppxl++, ppxlf++) {
		U8 vl = (U8) (checkrange (*ppxlf, 0.0f, 255.0f) + 0.5f);
		*ppxl = CPixel (vl, vl, vl, opaqueValue);
	}
	vop.vdlDump (fileName);
}
	
Void CFloatImage::dump (FILE* pf) const
{
	assert (pf != NULL);
	UInt area = where ().area ();
	U8* rguchPixelData = new U8 [where().area()];
	U8* puch = rguchPixelData;
	const PixelF* ppxlf = pixels ();
	for (UInt ip = 0; ip < area; ip++, puch++, ppxlf++) {
		U8 vl = (U8) checkrange (*ppxlf, 0.0f, 255.0f);
		*puch = vl;
	}
	fwrite (rguchPixelData, sizeof (U8), area, pf);
	delete [] rguchPixelData;
}
	
Void CFloatImage::txtDump (const Char* fileName) const
{
	FILE* pfTxt;
	const PixelF* ppxlf = pixels ();
	if (fileName != NULL)
		pfTxt = fopen (fileName, "w");
	else
		pfTxt = NULL;
	for (CoordI y = 0; y < where ().height (); y++) {
		for (CoordI x = 0; x < where ().width; x++) {
			if (pfTxt != NULL)
				fprintf (pfTxt, "%6.2f  ", *ppxlf);
			else
				printf ("%d  ", (Int) *ppxlf);
			ppxlf++;
		}
		if (pfTxt != NULL)
			fprintf (pfTxt, "\n");
		else
			printf ("\n");
	}
	if (pfTxt != NULL)
		fclose (pfTxt);

}


Void CFloatImage::txtDump(FILE *pf) const
{
	const PixelF *ppxlf = pixels();
	for (CoordI y = 0; y < where ().height (); y++) {
		for (CoordI x = 0; x < where ().width; x++) {
			fprintf (pf, "%6.2f ", *ppxlf);
			ppxlf++;
		}
		fprintf (pf,"\n");
	}
	fprintf (pf,"\n");
}

Void CFloatImage::txtDumpMask(FILE *pf) const
{
	const PixelF *ppxlf = pixels();
	for (CoordI y = 0; y < where ().height (); y++) {
		for (CoordI x = 0; x < where ().width; x++) {
			if(*ppxlf==transpValue)
				fprintf (pf, "..");
			else
				fprintf (pf, "[]");
			ppxlf++;
		}
		fprintf (pf,"\n");
	}
	fprintf (pf,"\n");
}

Void CFloatImage::quantize (Int stepsize, Bool dpcm)
{
	CoordI x, y;
	const Int owidth = where ().width;
	const CoordI left = where ().left;
	const CoordI top = where ().top;
	const CoordI right = where ().right;
	const CoordI bottom = where ().bottom;
	PixelF* px = (PixelF*) pixels ();
	if (TRUE){
		//Float halfStepsize = (Float) stepsize / 2;
		for (y = top; y != bottom; y++) {
			for (x = left; x != right; x++) {
				//Float round = (*px >= 0) ? halfStepsize : -halfStepsize;
				//*px++ = (Float) ((Int) ((*px + round) / stepsize));
				*px++ = (Float) ((Int) (*px / stepsize));
			}
		}
	}
	else {
		Float halfStepsize = (Float) stepsize / 4;
		//Float halfStepsize = (Float) stepsize / 2;
		for (y = top; y != bottom; y++) {
			for (x = left; x != right; x++) {
				Float round = (*px >= 0) ? halfStepsize : -halfStepsize;
				*px++ = (Float) ((Int) ((*px - round) / stepsize));
				//*px++ = (Float) ((Int) ((*px + round) / stepsize));
				//*px++ = (Float) ((Int) ((*px ) / stepsize));
			}
		}
	}

	if (dpcm) { // zig-zag dpcm
		CFloatImage* buf1 = new CFloatImage (*this);
		for (y = top; y != bottom; y++) {
			if (((y - top) & 1) == 0) { // even
				const Float* pbuf = buf1 -> pixels (left, y);
				PixelF* psig = (PixelF*) pixels (left, y);
				*psig++ = (y != top) ? *pbuf - *(pbuf - owidth) : *pbuf;
				pbuf++;
				for (x = left + 1; x != right; x++) {
					*psig = *pbuf - *(pbuf - 1);
					psig++;
					pbuf++;
				}
			}
			else {
				const PixelF* pbuf = buf1 -> pixels (right - 1, y);
				PixelF* psig = (PixelF*) pixels (right - 1, y);
				*psig-- = *pbuf - *(pbuf - owidth);
				pbuf--;
				for (x = right - 2; x >= left; x--) {
					*psig = *pbuf - *(pbuf + 1);
					psig--;
					pbuf--;
				}
			}
		}
		delete buf1;
	}
}


Void CFloatImage::deQuantize (Int stepsize, Bool dpcm)
{
	CoordI x, y;
	const Int owidth = where ().width;
	const CoordI left = where ().left;
	const CoordI top = where ().top;
	const CoordI right = where ().right;
	const CoordI bottom = where ().bottom;
	if (dpcm) {
		for (y = top; y != bottom; y++) {
			if (((y - top) & 1) == 0) {
				PixelF* psig = (PixelF*) pixels (left, y);
				if (y != top)
					*psig += *(psig - owidth);
				psig++;
				for (x = left + 1; x != right; x++) {
					*psig += *(psig - 1);
					psig++;
				}
			}
			else {
				PixelF* psig = (PixelF*) pixels (right - 1, y);
				*psig += *(psig - owidth);
				psig--;
				for (x = right - 2; x >= left; x--) {
					*psig += *(psig + 1);
					psig--;
				}
			}
		}
	}
	Float halfStepsize ;
	if ( stepsize % 4 == 0)
		halfStepsize = (Float) stepsize / 2 - 1;
	else
		halfStepsize = (Float) stepsize / 2;

	PixelF* px = (PixelF*) pixels ();
	for (y = top; y != bottom; y++) {
		for (x = left; x != right; x++){
			//Int round = (*px >= 0) ? (stepsize >> 1) : -(stepsize >> 1);
			Float round = (*px >= 0) ? (halfStepsize) : -(halfStepsize);
			*px = (*px == 0)? *px: (*px * (Float) stepsize + round);
			px++;
			//*px++ *= (Float) stepsize;
		}
	}
}

Void CFloatImage::roundNearestInt()
{
	Int i;
	PixelF fVal,*ppxlf = (PixelF *)pixels();

	for(i=where().area();i;i--,ppxlf++)
	{
		fVal = *ppxlf;
		if(fVal>=0)
			*ppxlf = (PixelF)floor(fVal+0.5);
		else
			*ppxlf = (PixelF)ceil(fVal-0.5);
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -