📄 grayc.cpp
字号:
/* CU8Image* puciBuffer = new CU8Image (CRct (where().left, rctBuf2.top, where().right, rctBuf2.bottom));
CU8Image* puciBuffer2 = new CU8Image (CRct (rctBuf2.left,rctBuf2.top, rctBuf2.right,rctBuf2.bottom));*/
CU8Image* puciRet = new CU8Image (CRct (rctDst.left, rctDst.top, rctDst.right, rctDst.bottom));
//upsample vertically
PixelC* ppxlcDst;
const PixelC* ppxlcColumnHeadSrc = pixels ();
/*
Int iVerticalSamplingFactorM = m_volmd.iver_sampling_factor_m;
Int iVerticalSamplingFactorN = m_volmd.iver_sampling_factor_n;
Int iHorizontalSamplingFactorM = m_volmd.ihor_sampling_factor_m;
Int iHorizontalSamplingFactorN = m_volmd.ihor_sampling_factor_n;
*/
Int x,y,y1,y2,x1,x2;
Int phase = 0;
Int *pict_vert;
const PixelC* pict= ppxlcColumnHeadSrc +iWidthSrc*iExpandYRefFrame/iType+iExpandYRefFrame/iType;
ppxlcDst = (PixelC *)puciRet->pixels();
//wchen: should not malloc at every call; need to change later
//pict_vert = (Int *) malloc (sizeof(Int)*((where().width-iExpandYRefFrame/iType*2)*(where().height()-iExpandYRefFrame/iType*2)*2));
// bug fix from Takefumi Nagumo 6/21/99
pict_vert = (Int *) malloc (sizeof(Int) * (Int)(1 + (where().width-iExpandYRefFrame/iType*2)
* (where().height()-iExpandYRefFrame/iType*2) * ((Float)iVerticalSamplingFactorN / (Float)iVerticalSamplingFactorM) ) );
for(x=0;x< (where().width-iExpandYRefFrame/iType*2); x++)
for(y=0;y< (rctDst.height()-iExpandYRefFrame/iType*2); y++) {
y1 = (y*iVerticalSamplingFactorM )/iVerticalSamplingFactorN;
if(y1 < where().height() - iExpandYRefFrame/iType*2 - 1)
y2 = y1+1;
else
y2 = y1;
phase = (16*((y*iVerticalSamplingFactorM) %
iVerticalSamplingFactorN) + iVerticalSamplingFactorN/2 /*for rounding*/)
/iVerticalSamplingFactorN;
*(pict_vert+y*(where().width-iExpandYRefFrame/iType*2)+x) = (Int)(16-phase)*(*(pict+y1*where().width+x))
+ phase *(*(pict+y2*where().width+x));
}
for(y=0;y< rctDst.height()-iExpandYRefFrame*2/iType; y++)
for(x=0;x< rctDst.width -iExpandYRefFrame*2/iType; x++) {
x1 = (x*iHorizontalSamplingFactorM )/iHorizontalSamplingFactorN;
if(x1 < where().width - iExpandYRefFrame/iType*2 - 1)
x2 = x1+1;
else
x2 = x1;
// Warning:no rounding
phase = (16*((x*iHorizontalSamplingFactorM) %
iHorizontalSamplingFactorN) + iHorizontalSamplingFactorN/2 /*for rounding*/)
/ iHorizontalSamplingFactorN;
*(ppxlcDst+(x+iExpandYRefFrame/iType)+(y+iExpandYRefFrame/iType)*rctDst.width)
=(PixelC)(((16-phase)*(*(pict_vert+y*(where().width-iExpandYRefFrame/iType*2)+x1))
+ phase *(*(pict_vert+y*(where().width-iExpandYRefFrame/iType*2)+x2)) +128 )>>8 );
}
free(pict_vert);
/* delete puciBuffer;
delete puciBuffer2;*/
return puciRet;
}
own CU8Image* CU8Image::biInterpolate (UInt accuracy) const // bilinearly interpolate the vframe
{
const CoordI left = where ().left * accuracy;
const CoordI top = where ().top * accuracy;
const CoordI right = where ().right * accuracy;
const CoordI bottom = where ().bottom * accuracy;
CU8Image* puciRet = new CU8Image (CRct (left, top, right, bottom));
PixelC* ppxlcRet = (PixelC*) puciRet -> pixels ();
for (CoordI y = top; y < bottom; y++) { // x-direction interpolation
for (CoordI x = left; x < right; x++) {
*ppxlcRet = pixel (x, y, accuracy);
ppxlcRet++;
}
}
return puciRet;
}
own CU8Image* CU8Image::transpose () const
{
CRct rctDst = where ();
rctDst.transpose ();
CU8Image* puciDst = new CU8Image (rctDst);
const PixelC* ppxlSrc = pixels ();
PixelC* ppxlDstRow = (PixelC*) puciDst->pixels ();
PixelC* ppxlDst;
UInt height = where ().height ();
for (CoordI iy = where ().top; iy < where ().bottom; iy++) {
ppxlDst = ppxlDstRow;
for (CoordI ix = where ().left; ix < where ().right; ix++) {
*ppxlDst = *ppxlSrc++;
ppxlDst += height;
}
ppxlDstRow++;
}
return puciDst;
}
own CU8Image* CU8Image::warp (const CAffine2D& aff) const // affine warp
{
CSiteD stdLeftTopWarp = aff * CSiteD (where ().left, where ().top);
CSiteD stdRightTopWarp = aff * CSiteD (where ().right, where ().top);
CSiteD stdLeftBottomWarp = aff * CSiteD (where ().left, where ().bottom);
CSiteD stdRightBottomWarp = aff * CSiteD (where ().right, where ().bottom);
CRct rctWarp (stdLeftTopWarp, stdRightTopWarp, stdLeftBottomWarp, stdRightBottomWarp);
CU8Image* puciRet = new CU8Image (rctWarp);
PixelC* ppxlcRet = (PixelC*) puciRet -> pixels ();
CAffine2D affInv = aff.inverse ();
for (CoordI y = rctWarp.top; y < rctWarp.bottom; y++) {
for (CoordI x = rctWarp.left; x < rctWarp.right; x++) {
CSiteD src = affInv * CSiteD (x, y);
CoordI fx = (CoordI) floor (src.x); //.5 is for better truncation
CoordI fy = (CoordI) floor (src.y); //.5 is for better truncation
CoordI cx = (CoordI) ceil (src.x); //.5 is for better truncation
CoordI cy = (CoordI) ceil (src.y); //.5 is for better truncation
if (
where ().includes (fx, fy) &&
where ().includes (fx, cy) &&
where ().includes (cx, fy) &&
where ().includes (cx, cy)
)
*ppxlcRet = pixel (src);
ppxlcRet++;
}
}
return puciRet;
}
own CU8Image* CU8Image::warp (const CPerspective2D& persp) const // perspective warp
{
CSiteD src [4], dest [4];
src [0] = CSiteD (where ().left, where ().top);
src [1] = CSiteD (where ().right, where ().top);
src [2] = CSiteD (where ().left, where ().bottom);
src [3] = CSiteD (where ().right, where ().bottom);
for (UInt i = 0; i < 4; i++) {
dest [i] = (persp * src [i]).s;
}
CRct rctWarp (dest [0], dest [1], dest [2], dest [3]);
CU8Image* puciRet = new CU8Image (rctWarp);
PixelC* ppxlcRet = (PixelC*) puciRet -> pixels ();
CPerspective2D perspInv = CPerspective2D (dest, src);
for (CoordI y = rctWarp.top; y != rctWarp.bottom; y++) {
for (CoordI x = rctWarp.left; x != rctWarp.right; x++) {
CSiteD src = (perspInv * CSiteD (x, y)).s;
CoordI fx = (CoordI) floor (src.x); //.5 is for better truncation
CoordI fy = (CoordI) floor (src.y); //.5 is for better truncation
CoordI cx = (CoordI) ceil (src.x); //.5 is for better truncation
CoordI cy = (CoordI) ceil (src.y); //.5 is for better truncation
if (
where ().includes (fx, fy) &&
where ().includes (fx, cy) &&
where ().includes (cx, fy) &&
where ().includes (cx, cy)
)
*ppxlcRet = pixel (src);
ppxlcRet++;
}
}
return puciRet;
}
own CU8Image* CU8Image::warp (const CPerspective2D& persp, const CRct& rctWarp) const // perspective warp
{
CU8Image* puciRet = new CU8Image (rctWarp);
PixelC* ppxlcRet = (PixelC*) puciRet -> pixels ();
CPerspective2D perspInv = persp.inverse ();
for (CoordI y = rctWarp.top; y != rctWarp.bottom; y++) {
for (CoordI x = rctWarp.left; x != rctWarp.right; x++) {
CSiteD src = (perspInv * CSiteD (x, y)).s;
CoordI fx = (CoordI) floor (src.x); //.5 is for better truncation
CoordI fy = (CoordI) floor (src.y); //.5 is for better truncation
CoordI cx = (CoordI) ceil (src.x); //.5 is for better truncation
CoordI cy = (CoordI) ceil (src.y); //.5 is for better truncation
if (
where ().includes (fx, fy) &&
where ().includes (fx, cy) &&
where ().includes (cx, fy) &&
where ().includes (cx, cy)
)
*ppxlcRet = pixel (src);
ppxlcRet++;
}
}
return puciRet;
}
own CU8Image* CU8Image::warp (const CPerspective2D& persp, const CRct& rctWarp, const UInt accuracy) const // perspective warp
{
CU8Image* puciRet = new CU8Image (rctWarp);
PixelC* ppxlcRet = (PixelC*) puciRet -> pixels ();
for (CoordI y = rctWarp.top; y != rctWarp.bottom; y++) {
for (CoordI x = rctWarp.left; x != rctWarp.right; x++) {
CSite src = (persp * CSite (x, y)).s;
CoordI fx = (CoordI) floor ((CoordD) src.x / (CoordD) accuracy); //.5 is for better truncation
CoordI fy = (CoordI) floor ((CoordD) src.y / (CoordD) accuracy); //.5 is for better truncation
CoordI cx = (CoordI) ceil ((CoordD) src.x / (CoordD) accuracy); //.5 is for better truncation
CoordI cy = (CoordI) ceil ((CoordD) src.y / (CoordD) accuracy); //.5 is for better truncation
if (
where ().includes (fx, fy) &&
where ().includes (fx, cy) &&
where ().includes (cx, fy) &&
where ().includes (cx, cy)
)
*ppxlcRet = pixel (src, accuracy);
ppxlcRet++;
}
}
return puciRet;
}
PixelC CU8Image::pixel (CoordD x, CoordD y) const
{
CoordI left = (CoordI) floor (x); // find the coordinates of the four corners
CoordI wLeft = where ().left, wRight1 = where ().right - 1, wTop = where ().top, wBottom1 = where ().bottom - 1;
left = checkrange (left, wLeft, wRight1);
CoordI right = (CoordI) ceil (x);
right = checkrange (right, wLeft, wRight1);
CoordI top = (CoordI) floor (y);
top = checkrange (top, wTop, wBottom1);
CoordI bottom = (CoordI) ceil (y);
bottom = checkrange (bottom, wTop, wBottom1);
const PixelC lt = pixel (left, top);
const PixelC rt = pixel (right, top);
const PixelC lb = pixel (left, bottom);
const PixelC rb = pixel (right, bottom);
const Double distX = x - left;
const Double distY = y - top;
Double x01 = distX * (rt - lt) + lt; // use p.59's notation (Wolberg, Digital Image Warping)
Double x23 = distX * (rb - lb) + lb;
PixelC pxlRet = checkrangeU8 ((U8) (x01 + (x23 - x01) * distY + .5), 0, 255);
return pxlRet;
}
PixelC CU8Image::pixel (CoordI x, CoordI y, UInt accuracy) const
{
UInt uis = 1 << (accuracy + 1);
UInt accuracy1 = accuracy + 1;
CoordD dx = (CoordD) ((CoordD) x / uis);
CoordD dy = (CoordD) ((CoordD) y / uis);
CoordI left = (CoordI) floor (dx); // find the coordinates of the four corners
CoordI wLeft = where ().left, wRight1 = where ().right - 1, wTop = where ().top, wBottom1 = where ().bottom - 1;
left = checkrange (left, wLeft, wRight1);
CoordI right = (CoordI) ceil (dx);
right = checkrange (right, wLeft, wRight1);
CoordI top = (CoordI) floor (dy);
top = checkrange (top, wTop, wBottom1);
CoordI bottom = (CoordI) ceil (dy);
bottom = checkrange (bottom, wTop, wBottom1);
UInt accuracy2 = (accuracy << 1) + 2;
const PixelC lt = pixel (left, top);
const PixelC rt = pixel (right, top);
const PixelC lb = pixel (left, bottom);
const PixelC rb = pixel (right, bottom);
const CoordI distX = x - (left << accuracy1);
const CoordI distY = y - (top << accuracy1);
Int dRet = ((uis-distY)*((uis-distX)*lt + distX*rt) + distY*((uis-distX)*lb + distX*rb) + (1<<(accuracy2-1))) >> accuracy2;
PixelC pxlRet = (U8) checkrange ((PixelC)dRet, 0U, 255U);
return pxlRet;
}
own CU8Image* CU8Image::complement () const
{
CU8Image* puciDst = new CU8Image (where(), (PixelC) transpValue);
const PixelC* ppxlcSrc = pixels ();
PixelC* ppxlcDst = (PixelC*) puciDst->pixels ();
for (UInt iPxl = 0; iPxl < where ().area (); iPxl++)
*ppxlcDst++ = *ppxlcSrc++ ^ 0xFF;
return puciDst;
}
Void CU8Image::overlay (const CU8Image& uci)
{
if (!valid () || !uci.valid () || uci.where ().empty ()) return;
CRct r = m_rc;
r.include (uci.m_rc); // overlay is defined on union of rects
where (r);
if (!valid ()) return;
assert (uci.m_ppxlc != NULL);
CRct rctFi = uci.m_rc;
Int widthFi = rctFi.width;
Int widthCurr = where ().width;
PixelC* ppxlcThis = (PixelC*) pixels (rctFi.left, rctFi.top);
const PixelC* ppxlcFi = uci.pixels ();
for (CoordI y = rctFi.top; y < rctFi.bottom; y++) { // loop through VOP CRct
memcpy (ppxlcThis, ppxlcFi, rctFi.width * sizeof (PixelC));
ppxlcThis += widthCurr;
ppxlcFi += widthFi;
}
}
Void CU8Image::overlay (const CU8Image& uci, const CRct& rctSrc)
{
if (!valid () || !uci.valid () || uci.where ().empty () || !rctSrc.valid ()) return;
if (!(uci.m_rc >= rctSrc))
return;
CRct r = m_rc;
r.include (rctSrc); // overlay is defined on union of rects
where (r);
if (!valid ()) return;
assert (uci.m_ppxlc != NULL);
Int widthSrc = rctSrc.width;
Int widthDst = where ().width;
PixelC* ppxlcThis = (PixelC*) pixels (rctSrc.left, rctSrc.top);
const PixelC* ppxlcSrc = uci.pixels (rctSrc.left, rctSrc.top);
for (CoordI y = rctSrc.top; y < rctSrc.bottom; y++) { // loop through VOP CRct
memcpy (ppxlcThis, ppxlcSrc, widthSrc * sizeof (PixelC));
ppxlcThis += widthDst;
ppxlcSrc += widthSrc;
}
}
own CU8Image* CU8Image::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;
CU8Image* pfmgRet = new CU8Image (*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;
PixelC* rgValues = new PixelC [size];
PixelC* pRet = (PixelC*) pfmgRet -> pixels (left, top);
const PixelC* p = pixels (left, top);
for (CoordI y = top; y != bottom; y++) {
for (CoordI x = left; x != right; x++) {
const PixelC* 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++ = (PixelC) ((numTransp <= med) ? opaqueValue : transpValue);
p++;
}
pRet += offset2;
p += offset2;
}
delete [] rgValues;
return pfmgRet;
}
own CU8Image* CU8Image::smooth (UInt window) const
{
UInt offset = window >> 1;
CRct rctExp (where ());
rctExp.expand (offset);
CU8Image* puciExp = new CU8Image (*this, rctExp);
CU8Image* puciSmooth = puciExp -> smooth_ (window);
puciSmooth -> where (where ());
delete puciExp;
return puciSmooth;
}
Void CU8Image::CU8Image_xor (const CU8Image& uci)
{
CRct rctIntersect = m_rc;
rctIntersect.clip (uci.where());
if (!rctIntersect.valid () || rctIntersect.empty ()) return;
PixelC* ppxlcRowStart1 = (PixelC*) pixels (rctIntersect.left, rctIntersect.top);
const PixelC* ppxlcRowStart2 = uci.pixels (rctIntersect.left, rctIntersect.top);
for (CoordI iy = rctIntersect.top; iy < rctIntersect.bottom; iy++) {
PixelC* ppxlc1 = ppxlcRowStart1;
const PixelC* ppxlc2 = ppxlcRowStart2;
for (CoordI ix = rctIntersect.left; ix < rctIntersect.right; ix++) {
assert (*ppxlc1 == transpValue || *ppxlc1 == opaqueValue);
assert (*ppxlc2 == transpValue || *ppxlc2 == opaqueValue);
if (*ppxlc1 == *ppxlc2)
*ppxlc1 = (PixelC) transpValue;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -