Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "trandom.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class NoiseFx final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(NoiseFx)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_value;
Shinya Kitaoka 120a6e
  TBoolParamP m_Red;
Shinya Kitaoka 120a6e
  TBoolParamP m_Green;
Shinya Kitaoka 120a6e
  TBoolParamP m_Blue;
Shinya Kitaoka 120a6e
  TBoolParamP m_BW;
Shinya Kitaoka 120a6e
  TBoolParamP m_Animate;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  NoiseFx()
Shinya Kitaoka 120a6e
      : m_value(100.0)
Shinya Kitaoka 120a6e
      , m_Red(1.0)
Shinya Kitaoka 120a6e
      , m_Green(1.0)
Shinya Kitaoka 120a6e
      , m_Blue(1.0)
Shinya Kitaoka 120a6e
      , m_BW(0.0)
Shinya Kitaoka 120a6e
      , m_Animate(0.0) {
Shinya Kitaoka 120a6e
    bindParam(this, "Intensity", m_value);
Shinya Kitaoka 120a6e
    bindParam(this, "Red", m_Red);
Shinya Kitaoka 120a6e
    bindParam(this, "Green", m_Green);
Shinya Kitaoka 120a6e
    bindParam(this, "Blue", m_Blue);
Shinya Kitaoka 120a6e
    bindParam(this, "Black_White", m_BW);
Shinya Kitaoka 120a6e
    bindParam(this, "Animate", m_Animate);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
    m_value->setValueRange(0, (std::numeric_limits<double>::max)());</double>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~NoiseFx(){};
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
      return m_input->doGetBBox(frame, bBox, info);
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
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
template <class pixel=""></class>
Shinya Kitaoka 120a6e
class RayleighNoise {
Shinya Kitaoka 120a6e
  std::vector<double> noise_buf;</double>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  RayleighNoise(double sigma, int seed = 0) {
Shinya Kitaoka 120a6e
    noise_buf.resize(PIXEL::maxChannelValue + 1);
Shinya Kitaoka 120a6e
    TRandom random(seed);
Shinya Kitaoka 120a6e
    sigma = log(sigma * 0.07 + 1);
Shinya Kitaoka 120a6e
    if (PIXEL::maxChannelValue == 255)
Shinya Kitaoka 120a6e
      sigma = 2.0 * sigma * sigma * sigma * sigma;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      sigma = 257.0 * 2.0 * sigma * sigma * sigma * sigma;
Shinya Kitaoka 120a6e
    for (int i = 0; i < PIXEL::maxChannelValue + 1; i++) {
Shinya Kitaoka 120a6e
      noise_buf[i] = sigma * sqrt(-log(1.0 - random.getFloat())) *
Shinya Kitaoka 120a6e
                     cos(M_PI * (2.0 * random.getFloat() - 1.0));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  double get_value(int index) {
Shinya Kitaoka 120a6e
    tcrop(index, 0, PIXEL::maxChannelValue);
Shinya Kitaoka 120a6e
    return noise_buf[index];
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" pixelgray,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doNoise(TRasterPT<pixel> &ras, double sigma, bool bw, bool red, bool green,</pixel>
Shinya Kitaoka 120a6e
             bool blue, bool animate, double frame) {
Shinya Kitaoka 120a6e
  RayleighNoise<pixel> noise(sigma);</pixel>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRandom random;
Shinya Kitaoka 120a6e
  if (animate) random.setSeed(frame);
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 (bw) {
Shinya Kitaoka 120a6e
        double index;
Shinya Kitaoka 120a6e
        double value;
Shinya Kitaoka 120a6e
        index = random.getFloat() * pix->m;  // PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
        value = noise.get_value(tfloor(index));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        // value =
Shinya Kitaoka 120a6e
        // (value<pix->m)?((value>0)?(int)value:0):PIXEL::maxChannelValue;</pix->
Shinya Kitaoka 120a6e
        // if(value)
Shinya Kitaoka 120a6e
        //{
Shinya Kitaoka 120a6e
        int reference;
Shinya Kitaoka 120a6e
        reference = PIXELGRAY::from(*pix).value;
Shinya Kitaoka 120a6e
        pix->r    = tcrop<int>(value + reference, 0, pix->m);</int>
Shinya Kitaoka 120a6e
        pix->g    = tcrop<int>(value + reference, 0, pix->m);</int>
Shinya Kitaoka 120a6e
        pix->b    = tcrop<int>(value + reference, 0, pix->m);</int>
Shinya Kitaoka 120a6e
        //}
Shinya Kitaoka 120a6e
      } else {
Shinya Kitaoka 120a6e
        double r, g, b, index;
Shinya Kitaoka 120a6e
        if (red) {
Shinya Kitaoka 120a6e
          index  = random.getFloat() * pix->m;  // PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
          r      = noise.get_value(tfloor(index)) + pix->r;
Shinya Kitaoka 120a6e
          pix->r = tcrop<int>(r, 0, pix->m);</int>
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        if (green) {
Shinya Kitaoka 120a6e
          index  = random.getFloat() * pix->m;  // PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
          g      = noise.get_value(tfloor(index)) + pix->g;
Shinya Kitaoka 120a6e
          pix->g = tcrop<int>(g, 0, pix->m);</int>
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        if (blue) {
Shinya Kitaoka 120a6e
          index  = random.getFloat() * pix->m;  // PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
          b      = noise.get_value(tfloor(index)) + pix->b;
Shinya Kitaoka 120a6e
          pix->b = tcrop<int>(b, 0, pix->m);</int>
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      *pix++;
Shinya Kitaoka 120a6e
    }
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 NoiseFx::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
  double sigma = m_value->getValue(frame);
Shinya Kitaoka 120a6e
  bool bw      = m_BW->getValue();
Shinya Kitaoka 120a6e
  bool red     = m_Red->getValue();
Shinya Kitaoka 120a6e
  bool green   = m_Green->getValue();
Shinya Kitaoka 120a6e
  bool blue    = m_Blue->getValue();
Shinya Kitaoka 120a6e
  bool animate = m_Animate->getValue();
Shinya Kitaoka 120a6e
  if (sigma == 0.0) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P raster32 = tile.getRaster();
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doNoise<tpixel32, tpixelgr8,="" uchar="">(raster32, sigma, bw, red, green, blue,</tpixel32,>
Shinya Kitaoka 120a6e
                                        animate, frame);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64 = tile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doNoise<tpixel64, tpixelgr16,="" ushort="">(raster64, sigma, bw, red, green,</tpixel64,>
Shinya Kitaoka 120a6e
                                            blue, animate, frame);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      throw TException("Brightness&Contrast: unsupported Pixel Type");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
std::string NoiseFx::getAlias(double frame, 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
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(NoiseFx, "noiseFx")