📄 grayi.cpp
字号:
{
CIntImage* piiDst = new CIntImage (where(), (PixelI) transpValue);
const PixelI* ppxliSrc = pixels ();
PixelI* ppxliDst = (PixelI*) piiDst->pixels ();
for (UInt iPxl = 0; iPxl < where ().area (); iPxl++)
*ppxliDst++ = *ppxliSrc++ ^ 0xFF;
return piiDst;
}
Void CIntImage::overlay (const CIntImage& ii)
{
if (!valid () || !ii.valid () || ii.where ().empty ()) return;
CRct r = m_rc;
r.include (ii.m_rc); // overlay is defined on union of rects
where (r);
if (!valid ()) return;
assert (ii.m_ppxli != NULL);
CRct rctFi = ii.m_rc;
Int widthFi = rctFi.width;
Int widthCurr = where ().width;
PixelI* ppxliThis = (PixelI*) pixels (rctFi.left, rctFi.top);
const PixelI* ppxliFi = ii.pixels ();
for (CoordI y = rctFi.top; y < rctFi.bottom; y++) { // loop through VOP CRct
memcpy (ppxliThis, ppxliFi, rctFi.width * sizeof (PixelI));
ppxliThis += widthCurr;
ppxliFi += widthFi;
}
}
Void CIntImage::overlay (const CFloatImage& fi)
{
if (!valid () || !fi.valid () || fi.where ().empty ()) return;
CRct r = m_rc;
r.include (fi.where()); // overlay is defined on union of rects
where (r);
if (!valid ()) return;
assert (fi.pixels() != NULL);
CRct rctFi = fi.where();
PixelF fVal;
Int widthFi = rctFi.width;
Int widthCurr = where ().width;
PixelI* ppxliThis = (PixelI*) pixels (rctFi.left, rctFi.top);
const PixelF* ppxliFi = fi.pixels ();
CoordI x;
for (CoordI y = rctFi.top; y < rctFi.bottom; y++) { // loop through VOP CRct
for(x=0;x<rctFi.width;x++)
{
fVal=ppxliFi[x];
ppxliThis[x]=(fVal>=0)?(Int)(fVal+0.5):(Int)(fVal-0.5);
}
ppxliThis += widthCurr;
ppxliFi += widthFi;
}
}
own CIntImage* CIntImage::smooth_ (UInt window) const
{
const UInt offset = window >> 1;
const UInt offset2 = offset << 1;
const UInt size = window * window; // array size to be sorted
const UInt med = size >> 1;
CIntImage* pfmgRet = new CIntImage (*this);
// bound of the image to be filtered.
const CoordI left = where ().left + offset;
const CoordI top = where ().top + offset;
const CoordI right = where ().right - offset;
const CoordI bottom = where ().bottom - offset;
const Int width = where ().width;
const Int dist = offset + offset * width;
const Int wwidth = width - window;
PixelI* rgValues = new PixelI [size];
PixelI* pRet = (PixelI*) pfmgRet -> pixels (left, top);
const PixelI* p = pixels (left, top);
for (CoordI y = top; y != bottom; y++) {
for (CoordI x = left; x != right; x++) {
const PixelI* pp = p - dist; // get correct index
UInt numTransp = 0;
for (UInt sy = 0; sy < window; sy++) {
for (UInt sx = 0; sx < window; sx++) {
if (*pp == transpValue)
numTransp++;
pp++;
}
pp += wwidth;
}
*pRet++ = (PixelI) ((numTransp <= med) ? opaqueValue : transpValue);
p++;
}
pRet += offset2;
p += offset2;
}
delete [] rgValues;
return pfmgRet;
}
own CIntImage* CIntImage::smooth (UInt window) const
{
UInt offset = window >> 1;
CRct rctExp (where ());
rctExp.expand (offset);
CIntImage* piiExp = new CIntImage (*this, rctExp);
CIntImage* piiSmooth = piiExp -> smooth_ (window);
piiSmooth -> where (where ());
delete piiExp;
return piiSmooth;
}
Void CIntImage::xorIi (const CIntImage& ii)
{
CRct rctIntersect = m_rc;
rctIntersect.clip (ii.where());
if (!rctIntersect.valid () || rctIntersect.empty ()) return;
PixelI* ppxliRowStart1 = (PixelI*) pixels (rctIntersect.left, rctIntersect.top);
const PixelI* ppxliRowStart2 = ii.pixels (rctIntersect.left, rctIntersect.top);
for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++) {
PixelI* ppxli1 = ppxliRowStart1;
const PixelI* ppxli2 = ppxliRowStart2;
for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++) {
assert (*ppxli1 == transpValue || *ppxli1 == opaqueValue);
assert (*ppxli2 == transpValue || *ppxli2 == opaqueValue);
if (*ppxli1 == *ppxli2)
*ppxli1 = (PixelI) transpValue;
else
*ppxli1 = (PixelI) opaqueValue;
ppxli1++;
ppxli2++;
}
ppxliRowStart1 += where().width;
ppxliRowStart2 += ii.where().width;
}
}
Void CIntImage::orIi (const CIntImage& ii)
{
CRct rctIntersect = m_rc;
rctIntersect.clip (ii.where());
if (!rctIntersect.valid () || rctIntersect.empty ()) return;
PixelI* ppxliRowStart1 = (PixelI*) pixels (rctIntersect.left, rctIntersect.top);
const PixelI* ppxliRowStart2 = ii.pixels (rctIntersect.left, rctIntersect.top);
for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++) {
PixelI* ppxli1 = ppxliRowStart1;
const PixelI* ppxli2 = ppxliRowStart2;
for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++) {
assert (*ppxli1 == transpValue || *ppxli1 == opaqueValue);
assert (*ppxli2 == transpValue || *ppxli2 == opaqueValue);
if (*ppxli2 == opaqueValue)
*ppxli1 = opaqueValue;
ppxli1++;
ppxli2++;
}
ppxliRowStart1 += where ().width;
ppxliRowStart2 += ii.where ().width;
}
}
Void CIntImage::andIi (const CIntImage& ii)
{
CRct rctIntersect = m_rc;
rctIntersect.clip (ii.where());
if (!rctIntersect.valid () || rctIntersect.empty ()) return;
PixelI* ppxliRowStart1 = (PixelI*) pixels (rctIntersect.left, rctIntersect.top);
const PixelI* ppxliRowStart2 = ii.pixels (rctIntersect.left, rctIntersect.top);
for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++) {
PixelI* ppxli1 = ppxliRowStart1;
const PixelI* ppxli2 = ppxliRowStart2;
for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++) {
assert (*ppxli1 == transpValue || *ppxli1 == opaqueValue);
assert (*ppxli2 == transpValue || *ppxli2 == opaqueValue);
if (*ppxli2 == transpValue)
*ppxli1 = transpValue;
ppxli1++;
ppxli2++;
}
ppxliRowStart1 += where().width;
ppxliRowStart2 += ii.where().width;
}
}
Void CIntImage::maskOut (const CIntImage& ii)
{
CRct rctIntersect = m_rc;
rctIntersect.clip (ii.where());
if (!rctIntersect.valid () || rctIntersect.empty ()) return;
PixelI* ppxliRowStart = (PixelI*) pixels (rctIntersect.left, rctIntersect.top);
const PixelI* ppxliRowStartMask = ii.pixels (rctIntersect.left, rctIntersect.top);
for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++) {
PixelI* ppxli = ppxliRowStart;
const PixelI* ppxliMask = ppxliRowStartMask;
for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++) {
assert (*ppxliMask == transpValue || *ppxliMask == opaqueValue);
assert (*ppxli == transpValue || *ppxli == opaqueValue);
if (*ppxliMask != transpValue)
*ppxli = transpValue;
ppxli++;
ppxliMask++;
}
ppxliRowStart += where ().width;
ppxliRowStartMask += ii.where ().width;
}
}
Void CIntImage::mutiplyAlpha (const CIntImage& ii)
{
CRct rctIntersect = m_rc;
rctIntersect.clip (ii.where());
if (!rctIntersect.valid () || rctIntersect.empty ()) return;
PixelI* ppxliRowStart = (PixelI*) pixels (rctIntersect.left, rctIntersect.top);
const PixelI* ppxliRowStartMask = ii.pixels (rctIntersect.left, rctIntersect.top);
for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++) {
PixelI* ppxli = ppxliRowStart;
const PixelI* ppxliMask = ppxliRowStartMask;
for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++) {
assert (*ppxliMask == transpValue || *ppxliMask == opaqueValue);
if (*ppxliMask == transpValue)
*ppxli = transpValue;
else
*ppxli = (PixelI) ((*ppxli * *ppxliMask + 127) / opaqueValueF); //normalize
ppxli++;
ppxliMask++;
}
ppxliRowStart += where ().width;
ppxliRowStartMask += ii.where ().width;
}
}
Void CIntImage::cropOnAlpha ()
{
CRct rctVisible = whereVisible ();
where (rctVisible);
}
Void CIntImage::operator = (const CIntImage& ii)
{
delete [] m_ppxli;
copyConstruct (ii, ii.where ());
}
Bool CIntImage::operator == (const CIntImage& ii) const
{
if (ii.where () != where ())
return FALSE;
UInt area = where ().area ();
const PixelI* ppxli = ii.pixels ();
const PixelI* ppxliThis = pixels ();
for (UInt ip = 0; ip < area; ip++, ppxli++, ppxliThis++)
if (*ppxli != *ppxliThis)
return FALSE;
return TRUE;
}
Double CIntImage::mse (const CIntImage& iiCompare) const
{
assert (iiCompare.where () == where ());
Int sqr = 0;
const PixelI* ppxliThis = pixels ();
const PixelI* ppxliCompare = iiCompare.pixels ();
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
Int diffR = *ppxliThis - *ppxliCompare;
sqr += diffR * diffR;
ppxliThis++;
ppxliCompare++;
}
return ((Double) sqr / area);
}
Double CIntImage::mse (const CIntImage& iiCompare, const CIntImage& iiMsk) const
{
assert (iiCompare.where () == where () && iiMsk.where () == where ());
Int sqr = 0;
const PixelI* ppxliThis = pixels ();
const PixelI* ppxliCompare = iiCompare.pixels ();
const PixelI* ppxliMsk = iiMsk.pixels ();
UInt area = where ().area ();
UInt uiNonTransp = 0;
for (UInt i = 0; i < area; i++) {
if (*ppxliMsk != transpValue) {
Int diffR = *ppxliThis - *ppxliCompare;
sqr += diffR * diffR;
uiNonTransp++;
}
ppxliThis++;
ppxliCompare++;
ppxliMsk++;
}
if (uiNonTransp == 0)
return 0;
return ((Double) sqr / area);
}
Double CIntImage::snr (const CIntImage& iiCompare) const
{
Double msError = (Double) mse (iiCompare);
if (msError == 0.0)
return 1000000.0;
else
return (log10 (255 * 255 / msError) * 10.0);
}
Double CIntImage::snr (const CIntImage& iiCompare, const CIntImage& iiMsk) const
{
CIntImage* piiMskOp = NULL;
Double msError = 0;
if (&iiMsk == NULL) {
piiMskOp = new CIntImage (where (), (PixelI) opaqueValue);
msError = mse (iiCompare, *piiMskOp);
delete piiMskOp;
}
else
msError = mse (iiCompare, iiMsk);
if (msError == 0.0)
return 1000000.0;
else
return (log10 (255 * 255 / msError) * 10.0);
}
Void CIntImage::vdlDump (const Char* fileName) const
{
CVideoObjectPlane vop (where (), opaquePixel);
CPixel* ppxl = (CPixel*) vop.pixels ();
const PixelI* ppxli = pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxl++, ppxli++)
*ppxl = CPixel (*ppxli, *ppxli, *ppxli, opaqueValue);
vop.vdlDump (fileName);
}
Void CIntImage::dump (FILE* pf) const
{
// was a binary write, but now reflects code in CFloatImage::dump()
//fwrite (m_ppxli, sizeof (Int), where ().area (), pf);
assert (pf != NULL);
UInt area = where ().area ();
U8* rguchPixelData = new U8 [where().area()];
U8* puch = rguchPixelData;
const PixelI* ppxli = pixels ();
for (UInt ip = 0; ip < area; ip++, puch++, ppxli++) {
U8 vl = (U8) checkrange ((Int)*ppxli, (Int)0, (Int)255);
*puch = vl;
}
fwrite (rguchPixelData, sizeof (U8), area, pf);
delete [] rguchPixelData;
}
Void CIntImage::txtDump (const Char* fileName) const
{
FILE* pfTxt;
const PixelI* ppxli = 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, "%3d ", *ppxli);
else
printf ("%3d ", *ppxli);
ppxli++;
}
if (pfTxt != NULL)
fprintf (pfTxt, "\n");
else
printf ("\n");
}
if (pfTxt != NULL)
fclose (pfTxt);
}
Void CIntImage::txtDump (FILE *pf) const
{
const PixelI *ppxli = pixels();
for (CoordI y = 0; y < where ().height (); y++) {
for (CoordI x = 0; x < where ().width; x++) {
fprintf (pf, "%3d ", *ppxli);
ppxli++;
}
fprintf (pf,"\n");
}
fprintf (pf,"\n");
}
Void CIntImage::txtDumpMask (FILE *pf) const
{
const PixelI *ppxli = pixels ();
for (CoordI y = 0; y < where ().height (); y++) {
for (CoordI x = 0; x < where ().width; x++) {
if (*ppxli == transpValue)
fprintf (pf, "..");
else
fprintf (pf, "[]");
ppxli++;
}
fprintf (pf,"\n");
}
fprintf (pf,"\n");
}
own CIntImage* CIntImage::operator + (const CIntImage& ii) const
{
if (!valid () || !ii.valid ()) return NULL;
assert (where () == ii.where ());
CIntImage* piiSumRet = new CIntImage (where ());
PixelI* ppxliRet = (PixelI*) piiSumRet -> pixels ();
const PixelI* ppxliThis = pixels ();
const PixelI* ppxliFi = ii.pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxliRet++, ppxliThis++, ppxliFi++)
*ppxliRet = *ppxliThis + *ppxliFi;
return piiSumRet;
}
own CIntImage* CIntImage::operator - (const CIntImage& ii) const
{
if (!valid () || !ii.valid ()) return NULL;
assert (where () == ii.where ());
CIntImage* piiSumRet = new CIntImage (where ());
PixelI* ppxliRet = (PixelI*) piiSumRet -> pixels ();
const PixelI* ppxliThis = pixels ();
const PixelI* ppxliFi = ii.pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxliRet++, ppxliThis++, ppxliFi++)
*ppxliRet = *ppxliThis - *ppxliFi;
return piiSumRet;
}
own CIntImage* CIntImage::operator * (Int scale) const
{
if (!valid ()) return NULL;
CIntImage* piiSumRet = new CIntImage (where ());
PixelI* ppxliRet = (PixelI*) piiSumRet -> pixels ();
const PixelI* ppxliThis = pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxliRet++, ppxliThis++)
*ppxliRet = *ppxliThis * scale;
return piiSumRet;
}
own CIntImage* CIntImage::operator / (Int scale) const
{
if (!valid ()) return NULL;
assert (scale != .0f);
CIntImage* piiSumRet = new CIntImage (where ());
PixelI* ppxliRet = (PixelI*) piiSumRet -> pixels ();
const PixelI* ppxliThis = pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxliRet++, ppxliThis++)
*ppxliRet = *ppxliThis / scale;
return piiSumRet;
}
own CIntImage* CIntImage::average (const CIntImage& ii) const
{
if (!valid () || !ii.valid ()) return NULL;
assert (where () == ii.where ());
CIntImage* piiSumRet = new CIntImage (where ());
PixelI* ppxliRet = (PixelI*) piiSumRet -> pixels ();
const PixelI* ppxliThis = pixels ();
const PixelI* ppxliFi = ii.pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++, ppxliRet++, ppxliThis++, ppxliFi++)
*ppxliRet = (*ppxliThis + *ppxliFi + 1) / 2;
return piiSumRet;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -