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

📄 grayi.cpp

📁 MPEG-4编解码的实现(包括MPEG4视音频编解码)
💻 CPP
📖 第 1 页 / 共 3 页
字号:
/*************************************************************************

This software module was originally developed by 

	Ming-Chieh Lee (mingcl@microsoft.com), Microsoft Corporation
	Wei-ge Chen (wchen@microsoft.com), Microsoft Corporation
	Bruce Lin (blin@microsoft.com), Microsoft Corporation
	Chuang Gu (chuanggu@microsoft.com), Microsoft Corporation
	(date: March, 1996)

in the course of development of the MPEG-4 Video (ISO/IEC 14496-2). 
This software module is an implementation of a part of one or more MPEG-4 Video tools 
as specified by the MPEG-4 Video. 
ISO/IEC gives users of the MPEG-4 Video free license to this software module or modifications 
thereof for use in hardware or software products claiming conformance to the MPEG-4 Video. 
Those intending to use this software module in hardware or software products are advised that its use may infringe existing patents. 
The original developer of this software module and his/her company, 
the subsequent editors and their companies, 
and ISO/IEC have no liability for use of this software module or modifications thereof in an implementation. 
Copyright is not released for non MPEG-4 Video conforming products. 
Microsoft retains full right to use the code for his/her own purpose, 
assign or donate the code to a third party and to inhibit third parties from using the code for non <MPEG standard> conforming products. 
This copyright notice must be included in all copies or derivative works. 

Copyright (c) 1996, 1997.


Module Name:

	grayf.hpp

Abstract:

	Float image class for gray (one-plane) pictures 

Revision History:

*************************************************************************/

#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include "typeapi.h"

#ifdef __MFC_
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif

#define new DEBUG_NEW				   
#endif // __MFC_


CIntImage::~CIntImage ()
{
	delete [] m_ppxli;
	m_ppxli = NULL;
}

Void CIntImage::allocate (const CRct& r, PixelI pxli) 
{
	m_rc = r;
	delete [] m_ppxli, m_ppxli = NULL;

	// allocate pixels and initialize
	if (m_rc.empty ()) return;
	m_ppxli = new PixelI [m_rc.area ()];
	assert (m_ppxli);
	for (UInt ic = 0; ic < where ().area (); ic++)
		m_ppxli [ic] = pxli;
}

Void CIntImage::allocate (const CRct& r) 
{
	m_rc = r;
	delete [] m_ppxli, m_ppxli = NULL;

	// allocate pixels and initialize
	if (m_rc.empty ()) return;
	m_ppxli = new PixelI [m_rc.area ()];
	assert (m_ppxli);
}

Void CIntImage::copyConstruct (const CIntImage& ii, const CRct& rct) 
{
	CRct r = rct;
	if (!r.valid()) 
		r = ii.where ();
	if (!ii.valid () || (!ii.m_rc.empty () && ii.m_ppxli == NULL))
		assert (FALSE);
	allocate (r, (PixelI) 0);
	if (!valid ()) return; 

	// Copy data
	if (r == ii.where ())
		memcpy (m_ppxli, ii.pixels (), m_rc.area () * sizeof (PixelI));
	else {
		r.clip (ii.where ()); // find the intersection
		CoordI x = r.left; // copy pixels
		Int cbLine = r.width * sizeof (PixelI);
		PixelI* ppxl = (PixelI*) pixels (x, r.top);
		const PixelI* ppxlFi = ii.pixels (x, r.top);
		Int widthCurr = where ().width;
		Int widthFi = ii.where ().width;
		for (CoordI y = r.top; y < r.bottom; y++) {
			memcpy (ppxl, ppxlFi, cbLine);
			ppxl += widthCurr;
			ppxlFi += widthFi;
		}
	}
}

Void CIntImage::swap (CIntImage& ii) 
{
	assert (this && &ii);
	CRct rcT = ii.m_rc; 
	ii.m_rc = m_rc; 
	m_rc = rcT; 
	PixelI* ppxliT = ii.m_ppxli; 
	ii.m_ppxli = m_ppxli; 
	m_ppxli = ppxliT; 
}

CIntImage::CIntImage (const CIntImage& ii, const CRct& r) : m_ppxli (NULL)
{
	copyConstruct (ii, r);
}

CIntImage::CIntImage (const CRct& r, PixelI px) : m_ppxli (NULL)
{
	allocate (r, px);
}

CIntImage::CIntImage (const CVideoObjectPlane& vop, RGBA comp) : m_ppxli (NULL)
{
	if (!vop.valid ()) return;
	
	allocate (vop.where ());
	const CPixel* ppxl = vop.pixels ();
	for (UInt ip = 0; ip < where ().area (); ip++)
		m_ppxli [ip] = ppxl [ip].pxlU.color [comp];
}

CIntImage::CIntImage (
	const Char* pchFileName, 
	UInt ifr,
	const CRct& rct,
	UInt nszHeader
) : m_ppxli (NULL), m_rc (rct)
{
	assert (!rct.empty ());
	allocate (rct);
	UInt uiArea = rct.area ();
	
	// read data from a file
	FILE* fpSrc = fopen (pchFileName, "rb");
	assert (fpSrc != NULL);
	fseek (fpSrc, nszHeader + ifr * sizeof (U8) * uiArea, SEEK_SET);
	for (UInt ip = 0; ip < uiArea; ip++)
		m_ppxli [ip] = getc (fpSrc);
	fclose (fpSrc);
}

CIntImage::CIntImage (const Char* vdlFileName) : m_ppxli (NULL)// read from a VM file.
{
	CVideoObjectPlane vop (vdlFileName);
	allocate (vop.where ());
	const CPixel* ppxl = vop.pixels ();
	for (UInt ip = 0; ip < where ().area (); ip++)
		m_ppxli [ip] = ppxl [ip].pxlU.rgb.r;
}

Void CIntImage::where (const CRct& r) 
{
	if (!valid ()) return; 
	if (where () == r) return; 
	CIntImage* pii = new CIntImage (*this, r);
	swap (*pii);
	delete pii;
}


CRct CIntImage::boundingBox (const PixelI pxliOutsideColor) const
{
	if (allValue ((PixelI) pxliOutsideColor))	
		return CRct ();
	CoordI left = where ().right - 1;
	CoordI top = where ().bottom - 1;
	CoordI right = where ().left;
	CoordI bottom = where ().top;
	const PixelI* ppxliThis = pixels ();
	for (CoordI y = where ().top; y < where ().bottom; y++) {
		for (CoordI x = where ().left; x < where ().right; x++) {
			if (*ppxliThis != (PixelI) pxliOutsideColor) {
				left = min (left, x);
				top = min (top, y);
				right = max (right, x);
				bottom = max (bottom, y);
			}								
			ppxliThis++;
		}
	}
	right++;
	bottom++;
	return (CRct (left, top, right, bottom));				
}


Int CIntImage::mean () const
{
	if (where ().empty ()) 
		return 0;
	Int meanRet = 0;
	PixelI* ppxli = (PixelI*) pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++)
		meanRet += ppxli [ip];
	return (Int) (meanRet / area);
}

Int CIntImage::mean (const CIntImage* piiMsk) const
{
	assert (where () == piiMsk -> where ()); // no compute if rects are different
	if (where ().empty ()) return 0;
	Int meanRet = 0;
	PixelI* ppxli = (PixelI*) pixels ();
	const PixelI* ppxliMsk = piiMsk -> pixels ();
	UInt area = where ().area ();
	UInt uiNumNonTransp = 0;
	for (UInt ip = 0; ip < area; ip++) {
		if (ppxliMsk [ip] != transpValue) {
			uiNumNonTransp++;
			meanRet += ppxli [ip];
		}
	}
	return (Int) (meanRet / uiNumNonTransp);
}

Int CIntImage::sumAbs (const CRct& rct) const 
{
	CRct rctToDo = (!rct.valid ()) ? where () : rct;
	Int intRet = 0;
	if (rctToDo == where ())	{	
		const PixelI* ppxli = pixels ();
		UInt area = where ().area ();
		for (UInt ip = 0; ip < area; ip++, ppxli++)
			intRet += (*ppxli);
	}
	else	{
		Int width = where ().width;
		const PixelI* ppxliRow = pixels (rct.left, rct.top);
		for (CoordI y = rctToDo.top; y < rctToDo.bottom; y++) {
			const PixelI* ppxli = ppxliRow;
			for (CoordI x = rctToDo.left; x < rctToDo.right; x++, ppxli++)
				intRet += abs (*ppxli);
			ppxliRow += width;
		}
	}
	return intRet;
}

Int CIntImage::sumDeviation (const CIntImage* piiMsk) const // sum of first-order deviation
{
	Int meanPxl = mean (piiMsk);
	Int devRet = 0;
	PixelI* ppxli = (PixelI*) pixels ();
	const PixelI* ppxliMsk = piiMsk -> pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++) {
		if (ppxliMsk [ip] != transpValue)
			devRet += abs (meanPxl - ppxli [ip]);
	}
	return devRet;
}


Int CIntImage::sumDeviation () const // sum of first-order deviation
{
	Int meanPxl = mean ();
	Int devRet = 0;
	PixelI* ppxli = (PixelI*) pixels ();
	UInt area = where ().area ();
	for (UInt ip = 0; ip < area; ip++)
		devRet += abs (meanPxl - ppxli [ip]);
	return devRet;
}


Bool CIntImage::allValue (Int intVl, const CRct& rct) const
{
	CRct rctToDo = (!rct.valid ()) ? where () : rct;
	if (rctToDo == where ()) {	
		const PixelI* ppxli = pixels ();
		UInt area = where ().area ();
		for (UInt ip = 0; ip < area; ip++) {
			if (ppxli [ip] != intVl)
				return FALSE;
		}
	}
	else {
		Int width = where ().width;
		const PixelI* ppxli = pixels (rct.left, rct.top);
		for (CoordI y = rctToDo.top; y < rctToDo.bottom; y++) {
			const PixelI* ppxliRow = ppxli;
			for (CoordI x = rctToDo.left; x < rctToDo.right; x++, ppxliRow++) {
				if (*ppxliRow != intVl)
					return FALSE;
			}
			ppxli += width;
		}
	}
	return TRUE;
}

Bool CIntImage::biLevel (const CRct& rct) const
{
	CRct rctToDo = (!rct.valid ()) ? where () : rct;
	if (rctToDo == where ())	{	
		const PixelI* ppxli = pixels ();
		UInt area = where ().area ();
		for (UInt ip = 0; ip < area; ip++) {
			if (ppxli [ip] != opaqueValue && ppxli [ip] != transpValue)
				return FALSE;
		}
	}
	else {
		Int width = where ().width;
		const PixelI* ppxli = pixels (rct.left, rct.top);
		for (CoordI y = rctToDo.top; y < rctToDo.bottom; y++) {
			const PixelI* ppxliRow = ppxli;
			for (CoordI x = rctToDo.left; x < rctToDo.right; x++, ppxliRow++) {
				if (*ppxliRow != opaqueValue && *ppxliRow != transpValue)
					return FALSE;
			}
			ppxli += width;
		}
	}
	return TRUE;
}

CRct CIntImage::whereVisible () const
{
	CoordI left = where ().right - 1;
	CoordI top = where ().bottom - 1;
	CoordI right = where ().left;
	CoordI bottom = where ().top;
	const PixelI* ppxliThis = pixels ();
	for (CoordI y = where ().top; y < where ().bottom; y++) {
		for (CoordI x = where ().left; x < where ().right; x++) {
			if (*ppxliThis != transpValue) {
				left = min (left, x);
				top = min (top, y);
				right = max (right, x);
				bottom = max (bottom, y);
			}								
			ppxliThis++;
		}
	}
	right++;
	bottom++;
	return CRct (left, top, right, bottom);
}


Bool CIntImage::atLeastOneValue (Int ucVl, const CRct& rct) const
{
	CRct rctRegionOfInterest = (!rct.valid ()) ? where () : rct;
	assert (rctRegionOfInterest <= where ());
	if (rctRegionOfInterest == where ()) {
		const PixelI* ppxli = pixels ();
		UInt area = where ().area ();
		for (UInt ip = 0; ip < area; ip++) {
			if (ppxli [ip] == ucVl)
				return TRUE;
		}
	}
	else {
		Int width = where ().width;
		const PixelI* ppxli = pixels (rctRegionOfInterest.left, rctRegionOfInterest.top);
		for (CoordI y = rctRegionOfInterest.top; y < rctRegionOfInterest.bottom; y++) {
			const PixelI* ppxliRow = ppxli;
			for (CoordI x = rctRegionOfInterest.left; x < rctRegionOfInterest.right; x++, ppxliRow++) {
				if (*ppxliRow == ucVl)
					return TRUE;
			}
			ppxli += width;
		}
	}
	return FALSE;
}


UInt CIntImage::numPixelsNotValued (Int ucVl, const CRct& rct) const // number of pixels not valued vl in region rct
{
	CRct rctInterest = (!rct.valid ()) ? where () : rct;
	assert (rctInterest <= where ());
	UInt nRet = 0;
	if (rctInterest == where ()) {
		const PixelI* ppxli = pixels ();
		UInt area = where ().area ();
		for (UInt ip = 0; ip < area; ip++) {
			if (ppxli [ip] != ucVl)
				nRet++;
		}
	}
	else {
		Int width = where ().width;
		const PixelI* ppxli = pixels (rctInterest.left, rctInterest.top);
		for (CoordI y = rctInterest.top; y < rctInterest.bottom; y++) {
			const PixelI* ppxliRow = ppxli;
			for (CoordI x = rctInterest.left; x < rctInterest.right; x++, ppxliRow++) {
				if (*ppxliRow != ucVl)
					nRet++;
			}
			ppxli += width;
		}
	}

	return nRet;
}


Void CIntImage::setRect (const CRct& rct)
{
	assert (rct.area () == m_rc.area ());
	m_rc = rct;
}

Void CIntImage::threshold (Int ucThresh)
{
	PixelI* ppxlThis = (PixelI*) pixels ();
	UInt area = where ().area ();
	for (UInt id = 0; id < area; id++) {
		if (ppxlThis [id] < ucThresh)
			ppxlThis [id] = 0;
	}
}

Void CIntImage::binarize (Int ucThresh)
{
	PixelI* ppxliThis = (PixelI*) pixels ();
	UInt area = where ().area ();
	for (UInt id = 0; id < area; id++, ppxliThis) {
		if (*ppxliThis < ucThresh)
			*ppxliThis = transpValue;
		else
			*ppxliThis = opaqueValue;
	}
}


Void CIntImage::checkRange (Int ucMin, Int ucMax)
{
	PixelI* ppxliThis = (PixelI*) pixels ();
	UInt area = where ().area ();
	for (UInt id = 0; id < area; id++, ppxliThis++)
		*ppxliThis = checkrange (*ppxliThis, ucMin, ucMax);
}

own CIntImage* CIntImage::decimate (UInt rateX, UInt rateY) const
{
	const CoordI left = where ().left / (CoordI) rateX;
	const CoordI top = where ().top / (CoordI)  rateY;
	const CoordI right = 
		(where ().right >= 0) ? (where ().right + (CoordI) rateX - 1) / (CoordI) rateX :
		(where ().right - (CoordI) rateX + 1) / (CoordI) rateX;
	const CoordI bottom = 
		(where ().bottom >= 0) ? (where ().bottom + (CoordI) rateX - 1) / (CoordI) rateY :
		(where ().bottom - (CoordI) rateX + 1) / (CoordI) rateY;

	CIntImage* piiRet = new CIntImage (CRct (left, top, right, bottom));
	PixelI* ppxliRet = (PixelI*) piiRet -> pixels ();
	const PixelI* ppxliOrgY = pixels ();
	Int skipY = rateY * where ().width;
	
	for (CoordI y = top; y < bottom; y++) {
		const PixelI* ppxliOrgX = ppxliOrgY;

⌨️ 快捷键说明

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