Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "trandom.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class SaltPepperNoiseFx final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(SaltPepperNoiseFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_Intensity;
Shinya Kitaoka 120a6e
  TBoolParamP m_Animate;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  SaltPepperNoiseFx() : m_Intensity(30.0), m_Animate(0.0) {
Shinya Kitaoka 120a6e
    bindParam(this, "Intensity", m_Intensity);
Shinya Kitaoka 120a6e
    bindParam(this, "Animate", m_Animate);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
    m_Intensity->setValueRange(0.0, 100.0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~SaltPepperNoiseFx(){};
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
      bool ret = m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
      return ret;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      bBox = TRectD();
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &ri) override;
Shinya Kitaoka 38fd86
  std::string getAlias(double frame,
Shinya Kitaoka 38fd86
                       const TRenderSettings &info) const 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
Toshihiro Shimizu 890ddd
template <typename pixel=""></typename>
Shinya Kitaoka 120a6e
void doSaltPepperNoise(const TRasterPT<pixel> &ras, const double intensity,</pixel>
Shinya Kitaoka 120a6e
                       TRandom &rnd, bool animate, double frame) {
Shinya Kitaoka 120a6e
  if (animate) rnd.setSeed(frame);
Shinya Kitaoka 120a6e
  double data  = 0.5 * intensity;
Shinya Kitaoka 120a6e
  double data1 = data + 0.5;
Shinya Kitaoka 120a6e
  double data2 = 0.5 - data;
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
      if (pix->m) {
Shinya Kitaoka 120a6e
        data = rnd.getFloat();
Shinya Kitaoka 120a6e
        if (data >= 0.5 && data < data1) {
Shinya Kitaoka 120a6e
          pix->r = pix->g = pix->b = 0;
Shinya Kitaoka 120a6e
        } else if (data >= data2 && data < 0.5)
Shinya Kitaoka 120a6e
          pix->r = pix->g = pix->b = pix->m;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      *pix++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void SaltPepperNoiseFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                  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
  TRandom rnd;
Shinya Kitaoka 120a6e
  double intensity = m_Intensity->getValue(frame) / 100.0;
Shinya Kitaoka 120a6e
  ;
Shinya Kitaoka 120a6e
  bool animate        = m_Animate->getValue();
Shinya Kitaoka 120a6e
  TRaster32P raster32 = tile.getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doSaltPepperNoise<tpixel32>(raster32, intensity, rnd, animate, frame);</tpixel32>
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64 = tile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doSaltPepperNoise<tpixel64>(raster64, intensity, rnd, animate, frame);</tpixel64>
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      throw TException("SaltPepperNoiseFx: unsupported Pixel Type");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string SaltPepperNoiseFx::getAlias(double frame,
Shinya Kitaoka 120a6e
                                        const TRenderSettings &info) const {
Shinya Kitaoka 120a6e
  std::string alias = getFxType();
Shinya Kitaoka 120a6e
  alias += "[";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // alias degli effetti connessi alle porte di input separati da virgole
Shinya Kitaoka 120a6e
  // una porta non connessa da luogo a un alias vuoto (stringa vuota)
Shinya Kitaoka 120a6e
  int i = 0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < getInputPortCount(); i++) {
Shinya Kitaoka 120a6e
    TFxPort *port = getInputPort(i);
Shinya Kitaoka 120a6e
    if (port->isConnected()) {
Shinya Kitaoka 120a6e
      TRasterFxP ifx = port->getFx();
Shinya Kitaoka 120a6e
      assert(ifx);
Shinya Kitaoka 120a6e
      alias += ifx->getAlias(frame, info);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    alias += ",";
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bool addframe = 0;
Shinya Kitaoka 120a6e
  std::string paramalias("");
Shinya Kitaoka 120a6e
  for (i = 0; i < getParams()->getParamCount(); i++) {
Shinya Kitaoka 120a6e
    TParam *param = getParams()->getParam(i);
Shinya Kitaoka 120a6e
    paramalias += param->getName() + "=" + param->getValueAlias(frame, 3);
Shinya Kitaoka 120a6e
    if (param->getName() == "Animate" && param->getValueAlias(frame, 0) == "1")
Shinya Kitaoka 120a6e
      addframe = true;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (addframe) alias += std::to_string(frame) + ",";
Shinya Kitaoka 120a6e
  alias += paramalias + "]";
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return alias;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(SaltPepperNoiseFx, "saltpepperNoiseFx");