Toshihiro Shimizu 890ddd
#include "plugin_fxnode_interface.h"
Toshihiro Shimizu 890ddd
#include "plugin_utilities.h"
Toshihiro Shimizu 890ddd
#include "trasterfx.h"
Toshihiro Shimizu 890ddd
#include <functional></functional>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
static const TRenderSettings &restore_render_settings(
Shinya Kitaoka 120a6e
    const toonz_rendering_setting_t *src) {
Shinya Kitaoka 120a6e
  return *reinterpret_cast<const *="" trendersettings="">(src->context);</const>
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int fxnode_compute_to_tile(toonz_fxnode_handle_t fxnode,
Shinya Kitaoka 120a6e
                           const toonz_rendering_setting_t *rendering_setting,
Shinya Kitaoka 120a6e
                           double frame, const toonz_rect_t *rect,
Shinya Kitaoka 120a6e
                           toonz_tile_handle_t intile,
Shinya Kitaoka 120a6e
                           toonz_tile_handle_t tile) {
Shinya Kitaoka 120a6e
  if (!fxnode || !rendering_setting || !rect || !tile)
Shinya Kitaoka 120a6e
    return TOONZ_ERROR_NULL;  // inval
Shinya Kitaoka 120a6e
  TRasterFx *trasterfx =
Shinya Kitaoka 120a6e
      dynamic_cast<trasterfx *="">(reinterpret_cast<tfx *="">(fxnode));</tfx></trasterfx>
Shinya Kitaoka 120a6e
  if (!trasterfx) return TOONZ_ERROR_INVALID_HANDLE;
Shinya Kitaoka 120a6e
  const TRenderSettings renderSettings =
Shinya Kitaoka 120a6e
      restore_render_settings(rendering_setting);
Shinya Kitaoka 120a6e
  TTile *t = reinterpret_cast<ttile *="">(tile);</ttile>
Shinya Kitaoka 120a6e
  TTile *i = reinterpret_cast<ttile *="">(intile);</ttile>
Shinya Kitaoka 120a6e
  TRasterP raster;
Shinya Kitaoka 120a6e
  if (i)
Shinya Kitaoka 120a6e
    raster = i->getRaster();
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    raster = 0;
Shinya Kitaoka 120a6e
  TPointD pos(rect->x0, rect->y0);
Shinya Kitaoka 120a6e
  TDimension dim(rect->x1 - rect->x0, rect->y1 - rect->y0);
Shinya Kitaoka 120a6e
  trasterfx->allocateAndCompute(*t, pos, dim, raster, frame, renderSettings);
Shinya Kitaoka 120a6e
  return TOONZ_OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
struct fxnode_dipatch {
Shinya Kitaoka 120a6e
  int operator()(
Shinya Kitaoka 120a6e
      toonz_fxnode_handle_t fxnode, const toonz_rendering_setting_t *rs,
Shinya Kitaoka 120a6e
      toonz_rect_t *rect, int *ret,
Shinya Kitaoka 120a6e
      std::function<int(trasterfx &)="" &,="" *,="" const="" trectd="" trendersettings=""> f) {</int(trasterfx>
Shinya Kitaoka 120a6e
    using namespace plugin::utils;
Shinya Kitaoka 120a6e
    TRasterFx *trasterfx =
Shinya Kitaoka 120a6e
        dynamic_cast<trasterfx *="">(reinterpret_cast<tfx *="">(fxnode));</tfx></trasterfx>
Shinya Kitaoka 120a6e
    if (!trasterfx) return TOONZ_ERROR_INVALID_HANDLE;
Shinya Kitaoka 120a6e
    const TRenderSettings &renderSettings = restore_render_settings(rs);
Shinya Kitaoka 120a6e
    TRectD rc                             = restore_rect(rect);
Shinya Kitaoka 120a6e
    *ret                                  = f(trasterfx, renderSettings, rc);
Shinya Kitaoka 120a6e
    copy_rect(rect, rc);
Shinya Kitaoka 120a6e
    return TOONZ_OK;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int fxnode_get_bbox(toonz_fxnode_handle_t fxnode,
Shinya Kitaoka 120a6e
                    const toonz_rendering_setting_t *rendering_setting,
Shinya Kitaoka 120a6e
                    double frame, toonz_rect_t *iorect, int *ret) {
Shinya Kitaoka 120a6e
  fxnode_dipatch disp;
Shinya Kitaoka 120a6e
  return disp(fxnode, rendering_setting, iorect, ret,
Shinya Kitaoka 120a6e
              [&](TRasterFx *fx, const TRenderSettings &rs, TRectD &rect) {
Shinya Kitaoka 120a6e
                return fx->getBBox(frame, rect, rs);
Shinya Kitaoka 120a6e
              });
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int fxnode_can_handle(toonz_fxnode_handle_t fxnode,
Shinya Kitaoka 120a6e
                      const toonz_rendering_setting_t *rendering_setting,
Shinya Kitaoka 120a6e
                      double frame, int *ret) {
Shinya Kitaoka 120a6e
  TRasterFx *trasterfx =
Shinya Kitaoka 120a6e
      dynamic_cast<trasterfx *="">(reinterpret_cast<tfx *="">(fxnode));</tfx></trasterfx>
Shinya Kitaoka 120a6e
  if (!trasterfx) {
Shinya Kitaoka 120a6e
    return TOONZ_ERROR_INVALID_HANDLE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  const TRenderSettings &renderSettings =
Shinya Kitaoka 120a6e
      restore_render_settings(rendering_setting);
Shinya Kitaoka 120a6e
  *ret = trasterfx->canHandle(renderSettings, frame);
Shinya Kitaoka 120a6e
  return TOONZ_OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int fxnode_get_input_port_count(toonz_fxnode_handle_t fxnode, int *count) {
Shinya Kitaoka 120a6e
  if (!fxnode) {
Shinya Kitaoka 120a6e
    return TOONZ_ERROR_INVALID_HANDLE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TFx *tfx = reinterpret_cast<tfx *="">(fxnode);</tfx>
Shinya Kitaoka 120a6e
  *count   = tfx->getInputPortCount();
Shinya Kitaoka 120a6e
  return TOONZ_OK;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int fxnode_get_input_port(toonz_fxnode_handle_t fxnode, int index,
Shinya Kitaoka 120a6e
                          toonz_port_handle_t *port) {
Shinya Kitaoka 120a6e
  if (!fxnode) {
Shinya Kitaoka 120a6e
    return TOONZ_ERROR_INVALID_HANDLE;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TFx *tfx              = reinterpret_cast<tfx *="">(fxnode);</tfx>
Shinya Kitaoka 120a6e
  toonz_port_handle_t p = nullptr;
Shinya Kitaoka 120a6e
  p                     = tfx->getInputPort(index);
Shinya Kitaoka 120a6e
  if (!p) return TOONZ_ERROR_NOT_FOUND;
Shinya Kitaoka 120a6e
  *port = p;
Shinya Kitaoka 120a6e
  return TOONZ_OK;
Toshihiro Shimizu 890ddd
}