Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//#include "trop.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tparamset.h"
shun-iwasawa 8cac36
#include "globalcontrollablefx.h"
Toshihiro Shimizu 890ddd
shun-iwasawa 8cac36
class RGBKeyFx final : public GlobalControllableFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(RGBKeyFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TPixelParamP m_color;
Shinya Kitaoka 120a6e
  TDoubleParamP m_rrange;
Shinya Kitaoka 120a6e
  TDoubleParamP m_grange;
Shinya Kitaoka 120a6e
  TDoubleParamP m_brange;
Shinya Kitaoka 120a6e
  TBoolParamP m_gender;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RGBKeyFx()
Shinya Kitaoka 120a6e
      : m_color(TPixel32::Black)
Shinya Kitaoka 120a6e
      , m_rrange(0.0)
Shinya Kitaoka 120a6e
      , m_grange(0.0)
Shinya Kitaoka 120a6e
      , m_brange(0.0)
Shinya Kitaoka 120a6e
      , m_gender(false) {
Shinya Kitaoka 120a6e
    bindParam(this, "color", m_color);
Shinya Kitaoka 120a6e
    bindParam(this, "r_range", m_rrange);
Shinya Kitaoka 120a6e
    bindParam(this, "g_range", m_grange);
Shinya Kitaoka 120a6e
    bindParam(this, "b_range", m_brange);
Shinya Kitaoka 120a6e
    bindParam(this, "invert", m_gender);
Shinya Kitaoka 120a6e
    m_rrange->setValueRange(0.0, 255.0);
Shinya Kitaoka 120a6e
    m_grange->setValueRange(0.0, 255.0);
Shinya Kitaoka 120a6e
    m_brange->setValueRange(0.0, 255.0);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~RGBKeyFx(){};
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 38fd86
                 const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    if (m_input.isConnected()) {
Shinya Kitaoka 120a6e
      m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
      return true;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 38fd86
    return true;
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
void update_param(int ¶m, TRaster32P ras) { return; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void update_param(int ¶m, TRaster64P ras) {
Shinya Kitaoka 120a6e
  param = param * 257;
Shinya Kitaoka 120a6e
  return;
Toshihiro Shimizu 890ddd
}
shun-iwasawa 8cac36
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doRGBKey(TRasterPT<pixel> ras, int highR, int highG, int highB, int lowR,</pixel>
Shinya Kitaoka 120a6e
              int lowG, int lowB, bool gender) {
Shinya Kitaoka 120a6e
  update_param(highR, ras);
Shinya Kitaoka 120a6e
  update_param(highG, ras);
Shinya Kitaoka 120a6e
  update_param(highB, ras);
Shinya Kitaoka 120a6e
  update_param(lowR, ras);
Shinya Kitaoka 120a6e
  update_param(lowG, ras);
Shinya Kitaoka 120a6e
  update_param(lowB, ras);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int j;
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  for (j = 0; j < ras->getLy(); j++) {
Shinya Kitaoka 120a6e
    PIXEL *pix    = ras->pixels(j);
Shinya Kitaoka 120a6e
    PIXEL *endPix = pix + ras->getLx();
Shinya Kitaoka 120a6e
    while (pix < endPix) {
Shinya Kitaoka 120a6e
      bool condition = pix->r >= lowR && pix->r <= highR && pix->g >= lowG &&
Shinya Kitaoka 120a6e
                       pix->g <= highG && pix->b >= lowB && pix->b <= highB;
Shinya Kitaoka 120a6e
      if (condition != gender) *pix = PIXEL::Transparent;
Shinya Kitaoka 120a6e
      pix++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void RGBKeyFx::doCompute(TTile &tile, double frame, const TRenderSettings &ri) {
Shinya Kitaoka 120a6e
  if (!m_input.isConnected()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_input->compute(tile, frame, ri);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int r_range          = (int)m_rrange->getValue(frame);
Shinya Kitaoka 120a6e
  int g_range          = (int)m_grange->getValue(frame);
Shinya Kitaoka 120a6e
  int b_range          = (int)m_brange->getValue(frame);
Shinya Kitaoka 120a6e
  bool gender          = (int)m_gender->getValue();
Shinya Kitaoka 120a6e
  const TPixel32 Color = m_color->getPremultipliedValue(frame);
Shinya Kitaoka 120a6e
  TRaster32P raster32  = tile.getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int lowR  = std::max(0, Color.r - r_range);
Shinya Kitaoka 120a6e
  int highR = std::min(255, Color.r + r_range);
Shinya Kitaoka 120a6e
  int lowG  = std::max(0, Color.g - g_range);
Shinya Kitaoka 120a6e
  int highG = std::min(255, Color.g + g_range);
Shinya Kitaoka 120a6e
  int lowB  = std::max(0, Color.b - b_range);
Shinya Kitaoka 120a6e
  int highB = std::min(255, Color.b + b_range);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doRGBKey<tpixel32, uchar="">(raster32, highR, highG, highB, lowR, lowG, lowB,</tpixel32,>
Shinya Kitaoka 120a6e
                              gender);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64 = tile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doRGBKey<tpixel64, ushort="">(raster64, highR, highG, highB, lowR, lowG,</tpixel64,>
Shinya Kitaoka 120a6e
                                 lowB, gender);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      throw TException("RGBKeyFx: unsupported Pixel Type");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(RGBKeyFx, "rgbKeyFx")