tahoma2d/toonz/sources/stdfx/erodilatefx.cpp

139 lines
4.3 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
// TnzCore includes
#include "trop.h"
// TnzBase includes
#include "tfxparam.h"
#include "tparamset.h"
// TnzStdfx includes
#include "stdfx.h"
//****************************************************************************
// ErodeDilate Fx
//****************************************************************************
2016-06-15 18:43:10 +12:00
class ErodeDilateFx : public TStandardRasterFx {
FX_PLUGIN_DECLARATION(ErodeDilateFx)
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRasterFxPort m_input;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TIntEnumParamP m_type;
TDoubleParamP m_radius;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
ErodeDilateFx() : m_type(new TIntEnumParam(0, "Square")), m_radius(0.0) {
addInputPort("Source", m_input);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bindParam(this, "type", m_type);
m_type->addItem(1, "Circular");
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
m_radius->setMeasureName("fxLength");
bindParam(this, "radius", m_radius);
}
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
bool doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info) override;
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
void doDryCompute(TRectD &rect, double frame, const TRenderSettings &ri) override;
void doCompute(TTile &tile, double frame, const TRenderSettings &ri) override;
2016-06-15 18:43:10 +12:00
int getMemoryRequirement(const TRectD &rect, double frame,
2016-06-19 20:06:29 +12:00
const TRenderSettings &info) override;
2016-03-19 06:57:51 +13:00
2016-06-19 20:06:29 +12:00
bool canHandle(const TRenderSettings &info, double frame) override {
2016-06-15 18:43:10 +12:00
return isAlmostIsotropic(info.m_affine);
}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
bool ErodeDilateFx::doGetBBox(double frame, TRectD &bBox,
const TRenderSettings &info) {
// Remember: info.m_affine MUST NOT BE CONSIDERED in doGetBBox's
// implementation
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Retrieve the input bbox without applied affines.
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (!m_input.getFx()) return false;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bool ret = m_input->doGetBBox(frame, bBox, info);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
bBox = bBox.enlarge(tceil(m_radius->getValue(frame)));
return ret;
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ErodeDilateFx::doCompute(TTile &tile, double frame,
const TRenderSettings &info) {
if (!m_input.isConnected()) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double radius = m_radius->getValue(frame) * sqrt(info.m_affine.det());
bool dilate = (radius >= 0.0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRop::ErodilateMaskType type = TRop::ErodilateMaskType(m_type->getValue());
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (dilate) {
// Quite easy, this time - just compute the input tile by forwarding the
// request
m_input->compute(tile, frame, info);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
// Then, apply TRop::erodilate directly with the scale-adjusted radius
const TRasterP &ras = tile.getRaster();
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRop::erodilate(ras, ras, radius, type);
} else {
// In the erosion case, we need to know the input enlarged by the radius
// value
int radI = tceil(fabs(radius));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TDimension &tileSize = tile.getRaster()->getSize();
const TRectD &enlargedRect =
TRectD(tile.m_pos, TDimensionD(tileSize.lx, tileSize.ly)).enlarge(radI);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TDimension enlargedSize(tround(enlargedRect.getLx()),
tround(enlargedRect.getLy()));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TTile inTile;
m_input->allocateAndCompute(inTile, enlargedRect.getP00(), enlargedSize,
tile.getRaster(), frame, info);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
const TRasterP &inRas = inTile.getRaster();
TRop::erodilate(inRas, inRas, radius, type);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
tile.getRaster()->copy(inRas, -TPoint(radI, radI));
}
2016-03-19 06:57:51 +13:00
}
//-------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ErodeDilateFx::doDryCompute(TRectD &rect, double frame,
const TRenderSettings &info) {
if (!m_input.isConnected()) return;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
double radius = m_radius->getValue(frame) * sqrt(info.m_affine.det());
bool dilate = (radius >= 0.0);
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
if (dilate)
m_input->dryCompute(rect, frame, info);
else {
int radI = tceil(fabs(radius));
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRectD enlargedRect(rect.enlarge(radI));
m_input->dryCompute(enlargedRect, frame, info);
}
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
int ErodeDilateFx::getMemoryRequirement(const TRectD &rect, double frame,
const TRenderSettings &info) {
return (m_type->getValue() == 0) ? TRasterFx::memorySize(rect, 8)
: // One additional greymaps
2 * TRasterFx::memorySize(rect, 8); // Two additional greymaps
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------
FX_PLUGIN_IDENTIFIER(ErodeDilateFx, "erodeDilateFx");