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"
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
const double smoothing_edge_ = 1.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Shinya Kitaoka d1f6c4
class ino_maxmin final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(ino_maxmin)
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TRasterFxPort m_refer;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TIntEnumParamP m_max_min_select;
Shinya Kitaoka 120a6e
  TDoubleParamP m_radius;
Shinya Kitaoka 120a6e
  TDoubleParamP m_polygon_number;
Shinya Kitaoka 120a6e
  TDoubleParamP m_degree;
Shinya Kitaoka 120a6e
  TBoolParamP m_alpha_rendering;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TIntEnumParamP m_ref_mode;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ino_maxmin()
Shinya Kitaoka 120a6e
      : m_max_min_select(new TIntEnumParam(0, "Max"))
Shinya Kitaoka 120a6e
      , m_radius(1.0)
Shinya Kitaoka 120a6e
      /*	1mmにしたければ 1.0 * 640. / 12. / 25.4 とする
Shinya Kitaoka 120a6e
            0.35    = mm = ユーザ指定の初期値
Shinya Kitaoka 120a6e
            640/12  = 53.33333 = toonz独自の係数 =tunit.cpp参照
Shinya Kitaoka 120a6e
            25.4    = mm/inch
Shinya Kitaoka 120a6e
    */
Shinya Kitaoka 120a6e
      , m_polygon_number(2.0)
Shinya Kitaoka 120a6e
      , m_degree(0.0)
Shinya Kitaoka 120a6e
      , m_alpha_rendering(true)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      , m_ref_mode(new TIntEnumParam()) {
Shinya Kitaoka 120a6e
    this->m_radius->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    addInputPort("Source", this->m_input);
Shinya Kitaoka 120a6e
    addInputPort("Reference", this->m_refer);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    bindParam(this, "max_min_select", this->m_max_min_select);
Shinya Kitaoka 120a6e
    bindParam(this, "radius", this->m_radius);
Shinya Kitaoka 120a6e
    bindParam(this, "polygon_number", this->m_polygon_number);
Shinya Kitaoka 120a6e
    bindParam(this, "degree", this->m_degree);
Shinya Kitaoka 120a6e
    bindParam(this, "alpha_rendering", this->m_alpha_rendering);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    bindParam(this, "reference", this->m_ref_mode);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    this->m_max_min_select->addItem(1, "Min");
Shinya Kitaoka 120a6e
    this->m_radius->setValueRange(0.0, 100.0);
Shinya Kitaoka 120a6e
    this->m_polygon_number->setValueRange(2.0, 16.0);
Shinya Kitaoka 120a6e
    this->m_degree->setValueRange(0.0, std::numeric_limits<double>::max());</double>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    this->m_ref_mode->addItem(0, "Red");
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
    this->m_ref_mode->setDefaultValue(0);
Shinya Kitaoka 120a6e
    this->m_ref_mode->setValue(0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 38fd86
  bool doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 38fd86
                 const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    if (this->m_input.isConnected()) {
Shinya Kitaoka 120a6e
      const bool ret = this->m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
      const double margin =
Shinya Kitaoka 120a6e
          ceil(ino::pixel_per_mm() *
Shinya Kitaoka 120a6e
               (this->m_radius->getValue(frame) + smoothing_edge_));
Shinya Kitaoka 120a6e
      if (0.0 < margin) {
Shinya Kitaoka 120a6e
        bBox = bBox.enlarge(margin);
Shinya Kitaoka 120a6e
      }
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 38fd86
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 38fd86
    return true;
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 120a6e
  int getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 473e70
                           const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    const double radius = (this->m_radius->getValue(frame) + smoothing_edge_) *
Shinya Kitaoka 120a6e
                          ino::pixel_per_mm() *
Shinya Kitaoka 120a6e
                          sqrt(fabs(info.m_affine.det())) /
Shinya Kitaoka 120a6e
                          ((info.m_shrinkX + info.m_shrinkY) / 2.0);
Shinya Kitaoka 120a6e
    return TRasterFx::memorySize(rect.enlarge(ceil(radius) + 0.5), info.m_bpp);
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_maxmin, "inoMaxMinFx");
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
#include "igs_maxmin.h"
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
         ,
masafumi-inoue 26f824
         const TRasterP refer_ras, const int refer_mode
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
         ,
Shinya Kitaoka 120a6e
         const int min_sw, const double radius, const double smoothing_edge,
Shinya Kitaoka 120a6e
         const int npolygon, const double degree, const bool alp_rend_sw
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
         ,
Shinya Kitaoka 120a6e
         const int nthread) {
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();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  igs::maxmin::convert(
Shinya Kitaoka 120a6e
      in_ras->getRawData()  // BGRA
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      out_gr8->getRawData()
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
          ,
Shinya Kitaoka 120a6e
      in_ras->getLy(), in_ras->getLx(), ino::channels(), ino::bits(in_ras)
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
                                                             ,
masafumi-inoue 26f824
      (((refer_ras != nullptr) && (0 <= refer_mode)) ? refer_ras->getRawData()
masafumi-inoue 26f824
                                                     : nullptr)  // BGRA
Shinya Kitaoka 120a6e
      ,
masafumi-inoue 26f824
      (((refer_ras != nullptr) && (0 <= refer_mode)) ? ino::bits(refer_ras) : 0)
Shinya Kitaoka 120a6e
masafumi-inoue 26f824
          ,
masafumi-inoue 26f824
      refer_mode, radius, smoothing_edge  // smooth_outer_range
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      npolygon, degree + 180.0
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      min_sw, alp_rend_sw, false
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      ,
Shinya Kitaoka 120a6e
      nthread);
Shinya Kitaoka 120a6e
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_maxmin::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
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
  /* ------ Pixel単位で動作パラメータを得る ----------------- */
Shinya Kitaoka 120a6e
  const double mm2scale_shrink_pixel =
Shinya Kitaoka 120a6e
      ino::pixel_per_mm() * sqrt(fabs(rend_sets.m_affine.det())) /
Shinya Kitaoka 120a6e
      ((rend_sets.m_shrinkX + rend_sets.m_shrinkY) / 2.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* 動作パラメータを得る */
Shinya Kitaoka 120a6e
  const int min_sw    = this->m_max_min_select->getValue();  // 0,1
Shinya Kitaoka 120a6e
  const double radius = this->m_radius->getValue(frame) * mm2scale_shrink_pixel;
Shinya Kitaoka 120a6e
  const int npolygon  = this->m_polygon_number->getValue(frame);
Shinya Kitaoka 120a6e
  const double degree = this->m_degree->getValue(frame);
Shinya Kitaoka 120a6e
  const bool alp_rend_sw = this->m_alpha_rendering->getValue();
Shinya Kitaoka 120a6e
masafumi-inoue 26f824
  const int refer_mode = this->m_ref_mode->getValue();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* 	tcomposer(RenderManager)でRenderingするときはthreadは1つ、
Shinya Kitaoka 120a6e
          toonz(Desktop)でPreview,Outputするなら-1を指定
Shinya Kitaoka 120a6e
          -1は自動でthread数を決めCPUを最大に使い高速化 */
Shinya Kitaoka 120a6e
  const int nthread = -1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* ------ 参照マージン含めた画像生成 ---------------------- */
Shinya Kitaoka 120a6e
  /* Rendering画像のBBox値 --> Pixel単位のdouble値 */
Shinya Kitaoka 120a6e
  /*	typedef TRectT<double> TRectD;</double>
Shinya Kitaoka 120a6e
          inline TRectT<double>::TRectT(</double>
Shinya Kitaoka 120a6e
                  const TPointT<double> &bottomLeft</double>
Shinya Kitaoka 120a6e
                  , const TDimensionT<double> &d</double>
Shinya Kitaoka 120a6e
          )	: x0(bottomLeft.x)
Shinya Kitaoka 120a6e
                  , y0(bottomLeft.y)
Shinya Kitaoka 120a6e
                  , x1(bottomLeft.x+d.lx)
Shinya Kitaoka 120a6e
                  , y1(bottomLeft.y+d.ly)
Shinya Kitaoka 120a6e
          {}
Shinya Kitaoka 120a6e
  */
Shinya Kitaoka 120a6e
  TRectD enlarge_rect =
Shinya Kitaoka 120a6e
      TRectD(tile.m_pos /* TPointD */
Shinya Kitaoka 120a6e
             ,
Shinya Kitaoka 120a6e
             TDimensionD(/* int --> doubleにしてセット */
Shinya Kitaoka 120a6e
                         tile.getRaster()->getLx(), tile.getRaster()->getLy()));
Shinya Kitaoka 120a6e
  /* BBoxの拡大 Pixel単位 */
Shinya Kitaoka 120a6e
  const int enlarge_pixel = (int)(ceil(radius + smoothing_edge_) + 0.5);
Shinya Kitaoka 120a6e
  enlarge_rect            = enlarge_rect.enlarge(enlarge_pixel);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*	void TRasterFx::allocateAndCompute(
Shinya Kitaoka 120a6e
                  TTile &tile,
Shinya Kitaoka 120a6e
                  const TPointD &pos, const TDimension &size,
Shinya Kitaoka 120a6e
                  const TRasterP &templateRas, double frame,
Shinya Kitaoka 120a6e
                  const TRenderSettings &info
Shinya Kitaoka 120a6e
          ) {
Shinya Kitaoka 120a6e
                  if (templateRas) {
Shinya Kitaoka 120a6e
          tile.getRaster() = templateRas->create(size.lx, size.ly);
Shinya Kitaoka 120a6e
                  } else {
Shinya Kitaoka 120a6e
          tile.getRaster() = TRaster32P(size.lx, size.ly);
Shinya Kitaoka 120a6e
                  }
Shinya Kitaoka 120a6e
          tile.m_pos = pos;
Shinya Kitaoka 120a6e
                  compute(tile, frame, info);
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
  */
Shinya Kitaoka 120a6e
  TTile enlarge_tile;
Shinya Kitaoka 120a6e
  this->m_input->allocateAndCompute(
Shinya Kitaoka 120a6e
      enlarge_tile, enlarge_rect.getP00(),
Shinya Kitaoka 120a6e
      TDimensionI(/* Pixel単位のdouble -> intにしてセット */
Shinya Kitaoka 120a6e
                  (int)(enlarge_rect.getLx() + 0.5),
Shinya Kitaoka 120a6e
                  (int)(enlarge_rect.getLy() + 0.5)),
Shinya Kitaoka 120a6e
      tile.getRaster(), frame, rend_sets);
Shinya Kitaoka 120a6e
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, enlarge_tile.m_pos,
Shinya Kitaoka 120a6e
        TDimensionI(/* Pixel単位 */
Shinya Kitaoka 120a6e
                    enlarge_tile.getRaster()->getLx(),
Shinya Kitaoka 120a6e
                    enlarge_tile.getRaster()->getLy()),
Shinya Kitaoka 120a6e
        enlarge_tile.getRaster(), frame, rend_sets);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  /* ------ 保存すべき画像メモリを塗りつぶしクリア ---------- */
Shinya Kitaoka 120a6e
  tile.getRaster()->clear(); /* 塗りつぶしクリア */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /* ------ (app_begin)log記憶 ------------------------------ */
Shinya Kitaoka 120a6e
  const bool log_sw = ino::log_enable_sw();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (log_sw) {
Shinya Kitaoka 120a6e
    std::ostringstream os;
Shinya Kitaoka 120a6e
    os << "params"
Shinya Kitaoka 120a6e
       << "  min_sw " << min_sw << "  radius " << radius << "  npolygon "
Shinya Kitaoka 120a6e
       << npolygon << "  degree " << degree << "  alp_rend_sw " << alp_rend_sw
Shinya Kitaoka 120a6e
       << "  smoothing_edge " << smoothing_edge_ << "  nthread " << nthread
masafumi-inoue 26f824
       << "  refer_mode " << refer_mode << "   tile w "
masafumi-inoue 26f824
       << tile.getRaster()->getLx() << "  h " << tile.getRaster()->getLy()
masafumi-inoue 26f824
       << "  pixbits " << ino::pixel_bits(tile.getRaster()) << "   frame "
masafumi-inoue 26f824
       << frame << "   rand_sets affine_det " << rend_sets.m_affine.det()
Shinya Kitaoka 120a6e
       << "  shrink x " << rend_sets.m_shrinkX << "  y " << rend_sets.m_shrinkY;
masafumi-inoue 26f824
    if (refer_sw) {
masafumi-inoue 26f824
      os << "  refer_tile.m_pos " << refer_tile.m_pos << "  refer_tile_getLx "
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
        enlarge_pixel  // margin
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        tile.getRaster()  // out with no margin
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        ,
masafumi-inoue 26f824
        refer_tile.getRaster(), refer_mode
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        min_sw, radius, smoothing_edge_, npolygon, degree, alp_rend_sw
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
        ,
Shinya Kitaoka 120a6e
        nthread);
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
}