Toshihiro Shimizu 890ddd
#include "iwa_adjustexposurefx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 ソース画像を0〜1に正規化してホストメモリに読み込む
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel="" raster,="" typename=""></typename>
Shinya Kitaoka 120a6e
void Iwa_AdjustExposureFx::setSourceRaster(const RASTER srcRas, float4 *dstMem,
Shinya Kitaoka 120a6e
                                           TDimensionI dim) {
Shinya Kitaoka 120a6e
  float4 *chann_p = dstMem;
Shinya Kitaoka 120a6e
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++) {
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
      pix++;
Shinya Kitaoka 120a6e
      chann_p++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*------------------------------------------------------------
Toshihiro Shimizu 890ddd
 出力結果をChannel値に変換してタイルに格納
Toshihiro Shimizu 890ddd
------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
template <typename pixel="" raster,="" typename=""></typename>
Shinya Kitaoka 120a6e
void Iwa_AdjustExposureFx::setOutputRaster(float4 *srcMem, const RASTER dstRas,
Shinya Kitaoka 120a6e
                                           TDimensionI dim) {
Shinya Kitaoka 120a6e
  float4 *chan_p = srcMem;
Shinya Kitaoka 120a6e
  for (int j = 0; j < dim.ly; j++) {
Shinya Kitaoka 120a6e
    PIXEL *pix = dstRas->pixels(j);
Shinya Kitaoka 120a6e
    for (int i = 0; i < dim.lx; i++) {
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
      pix++;
Shinya Kitaoka 120a6e
      chan_p++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Iwa_AdjustExposureFx::Iwa_AdjustExposureFx()
Shinya Kitaoka 120a6e
    : m_hardness(3.3), m_scale(0.0), m_offset(0.0) {
Shinya Kitaoka 120a6e
  addInputPort("Source", m_source);
Shinya Kitaoka 120a6e
  bindParam(this, "hardness", m_hardness, false);
Shinya Kitaoka 120a6e
  bindParam(this, "scale", m_scale, false);
Shinya Kitaoka 120a6e
  bindParam(this, "offset", m_offset, false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_hardness->setValueRange(0.05, 20.0);
Shinya Kitaoka 120a6e
  m_scale->setValueRange(-10.0, 10.0);
Shinya Kitaoka 120a6e
  m_offset->setValueRange(-0.5, 0.5);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_AdjustExposureFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                     const TRenderSettings &settings) {
Shinya Kitaoka 120a6e
  /*- Sourceが無ければ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
  m_source->compute(tile, frame, settings);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float4 *tile_host;
Shinya Kitaoka 120a6e
  TDimensionI dim(tile.getRaster()->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*- ホストメモリ確保 -*/
Shinya Kitaoka 120a6e
  TRasterGR8P tile_host_ras(sizeof(float4) * dim.lx, dim.ly);
Shinya Kitaoka 120a6e
  tile_host_ras->lock();
Shinya Kitaoka 120a6e
  tile_host = (float4 *)tile_host_ras->getRawData();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRaster32P ras32 = tile.getRaster();
Shinya Kitaoka 120a6e
  TRaster64P ras64 = tile.getRaster();
Shinya Kitaoka 120a6e
  if (ras32)
Shinya Kitaoka 120a6e
    setSourceRaster<traster32p, tpixel32="">(ras32, tile_host, dim);</traster32p,>
Shinya Kitaoka 120a6e
  else if (ras64)
Shinya Kitaoka 120a6e
    setSourceRaster<traster64p, tpixel64="">(ras64, tile_host, dim);</traster64p,>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  doCompute_CPU(tile, frame, settings, dim, tile_host);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  /*-  出力結果をチャンネル値に変換 -*/
Shinya Kitaoka 120a6e
  tile.getRaster()->clear();
Shinya Kitaoka 120a6e
  if (ras32)
Shinya Kitaoka 120a6e
    setOutputRaster<traster32p, tpixel32="">(tile_host, ras32, dim);</traster32p,>
Shinya Kitaoka 120a6e
  else if (ras64)
Shinya Kitaoka 120a6e
    setOutputRaster<traster64p, tpixel64="">(tile_host, ras64, dim);</traster64p,>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  tile_host_ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Iwa_AdjustExposureFx::doCompute_CPU(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                         const TRenderSettings &settings,
Shinya Kitaoka 120a6e
                                         TDimensionI &dim, float4 *tile_host) {
Shinya Kitaoka 120a6e
  float hardness = (float)m_hardness->getValue(frame);
Shinya Kitaoka 120a6e
  float scale    = (float)m_scale->getValue(frame);
Shinya Kitaoka 120a6e
  float offset   = (float)m_offset->getValue(frame);
Shinya Kitaoka 120a6e
Rozhuk Ivan 823a31
  float exposureOffset = (powf(10.0f, (float)(std::abs(offset) / hardness)) - 1.0f) *
Shinya Kitaoka 120a6e
                         ((offset < 0.0f) ? -1.0f : 1.0f);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  float4 *pix = tile_host;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int size = dim.lx * dim.ly;
Shinya Kitaoka 120a6e
  for (int i = 0; i < size; i++, pix++) {
Shinya Kitaoka 120a6e
    /*- RGB->Exposure -*/
Shinya Kitaoka 120a6e
    pix->x = powf(10, (pix->x - 0.5f) * hardness);
Shinya Kitaoka 120a6e
    pix->y = powf(10, (pix->y - 0.5f) * hardness);
Shinya Kitaoka 120a6e
    pix->z = powf(10, (pix->z - 0.5f) * hardness);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*- スケール -*/
Shinya Kitaoka 120a6e
    pix->x *= powf(10, scale);
Shinya Kitaoka 120a6e
    pix->y *= powf(10, scale);
Shinya Kitaoka 120a6e
    pix->z *= powf(10, scale);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*- オフセット -*/
Shinya Kitaoka 120a6e
    pix->x += exposureOffset;
Shinya Kitaoka 120a6e
    pix->y += exposureOffset;
Shinya Kitaoka 120a6e
    pix->z += exposureOffset;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*- Exposure->RGB -*/
Shinya Kitaoka 120a6e
    pix->x = log10f(pix->x) / hardness + 0.5f;
Shinya Kitaoka 120a6e
    pix->y = log10f(pix->y) / hardness + 0.5f;
Shinya Kitaoka 120a6e
    pix->z = log10f(pix->z) / hardness + 0.5f;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    /*- クランプ -*/
Shinya Kitaoka 120a6e
    pix->x = (pix->x > 1.0f) ? 1.0f : ((pix->x < 0.0f) ? 0.0f : pix->x);
Shinya Kitaoka 120a6e
    pix->y = (pix->y > 1.0f) ? 1.0f : ((pix->y < 0.0f) ? 0.0f : pix->y);
Shinya Kitaoka 120a6e
    pix->z = (pix->z > 1.0f) ? 1.0f : ((pix->z < 0.0f) ? 0.0f : pix->z);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool Iwa_AdjustExposureFx::doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 120a6e
                                     const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  if (m_source.isConnected()) {
Shinya Kitaoka 120a6e
    bool ret = m_source->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
    return ret;
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    bBox = TRectD();
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool Iwa_AdjustExposureFx::canHandle(const TRenderSettings &info,
Shinya Kitaoka 120a6e
                                     double frame) {
Shinya Kitaoka 120a6e
  return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(Iwa_AdjustExposureFx, "iwa_AdjustExposureFx")