📄 vop.cpp
字号:
return pvopRet;
}
own CVideoObjectPlane* CVideoObjectPlane::warp (const CPerspective2D& persp) const // perspective warp
{
CSiteD stdLeftTopWarp = (persp * CSiteD (where ().left, where ().top)).s;
CSiteD stdRightTopWarp = (persp * CSiteD (where ().right, where ().top)).s;
CSiteD stdLeftBottomWarp = (persp * CSiteD (where ().left, where ().bottom)).s;
CSiteD stdRightBottomWarp = (persp * CSiteD (where ().right, where ().bottom)).s;
CRct rctWarp (stdLeftTopWarp, stdRightTopWarp, stdLeftBottomWarp, stdRightBottomWarp);
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (rctWarp);
CPixel* ppxlRet = (CPixel*) pvopRet -> 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)
)
*ppxlRet = pixel (src);
ppxlRet++;
}
}
return pvopRet;
}
own CVideoObjectPlane* CVideoObjectPlane::warp (const CPerspective2D& persp, const CRct& rctWarp) const // perspective warp
{
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (rctWarp);
CPixel* ppxlRet = (CPixel*) pvopRet -> 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 = (persp * 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)
)
*ppxlRet = pixel (src);
ppxlRet++;
}
}
return pvopRet;
}
own CVideoObjectPlane* CVideoObjectPlane::warp (const CPerspective2D& persp, const CRct& rctWarp, UInt accuracy) const // perspective warp
{
UInt accuracy1 = accuracy + 1;
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (rctWarp);
CPixel* ppxlRet = (CPixel*) pvopRet -> 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 ((Double) (src.x >> accuracy1));
CoordI fy = (CoordI) floor ((Double) (src.y >> accuracy1));
CoordI cx = (CoordI) ceil ((Double) (src.x >> accuracy1));
CoordI cy = (CoordI) ceil ((Double) (src.y >> accuracy1));
if (
where ().includes (fx, fy) &&
where ().includes (fx, cy) &&
where ().includes (cx, fy) &&
where ().includes (cx, cy)
)
*ppxlRet = pixel (src, accuracy);
ppxlRet++;
}
}
return pvopRet;
}
Void CVideoObjectPlane::multiplyAlpha ()
{
if (!valid ()) return;
CPixel* ppxlThis = (CPixel*) pixels ();
Float inv255 = (Float) 1. / (Float) 255.;
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
Float alpha = inv255 * ppxlThis -> pxlU.rgb.a;
ppxlThis -> pxlU.rgb.r = (U8) (alpha * ppxlThis -> pxlU.rgb.r + .5);
ppxlThis -> pxlU.rgb.g = (U8) (alpha * ppxlThis -> pxlU.rgb.g + .5);
ppxlThis -> pxlU.rgb.b = (U8) (alpha * ppxlThis -> pxlU.rgb.b + .5);
ppxlThis++;
}
}
Void CVideoObjectPlane::multiplyBiAlpha ()
{
if (!valid ()) return;
CPixel* ppxlThis = (CPixel*) pixels ();
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
if (ppxlThis -> pxlU.rgb.a == 0) {
ppxlThis -> pxlU.rgb.r = 0;
ppxlThis -> pxlU.rgb.g = 0;
ppxlThis -> pxlU.rgb.b = 0;
}
ppxlThis++;
}
}
Void CVideoObjectPlane::unmultiplyAlpha ()
{
if (!valid ()) return;
CPixel* ppxlThis = (CPixel*) pixels ();
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
if (ppxlThis->pxlU.rgb.a != 0) {
Float invAlpha = (Float) (255 / ppxlThis -> pxlU.rgb.a);
ppxlThis -> pxlU.rgb.r = (U8) checkrange (invAlpha * ppxlThis -> pxlU.rgb.r + 0.5f, 0.0F, 255.0F);
ppxlThis -> pxlU.rgb.g = (U8) checkrange (invAlpha * ppxlThis -> pxlU.rgb.g + 0.5f, 0.0F, 255.0F);
ppxlThis -> pxlU.rgb.b = (U8) checkrange (invAlpha * ppxlThis -> pxlU.rgb.b + 0.5f, 0.0F, 255.0F);
}
ppxlThis++;
}
}
Void CVideoObjectPlane::thresholdAlpha (U8 uThresh)
{
CPixel* ppxlThis = (CPixel*) pixels ();
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
ppxlThis -> pxlU.rgb.a = (ppxlThis -> pxlU.rgb.a > uThresh) ? opaqueValue : transpValue;
ppxlThis++;
}
}
Void CVideoObjectPlane::thresholdRGB (U8 uThresh)
{
CPixel* ppxlThis = (CPixel*) pixels ();
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
if (ppxlThis -> pxlU.rgb.r < uThresh && ppxlThis -> pxlU.rgb.g < uThresh && ppxlThis -> pxlU.rgb.b < uThresh) {
ppxlThis -> pxlU.rgb.r = 0;
ppxlThis -> pxlU.rgb.g = 0;
ppxlThis -> pxlU.rgb.b = 0;
}
ppxlThis++;
}
}
Void CVideoObjectPlane::cropOnAlpha ()
{
CoordI left = where ().right - 1;
CoordI top = where ().bottom - 1;
CoordI right = where ().left;
CoordI bottom = where ().top;
const CPixel* ppxlThis = pixels ();
for (CoordI y = where ().top; y < where ().bottom; y++) {
for (CoordI x = where ().left; x < where ().right; x++) {
if (ppxlThis -> pxlU.rgb.a != transpValue) {
left = min (left, x);
top = min (top, y);
right = max (right, x);
bottom = max (bottom, y);
}
ppxlThis++;
}
}
right++;
bottom++;
where (CRct (left, top, right, bottom));
}
own CVideoObjectPlane* CVideoObjectPlane::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;
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (CRct (left, top, right, bottom), opaquePixel);
CPixel* pret = (CPixel*) pvopRet -> pixels ();
const CPixel* pori = pixels ();
Int skipY = rateY * where ().width;
for (CoordI y = top; y != bottom; y++)
{
const CPixel* pvf = pori;
for (CoordI x = left; x != right; x++)
{
*pret++ = *pvf;
pvf += rateX;
}
pori += skipY;
}
return pvopRet;
}
Void CVideoObjectPlane::falseColor (CPixel pxl)
{
CPixel* ppxl = m_ppxl;
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
if (ppxl -> pxlU.rgb.a == transpValue) {
ppxl -> pxlU.rgb.r = pxl.pxlU.rgb.r;
ppxl -> pxlU.rgb.g = pxl.pxlU.rgb.g;
ppxl -> pxlU.rgb.b = pxl.pxlU.rgb.b;
}
ppxl++;
}
}
Void CVideoObjectPlane::falseColor (U8 r, U8 g, U8 b)
{
CPixel* ppxl = m_ppxl;
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
if (ppxl -> pxlU.rgb.a == transpValue) {
ppxl -> pxlU.rgb.r = r;
ppxl -> pxlU.rgb.g = g;
ppxl -> pxlU.rgb.b = b;
}
ppxl++;
}
}
own CVideoObjectPlane* CVideoObjectPlane::zoomup (UInt rateX, UInt rateY) const
{
const CoordI left = where ().left * rateX;
const CoordI top = where ().top * rateY;
const CoordI right = where ().right * rateX;
const CoordI bottom = where ().bottom * rateY;
CVideoObjectPlane* retVf = new CVideoObjectPlane (CRct (left, top, right, bottom), opaquePixel);
CPixel* pret = (CPixel*) retVf -> pixels ();
for (CoordI y = top; y != bottom; y++)
{
for (CoordI x = left; x != right; x++)
{
*pret++ = pixel ((CoordI) (x / rateX), (CoordI) (y / rateY));
}
}
return retVf;
}
own CVideoObjectPlane* CVideoObjectPlane::expand (UInt rateX, UInt rateY) const
{
const CoordI left = where ().left; // left-top coordinate remain the same
const CoordI top = where ().top;
const CoordI right = left + where ().width * rateX;
const CoordI bottom = top + where ().height () * rateY;
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (CRct (left, top, right, bottom), transpPixel);
CPixel* ppxlRet = (CPixel*) pvopRet -> pixels ();
const CPixel* ppxlThis = pixels ();
for (CoordI y = top; y != bottom; y++) {
for (CoordI x = left; x != right; x++) {
if (x % rateX == 0 && y % rateY == 0)
*ppxlRet++ = *ppxlThis++;
else
*ppxlRet++ = CPixel (0, 0, 0, opaqueValue);
}
}
return pvopRet;
}
inline CPixel averageP (const CPixel& p1, const CPixel& p2)
{
return CPixel (
(p1.pxlU.rgb.r + p2.pxlU.rgb.r + 1) >> 1,
(p1.pxlU.rgb.g + p2.pxlU.rgb.g + 1) >> 1,
(p1.pxlU.rgb.b + p2.pxlU.rgb.b + 1) >> 1,
p1.pxlU.rgb.a
);
}
own CVideoObjectPlane* CVideoObjectPlane::biInterpolate () const
{
const CoordI left = where ().left; // left-top coordinate remain the same
const CoordI top = where ().top;
const CoordI right = left + where ().width * 2;
const CoordI bottom = top + where ().height () * 2;
const CoordI width = right - left;
CoordI x, y;
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (CRct (left, top, right, bottom), opaquePixel);
CPixel* ppxlRet = (CPixel*) pvopRet -> pixels ();
const CPixel* ppxl = pixels ();
const CoordI right1 = right - 2;
for (y = top; y < bottom; y += 2) { // x-direction interpolation
for (x = left; x < right1; x += 2) {
*ppxlRet++ = *ppxl++;
*ppxlRet++ = averageP (*ppxl, *(ppxl - 1));
}
*ppxlRet++ = *ppxl;
*ppxlRet++ = *ppxl++; // the last pixel of every row do not need average
ppxlRet += width;
}
ppxlRet = (CPixel*) pvopRet -> pixels ();
ppxlRet += width; // start from the second row
const CoordI width2 = width << 1;
const CoordI bottom1 = bottom - 1;
for (x = left; x < right; x++) { // y-direction interpolation
CPixel* ppxlRett = ppxlRet++;
for (y = top + 1; y < bottom1; y += 2) {
*ppxlRett = averageP (*(ppxlRett - width), *(ppxlRett + width));
ppxlRett += width2;
}
*ppxlRett = *(ppxlRett - width); // the last pixel of every column do not need average
}
return pvopRet;
}
own CVideoObjectPlane* CVideoObjectPlane::biInterpolate (UInt accuracy) const
{
const CoordI left = where ().left * accuracy;
const CoordI top = where ().top * accuracy;
const CoordI right = where ().right * accuracy;
const CoordI bottom = where ().bottom * accuracy;
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (CRct (left, top, right, bottom));
CPixel* ppxlRet = (CPixel*) pvopRet -> pixels ();
for (CoordI y = top; y < bottom; y++) { // x-direction interpolation
for (CoordI x = left; x < right; x++) {
*ppxlRet = pixel (x, y, accuracy);
ppxlRet++;
}
}
return pvopRet;
}
own CVideoObjectPlane* CVideoObjectPlane::biInterpolate (const CRct& r) const
{
CVideoObjectPlane* pvopRet = new CVideoObjectPlane (r, opaquePixel);
for (CoordI x = r.left; x < r.right; x++)
for (CoordI y = r.top; y < r.bottom; y++) {
CoordD dx = (Double) ((where ().right - 1 - where ().left) / (Double) (r.right - 1 - r.left)) * (x - r.left) + where ().left;
CoordD dy = (Double) ((where ().bottom - 1 - where ().top) / (Double) (r.bottom - 1 - r.top)) * (y - r.top) + where ().top;
CPixel result = pixel (dx, dy);
pvopRet -> pixel (x, y, result);
}
return pvopRet;
}
own CFloatImage* CVideoObjectPlane::plane (RGBA pxlCom) const
{
if (!valid ()) return NULL;
CFloatImage* pfiRet = new CFloatImage (where ());
const CPixel* ppxlVop = pixels ();
PixelF* ppxlRet = (PixelF*) pfiRet -> pixels ();
UInt area = where ().area ();
for (UInt ip = 0; ip < area; ip++) {
*ppxlRet = (PixelF) ppxlVop -> pxlU.color [pxlCom];
ppxlRet++;
ppxlVop++;
}
return pfiRet;
}
Void CVideoObjectPlane::lightChange (Int rShift, Int gShift, Int bShift)
{
CPixel* ppix = (CPixel*) pixels ();
UInt area = where ().area ();
for (UInt i = 0; i < area; i++) {
ppix -> pxlU.rgb.r = (U8) checkrange (rShift + ppix -> pxlU.rgb.r, 0, 255);
ppix -> pxlU.rgb.g = (U8) checkrange (gShift + ppix -> pxlU.rgb.g, 0, 255);
ppix -> pxlU.rgb.b = (U8) checkrange (bShift + ppix -> pxlU.rgb.b, 0, 255);
ppix++;
}
}
Void CVideoObjectPlane::overlay (const CVideoObjectPlane& vop)
{
if (!valid () || !vop.valid () || vop.where ().empty ()) return;
CRct r = m_rc;
r.include (vop.m_rc); // overlay is defined on union of rects
where (r);
if (!valid ()) return;
assert (vop.m_ppxl != NULL);
CRct rctVop = vop.m_rc;
Float inv255 = 1.0f / 255.0f;
const CPixel* ppxlVop = vop.pixels ();
CPixel* ppxlThisY = (CPixel*) pixels (rctVop.left, rctVop.top);
Int widthCurr = where ().width;
for (CoordI y = rctVop.top; y < rctVop.bottom; y++) { // loop through VOP CRct
CPixel* ppxlThisX = ppxlThisY;
for (CoordI x = rctVop.left; x < rctVop.right; x++) {
Float beta = inv255 * (Float) ppxlVop -> pxlU.rgb.a;
Float alpha = inv255 * (Float) ppxlThisX -> pxlU.rgb.a;
for (UInt ip = 0; ip < 3; ip++) {
Int value = (Int) (
beta * (Float) ppxlVop -> pxlU.color [ip] +
alpha * (Float) ppxlThisX -> pxlU.color [ip] -
alpha * beta * (Float) ppxlThisX -> pxlU.color [ip] + .5f
);
ppxlThisX -> pxlU.color [ip] = (U8) checkrange (value, 0, 255);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -