shun-iwasawa 021eef
#include "iwa_bloomfx.h"
shun-iwasawa 021eef
shun-iwasawa 021eef
#include "tparamuiconcept.h"
shun-iwasawa 021eef
shun-iwasawa 021eef
#include <qvector></qvector>
shun-iwasawa 021eef
#include <qpair></qpair>
shun-iwasawa 021eef
shun-iwasawa 021eef
namespace {
shun-iwasawa 021eef
// convert sRGB color space to power space
shun-iwasawa 021eef
template <typename t="double"></typename>
shun-iwasawa 021eef
inline T to_linear_color_space(T nonlinear_color, T exposure, T gamma) {
shun-iwasawa 021eef
  return std::pow(nonlinear_color, gamma) / exposure;
shun-iwasawa 021eef
}
shun-iwasawa 021eef
// convert power space to sRGB color space
shun-iwasawa 021eef
template <typename t="double"></typename>
shun-iwasawa 021eef
inline T to_nonlinear_color_space(T linear_color, T exposure, T gamma) {
shun-iwasawa 021eef
  return std::pow(linear_color * exposure, T(1) / gamma);
shun-iwasawa 021eef
}
shun-iwasawa 021eef
template <class t="double"></class>
shun-iwasawa 021eef
const T &clamp(const T &v, const T &lo, const T &hi) {
shun-iwasawa 021eef
  assert(!(hi < lo));
shun-iwasawa 021eef
  return (v < lo) ? lo : (hi < v) ? hi : v;
shun-iwasawa 021eef
}
shun-iwasawa 021eef
shun-iwasawa 021eef
void blurByRotate(cv::Mat &mat) {
shun-iwasawa 021eef
  double angle = 45.0;
shun-iwasawa 021eef
shun-iwasawa 021eef
  int size   = std::ceil(std::sqrt(mat.cols * mat.cols + mat.rows * mat.rows));
shun-iwasawa 021eef
  int width  = ((size - mat.cols) % 2 == 0) ? size : size + 1;
shun-iwasawa 021eef
  int height = ((size - mat.rows) % 2 == 0) ? size : size + 1;
shun-iwasawa 021eef
shun-iwasawa 021eef
  cv::Point2f center((mat.cols - 1) / 2.0, (mat.rows - 1) / 2.0);
shun-iwasawa 021eef
shun-iwasawa 021eef
  cv::Mat rot = cv::getRotationMatrix2D(center, angle, 1.0);
shun-iwasawa 021eef
  rot.at<double>(0, 2) += (width - mat.cols) / 2.0;</double>
shun-iwasawa 021eef
  rot.at<double>(1, 2) += (height - mat.rows) / 2.0;</double>
shun-iwasawa 021eef
shun-iwasawa 021eef
  cv::Mat tmp;
shun-iwasawa 021eef
  cv::warpAffine(mat, tmp, rot, cv::Size(width, height));
shun-iwasawa 021eef
shun-iwasawa 021eef
  center = cv::Point2f((width - 1) / 2.0, (height - 1) / 2.0);
shun-iwasawa 021eef
  rot    = cv::getRotationMatrix2D(center, -angle, 1.0);
shun-iwasawa 021eef
  rot.at<double>(0, 2) += (mat.cols - width) / 2.0;</double>
shun-iwasawa 021eef
  rot.at<double>(1, 2) += (mat.rows - height) / 2.0;</double>
shun-iwasawa 021eef
shun-iwasawa 021eef
  cv::warpAffine(tmp, mat, rot, mat.size());
shun-iwasawa 021eef
}
shun-iwasawa 021eef
shun-iwasawa 021eef
}  // namespace
shun-iwasawa 021eef
shun-iwasawa 021eef
//--------------------------------------------
shun-iwasawa 021eef
// Iwa_BloomFx
shun-iwasawa 021eef
//--------------------------------------------
shun-iwasawa 021eef
Iwa_BloomFx::Iwa_BloomFx()
shun-iwasawa 021eef
    : m_gamma(2.2), m_gain(2.0), m_size(100.0), m_alpha_rendering(false) {
shun-iwasawa 021eef
  addInputPort("Source", m_source);
shun-iwasawa 021eef
  bindParam(this, "gamma", m_gamma);
shun-iwasawa 021eef
  bindParam(this, "gain", m_gain);
shun-iwasawa 021eef
  bindParam(this, "size", m_size);
shun-iwasawa 021eef
  bindParam(this, "alpha_rendering", m_alpha_rendering);
shun-iwasawa 021eef
shun-iwasawa 021eef
  m_gamma->setValueRange(0.1, 5.0);
shun-iwasawa 021eef
  m_gain->setValueRange(0.1, 10.0);
shun-iwasawa 021eef
  m_size->setValueRange(0.1, 1024.0);
shun-iwasawa 021eef
shun-iwasawa 021eef
  m_size->setMeasureName("fxLength");
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
double Iwa_BloomFx::getSizePixelAmount(const double val, const TAffine affine) {
shun-iwasawa 021eef
  /*--- Convert to vector --- */
shun-iwasawa 021eef
  TPointD vect;
shun-iwasawa 021eef
  vect.x = val;
shun-iwasawa 021eef
  vect.y = 0.0;
shun-iwasawa 021eef
  /*--- Apply geometrical transformation ---*/
shun-iwasawa 021eef
  // For the following lines I referred to lines 586-592 of
shun-iwasawa 021eef
  // sources/stdfx/motionblurfx.cpp
shun-iwasawa 021eef
  TAffine aff(affine);
shun-iwasawa 021eef
  aff.a13 = aff.a23 = 0; /* ignore translation */
shun-iwasawa 021eef
  vect              = aff * vect;
shun-iwasawa 021eef
  /*--- return the length of the vector ---*/
shun-iwasawa 021eef
  return sqrt(vect.x * vect.x + vect.y * vect.y);
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
template <typename pixel="" raster,="" typename=""></typename>
shun-iwasawa 021eef
void Iwa_BloomFx::setSourceTileToMat(const RASTER ras, cv::Mat &imgMat,
shun-iwasawa 021eef
                                     const double gamma) {
shun-iwasawa 021eef
  double maxi = static_cast<double>(PIXEL::maxChannelValue);  // 255or65535</double>
shun-iwasawa 021eef
  for (int j = 0; j < ras->getLy(); j++) {
shun-iwasawa 021eef
    const PIXEL *pix = ras->pixels(j);
shun-iwasawa 021eef
    cv::Vec3f *mat_p = imgMat.ptr<cv::vec3f>(j);</cv::vec3f>
shun-iwasawa 021eef
    for (int i = 0; i < ras->getLx(); i++, pix++, mat_p++) {
shun-iwasawa 021eef
      double pix_a = static_cast<double>(pix->m) / maxi;</double>
shun-iwasawa 021eef
      if (pix_a <= 0.0) {
shun-iwasawa 021eef
        *mat_p = cv::Vec3f(0, 0, 0);
shun-iwasawa 021eef
        continue;
shun-iwasawa 021eef
      }
shun-iwasawa 021eef
      double bgra[3];
shun-iwasawa 021eef
      bgra[0] = static_cast<double>(pix->b) / maxi;</double>
shun-iwasawa 021eef
      bgra[1] = static_cast<double>(pix->g) / maxi;</double>
shun-iwasawa 021eef
      bgra[2] = static_cast<double>(pix->r) / maxi;</double>
shun-iwasawa 021eef
      for (int c = 0; c < 3; c++) {
shun-iwasawa 021eef
        // assuming that the source image is premultiplied
shun-iwasawa 021eef
        bgra[c] = to_linear_color_space(bgra[c] / pix_a, 1.0, gamma) * pix_a;
shun-iwasawa 021eef
      }
shun-iwasawa 021eef
      *mat_p = cv::Vec3f(bgra[0], bgra[1], bgra[2]);
shun-iwasawa 021eef
    }
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
template <typename pixel="" raster,="" typename=""></typename>
shun-iwasawa 021eef
void Iwa_BloomFx::setMatToOutput(const RASTER ras, const RASTER srcRas,
shun-iwasawa 021eef
                                 cv::Mat &ingMat, const double gamma,
shun-iwasawa 021eef
                                 const double gain, const bool withAlpha,
shun-iwasawa 021eef
                                 const int margin) {
shun-iwasawa 021eef
  double maxi = static_cast<double>(PIXEL::maxChannelValue);  // 255or65535</double>
shun-iwasawa 021eef
  for (int j = 0; j < ras->getLy(); j++) {
shun-iwasawa 021eef
    cv::Vec3f const *mat_p = ingMat.ptr<cv::vec3f>(j);</cv::vec3f>
shun-iwasawa 021eef
    PIXEL *pix             = ras->pixels(j);
shun-iwasawa 021eef
    PIXEL *srcPix          = srcRas->pixels(j + margin) + margin;
shun-iwasawa 021eef
shun-iwasawa 021eef
    for (int i = 0; i < ras->getLx(); i++, pix++, srcPix++, mat_p++) {
shun-iwasawa 021eef
      double nonlinear_b =
shun-iwasawa 021eef
          to_nonlinear_color_space((double)(*mat_p)[0] * gain, 1.0, gamma);
shun-iwasawa 021eef
      double nonlinear_g =
shun-iwasawa 021eef
          to_nonlinear_color_space((double)(*mat_p)[1] * gain, 1.0, gamma);
shun-iwasawa 021eef
      double nonlinear_r =
shun-iwasawa 021eef
          to_nonlinear_color_space((double)(*mat_p)[2] * gain, 1.0, gamma);
shun-iwasawa 021eef
shun-iwasawa 021eef
      nonlinear_b = clamp(nonlinear_b, 0.0, 1.0);
shun-iwasawa 021eef
      nonlinear_g = clamp(nonlinear_g, 0.0, 1.0);
shun-iwasawa 021eef
      nonlinear_r = clamp(nonlinear_r, 0.0, 1.0);
shun-iwasawa 021eef
shun-iwasawa 021eef
      pix->r = (typename PIXEL::Channel)(nonlinear_r * (maxi + 0.999999));
shun-iwasawa 021eef
      pix->g = (typename PIXEL::Channel)(nonlinear_g * (maxi + 0.999999));
shun-iwasawa 021eef
      pix->b = (typename PIXEL::Channel)(nonlinear_b * (maxi + 0.999999));
shun-iwasawa 021eef
      if (withAlpha) {
shun-iwasawa 021eef
        double chan_a =
shun-iwasawa 021eef
            std::max(std::max(nonlinear_b, nonlinear_g), nonlinear_r);
shun-iwasawa 021eef
        pix->m = std::max((typename PIXEL::Channel)(chan_a * (maxi + 0.999999)),
shun-iwasawa 021eef
                          srcPix->m);
shun-iwasawa 021eef
      } else
shun-iwasawa 021eef
        pix->m = (typename PIXEL::Channel)(PIXEL::maxChannelValue);
shun-iwasawa 021eef
    }
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
}
shun-iwasawa 021eef
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
void Iwa_BloomFx::doCompute(TTile &tile, double frame,
shun-iwasawa 021eef
                            const TRenderSettings &settings) {
shun-iwasawa 021eef
  // If the source is not connected, then do nothing
shun-iwasawa 021eef
  if (!m_source.isConnected()) {
shun-iwasawa 021eef
    tile.getRaster()->clear();
shun-iwasawa 021eef
    return;
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
  // obtain parameters
shun-iwasawa 021eef
  double gamma = m_gamma->getValue(frame);
shun-iwasawa 021eef
  double gain  = m_gain->getValue(frame);
shun-iwasawa 021eef
  double size  = getSizePixelAmount(m_size->getValue(frame), settings.m_affine);
shun-iwasawa 021eef
  bool withAlpha = m_alpha_rendering->getValue();
shun-iwasawa 021eef
shun-iwasawa 021eef
  int margin = static_cast<int>(std::ceil(size));</int>
shun-iwasawa 021eef
  TRectD _rect(tile.m_pos, TDimensionD(tile.getRaster()->getLx(),
shun-iwasawa 021eef
                                       tile.getRaster()->getLy()));
shun-iwasawa 021eef
  _rect = _rect.enlarge(static_cast<double>(margin));</double>
shun-iwasawa 021eef
  TDimensionI dimSrc(static_cast<int>(_rect.getLx() + 0.5),</int>
shun-iwasawa 021eef
                     static_cast<int>(_rect.getLy() + 0.5));</int>
shun-iwasawa 021eef
shun-iwasawa 021eef
  // obtain the source tile
shun-iwasawa 021eef
  TTile sourceTile;
shun-iwasawa 021eef
  m_source->allocateAndCompute(sourceTile, _rect.getP00(), dimSrc,
shun-iwasawa 021eef
                               tile.getRaster(), frame, settings);
shun-iwasawa 021eef
shun-iwasawa 021eef
  // set the source image to cvMat, converting to linear color space
shun-iwasawa 021eef
  cv::Mat imgMat(cv::Size(dimSrc.lx, dimSrc.ly), CV_32FC3);
shun-iwasawa 021eef
  TRaster32P ras32 = tile.getRaster();
shun-iwasawa 021eef
  TRaster64P ras64 = tile.getRaster();
shun-iwasawa 021eef
  if (ras32)
shun-iwasawa 021eef
    setSourceTileToMat<traster32p, tpixel32="">(sourceTile.getRaster(), imgMat,</traster32p,>
shun-iwasawa 021eef
                                             gamma);
shun-iwasawa 021eef
  else if (ras64)
shun-iwasawa 021eef
    setSourceTileToMat<traster64p, tpixel64="">(sourceTile.getRaster(), imgMat,</traster64p,>
shun-iwasawa 021eef
                                             gamma);
shun-iwasawa 021eef
shun-iwasawa 021eef
  // compute size and intensity ratios of resampled layers
shun-iwasawa 021eef
  // resample size is reduced from the specified size, taking into account
shun-iwasawa 021eef
  // that the gaussian blur (x 2) and the blur by rotation resampling (x sqrt2)
shun-iwasawa 021eef
  double no_blur_size = size / (2 * 1.5);
shun-iwasawa 021eef
  // find the mimimum "power of 2" value which is the same as or larger than the
shun-iwasawa 021eef
  // filter size
shun-iwasawa 021eef
  int level         = 1;
shun-iwasawa 021eef
  double power_of_2 = 1.0;
shun-iwasawa 021eef
  while (1) {
shun-iwasawa 021eef
    if (power_of_2 >= no_blur_size) break;
shun-iwasawa 021eef
    level++;
shun-iwasawa 021eef
    power_of_2 *= 2.0;
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
  // store the size of resampled layers
shun-iwasawa 021eef
  QVector<cv::size> sizes;</cv::size>
shun-iwasawa 021eef
  double tmp_filterSize = no_blur_size;
shun-iwasawa 021eef
  double width          = static_cast<double>(imgMat.size().width);</double>
shun-iwasawa 021eef
  double height         = static_cast<double>(imgMat.size().height);</double>
shun-iwasawa 021eef
  for (int lvl = 0; lvl < level - 1; lvl++) {
shun-iwasawa 021eef
    int tmp_w = static_cast<int>(std::ceil(width / tmp_filterSize));</int>
shun-iwasawa 021eef
    int tmp_h = static_cast<int>(std::ceil(height / tmp_filterSize));</int>
shun-iwasawa 021eef
    sizes.push_front(cv::Size(tmp_w, tmp_h));
shun-iwasawa 021eef
    tmp_filterSize *= 0.5;
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
  sizes.push_front(imgMat.size());
shun-iwasawa 021eef
shun-iwasawa 021eef
  // the filter is based on the nearest power-of-2 sized one with an adjustment
shun-iwasawa 021eef
  // reducing the sizes and increasing the intensity with this ratio
shun-iwasawa 021eef
  double ratio = power_of_2 / no_blur_size;
shun-iwasawa 021eef
  // base filter sizes will be 1, 2, 4, ... 2^(level-1)
shun-iwasawa 021eef
  // intensity of the filter with sizes > 2
shun-iwasawa 021eef
  double intensity_all = power_of_2 / (power_of_2 * 2.0 - 1.0);
shun-iwasawa 021eef
  // intensity of the filter with size 1, so that the amount of the filter at
shun-iwasawa 021eef
  // the center point is always 1.0
shun-iwasawa 021eef
  double intensity_front = 1.0 - (1.0 - intensity_all) * ratio;
shun-iwasawa 021eef
shun-iwasawa 021eef
  std::vector<cv::mat> dst(level);</cv::mat>
shun-iwasawa 021eef
  cv::Size const ksize(3, 3);
shun-iwasawa 021eef
shun-iwasawa 021eef
  cv::Mat tmp;
shun-iwasawa 021eef
  int i;
shun-iwasawa 021eef
  // for each level of filter (from larger to smaller)
shun-iwasawa 021eef
  for (i = 0; i < level;) {
shun-iwasawa 021eef
    // scaling down the size
shun-iwasawa 021eef
    if (i) {
shun-iwasawa 021eef
      cv::resize(imgMat, tmp, sizes[i], 0.0, 0.0, cv::INTER_AREA);
shun-iwasawa 021eef
      imgMat = tmp;
shun-iwasawa 021eef
    }
shun-iwasawa 021eef
    // gaussian blur
shun-iwasawa 021eef
    cv::GaussianBlur(imgMat, dst[i], ksize, 0.0);
shun-iwasawa 021eef
    ++i;
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
  // for each level of filter (from smaller to larger)
shun-iwasawa 021eef
  for (--i; i > 0; --i) {
shun-iwasawa 021eef
    // scaling up the size
shun-iwasawa 021eef
    cv::resize(dst[i], tmp, dst[i - 1].size());
shun-iwasawa 021eef
    // blur by rotational resampling in order to reduce box-shaped artifact
shun-iwasawa 021eef
    blurByRotate(tmp);
shun-iwasawa 021eef
    // add to the upper resampled image
shun-iwasawa 021eef
    if (i > 1)
shun-iwasawa 021eef
      dst[i - 1] += tmp;
shun-iwasawa 021eef
    else
shun-iwasawa 021eef
      imgMat = dst[0] * intensity_front + tmp * intensity_all;
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
shun-iwasawa 021eef
  // get the subimage without margin
shun-iwasawa 021eef
  cv::Rect roi(cv::Point(margin, margin),
shun-iwasawa 021eef
               cv::Size(tile.getRaster()->getLx(), tile.getRaster()->getLy()));
shun-iwasawa 021eef
  imgMat = imgMat(roi);
shun-iwasawa 021eef
shun-iwasawa 021eef
  // set the result to the tile, converting to rgb channel values
shun-iwasawa 021eef
  if (ras32)
shun-iwasawa 021eef
    setMatToOutput<traster32p, tpixel32="">(tile.getRaster(),</traster32p,>
shun-iwasawa 021eef
                                         sourceTile.getRaster(), imgMat, gamma,
shun-iwasawa 021eef
                                         gain, withAlpha, margin);
shun-iwasawa 021eef
  else if (ras64)
shun-iwasawa 021eef
    setMatToOutput<traster64p, tpixel64="">(tile.getRaster(),</traster64p,>
shun-iwasawa 021eef
                                         sourceTile.getRaster(), imgMat, gamma,
shun-iwasawa 021eef
                                         gain, withAlpha, margin);
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
bool Iwa_BloomFx::doGetBBox(double frame, TRectD &bBox,
shun-iwasawa 021eef
                            const TRenderSettings &info) {
shun-iwasawa 021eef
  if (!m_source.isConnected()) {
shun-iwasawa 021eef
    bBox = TRectD();
shun-iwasawa 021eef
    return false;
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
  bool ret   = m_source->doGetBBox(frame, bBox, info);
shun-iwasawa 021eef
  int margin = static_cast<int>(</int>
shun-iwasawa 021eef
      std::ceil(getSizePixelAmount(m_size->getValue(frame), info.m_affine)));
shun-iwasawa 021eef
  if (margin > 0) {
shun-iwasawa 021eef
    bBox = bBox.enlarge(static_cast<double>(margin));</double>
shun-iwasawa 021eef
  }
shun-iwasawa 021eef
  return ret;
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
bool Iwa_BloomFx::canHandle(const TRenderSettings &info, double frame) {
shun-iwasawa 021eef
  return false;
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
void Iwa_BloomFx::getParamUIs(TParamUIConcept *&concepts, int &length) {
shun-iwasawa 021eef
  concepts = new TParamUIConcept[length = 1];
shun-iwasawa 021eef
shun-iwasawa 021eef
  concepts[0].m_type  = TParamUIConcept::RADIUS;
shun-iwasawa 021eef
  concepts[0].m_label = "Size";
shun-iwasawa 021eef
  concepts[0].m_params.push_back(m_size);
shun-iwasawa 021eef
}
shun-iwasawa 021eef
//------------------------------------------------
shun-iwasawa 021eef
shun-iwasawa 021eef
FX_PLUGIN_IDENTIFIER(Iwa_BloomFx, "iwa_BloomFx")