Toshihiro Shimizu 890ddd
/*------------------------------------
Toshihiro Shimizu 890ddd
Iwa_PerspectiveDistortFx
Toshihiro Shimizu 890ddd
奥行き方向に台形歪みを行うエフェクト
Toshihiro Shimizu 890ddd
ディティールを保持するため、引き伸ばす量に応じて素材の解像度を上げる
Toshihiro Shimizu 890ddd
------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "iwa_perspectivedistortfx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tparamuiconcept.h"
shun-iwasawa 481b59
// #include "ino_common.h"
shun-iwasawa 481b59
#include "trop.h"
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
inline float4 getSource_CPU(float4 *source_host, TDimensionI &dim, int pos_x,
shun-iwasawa 481b59
                            int pos_y) {
Shinya Kitaoka 120a6e
  if (pos_x < 0 || pos_x >= dim.lx || pos_y < 0 || pos_y >= dim.ly)
Shinya Kitaoka 120a6e
    return float4{0.0f, 0.0f, 0.0f, 0.0f};
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return source_host[pos_y * dim.lx + pos_x];
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun-iwasawa 481b59
inline float4 interp_CPU(float4 val1, float4 val2, float ratio) {
shun-iwasawa 481b59
  float4 ret;
shun-iwasawa 481b59
  ret.x = (1.0f - ratio) * val1.x + ratio * val2.x;
shun-iwasawa 481b59
  ret.y = (1.0f - ratio) * val1.y + ratio * val2.y;
shun-iwasawa 481b59
  ret.z = (1.0f - ratio) * val1.z + ratio * val2.z;
shun-iwasawa 481b59
  ret.w = (1.0f - ratio) * val1.w + ratio * val2.w;
shun-iwasawa 481b59
  return ret;
Toshihiro Shimizu 890ddd
}
shun-iwasawa 481b59
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 出力結果をChannel値に変換して格納
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel="" raster,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void Iwa_PerspectiveDistortFx::setOutputRaster(float4 *srcMem,
Shinya Kitaoka 120a6e
                                               const RASTER dstRas,
Shinya Kitaoka 120a6e
                                               TDimensionI dim, int drawLevel) {
Shinya Kitaoka 120a6e
  dstRas->fill(PIXEL::Transparent);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float4 *chan_p = srcMem;
Shinya Kitaoka 120a6e
  for (int j = 0; j < drawLevel; j++) {
Shinya Kitaoka 120a6e
    if (j >= dstRas->getLy()) break;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    PIXEL *pix = dstRas->pixels(j);
Shinya Kitaoka 120a6e
    for (int i = 0; i < dstRas->getLx(); i++, chan_p++, pix++) {
Shinya Kitaoka 120a6e
      float val;
Shinya Kitaoka 120a6e
      val    = (*chan_p).x * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->r = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
      val    = (*chan_p).y * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->g = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
      val    = (*chan_p).z * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->b = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
      val    = (*chan_p).w * (float)PIXEL::maxChannelValue + 0.5f;
Shinya Kitaoka 120a6e
      pix->m = (typename PIXEL::Channel)((val > (float)PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                                             ? (float)PIXEL::maxChannelValue
Shinya Kitaoka 120a6e
                                             : val);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun-iwasawa 481b59
template <>
shun-iwasawa 481b59
void Iwa_PerspectiveDistortFx::setOutputRaster<trasterfp, tpixelf="">(</trasterfp,>
shun-iwasawa 481b59
    float4 *srcMem, const TRasterFP dstRas, TDimensionI dim, int drawLevel) {
shun-iwasawa 481b59
  dstRas->fill(TPixelF::Transparent);
shun-iwasawa 481b59
  float4 *chan_p = srcMem;
shun-iwasawa 481b59
  for (int j = 0; j < drawLevel; j++) {
shun-iwasawa 481b59
    if (j >= dstRas->getLy()) break;
shun-iwasawa 481b59
    TPixelF *pix = dstRas->pixels(j);
shun-iwasawa 481b59
    for (int i = 0; i < dstRas->getLx(); i++, chan_p++, pix++) {
shun-iwasawa 481b59
      pix->r = (*chan_p).x;
shun-iwasawa 481b59
      pix->g = (*chan_p).y;
shun-iwasawa 481b59
      pix->b = (*chan_p).z;
shun-iwasawa 481b59
      pix->m = (*chan_p).w;
shun-iwasawa 481b59
    }
shun-iwasawa 481b59
  }
shun-iwasawa 481b59
}
shun-iwasawa 481b59
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 ソース画像を0〜1に正規化してホストメモリに読み込む
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel="" raster,="" typename=""></typename>
Toshihiro Shimizu 890ddd
void Iwa_PerspectiveDistortFx::setSourceRaster(const RASTER srcRas,
Shinya Kitaoka 120a6e
                                               float4 *dstMem,
Shinya Kitaoka 120a6e
                                               TDimensionI dim) {
Shinya Kitaoka 120a6e
  float4 *chann_p = dstMem;
Shinya Kitaoka 120a6e
  for (int j = 0; j < dim.ly; j++) {
Shinya Kitaoka 120a6e
    PIXEL *pix = srcRas->pixels(j);
Shinya Kitaoka 120a6e
    for (int i = 0; i < dim.lx; i++, pix++, chann_p++) {
Shinya Kitaoka 120a6e
      (*chann_p).x = (float)pix->r / (float)PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
      (*chann_p).y = (float)pix->g / (float)PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
      (*chann_p).z = (float)pix->b / (float)PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
      (*chann_p).w = (float)pix->m / (float)PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Iwa_PerspectiveDistortFx::Iwa_PerspectiveDistortFx()
Shinya Kitaoka 120a6e
    : m_vanishingPoint(TPointD(0, 0))
Shinya Kitaoka 120a6e
    , m_anchorPoint(TPointD(0, -100.0))
Shinya Kitaoka 120a6e
    , m_precision(300.0 / 162.5) {
Shinya Kitaoka 120a6e
  /*- 共通パラメータのバインド -*/
Shinya Kitaoka 120a6e
  addInputPort("Source", m_source);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  bindParam(this, "vanishingPoint", m_vanishingPoint);
Shinya Kitaoka 120a6e
  bindParam(this, "anchorPoint", m_anchorPoint);
Shinya Kitaoka 120a6e
  bindParam(this, "precision", m_precision);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_vanishingPoint->getX()->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
  m_vanishingPoint->getY()->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
  m_anchorPoint->getX()->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
  m_anchorPoint->getY()->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_precision->setValueRange(1.0, 2.0);
shun-iwasawa 481b59
shun-iwasawa 481b59
  enableComputeInFloat(true);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Iwa_PerspectiveDistortFx::doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 120a6e
                                         const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  if (m_source.isConnected()) {
shun-iwasawa 481b59
    bool ret = m_source->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
    if (ret) bBox = TConsts::infiniteRectD;
Shinya Kitaoka 120a6e
    return ret;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool Iwa_PerspectiveDistortFx::canHandle(const TRenderSettings &info,
Shinya Kitaoka 120a6e
                                         double frame) {
Shinya Kitaoka 120a6e
  return false;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_PerspectiveDistortFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                         const TRenderSettings &rend_sets) {
Shinya Kitaoka 120a6e
  /*- ソース画像が刺さっていなければreturn -*/
Shinya Kitaoka 120a6e
  if (!m_source.isConnected()) {
Shinya Kitaoka 120a6e
    tile.getRaster()->clear();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*- パラメータの取得 -*/
Shinya Kitaoka 120a6e
  TPointD vp       = m_vanishingPoint->getValue(frame);
Shinya Kitaoka 120a6e
  TPointD ap       = m_anchorPoint->getValue(frame);
Shinya Kitaoka 120a6e
  double precision = m_precision->getValue(frame);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*- 基準点が消失点よりも上にあったらreturn -*/
Shinya Kitaoka 120a6e
  if (vp.y <= ap.y) {
Shinya Kitaoka 120a6e
    std::cout << "the anchor must be lower than the vanishing point"
Shinya Kitaoka 120a6e
              << std::endl;
Shinya Kitaoka 120a6e
    tile.getRaster()->clear();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TAffine aff = rend_sets.m_affine;
Shinya Kitaoka 120a6e
  /*- カメラ左下を中心としたピクセル座標に変換する -*/
Shinya Kitaoka 120a6e
  TPointD vanishingPoint = aff * vp -
Shinya Kitaoka 120a6e
                           (tile.m_pos + tile.getRaster()->getCenterD()) +
Shinya Kitaoka 120a6e
                           TPointD(rend_sets.m_cameraBox.getLx() / 2.0,
Shinya Kitaoka 120a6e
                                   rend_sets.m_cameraBox.getLy() / 2.0);
Shinya Kitaoka 120a6e
  TPointD anchorPoint = aff * ap -
Shinya Kitaoka 120a6e
                        (tile.m_pos + tile.getRaster()->getCenterD()) +
Shinya Kitaoka 120a6e
                        TPointD(rend_sets.m_cameraBox.getLx() / 2.0,
Shinya Kitaoka 120a6e
                                rend_sets.m_cameraBox.getLy() / 2.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double offs = vanishingPoint.x - rend_sets.m_cameraBox.getLx() / 2.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*- 取り込む解像度の倍率 -*/
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRectD rectOut(tile.m_pos, TDimensionD(tile.getRaster()->getLx(),
Shinya Kitaoka 120a6e
                                         tile.getRaster()->getLy()));
Shinya Kitaoka 120a6e
  TDimensionI outDim(rectOut.getLx(), rectOut.getLy());
Shinya Kitaoka 120a6e
  /*- ソース画像を正規化して格納 -*/
Shinya Kitaoka 120a6e
  /*- 素材を拡大させて持ち込む -*/
Shinya Kitaoka 120a6e
  TDimensionI sourceDim(rectOut.getLx() * (int)tceil(precision), anchorPoint.y);
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  float4 *source_host = nullptr;
shun-iwasawa 481b59
shun-iwasawa 481b59
  TRenderSettings new_sets(rend_sets);
shun-iwasawa 481b59
shun-iwasawa 481b59
  new_sets.m_affine *= TTranslation(vp.x, vp.y);
shun-iwasawa 481b59
  new_sets.m_affine *= TScale(precision, 1.0);
shun-iwasawa 481b59
  new_sets.m_affine *= TTranslation(-vp.x, -vp.y);
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  double sourcePosX = offs + precision * (tile.m_pos.x - offs);
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  TTile sourceTile;
shun-iwasawa 481b59
  m_source->allocateAndCompute(sourceTile, TPointD(sourcePosX, tile.m_pos.y),
shun-iwasawa 481b59
                               sourceDim, tile.getRaster(), frame, new_sets);
shun-iwasawa 481b59
shun-iwasawa 481b59
  TRasterGR8P source_host_ras;
shun-iwasawa 481b59
shun-iwasawa 481b59
  if (tile.getRaster()->getPixelSize() == 4 ||
shun-iwasawa 481b59
      tile.getRaster()->getPixelSize() == 8) {
shun-iwasawa 481b59
    source_host_ras = TRasterGR8P(sourceDim.lx * sizeof(float4), sourceDim.ly);
shun-iwasawa 481b59
    source_host_ras->lock();
shun-iwasawa 481b59
    source_host = (float4 *)source_host_ras->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*- タイルの画像を0〜1に正規化してホストメモリに読み込む -*/
Shinya Kitaoka 120a6e
    TRaster32P ras32 = (TRaster32P)sourceTile.getRaster();
Shinya Kitaoka 120a6e
    TRaster64P ras64 = (TRaster64P)sourceTile.getRaster();
Shinya Kitaoka 120a6e
    if (ras32)
Shinya Kitaoka 120a6e
      setSourceRaster<traster32p, tpixel32="">(ras32, source_host, sourceDim);</traster32p,>
Shinya Kitaoka 120a6e
    else if (ras64)
Shinya Kitaoka 120a6e
      setSourceRaster<traster64p, tpixel64="">(ras64, source_host, sourceDim);</traster64p,>
shun-iwasawa 481b59
  } else if (tile.getRaster()->getPixelSize() == 16) {
shun-iwasawa 481b59
    source_host =
shun-iwasawa 481b59
        reinterpret_cast<float4 *="">(sourceTile.getRaster()->getRawData());</float4>
shun-iwasawa 481b59
  } else {
shun-iwasawa 481b59
    throw TRopException("unsupported input pixel type");
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TDimensionI resultDim(rectOut.getLx(), anchorPoint.y);
Shinya Kitaoka 120a6e
  TRasterGR8P result_host_ras(resultDim.lx * sizeof(float4), resultDim.ly);
Shinya Kitaoka 120a6e
  /*- 結果を収めるメモリ -*/
Shinya Kitaoka 120a6e
  float4 *result_host;
Shinya Kitaoka 120a6e
  result_host_ras->lock();
Shinya Kitaoka 120a6e
  result_host = (float4 *)result_host_ras->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  doCompute_CPU(tile, frame, rend_sets, vanishingPoint, anchorPoint,
Shinya Kitaoka 120a6e
                source_host, result_host, sourceDim, resultDim, precision,
Shinya Kitaoka 120a6e
                offs);
Shinya Kitaoka 120a6e
shun-iwasawa 481b59
  if (source_host_ras) source_host_ras->unlock();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*- 出力結果をChannel値に変換して格納 -*/
Shinya Kitaoka 120a6e
  TRaster32P outRas32 = (TRaster32P)tile.getRaster();
Shinya Kitaoka 120a6e
  TRaster64P outRas64 = (TRaster64P)tile.getRaster();
shun-iwasawa 481b59
  TRasterFP outRasF   = (TRasterFP)tile.getRaster();
Shinya Kitaoka 120a6e
  if (outRas32)
Shinya Kitaoka 120a6e
    setOutputRaster<traster32p, tpixel32="">(result_host, outRas32, outDim,</traster32p,>
Shinya Kitaoka 120a6e
                                          resultDim.ly);
Shinya Kitaoka 120a6e
  else if (outRas64)
Shinya Kitaoka 120a6e
    setOutputRaster<traster64p, tpixel64="">(result_host, outRas64, outDim,</traster64p,>
Shinya Kitaoka 120a6e
                                          resultDim.ly);
shun-iwasawa 481b59
  else if (outRasF)
shun-iwasawa 481b59
    setOutputRaster<trasterfp, tpixelf="">(result_host, outRasF, outDim,</trasterfp,>
shun-iwasawa 481b59
                                        resultDim.ly);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  result_host_ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_PerspectiveDistortFx::doCompute_CPU(
Shinya Kitaoka 120a6e
    TTile &tile, const double frame, const TRenderSettings &settings,
Shinya Kitaoka 120a6e
    TPointD &vp, TPointD &ap, float4 *source_host, float4 *result_host,
Shinya Kitaoka 120a6e
    TDimensionI &sourceDim, TDimensionI &resultDim, const double precision,
Shinya Kitaoka 120a6e
    const double offs) {
Shinya Kitaoka 120a6e
  /*- 結果ポインタ -*/
Shinya Kitaoka 120a6e
  float4 *result_p = result_host;
Shinya Kitaoka 120a6e
  /*- 結果画像内でループ -*/
Shinya Kitaoka 120a6e
  for (int j = 0; j < resultDim.ly; j++) {
Shinya Kitaoka 120a6e
    /*- サンプルスタート地点 -*/
Shinya Kitaoka 120a6e
    double sampleX =
Shinya Kitaoka 120a6e
        precision * (vp.x * (ap.y - (double)j) / (vp.y - (double)j));
Shinya Kitaoka 120a6e
    /*- 1ピクセル横に移動した場合のサンプリング位置の移動量 -*/
Shinya Kitaoka 120a6e
    double dx = precision * (vp.y - ap.y) / (vp.y - (double)j);
Shinya Kitaoka 120a6e
    for (int i = 0; i < resultDim.lx; i++, result_p++, sampleX += dx) {
Shinya Kitaoka 120a6e
      int index    = (int)tfloor(sampleX);
Shinya Kitaoka 120a6e
      double ratio = sampleX - (double)index;
Shinya Kitaoka 120a6e
      (*result_p)  = interp_CPU(
Shinya Kitaoka 120a6e
          getSource_CPU(source_host, sourceDim, index, j),
Shinya Kitaoka 120a6e
          getSource_CPU(source_host, sourceDim, index + 1, j), ratio);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_PerspectiveDistortFx::getParamUIs(TParamUIConcept *&concepts,
Shinya Kitaoka 120a6e
                                           int &length) {
Shinya Kitaoka 120a6e
  concepts = new TParamUIConcept[length = 2];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  concepts[0].m_type  = TParamUIConcept::POINT;
Shinya Kitaoka 120a6e
  concepts[0].m_label = "Vanishing Point";
Shinya Kitaoka 120a6e
  concepts[0].m_params.push_back(m_vanishingPoint);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  concepts[1].m_type  = TParamUIConcept::POINT;
Shinya Kitaoka 120a6e
  concepts[1].m_label = "Anchor Point";
Shinya Kitaoka 120a6e
  concepts[1].m_params.push_back(m_anchorPoint);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 481b59
bool Iwa_PerspectiveDistortFx::toBeComputedInLinearColorSpace(
shun-iwasawa 481b59
    bool settingsIsLinear, bool tileIsLinear) const {
shun-iwasawa 481b59
  return tileIsLinear;
shun-iwasawa 481b59
}
shun-iwasawa 481b59
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(Iwa_PerspectiveDistortFx, "iwa_PerspectiveDistortFx")