Toshihiro Shimizu 890ddd
#include <sstream> /* std::ostringstream */</sstream>
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxattributes.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ino_common.h"
Toshihiro Shimizu 890ddd
#include "igs_gaussian_blur.h"
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka d1f6c4
class ino_blur final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(ino_blur)
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TRasterFxPort m_refer;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TDoubleParamP m_radius;
Shinya Kitaoka 120a6e
  TIntEnumParamP m_ref_mode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ino_blur() : m_radius(1.0), m_ref_mode(new TIntEnumParam(0, "Red")) {
Shinya Kitaoka 120a6e
    addInputPort("Source", this->m_input);
Shinya Kitaoka 120a6e
    addInputPort("Reference", this->m_refer);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    bindParam(this, "radius", this->m_radius);
Shinya Kitaoka 120a6e
    bindParam(this, "reference", this->m_ref_mode);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    this->m_radius->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
    this->m_radius->setValueRange(0.0, 1000.0);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    this->m_ref_mode->addItem(1, "Green");
Shinya Kitaoka 120a6e
    this->m_ref_mode->addItem(2, "Blue");
Shinya Kitaoka 120a6e
    this->m_ref_mode->addItem(3, "Alpha");
Shinya Kitaoka 120a6e
    this->m_ref_mode->addItem(4, "Luminance");
Shinya Kitaoka 120a6e
    this->m_ref_mode->addItem(-1, "Nothing");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  //------------------------------------------------------------
Shinya Kitaoka 120a6e
  double get_render_real_radius(const double frame, const TAffine affine) {
Shinya Kitaoka 120a6e
    /*--- ベクトルにする --- */
Shinya Kitaoka 120a6e
    TPointD rend_vect;
Shinya Kitaoka 120a6e
    rend_vect.x = this->m_radius->getValue(frame);
Shinya Kitaoka 120a6e
    rend_vect.y = 0.0;
Shinya Kitaoka 120a6e
    /*--- render用単位にする ---*/
Shinya Kitaoka 120a6e
    rend_vect = rend_vect * ino::pixel_per_mm();
Shinya Kitaoka 120a6e
    /* 単位変換(mm --> render_pixel) */
Shinya Kitaoka 120a6e
    /*--- 回転と拡大縮小のGeometryを反映させる ---*/
Shinya Kitaoka 120a6e
    /* 以下は、
Toshihiro Shimizu 890ddd
toonz/main/sources/stdfx/motionblurfx.cpp
Toshihiro Shimizu 890ddd
586-592行を参照して書いた
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
    TAffine aff(affine);
Shinya Kitaoka 120a6e
    aff.a13 = aff.a23 = 0; /* 移動変換をしないで... */
Shinya Kitaoka 120a6e
    rend_vect         = aff * rend_vect;
Shinya Kitaoka 120a6e
    /*--- 方向は無視して長さを返す ---*/
Shinya Kitaoka 120a6e
    return sqrt(rend_vect.x * rend_vect.x + rend_vect.y * rend_vect.y);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void get_render_enlarge(const double frame, const TAffine affine,
Shinya Kitaoka 120a6e
                          TRectD &bBox) {
Shinya Kitaoka 120a6e
    const int margin = igs::gaussian_blur_hv::int_radius(
Shinya Kitaoka 120a6e
        this->get_render_real_radius(frame, affine));
Shinya Kitaoka 120a6e
    if (0 < margin) {
Shinya Kitaoka 120a6e
      bBox = bBox.enlarge(static_cast<double>(margin));</double>
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  //------------------------------------------------------------
Shinya Kitaoka 38fd86
  bool doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 38fd86
                 const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    if (false == this->m_input.isConnected()) {
Shinya Kitaoka 120a6e
      bBox = TRectD();
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    const bool ret = this->m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
    this->get_render_enlarge(frame, info.m_affine, bBox);
Shinya Kitaoka 120a6e
    return ret;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  int getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 473e70
                           const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    TRectD bBox(rect);
Shinya Kitaoka 120a6e
    this->get_render_enlarge(frame, info.m_affine, bBox);
Shinya Kitaoka 120a6e
    return TRasterFx::memorySize(bBox, info.m_bpp);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  void transform(double frame, int port, const TRectD &rectOnOutput,
Shinya Kitaoka 120a6e
                 const TRenderSettings &infoOnOutput, TRectD &rectOnInput,
Shinya Kitaoka 473e70
                 TRenderSettings &infoOnInput) override {
Shinya Kitaoka 120a6e
    rectOnInput = rectOnOutput;
Shinya Kitaoka 120a6e
    infoOnInput = infoOnOutput;
Shinya Kitaoka 120a6e
    this->get_render_enlarge(frame, infoOnOutput.m_affine, rectOnInput);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 473e70
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 120a6e
    if (0 == this->m_radius->getValue(frame)) {
Shinya Kitaoka 120a6e
      return true;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      return isAlmostIsotropic(info.m_affine);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 38fd86
  void doCompute(TTile &tile, double frame,
Shinya Kitaoka 38fd86
                 const TRenderSettings &rend_sets) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(ino_blur, "inoBlurFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
void fx_(const TRasterP in_ras  // with margin
Shinya Kitaoka 120a6e
         ,
Shinya Kitaoka 120a6e
         TRasterP out_ras  // no margin
Shinya Kitaoka 120a6e
         ,
masafumi-inoue 26f824
         const TRasterP refer_ras, const int refer_mode, const int int_radius,
masafumi-inoue 26f824
         const double real_radius) {
Shinya Kitaoka 120a6e
  TRasterGR8P out_buffer(out_ras->getLy(),
Shinya Kitaoka 120a6e
                         out_ras->getLx() * ino::channels() *
Shinya Kitaoka 120a6e
                             ((TRaster64P)in_ras ? sizeof(unsigned short)
Shinya Kitaoka 120a6e
                                                 : sizeof(unsigned char)));
Shinya Kitaoka 120a6e
  const int buffer_bytes = igs::gaussian_blur_hv::buffer_bytes(
Shinya Kitaoka 120a6e
      in_ras->getLy(), in_ras->getLx(), int_radius);
Shinya Kitaoka 120a6e
  TRasterGR8P cvt_buffer(buffer_bytes, 1);
Shinya Kitaoka 120a6e
  out_buffer->lock();
Shinya Kitaoka 120a6e
  cvt_buffer->lock();
Shinya Kitaoka 120a6e
  igs::gaussian_blur_hv::convert(
Shinya Kitaoka 120a6e
      in_ras->getRawData()  // const void *in_with_margin (BGRA)
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      out_buffer->getRawData()  // void *out_no_margin (BGRA)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      in_ras->getLy()  // const int height_with_margin
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      in_ras->getLx()  // const int width_with_margin
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      ino::channels()  // const int channels
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      ino::bits(in_ras)  // const int bits
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      ,
masafumi-inoue 26f824
      (((refer_ras != nullptr) && (0 <= refer_mode))
masafumi-inoue 26f824
           ? refer_ras->getRawData()
masafumi-inoue 26f824
           : nullptr)  // BGRA // const unsigned char *ref
Shinya Kitaoka 120a6e
      ,
masafumi-inoue 26f824
      (((refer_ras != nullptr) && (0 <= refer_mode)) ? ino::bits(refer_ras)
masafumi-inoue 26f824
                                                     : 0)  // const int ref_bits
Shinya Kitaoka 120a6e
      ,
masafumi-inoue 26f824
      refer_mode  // const int refer_mode
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      cvt_buffer->getRawData()  // void *buffer
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      buffer_bytes  // int buffer_bytes
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      int_radius  // const int int_radius
Shinya Kitaoka 120a6e
      ,
masafumi-inoue 26f824
      real_radius  // const double real_radius // , 0.25
Shinya Kitaoka 120a6e
      );
Shinya Kitaoka 120a6e
  ino::arr_to_ras(out_buffer->getRawData(), ino::channels(), out_ras, 0);
Shinya Kitaoka 120a6e
  cvt_buffer->unlock();
Shinya Kitaoka 120a6e
  out_buffer->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
void ino_blur::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                         const TRenderSettings &rend_sets) {
Shinya Kitaoka 120a6e
  /*------ 接続していなければ処理しない ----------------------*/
Shinya Kitaoka 120a6e
  if (!this->m_input.isConnected()) {
Shinya Kitaoka 120a6e
    tile.getRaster()->clear(); /* 塗りつぶしクリア */
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /*------ サポートしていないPixelタイプはエラーを投げる -----*/
Shinya Kitaoka 120a6e
  if (!((TRaster32P)tile.getRaster()) && !((TRaster64P)tile.getRaster())) {
Shinya Kitaoka 120a6e
    throw TRopException("unsupported input pixel type");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /*------ ボケ足長さの実際の長さをえる ----------------------*/
Shinya Kitaoka 120a6e
  const double real_radius =
Shinya Kitaoka 120a6e
      this->get_render_real_radius(frame, rend_sets.m_affine);
Shinya Kitaoka 120a6e
  const int int_radius = igs::gaussian_blur_hv::int_radius(real_radius);
Shinya Kitaoka 120a6e
  /*------ ボケ足長さがゼロのときはblur処理しない ------------*/
Shinya Kitaoka 120a6e
  if (0 == int_radius) {
Shinya Kitaoka 120a6e
    this->m_input->compute(tile, frame, rend_sets);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /*------- 動作パラメータを得る -----------------------------*/
masafumi-inoue 26f824
  const int refer_mode = this->m_ref_mode->getValue();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*------ 表示の範囲を得る ----------------------------------*/
Shinya Kitaoka 120a6e
  TRectD bBox =
Shinya Kitaoka 120a6e
      TRectD(tile.m_pos /* Render画像上(Pixel単位)の位置 */
Shinya Kitaoka 120a6e
             ,
Shinya Kitaoka 120a6e
             TDimensionD(/* Render画像上(Pixel単位)のサイズ */
Shinya Kitaoka 120a6e
                         tile.getRaster()->getLx(), tile.getRaster()->getLy()));
Shinya Kitaoka 120a6e
  /*------ ぼけ半径(=マージン)分表示範囲を広げる -------------*/
Shinya Kitaoka 120a6e
  if (0 < int_radius) {
Shinya Kitaoka 120a6e
    bBox = bBox.enlarge(static_cast<double>(int_radius));</double>
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* ------ margin付き画像生成 ------------------------------ */
Shinya Kitaoka 120a6e
  /*
Toshihiro Shimizu 890ddd
void allocateAndCompute(
Toshihiro Shimizu 890ddd
TTile &tile
Toshihiro Shimizu 890ddd
,const TPointD &pos
Toshihiro Shimizu 890ddd
,const TDimension &size // TDimension = TDimensionI = TDimensionT<int></int>
Toshihiro Shimizu 890ddd
,TRasterP templateRas
Toshihiro Shimizu 890ddd
,double frame
Toshihiro Shimizu 890ddd
,const TRenderSettings &info
Toshihiro Shimizu 890ddd
);
Toshihiro Shimizu 890ddd
*/
Shinya Kitaoka 120a6e
  TTile enlarge_tile;
Shinya Kitaoka 120a6e
  this->m_input->allocateAndCompute(
Shinya Kitaoka 120a6e
      enlarge_tile, bBox.getP00(),
Shinya Kitaoka 120a6e
      TDimensionI(/* Pixel単位に四捨五入 */
Shinya Kitaoka 120a6e
                  static_cast<int>(bBox.getLx() + 0.5),</int>
Shinya Kitaoka 120a6e
                  static_cast<int>(bBox.getLy() + 0.5)),</int>
Shinya Kitaoka 120a6e
      tile.getRaster(), frame, rend_sets);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /*------ 参照画像生成 --------------------------------------*/
masafumi-inoue 26f824
  TTile refer_tile;
masafumi-inoue 26f824
  bool refer_sw = false;
Shinya Kitaoka 120a6e
  if (this->m_refer.isConnected()) {
masafumi-inoue 26f824
    refer_sw = true;
Shinya Kitaoka 120a6e
    this->m_refer->allocateAndCompute(
masafumi-inoue 26f824
        refer_tile, tile.m_pos /* TPointD */
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        tile.getRaster()->getSize() /* Pixel単位 */
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        tile.getRaster(), frame, rend_sets);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* ------ 保存すべき画像メモリを塗りつぶしクリア ---------- */
Shinya Kitaoka 120a6e
  tile.getRaster()->clear(); /* 塗りつぶしクリア */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  /* ------ (app_begin)log記憶 ------------------------------ */
Shinya Kitaoka 120a6e
  const bool log_sw = ino::log_enable_sw();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (log_sw) {
Shinya Kitaoka 120a6e
    std::ostringstream os;
Shinya Kitaoka 120a6e
    os << "params"
Shinya Kitaoka 120a6e
       << "  usr_radius " << this->m_radius->getValue(frame) << "  real_radius "
masafumi-inoue 26f824
       << real_radius << "  int_radius " << int_radius << "  refer_mode "
masafumi-inoue 26f824
       << refer_mode << "  tile"
Shinya Kitaoka 120a6e
       << " pos " << tile.m_pos << " w " << tile.getRaster()->getLx() << " h "
Shinya Kitaoka 120a6e
       << tile.getRaster()->getLy() << "  enl_tile"
Shinya Kitaoka 120a6e
       << " w " << enlarge_tile.getRaster()->getLx() << " h "
Shinya Kitaoka 120a6e
       << enlarge_tile.getRaster()->getLy() << "  pixbits "
Shinya Kitaoka 120a6e
       << ino::pixel_bits(tile.getRaster()) << "  frame " << frame
Shinya Kitaoka 120a6e
       << "  affine a11 " << rend_sets.m_affine.a11 << "  a12 "
Shinya Kitaoka 120a6e
       << rend_sets.m_affine.a12 << "  a21 " << rend_sets.m_affine.a21
Shinya Kitaoka 120a6e
       << "  a22 " << rend_sets.m_affine.a22;
masafumi-inoue 26f824
    if (refer_sw) {
masafumi-inoue 26f824
      os << "  refer_tile"
masafumi-inoue 26f824
         << " pos " << refer_tile.m_pos << " x "
masafumi-inoue 26f824
         << refer_tile.getRaster()->getLx() << " y "
masafumi-inoue 26f824
         << refer_tile.getRaster()->getLy();
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /* ------ fx処理 ------------------------------------------ */
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    tile.getRaster()->lock();
masafumi-inoue 26f824
    enlarge_tile.getRaster()->lock();
masafumi-inoue 26f824
    if (refer_tile.getRaster() != nullptr) {
masafumi-inoue 26f824
      refer_tile.getRaster()->lock();
masafumi-inoue 26f824
    }
Shinya Kitaoka 120a6e
    fx_(enlarge_tile.getRaster()  // in with margin
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        tile.getRaster()  // out with no margin
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        ,
masafumi-inoue 26f824
        refer_tile.getRaster(), refer_mode
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        int_radius  // margin
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        real_radius);
masafumi-inoue 26f824
    if (refer_tile.getRaster() != nullptr) {
masafumi-inoue 26f824
      refer_tile.getRaster()->unlock();
masafumi-inoue 26f824
    }
masafumi-inoue 26f824
    enlarge_tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /* ------ error処理 --------------------------------------- */
Shinya Kitaoka 120a6e
  catch (std::bad_alloc &e) {
masafumi-inoue 26f824
    if (refer_tile.getRaster() != nullptr) {
masafumi-inoue 26f824
      refer_tile.getRaster()->unlock();
masafumi-inoue 26f824
    }
masafumi-inoue 26f824
    enlarge_tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    if (log_sw) {
Shinya Kitaoka 120a6e
      std::string str("std::bad_alloc <");
Shinya Kitaoka 120a6e
      str += e.what();
Shinya Kitaoka 120a6e
      str += '>';
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  } catch (std::exception &e) {
masafumi-inoue 26f824
    if (refer_tile.getRaster() != nullptr) {
masafumi-inoue 26f824
      refer_tile.getRaster()->unlock();
masafumi-inoue 26f824
    }
masafumi-inoue 26f824
    enlarge_tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    if (log_sw) {
Shinya Kitaoka 120a6e
      std::string str("exception <");
Shinya Kitaoka 120a6e
      str += e.what();
Shinya Kitaoka 120a6e
      str += '>';
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  } catch (...) {
masafumi-inoue 26f824
    if (refer_tile.getRaster() != nullptr) {
masafumi-inoue 26f824
      refer_tile.getRaster()->unlock();
masafumi-inoue 26f824
    }
masafumi-inoue 26f824
    enlarge_tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
    if (log_sw) {
Shinya Kitaoka 120a6e
      std::string str("other exception");
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    throw;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}