Toshihiro Shimizu 890ddd
#include <sstream> /* std::ostringstream */</sstream>
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "ino_common.h"
Toshihiro Shimizu 890ddd
#include "igs_fog.h"
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka d1f6c4
class ino_fog final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(ino_fog)
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_radius;
Shinya Kitaoka 120a6e
  TDoubleParamP m_curve;
Shinya Kitaoka 120a6e
  TDoubleParamP m_power;
Shinya Kitaoka 120a6e
  TDoubleParamP m_threshold_min;
Shinya Kitaoka 120a6e
  TDoubleParamP m_threshold_max;
Shinya Kitaoka 120a6e
  TBoolParamP m_alpha_rendering;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ino_fog()
Shinya Kitaoka 120a6e
      : m_radius(1.0)
Shinya Kitaoka 120a6e
      , m_curve(1.0 * ino::param_range())
Shinya Kitaoka 120a6e
      , m_power(1.0 * ino::param_range())
Shinya Kitaoka 120a6e
      , m_threshold_min(0.0 * ino::param_range())
Shinya Kitaoka 120a6e
      , m_threshold_max(0.0 * ino::param_range())
Shinya Kitaoka 120a6e
      , m_alpha_rendering(false) {
Shinya Kitaoka 120a6e
    this->m_radius->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    addInputPort("Source", this->m_input);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    bindParam(this, "radius", this->m_radius);
Shinya Kitaoka 120a6e
    bindParam(this, "curve", this->m_curve);
Shinya Kitaoka 120a6e
    bindParam(this, "power", this->m_power);
Shinya Kitaoka 120a6e
    bindParam(this, "threshold_min", this->m_threshold_min);
Shinya Kitaoka 120a6e
    bindParam(this, "threshold_max", this->m_threshold_max);
Shinya Kitaoka 120a6e
    bindParam(this, "alpha_rendering", this->m_alpha_rendering);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
    this->m_radius->setValueRange(0.0, 100.0);
Shinya Kitaoka 120a6e
    this->m_curve->setValueRange(0.1 * ino::param_range(),
Shinya Kitaoka 120a6e
                                 10.0 * ino::param_range()); /* gammaカーブ */
Shinya Kitaoka 120a6e
    this->m_power->setValueRange(-2.0 * ino::param_range(),
Shinya Kitaoka 120a6e
                                 2.0 * ino::param_range());
Shinya Kitaoka 120a6e
    this->m_threshold_min->setValueRange(0.0 * ino::param_range(),
Shinya Kitaoka 120a6e
                                         1.01 * ino::param_range());
Shinya Kitaoka 120a6e
    this->m_threshold_max->setValueRange(0.0 * ino::param_range(),
Shinya Kitaoka 120a6e
                                         1.01 * ino::param_range());
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  //------------------------------------------------------------
Shinya Kitaoka 120a6e
  double get_render_real_radius(const double frame, const TAffine affine) {
Shinya Kitaoka 120a6e
    return this->m_radius->getValue(frame) *
Shinya Kitaoka 120a6e
           ino::pixel_per_mm()        /* render用単位にする */
Shinya Kitaoka 120a6e
           * sqrt(fabs(affine.det())) /* 拡大縮小のGeometryを反映 */
Shinya Kitaoka 120a6e
        ;
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 =
Shinya Kitaoka 120a6e
        static_cast<int>(ceil(this->get_render_real_radius(frame, affine)));</int>
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 38fd86
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 38fd86
    return true;
Shinya Kitaoka 38fd86
  }
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_fog, "inoFogFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
void fx_(const TRasterP in_ras  // with margin
Shinya Kitaoka 120a6e
         ,
Shinya Kitaoka 120a6e
         const int margin, TRasterP out_ras  // no margin
Shinya Kitaoka 120a6e
         ,
Shinya Kitaoka 120a6e
         const int nthread, const double radius, const double curve,
Shinya Kitaoka 120a6e
         const double power, const double threshold_min,
Shinya Kitaoka 120a6e
         const double threshold_max, const bool alp_rend_sw) {
Shinya Kitaoka 120a6e
  TRasterGR8P out_gr8(in_ras->getLy(),
Shinya Kitaoka 120a6e
                      in_ras->getLx() * ino::channels() *
Shinya Kitaoka 120a6e
                          ((TRaster64P)in_ras ? sizeof(unsigned short)
Shinya Kitaoka 120a6e
                                              : sizeof(unsigned char)));
Shinya Kitaoka 120a6e
  out_gr8->lock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterGR8P ref_gr8(in_ras->getLy(), in_ras->getLx() * sizeof(double));
Shinya Kitaoka 120a6e
  ref_gr8->lock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  igs::fog::convert(
Shinya Kitaoka 120a6e
      in_ras->getRawData()  // BGRA
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      out_gr8->getRawData(), reinterpret_cast<double *="">(ref_gr8->getRawData())</double>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
                                 ,
Shinya Kitaoka 120a6e
      in_ras->getLy(), in_ras->getLx(), ino::channels(), ino::bits(in_ras)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
                                                             ,
Shinya Kitaoka 120a6e
      nthread
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      radius, curve, 2, 0.0, power, threshold_min, threshold_max, alp_rend_sw);
Shinya Kitaoka 120a6e
  ref_gr8->unlock();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ino::arr_to_ras(out_gr8->getRawData(), ino::channels(), out_ras, margin);
Shinya Kitaoka 120a6e
  out_gr8->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka 120a6e
void ino_fog::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 radius = this->get_render_real_radius(frame, rend_sets.m_affine);
Shinya Kitaoka 120a6e
  const double curve  = this->m_curve->getValue(frame) / ino::param_range();
Shinya Kitaoka 120a6e
  const double power  = this->m_power->getValue(frame) / ino::param_range();
Shinya Kitaoka 120a6e
  const double threshold_min =
Shinya Kitaoka 120a6e
      this->m_threshold_min->getValue(frame) / ino::param_range();
Shinya Kitaoka 120a6e
  const double threshold_max =
Shinya Kitaoka 120a6e
      this->m_threshold_max->getValue(frame) / ino::param_range();
Shinya Kitaoka 120a6e
  const bool alp_rend_sw = this->m_alpha_rendering->getValue();
Shinya Kitaoka 120a6e
  const int nthread      = 1;
Shinya Kitaoka 120a6e
  /*------ fogがからないパラメータ値のときはfog処理しない ----*/
Shinya Kitaoka 120a6e
  if (!igs::fog::have_change(radius, power, threshold_min)) {
Shinya Kitaoka 120a6e
    this->m_input->compute(tile, frame, rend_sets);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
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
  /*------ fog半径(=マージン)分表示範囲を広げる --------------*/
Shinya Kitaoka 120a6e
  const int int_radius = static_cast<int>(ceil(radius));</int>
Shinya Kitaoka 120a6e
  if (0 < int_radius) {
Shinya Kitaoka 120a6e
    bBox = bBox.enlarge(static_cast<double>(int_radius));</double>
Shinya Kitaoka 120a6e
  }
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);
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
       << "  nthread " << nthread << "  usr_radius "
Shinya Kitaoka 120a6e
       << this->m_radius->getValue(frame) << "  real_radius " << radius
Shinya Kitaoka 120a6e
       << "  int_radius " << int_radius << "  curve " << curve << "  power "
Shinya Kitaoka 120a6e
       << power << "  threshold_min " << threshold_min << "  threshold_max "
Shinya Kitaoka 120a6e
       << threshold_max << "  alp_rend_sw " << alp_rend_sw << "   tile w "
Shinya Kitaoka 120a6e
       << tile.getRaster()->getLx() << "  h " << tile.getRaster()->getLy()
Shinya Kitaoka 120a6e
       << "  pixbits " << ino::pixel_bits(tile.getRaster()) << "   frame "
Shinya Kitaoka 120a6e
       << frame << "   rand_sets affine_det " << rend_sets.m_affine.det()
Shinya Kitaoka 120a6e
       << "  shrink x " << rend_sets.m_shrinkX << "  y " << rend_sets.m_shrinkY;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /* ------ fx処理 ------------------------------------------ */
Shinya Kitaoka 120a6e
  try {
Shinya Kitaoka 120a6e
    tile.getRaster()->lock();
Shinya Kitaoka 120a6e
    fx_(enlarge_tile.getRaster()  // with margin
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        int_radius  // margin
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        tile.getRaster()  // no margin
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        nthread, radius, curve, power, threshold_min, threshold_max,
Shinya Kitaoka 120a6e
        alp_rend_sw);
Shinya Kitaoka 120a6e
    tile.getRaster()->unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /* ------ error処理 --------------------------------------- */
Shinya Kitaoka 120a6e
  catch (std::bad_alloc &e) {
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) {
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 (...) {
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
}