📄 grayc.cpp
字号:
/*************************************************************************
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:
grayc.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_
#ifdef __NBIT_
void pxlcmemset(PixelC *ppxlc, PixelC pxlcVal, Int iCount)
{
Int i;
for(i=0; i<iCount;i++)
ppxlc[i] = pxlcVal;
}
#endif
CU8Image::~CU8Image ()
{
delete [] m_ppxlc;
m_ppxlc = NULL;
}
Void CU8Image::allocate (const CRct& r, PixelC pxlc)
{
m_rc = r;
delete [] m_ppxlc, m_ppxlc = NULL;
// allocate pixels and initialize
if (m_rc.empty ()) return;
m_ppxlc = new PixelC [m_rc.area ()];
memset (m_ppxlc, pxlc, where ().area () * sizeof (PixelC));
}
Void CU8Image::allocate (const CRct& r)
{
m_rc = r;
// allocate pixels and initialize
m_ppxlc = new PixelC [m_rc.area ()];
assert (m_ppxlc != NULL);
}
Void CU8Image::copyConstruct (const CU8Image& uci, const CRct& rct)
{
CRct r = rct;
if (!r.valid())
r = uci.where ();
if (!uci.valid () || (!uci.m_rc.empty () && uci.m_ppxlc == NULL))
assert (FALSE);
allocate (r, (PixelC) 0);
if (!valid ()) return;
// Copy data
if (r == uci.where ())
memcpy (m_ppxlc, uci.pixels (), m_rc.area () * sizeof (PixelC));
else {
r.clip (uci.where ()); // find the intersection
CoordI x = r.left; // copy pixels
Int cbLine = r.width * sizeof (PixelC);
PixelC* ppxl = (PixelC*) pixels (x, r.top);
const PixelC* ppxlFi = uci.pixels (x, r.top);
Int widthCurr = where ().width;
Int widthFi = uci.where ().width;
for (CoordI y = r.top; y < r.bottom; y++) {
memcpy (ppxl, ppxlFi, cbLine);
ppxl += widthCurr;
ppxlFi += widthFi;
}
}
}
Void CU8Image::swap (CU8Image& uci)
{
assert (this && &uci);
CRct rcT = uci.m_rc;
uci.m_rc = m_rc;
m_rc = rcT;
PixelC* ppxlcT = uci.m_ppxlc;
uci.m_ppxlc = m_ppxlc;
m_ppxlc = ppxlcT;
}
CU8Image::CU8Image (const CU8Image& uci, const CRct& r) : m_ppxlc (NULL)
{
copyConstruct (uci, r);
}
CU8Image::CU8Image (const CRct& r, PixelC px) : m_ppxlc (NULL)
{
allocate (r, px);
}
CU8Image::CU8Image (const CRct& r) : m_ppxlc (NULL)
{
allocate (r);
}
CU8Image::CU8Image (const CVideoObjectPlane& vop, RGBA comp, const CRct& r) : m_ppxlc (NULL)
{
if (!vop.valid ()) return;
CU8Image* puci = new CU8Image (vop.where ());
PixelC* ppxlc = (PixelC*) puci -> pixels ();
const CPixel* ppxl = vop.pixels ();
UInt area = puci -> where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxl++)
*ppxlc++ = (PixelC) ppxl -> pxlU.color [comp];
copyConstruct (*puci, r);
delete puci;
}
CU8Image::CU8Image (
const Char* pchFileName,
UInt ifr,
const CRct& rct,
UInt nszHeader
) : m_ppxlc (NULL), m_rc (rct)
{
assert (!rct.empty ());
assert (ifr >= 0);
assert (nszHeader >= 0);
UInt uiArea = rct.area ();
delete [] m_ppxlc;
m_ppxlc = new PixelC [uiArea];
assert (m_ppxlc);
// read data from a file
FILE* fpSrc = fopen (pchFileName, "rb");
assert (fpSrc != NULL);
fseek (fpSrc, nszHeader + ifr * sizeof (U8) * uiArea, SEEK_SET);
Int size = (Int) fread (m_ppxlc, sizeof (U8), uiArea, fpSrc);
assert (size != 0);
fclose (fpSrc);
}
CU8Image::CU8Image (const Char* vdlFileName) : m_ppxlc (NULL)// read from a VM file.
{
CVideoObjectPlane vop (vdlFileName);
m_rc = vop.where ();
UInt uiArea = where ().area ();
delete [] m_ppxlc;
m_ppxlc = new PixelC [uiArea];
assert (m_ppxlc);
const CPixel* ppxlVop = vop.pixels ();
PixelC* ppxlcRet = (PixelC*) pixels ();
for (UInt ip = 0; ip < uiArea; ip++, ppxlcRet++, ppxlVop++)
*ppxlcRet = (PixelC) ppxlVop -> pxlU.rgb.r;
}
Void CU8Image::where (const CRct& r)
{
if (!valid ()) return;
if (where () == r) return;
CU8Image* puci = new CU8Image (*this, r);
swap (*puci);
delete puci;
}
CRct CU8Image::boundingBox (const PixelC pxlcOutsideColor) const
{
if (allValue ((PixelC) pxlcOutsideColor))
return CRct ();
CoordI left = where ().right - 1;
CoordI top = where ().bottom - 1;
CoordI right = where ().left;
CoordI bottom = where ().top;
const PixelC* ppxlcThis = pixels ();
for (CoordI y = where ().top; y < where ().bottom; y++) {
for (CoordI x = where ().left; x < where ().right; x++) {
if (*ppxlcThis != (PixelC) pxlcOutsideColor) {
left = min (left, x);
top = min (top, y);
right = max (right, x);
bottom = max (bottom, y);
}
ppxlcThis++;
}
}
right++;
bottom++;
return (CRct (left, top, right, bottom));
}
U8 CU8Image::mean () const
{
if (where ().empty ()) return 0;
Int meanRet = 0;
PixelC* ppxlc = (PixelC*) pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++)
meanRet += ppxlc [ip];
return (U8) (meanRet / area);
}
U8 CU8Image::mean (const CU8Image* puciMsk) const
{
assert (where () == puciMsk -> where ()); // no compute if rects are different
if (where ().empty ()) return 0;
Int meanRet = 0;
PixelC* ppxlc = (PixelC*) pixels ();
const PixelC* ppxlcMsk = puciMsk -> pixels ();
UInt area = where ().area ();
UInt uiNumNonTransp = 0;
for (UInt ip = 0; ip < area; ip++) {
if (ppxlcMsk [ip] != transpValue) {
uiNumNonTransp++;
meanRet += ppxlc [ip];
}
}
return (U8) (meanRet / uiNumNonTransp);
}
Int CU8Image::sumDeviation (const CU8Image* puciMsk) const // sum of first-order deviation
{
U8 meanPxl = mean (puciMsk);
Int devRet = 0;
PixelC* ppxlc = (PixelC*) pixels ();
const PixelC* ppxlcMsk = puciMsk -> pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++) {
if (ppxlcMsk [ip] != transpValue)
devRet += abs (meanPxl - ppxlc [ip]);
}
return devRet;
}
Int CU8Image::sumDeviation () const // sum of first-order deviation
{
U8 meanPxl = mean ();
Int devRet = 0;
PixelC* ppxlc = (PixelC*) pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++)
devRet += abs (meanPxl - ppxlc [ip]);
return devRet;
}
UInt CU8Image::sumAbs (const CRct& rct) const // sum of absolute value
{
CRct rctToDo = (!rct.valid ()) ? where () : rct;
UInt uiRet = 0;
if (rctToDo == where ()) {
const PixelC* ppxlc = pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxlc++) {
if (*ppxlc > 0)
uiRet += *ppxlc;
else
uiRet -= *ppxlc;
}
}
else {
Int width = where ().width;
const PixelC* ppxlcRow = pixels (rct.left, rct.top);
for (CoordI y = rctToDo.top; y < rctToDo.bottom; y++) {
const PixelC* ppxlc = ppxlcRow;
for (CoordI x = rctToDo.left; x < rctToDo.right; x++, ppxlc++) {
if (*ppxlc > 0)
uiRet += *ppxlc;
else
uiRet -= *ppxlc;
}
ppxlcRow += width;
}
}
return uiRet;
}
Bool CU8Image::allValue (PixelC ucVl, const CRct& rct) const
{
CRct rctToDo = (!rct.valid ()) ? where () : rct;
if (rctToDo == where ()) {
const PixelC* ppxlc = pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++) {
if (ppxlc [ip] != ucVl)
return FALSE;
}
}
else {
Int width = where ().width;
const PixelC* ppxlc = pixels (rct.left, rct.top);
for (CoordI y = rctToDo.top; y < rctToDo.bottom; y++) {
const PixelC* ppxlcRow = ppxlc;
for (CoordI x = rctToDo.left; x < rctToDo.right; x++, ppxlcRow++) {
if (*ppxlcRow != ucVl)
return FALSE;
}
ppxlc += width;
}
}
return TRUE;
}
Bool CU8Image::biLevel (const CRct& rct) const
{
CRct rctToDo = (!rct.valid ()) ? where () : rct;
if (rctToDo == where ()) {
const PixelC* ppxlc = pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++) {
if (ppxlc [ip] != opaqueValue && ppxlc [ip] != transpValue )
return FALSE;
}
}
else {
Int width = where ().width;
const PixelC* ppxlc = pixels (rct.left, rct.top);
for (CoordI y = rctToDo.top; y < rctToDo.bottom; y++) {
const PixelC* ppxlcRow = ppxlc;
for (CoordI x = rctToDo.left; x < rctToDo.right; x++, ppxlcRow++) {
if (*ppxlcRow != opaqueValue && *ppxlcRow != transpValue )
return FALSE;
}
ppxlc += width;
}
}
return TRUE;
}
CRct CU8Image::whereVisible () const
{
CoordI left = where ().right - 1;
CoordI top = where ().bottom - 1;
CoordI right = where ().left;
CoordI bottom = where ().top;
const PixelC* ppxlcThis = pixels ();
for (CoordI y = where ().top; y < where ().bottom; y++) {
for (CoordI x = where ().left; x < where ().right; x++) {
if (*ppxlcThis != transpValue) {
left = min (left, x);
top = min (top, y);
right = max (right, x);
bottom = max (bottom, y);
}
ppxlcThis++;
}
}
right++;
bottom++;
return CRct (left, top, right, bottom);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -