tahoma2d/toonz/sources/stdfx/rgbkeyfx.cpp

146 lines
4.3 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
// #include "trop.h"
2016-03-19 06:57:51 +13:00
#include "tfxparam.h"
#include <math.h>
#include "stdfx.h"
#include "tparamset.h"
2021-09-02 20:36:37 +12:00
#include "globalcontrollablefx.h"
#include "tpixelutils.h"
2016-03-19 06:57:51 +13:00
2021-09-02 20:36:37 +12:00
class RGBKeyFx final : public GlobalControllableFx {
2016-06-15 18:43:10 +12:00
FX_PLUGIN_DECLARATION(RGBKeyFx)
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TRasterFxPort m_input;
TPixelParamP m_color;
TDoubleParamP m_rrange;
TDoubleParamP m_grange;
TDoubleParamP m_brange;
TBoolParamP m_gender;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
RGBKeyFx()
: m_color(TPixel32::Black)
, m_rrange(0.0)
, m_grange(0.0)
, m_brange(0.0)
, m_gender(false) {
bindParam(this, "color", m_color);
bindParam(this, "r_range", m_rrange);
bindParam(this, "g_range", m_grange);
bindParam(this, "b_range", m_brange);
bindParam(this, "invert", m_gender);
m_rrange->setValueRange(0.0, 255.0);
m_grange->setValueRange(0.0, 255.0);
m_brange->setValueRange(0.0, 255.0);
addInputPort("Source", m_input);
enableComputeInFloat(true);
2016-06-15 18:43:10 +12:00
}
~RGBKeyFx(){};
2016-06-20 14:23:05 +12:00
bool doGetBBox(double frame, TRectD &bBox,
const TRenderSettings &info) override {
2016-06-15 18:43:10 +12:00
if (m_input.isConnected()) {
m_input->doGetBBox(frame, bBox, info);
return true;
}
return false;
};
2016-06-19 20:06:29 +12:00
void doCompute(TTile &tile, double frame, const TRenderSettings &) override;
2016-06-15 18:43:10 +12:00
2016-06-20 14:23:05 +12:00
bool canHandle(const TRenderSettings &info, double frame) override {
2022-03-21 07:04:12 +13:00
return false;
2016-06-20 14:23:05 +12:00
}
2016-03-19 06:57:51 +13:00
};
//-------------------------------------------------------------------
template <typename PIXEL>
void doRGBKey(TRasterPT<PIXEL> ras, PIXEL highColor, PIXEL lowColor,
bool gender) {
2016-06-15 18:43:10 +12:00
int j;
ras->lock();
for (j = 0; j < ras->getLy(); j++) {
PIXEL *pix = ras->pixels(j);
PIXEL *endPix = pix + ras->getLx();
while (pix < endPix) {
bool condition = pix->r >= lowColor.r && pix->r <= highColor.r &&
pix->g >= lowColor.g && pix->g <= highColor.g &&
pix->b >= lowColor.b && pix->b <= highColor.b;
2016-06-15 18:43:10 +12:00
if (condition != gender) *pix = PIXEL::Transparent;
pix++;
}
}
ras->unlock();
2016-03-19 06:57:51 +13:00
}
template <>
void doRGBKey(TRasterFP ras, TPixelF highColor, TPixelF lowColor, bool gender) {
int j;
ras->lock();
for (j = 0; j < ras->getLy(); j++) {
TPixelF *pix = ras->pixels(j);
TPixelF *endPix = pix + ras->getLx();
while (pix < endPix) {
// clamp 0.f to 1.f in case computing HDR
float clampedR = std::min(1.f, std::max(0.f, pix->r));
float clampedG = std::min(1.f, std::max(0.f, pix->g));
float clampedB = std::min(1.f, std::max(0.f, pix->b));
bool condition = clampedR >= lowColor.r && clampedR <= highColor.r &&
clampedG >= lowColor.g && clampedG <= highColor.g &&
clampedB >= lowColor.b && clampedB <= highColor.b;
if (condition != gender) *pix = TPixelF::Transparent;
pix++;
}
}
ras->unlock();
}
2016-03-19 06:57:51 +13:00
//------------------------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void RGBKeyFx::doCompute(TTile &tile, double frame, const TRenderSettings &ri) {
if (!m_input.isConnected()) return;
m_input->compute(tile, frame, ri);
double r_range = m_rrange->getValue(frame) / 255.0;
double g_range = m_grange->getValue(frame) / 255.0;
double b_range = m_brange->getValue(frame) / 255.0;
bool gender = m_gender->getValue();
2016-06-15 18:43:10 +12:00
const TPixelF color = premultiply(toPixelF(m_color->getValueD(frame)));
2016-06-15 18:43:10 +12:00
TPixelF lowColor(color.r - r_range, color.g - g_range, color.b - b_range);
TPixelF highColor(color.r + r_range, color.g + g_range, color.b + b_range);
// currently the tile should always be nonlinear
assert(!tile.getRaster()->isLinear());
if (tile.getRaster()->isLinear()) {
lowColor = toLinear(lowColor, ri.m_colorSpaceGamma);
highColor = toLinear(highColor, ri.m_colorSpaceGamma);
2016-06-15 18:43:10 +12:00
}
TRaster32P raster32 = tile.getRaster();
TRaster64P raster64 = tile.getRaster();
TRasterFP rasterF = tile.getRaster();
if (raster32)
doRGBKey<TPixel32>(raster32, toPixel32(highColor), toPixel32(lowColor),
gender);
else if (raster64)
doRGBKey<TPixel64>(raster64, toPixel64(highColor), toPixel64(lowColor),
gender);
else if (rasterF)
doRGBKey<TPixelF>(rasterF, highColor, lowColor, gender);
else
throw TException("RGBKeyFx: unsupported Pixel Type");
2016-03-19 06:57:51 +13:00
}
//------------------------------------------------------------------
FX_PLUGIN_IDENTIFIER(RGBKeyFx, "rgbKeyFx")