Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
shun-iwasawa 481b59
// #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"
shun-iwasawa 481b59
#include "tpixelutils.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);
shun-iwasawa 481b59
    enableComputeInFloat(true);
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 {
shun-iwasawa 668e74
    return false;
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 481b59
template <typename pixel=""></typename>
shun-iwasawa 481b59
void doRGBKey(TRasterPT<pixel> ras, PIXEL highColor, PIXEL lowColor,</pixel>
shun-iwasawa 481b59
              bool gender) {
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) {
shun-iwasawa 481b59
      bool condition = pix->r >= lowColor.r && pix->r <= highColor.r &&
shun-iwasawa 481b59
                       pix->g >= lowColor.g && pix->g <= highColor.g &&
shun-iwasawa 481b59
                       pix->b >= lowColor.b && pix->b <= highColor.b;
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
shun-iwasawa 481b59
template <>
shun-iwasawa 481b59
void doRGBKey(TRasterFP ras, TPixelF highColor, TPixelF lowColor, bool gender) {
shun-iwasawa 481b59
  int j;
shun-iwasawa 481b59
  ras->lock();
shun-iwasawa 481b59
  for (j = 0; j < ras->getLy(); j++) {
shun-iwasawa 481b59
    TPixelF *pix    = ras->pixels(j);
shun-iwasawa 481b59
    TPixelF *endPix = pix + ras->getLx();
shun-iwasawa 481b59
    while (pix < endPix) {
shun-iwasawa 481b59
      // clamp 0.f to 1.f in case computing HDR
shun-iwasawa 481b59
      float clampedR = std::min(1.f, std::max(0.f, pix->r));
shun-iwasawa 481b59
      float clampedG = std::min(1.f, std::max(0.f, pix->g));
shun-iwasawa 481b59
      float clampedB = std::min(1.f, std::max(0.f, pix->b));
shun-iwasawa 481b59
shun-iwasawa 481b59
      bool condition = clampedR >= lowColor.r && clampedR <= highColor.r &&
shun-iwasawa 481b59
                       clampedG >= lowColor.g && clampedG <= highColor.g &&
shun-iwasawa 481b59
                       clampedB >= lowColor.b && clampedB <= highColor.b;
shun-iwasawa 481b59
      if (condition != gender) *pix = TPixelF::Transparent;
shun-iwasawa 481b59
      pix++;
shun-iwasawa 481b59
    }
shun-iwasawa 481b59
  }
shun-iwasawa 481b59
  ras->unlock();
shun-iwasawa 481b59
}
shun-iwasawa 481b59
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
shun-iwasawa 481b59
  double r_range = m_rrange->getValue(frame) / 255.0;
shun-iwasawa 481b59
  double g_range = m_grange->getValue(frame) / 255.0;
shun-iwasawa 481b59
  double b_range = m_brange->getValue(frame) / 255.0;
shun-iwasawa 481b59
  bool gender    = m_gender->getValue();
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  const TPixelF color = premultiply(toPixelF(m_color->getValueD(frame)));
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  TPixelF lowColor(color.r - r_range, color.g - g_range, color.b - b_range);
shun-iwasawa 481b59
  TPixelF highColor(color.r + r_range, color.g + g_range, color.b + b_range);
shun-iwasawa 481b59
shun-iwasawa 481b59
  // currently the tile should always be nonlinear
shun-iwasawa 481b59
  assert(!tile.getRaster()->isLinear());
shun-iwasawa 481b59
  if (tile.getRaster()->isLinear()) {
shun-iwasawa 481b59
    lowColor  = toLinear(lowColor, ri.m_colorSpaceGamma);
shun-iwasawa 481b59
    highColor = toLinear(highColor, ri.m_colorSpaceGamma);
Shinya Kitaoka 120a6e
  }
shun-iwasawa 481b59
shun-iwasawa 481b59
  TRaster32P raster32 = tile.getRaster();
shun-iwasawa 481b59
  TRaster64P raster64 = tile.getRaster();
shun-iwasawa 481b59
  TRasterFP rasterF   = tile.getRaster();
shun-iwasawa 481b59
  if (raster32)
shun-iwasawa 481b59
    doRGBKey<tpixel32>(raster32, toPixel32(highColor), toPixel32(lowColor),</tpixel32>
shun-iwasawa 481b59
                       gender);
shun-iwasawa 481b59
  else if (raster64)
shun-iwasawa 481b59
    doRGBKey<tpixel64>(raster64, toPixel64(highColor), toPixel64(lowColor),</tpixel64>
shun-iwasawa 481b59
                       gender);
shun-iwasawa 481b59
  else if (rasterF)
shun-iwasawa 481b59
    doRGBKey<tpixelf>(rasterF, highColor, lowColor, gender);</tpixelf>
shun-iwasawa 481b59
  else
shun-iwasawa 481b59
    throw TException("RGBKeyFx: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(RGBKeyFx, "rgbKeyFx")