Shinya Kitaoka 6aada0
#include <memory></memory>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tpixel.h"
Toshihiro Shimizu 890ddd
#include <stdio.h></stdio.h>
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include <stdlib.h></stdlib.h>
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include <assert.h></assert.h>
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tgeometry.h"
Toshihiro Shimizu 890ddd
#include "tfxattributes.h"
Toshihiro Shimizu 890ddd
#include "tparamuiconcept.h"
Toshihiro Shimizu 890ddd
//#include "timage_io.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
template<class t=""></class>
Toshihiro Shimizu 890ddd
void overlayPixels(const T& dw, const T& up, T& ris, double glob)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
int value;
Toshihiro Shimizu 890ddd
double aux;
Toshihiro Shimizu 890ddd
int max = T::maxChannelValue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
if (up.m == 0)
Toshihiro Shimizu 890ddd
  ris = dw;
Shinya Kitaoka 120a6e
else if ((up.m == max)&&(glob==1))
Toshihiro Shimizu 890ddd
  ris = up;
Toshihiro Shimizu 890ddd
else if (glob==1)
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  aux = (max-up.m)/max;
Toshihiro Shimizu 890ddd
  value = up.r + troundp(dw.r * aux);
Toshihiro Shimizu 890ddd
  ris.r = (value 
Toshihiro Shimizu 890ddd
  value = up.g + troundp(dw.g * aux);
Toshihiro Shimizu 890ddd
  ris.g = (value 
Toshihiro Shimizu 890ddd
  value = up.b + troundp(dw.b * aux);
Toshihiro Shimizu 890ddd
  ris.b = (value 
Toshihiro Shimizu 890ddd
  value = up.m + dw.m;
Toshihiro Shimizu 890ddd
  ris.m = (value 
Toshihiro Shimizu 890ddd
  }
Toshihiro Shimizu 890ddd
else
Toshihiro Shimizu 890ddd
  {
Toshihiro Shimizu 890ddd
  aux = (max-up.m*glob)/max;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
  value = (int)(up.r*glob) + troundp(dw.r * aux);
Toshihiro Shimizu 890ddd
  ris.r = (value 
Toshihiro Shimizu 890ddd
  value = (int)(up.g*glob) + troundp(dw.g * aux);
Toshihiro Shimizu 890ddd
  ris.g = (value 
Toshihiro Shimizu 890ddd
  value = (int)(up.b*glob) + troundp(dw.b * aux);
Toshihiro Shimizu 890ddd
  ris.b = (value 
Toshihiro Shimizu 890ddd
  value = (int)(up.m*glob) + dw.m;
Toshihiro Shimizu 890ddd
  ris.m = (value 
Shinya Kitaoka 120a6e
        }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
inline void blur_code(T *row1, T *row2, int length, double coeff, double coeffq,
Shinya Kitaoka 120a6e
                      int brad, double diff, double globmatte) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  double rsum, gsum, bsum, msum;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPixelD sigma1, sigma2, sigma3, desigma;
Shinya Kitaoka 120a6e
  T *pix1, *pix2, *pix3, *pix4;
Shinya Kitaoka 120a6e
  int max = T::maxChannelValue;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pix1 = row1;
Shinya Kitaoka 120a6e
  pix2 = row1 - 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  sigma1.r = pix1->r;
Shinya Kitaoka 120a6e
  sigma1.g = pix1->g;
Shinya Kitaoka 120a6e
  sigma1.b = pix1->b;
Shinya Kitaoka 120a6e
  sigma1.m = pix1->m;
Shinya Kitaoka 120a6e
  pix1++;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  sigma2.r = sigma2.g = sigma2.b = sigma2.m = 0.0;
Shinya Kitaoka 120a6e
  sigma3.r = sigma3.g = sigma3.b = sigma3.m = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 1; i < brad; i++) {
Shinya Kitaoka 120a6e
    sigma1.r += pix1->r;
Shinya Kitaoka 120a6e
    sigma1.g += pix1->g;
Shinya Kitaoka 120a6e
    sigma1.b += pix1->b;
Shinya Kitaoka 120a6e
    sigma1.m += pix1->m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    sigma2.r += pix2->r;
Shinya Kitaoka 120a6e
    sigma2.g += pix2->g;
Shinya Kitaoka 120a6e
    sigma2.b += pix2->b;
Shinya Kitaoka 120a6e
    sigma2.m += pix2->m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    sigma3.r += i * (pix1->r + pix2->r);
Shinya Kitaoka 120a6e
    sigma3.g += i * (pix1->g + pix2->g);
Shinya Kitaoka 120a6e
    sigma3.b += i * (pix1->b + pix2->b);
Shinya Kitaoka 120a6e
    sigma3.m += i * (pix1->m + pix2->m);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    pix1++;
Shinya Kitaoka 120a6e
    pix2--;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  rsum = (sigma1.r + sigma2.r) * coeff - sigma3.r * coeffq + 0.5;
Shinya Kitaoka 120a6e
  gsum = (sigma1.g + sigma2.g) * coeff - sigma3.g * coeffq + 0.5;
Shinya Kitaoka 120a6e
  bsum = (sigma1.b + sigma2.b) * coeff - sigma3.b * coeffq + 0.5;
Shinya Kitaoka 120a6e
  msum = (sigma1.m + sigma2.m) * coeff - sigma3.m * coeffq + 0.5;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  row2->r = tcrop((int)rsum, 0, max);
Shinya Kitaoka 120a6e
  row2->g = tcrop((int)gsum, 0, max);
Shinya Kitaoka 120a6e
  row2->b = tcrop((int)bsum, 0, max);
Shinya Kitaoka 120a6e
  row2->m = tcrop((int)msum, 0, max);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (globmatte != 1.0) {
Shinya Kitaoka 120a6e
    row2->r = (int)(row2->r * globmatte);
Shinya Kitaoka 120a6e
    row2->g = (int)(row2->g * globmatte);
Shinya Kitaoka 120a6e
    row2->b = (int)(row2->b * globmatte);
Shinya Kitaoka 120a6e
    row2->m = (int)(row2->m * globmatte);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  *row2 = overPix(*row1, *row2);
Shinya Kitaoka 120a6e
  // overlayPixels<t>(*row1, *row2, *row2, globmatte);</t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // row2++;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  sigma2.r += row1[-brad].r;
Shinya Kitaoka 120a6e
  sigma2.g += row1[-brad].g;
Shinya Kitaoka 120a6e
  sigma2.b += row1[-brad].b;
Shinya Kitaoka 120a6e
  sigma2.m += row1[-brad].m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  pix1 = row1 + brad;
Shinya Kitaoka 120a6e
  pix2 = row1;
Shinya Kitaoka 120a6e
  pix3 = row1 - brad;
Shinya Kitaoka 120a6e
  pix4 = row1 - brad + 1;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  desigma.r = sigma1.r - sigma2.r;
Shinya Kitaoka 120a6e
  desigma.g = sigma1.g - sigma2.g;
Shinya Kitaoka 120a6e
  desigma.b = sigma1.b - sigma2.b;
Shinya Kitaoka 120a6e
  desigma.m = sigma1.m - sigma2.m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 1; i < length; i++) {
Shinya Kitaoka 120a6e
    desigma.r += pix1->r - 2 * pix2->r + pix3->r;
Shinya Kitaoka 120a6e
    desigma.g += pix1->g - 2 * pix2->g + pix3->g;
Shinya Kitaoka 120a6e
    desigma.b += pix1->b - 2 * pix2->b + pix3->b;
Shinya Kitaoka 120a6e
    desigma.m += pix1->m - 2 * pix2->m + pix3->m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    rsum += (desigma.r + diff * (pix1->r - pix4->r)) * coeffq;
Shinya Kitaoka 120a6e
    gsum += (desigma.g + diff * (pix1->g - pix4->g)) * coeffq;
Shinya Kitaoka 120a6e
    bsum += (desigma.b + diff * (pix1->b - pix4->b)) * coeffq;
Shinya Kitaoka 120a6e
    msum += (desigma.m + diff * (pix1->m - pix4->m)) * coeffq;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    row2->r = tcrop((int)rsum, 0, max);
Shinya Kitaoka 120a6e
    row2->g = tcrop((int)gsum, 0, max);
Shinya Kitaoka 120a6e
    row2->b = tcrop((int)bsum, 0, max);
Shinya Kitaoka 120a6e
    row2->m = tcrop((int)msum, 0, max);
Shinya Kitaoka 120a6e
    if (globmatte != 1.0) {
Shinya Kitaoka 120a6e
      row2->r = (int)(row2->r * globmatte);
Shinya Kitaoka 120a6e
      row2->g = (int)(row2->g * globmatte);
Shinya Kitaoka 120a6e
      row2->b = (int)(row2->b * globmatte);
Shinya Kitaoka 120a6e
      row2->m = (int)(row2->m * globmatte);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    *row2 = overPix(*pix2, *row2);
Shinya Kitaoka 120a6e
    // overlayPixels<t>(*pix2, *row2, *row2, 0.8);</t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    row2++;
Shinya Kitaoka 120a6e
    pix1++, pix2++, pix3++, pix4++;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void do_filtering(T *row1, T *row2, int length, double coeff, int brad,
Shinya Kitaoka 120a6e
                  double Mblur, double globmatte) {
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  double rsum, gsum, bsum, msum;
Shinya Kitaoka 120a6e
  TPixelD sigma1, sigma2;
Shinya Kitaoka 120a6e
  int max = T::maxChannelValue;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  sigma1.r = sigma1.g = sigma1.b = sigma1.m = 0.0;
Shinya Kitaoka 120a6e
  sigma2.r = sigma2.g = sigma2.b = sigma2.m = 0.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i <= brad; i++) {
Shinya Kitaoka 120a6e
    sigma1.r += row1[-i].r;
Shinya Kitaoka 120a6e
    sigma1.g += row1[-i].g;
Shinya Kitaoka 120a6e
    sigma1.b += row1[-i].b;
Shinya Kitaoka 120a6e
    sigma1.m += row1[-i].m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    sigma2.r += -i * row1[-i].r;
Shinya Kitaoka 120a6e
    sigma2.g += -i * row1[-i].g;
Shinya Kitaoka 120a6e
    sigma2.b += -i * row1[-i].b;
Shinya Kitaoka 120a6e
    sigma2.m += -i * row1[-i].m;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  for (i = 0; i < length; i++) /* for the ith point the previous computing is
Shinya Kitaoka 120a6e
                                  used, with the values */
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    /* stored in the auxiliar variables sigma1 and sigma2. */
Shinya Kitaoka 120a6e
    rsum = ((Mblur - i) * sigma1.r + sigma2.r) / coeff;
Shinya Kitaoka 120a6e
    gsum = ((Mblur - i) * sigma1.g + sigma2.g) / coeff;
Shinya Kitaoka 120a6e
    bsum = ((Mblur - i) * sigma1.b + sigma2.b) / coeff;
Shinya Kitaoka 120a6e
    msum = ((Mblur - i) * sigma1.m + sigma2.m) / coeff;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    row2[i].r = (rsum < max) ? troundp(rsum) : max;
Shinya Kitaoka 120a6e
    row2[i].g = (gsum < max) ? troundp(gsum) : max;
Shinya Kitaoka 120a6e
    row2[i].b = (bsum < max) ? troundp(bsum) : max;
Shinya Kitaoka 120a6e
    row2[i].m = (msum < max) ? troundp(msum) : max;
Shinya Kitaoka 120a6e
    if (globmatte != 1.0) {
Shinya Kitaoka 120a6e
      row2[i].r = (int)(row2[i].r * globmatte);
Shinya Kitaoka 120a6e
      row2[i].g = (int)(row2[i].g * globmatte);
Shinya Kitaoka 120a6e
      row2[i].b = (int)(row2[i].b * globmatte);
Shinya Kitaoka 120a6e
      row2[i].m = (int)(row2[i].m * globmatte);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    row2[i] = overPix(row1[i], row2[i]);
Shinya Kitaoka 120a6e
    // overlayPixels<t>(row1[i], row2[i], row2[i], globmatte);</t>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (i < length - 1) {
Shinya Kitaoka 120a6e
      sigma1.r += row1[i + 1].r - row1[i - brad].r;
Shinya Kitaoka 120a6e
      sigma1.g += row1[i + 1].g - row1[i - brad].g;
Shinya Kitaoka 120a6e
      sigma1.b += row1[i + 1].b - row1[i - brad].b;
Shinya Kitaoka 120a6e
      sigma1.m += row1[i + 1].m - row1[i - brad].m;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      sigma2.r += (i + 1) * row2[i + 1].r - (i - brad) * row1[i - brad].r;
Shinya Kitaoka 120a6e
      sigma2.g += (i + 1) * row2[i + 1].g - (i - brad) * row1[i - brad].g;
Shinya Kitaoka 120a6e
      sigma2.b += (i + 1) * row2[i + 1].b - (i - brad) * row1[i - brad].b;
Shinya Kitaoka 120a6e
      sigma2.m += (i + 1) * row2[i + 1].m - (i - brad) * row1[i - brad].m;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void takeRow(T *rin, T *row, int lx, int brad, bool bidirectional) {
Shinya Kitaoka 120a6e
  int i;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (i = 0; i < lx; i++) row[i] = rin[i];
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  for (i = -1; i > -brad;
Shinya Kitaoka 120a6e
       i--) /* pixels equal to the ones of border of image are added   */
Shinya Kitaoka 120a6e
    row[i] = row[0]; /* to avoid a black blur to get into the picture. */
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (bidirectional)
Shinya Kitaoka 120a6e
    for (i = lx; i < lx + brad;
Shinya Kitaoka 120a6e
         i++) /* pixels equal to the ones of border of image are added   */
Shinya Kitaoka 120a6e
      row[i] = row[lx - 1]; /* to avoid a black blur to get into the picture. */
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void doDirectionalBlur(TRasterPT<t> r, double blur, bool bidirectional) {</t>
Shinya Kitaoka 120a6e
  int i, lx, ly, brad;
Shinya Kitaoka 120a6e
  double coeff, coeffq, diff, globmatte;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  brad = tfloor(blur); /* number of pixels involved in the filtering. */
Shinya Kitaoka 120a6e
  if (bidirectional) {
Shinya Kitaoka 120a6e
    coeff =
Shinya Kitaoka 120a6e
        blur /
Shinya Kitaoka 120a6e
        (brad - brad * brad +
Shinya Kitaoka 120a6e
         blur * (2 * brad - 1));  // sum of the weights of triangolar filter.
Shinya Kitaoka 120a6e
    coeffq = coeff / blur;
Shinya Kitaoka 120a6e
    diff   = blur - brad;
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    coeff = (brad + 1) * (1 - brad / (2 * blur)) *
Shinya Kitaoka 120a6e
            blur; /*sum of the weights of triangolar filter.              */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  lx = r->getLx();
Shinya Kitaoka 120a6e
  ly = r->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if ((lx == 0) || (ly == 0)) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::unique_ptr<t[]> row(new T[lx + 2 * brad + 2]);</t[]>
Shinya Kitaoka 120a6e
  if (!row) return;
Shinya Kitaoka 120a6e
  memset(row.get(), 0, (lx + 2 * brad + 2) * sizeof(T));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  globmatte = 0.8; /* a little bit of transparency is also added */
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  r->lock();
Shinya Kitaoka 120a6e
  for (i = 0; i < ly; i++) {
Shinya Kitaoka 120a6e
    T *buffer = (T *)r->pixels(i);
Shinya Kitaoka 120a6e
    takeRow(buffer, row.get() + brad, lx, brad, bidirectional);
Shinya Kitaoka 120a6e
    if (bidirectional)
Shinya Kitaoka 120a6e
      blur_code<t>(row.get() + brad, buffer, lx, coeff, coeffq, brad, diff,</t>
Shinya Kitaoka 120a6e
                   globmatte);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      do_filtering<t>(row.get() + brad, buffer, lx, coeff, brad, blur,</t>
Shinya Kitaoka 120a6e
                      globmatte);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  r->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
/*---------------------------------------------------------------------------*/
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void directionalBlur(TRasterPT<t> rout, TRasterPT<t> rin, const TPointD &blur,</t></t>
Shinya Kitaoka 120a6e
                     const TPoint &offset, bool bidirectional) {
Shinya Kitaoka 120a6e
  double cs, sn, cx_aux, cy_aux;
Shinya Kitaoka 120a6e
  int lx, ly, lx_aux, ly_aux;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double blurValue = tdistance(TPointD(), blur);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  lx = rin->getLx();
Shinya Kitaoka 120a6e
  ly = rin->getLy();
Shinya Kitaoka 120a6e
  cs = blur.x / blurValue;
Shinya Kitaoka 120a6e
  sn = blur.y / blurValue;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  lx_aux = (int)tceil(lx * fabs(cs) + ly * fabs(sn));
Shinya Kitaoka 120a6e
  ly_aux = (int)tceil(lx * fabs(sn) + ly * fabs(cs));
Shinya Kitaoka 120a6e
  cx_aux = lx_aux * .5;
Shinya Kitaoka 120a6e
  cy_aux = ly_aux * .5;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterPT<t> raux = TRasterPT<t>(lx_aux, ly_aux);</t></t>
Shinya Kitaoka 120a6e
  TAffine rot(cs, sn, 0, -sn, cs, 0);
Shinya Kitaoka 120a6e
  rot = rot.place(convert(rin->getCenter()), convert(raux->getCenter()));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRop::resample(raux, rin, rot);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  doDirectionalBlur<t>(raux, blurValue, bidirectional);</t>
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TAffine rotInv(cs, -sn, 0, sn, cs, 0);
Shinya Kitaoka 120a6e
  rotInv = rotInv.place(convert(raux->getCenter()),
Shinya Kitaoka 120a6e
                        convert(rin->getCenter() + offset));
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRop::resample(rout, raux, rotInv);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Shinya Kitaoka 120a6e
Campbell Barton 8c6c57
static void enlargeDir(TRectD &r, TPointD p, bool bidirectional) {
Shinya Kitaoka 120a6e
  if (bidirectional) {
Shinya Kitaoka 120a6e
    r.x1 += fabs(p.x);
Shinya Kitaoka 120a6e
    r.x0 -= fabs(p.x);
Shinya Kitaoka 120a6e
    r.y1 += fabs(p.y);
Shinya Kitaoka 120a6e
    r.y0 -= fabs(p.y);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (p.x > 0)
Shinya Kitaoka 120a6e
      r.x1 += p.x;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      r.x0 -= -p.x;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (p.y > 0)
Shinya Kitaoka 120a6e
      r.y1 += p.y;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      r.y0 -= -p.y;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Campbell Barton 8c6c57
static void reduceDir(TRectD &r, TPointD p, bool bidirectional) {
Shinya Kitaoka 120a6e
  if (bidirectional) {
Shinya Kitaoka 120a6e
    r.x1 -= fabs(p.x);
Shinya Kitaoka 120a6e
    r.x0 += fabs(p.x);
Shinya Kitaoka 120a6e
    r.y1 -= fabs(p.y);
Shinya Kitaoka 120a6e
    r.y0 += fabs(p.y);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    if (p.x > 0)
Shinya Kitaoka 120a6e
      r.x1 -= p.x;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      r.x0 += -p.x;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (p.y > 0)
Shinya Kitaoka 120a6e
      r.y1 -= p.y;
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      r.y0 += -p.y;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class DirectionalBlurBaseFx : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  // FX_PLUGIN_DECLARATION(DirectionalBlurBaseFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
protected:
Shinya Kitaoka 120a6e
  bool m_isMotionBlur;
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_angle;
Shinya Kitaoka 120a6e
  TDoubleParamP m_intensity;
Shinya Kitaoka 120a6e
  TBoolParamP m_bidirectional;
Shinya Kitaoka 120a6e
  TBoolParamP m_spread;
Shinya Kitaoka 120a6e
  //  TBoolParamP m_useSSE;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 38fd86
  bool doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 38fd86
                 const TRenderSettings &info) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  DirectionalBlurBaseFx(bool isMotionBLur)
Shinya Kitaoka 120a6e
      : m_isMotionBlur(isMotionBLur)
Shinya Kitaoka 120a6e
      , m_angle(0.0)
Shinya Kitaoka 120a6e
      , m_intensity(10.0)
Shinya Kitaoka 120a6e
      , m_bidirectional(false)
Shinya Kitaoka 120a6e
      , m_spread(true) {
Shinya Kitaoka 120a6e
    bindParam(this, "intensity", m_intensity);
Shinya Kitaoka 120a6e
    bindParam(this, "bidirectional", m_bidirectional);
Shinya Kitaoka 120a6e
    bindParam(this, "spread", m_spread);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
    m_intensity->setValueRange(0, (std::numeric_limits<double>::max)());</double>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    getAttributes()->setIsSpeedAware(true);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPointD getBlurVector(double frame) const {
Shinya Kitaoka 120a6e
    TPointD speed = getAttributes()->getSpeed();
Shinya Kitaoka 120a6e
    double value  = m_intensity->getValue(frame);
Shinya Kitaoka 120a6e
    return value * speed;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ~DirectionalBlurBaseFx(){};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &) override;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 473e70
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 120a6e
    return isAlmostIsotropic(info.m_affine) ||
Shinya Kitaoka 120a6e
           m_intensity->getValue(frame) == 0;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  int getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 473e70
                           const TRenderSettings &info) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class DirectionalBlurFx final : public DirectionalBlurBaseFx
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(DirectionalBlurFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  DirectionalBlurFx() : DirectionalBlurBaseFx(false) {
Shinya Kitaoka 120a6e
    m_intensity->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
    m_angle->setMeasureName("angle");
Shinya Kitaoka 120a6e
    bindParam(this, "angle", m_angle);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void getParamUIs(TParamUIConcept *&concepts, int &length) override {
Shinya Kitaoka 120a6e
    concepts = new TParamUIConcept[length = 1];
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    concepts[0].m_type  = TParamUIConcept::POLAR;
Shinya Kitaoka 120a6e
    concepts[0].m_label = "Angle and Intensity";
Shinya Kitaoka 120a6e
    concepts[0].m_params.push_back(m_angle);
Shinya Kitaoka 120a6e
    concepts[0].m_params.push_back(m_intensity);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class MotionBlurFx final : public DirectionalBlurBaseFx
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
{
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(MotionBlurFx)
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  MotionBlurFx() : DirectionalBlurBaseFx(true) {}
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  std::string getAlias(double frame,
Shinya Kitaoka 38fd86
                       const TRenderSettings &info) const override {
Shinya Kitaoka 120a6e
    unsigned long id = getIdentifier();
Shinya Kitaoka 120a6e
    double value     = m_intensity->getValue(frame);
Shinya Kitaoka 120a6e
    return getFxType() + "[" + std::to_string(id) + "," +
Shinya Kitaoka 120a6e
           std::to_string(frame) + "," + std::to_string(value) + "]";
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool DirectionalBlurBaseFx::doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 120a6e
                                      const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  if (m_input.isConnected()) {
Shinya Kitaoka 120a6e
    bool ret = m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
    if (m_spread->getValue()) {
Shinya Kitaoka 120a6e
      TPointD blur;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      if (m_isMotionBlur)
Shinya Kitaoka 120a6e
        blur = getBlurVector(frame);
Shinya Kitaoka 120a6e
      else {
Shinya Kitaoka 120a6e
        double angle = m_angle->getValue(frame) * M_PI_180;
Shinya Kitaoka 120a6e
        blur.x       = m_intensity->getValue(frame) * cos(angle);
Shinya Kitaoka 120a6e
        blur.y       = m_intensity->getValue(frame) * sin(angle);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      enlargeDir(bBox, blur, m_bidirectional->getValue());
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
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void DirectionalBlurBaseFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                                      const TRenderSettings &ri) {
Shinya Kitaoka 120a6e
  if (!m_input.isConnected()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPointD blurVector;
Shinya Kitaoka 120a6e
  if (m_isMotionBlur) {
Shinya Kitaoka 120a6e
    blurVector = getBlurVector(frame);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    double angle = m_angle->getValue(frame) * M_PI_180;
Shinya Kitaoka 120a6e
    blurVector.x = m_intensity->getValue(frame) * cos(angle);
Shinya Kitaoka 120a6e
    blurVector.y = m_intensity->getValue(frame) * sin(angle);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const double limit = 900.0;
Shinya Kitaoka 120a6e
  blurVector.x       = tcrop(blurVector.x, -limit, limit);
Shinya Kitaoka 120a6e
  blurVector.y       = tcrop(blurVector.y, -limit, limit);
Shinya Kitaoka 120a6e
  double shrink      = (ri.m_shrinkX + ri.m_shrinkY) / 2.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TAffine aff = ri.m_affine;
Shinya Kitaoka 120a6e
  aff.a13 = aff.a23 = 0;
Shinya Kitaoka 120a6e
  TPointD blur      = (1.0 / shrink) * (aff * blurVector);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double blurValue = norm(blur);
Shinya Kitaoka 120a6e
  if (areAlmostEqual(blurValue, 0, 1e-3)) {
Shinya Kitaoka 120a6e
    m_input->compute(tile, frame, ri);
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRectD rectTile = TRectD(tile.m_pos, TDimensionD(tile.getRaster()->getLx(),
Shinya Kitaoka 120a6e
                                                   tile.getRaster()->getLy()));
Shinya Kitaoka 120a6e
  if (!rectTile.isEmpty()) {
Shinya Kitaoka 120a6e
    TRectD bboxIn;
Shinya Kitaoka 120a6e
    m_input->getBBox(frame, bboxIn, ri);
Shinya Kitaoka 120a6e
    if (bboxIn == TConsts::infiniteRectD) bboxIn = rectTile;
Shinya Kitaoka 120a6e
    TPointD blur = (1.0 / shrink) * (aff * blurVector);
Shinya Kitaoka 120a6e
    // enlarge must be bidirectional, because we need pixel on the ohter side of
Shinya Kitaoka 120a6e
    // blur
Shinya Kitaoka 120a6e
    enlargeDir(bboxIn, blur, true);
Shinya Kitaoka 120a6e
    rectTile = bboxIn * rectTile.enlarge(fabs(blur.x), fabs(blur.y));
Shinya Kitaoka 120a6e
    TRect rectIn;
Shinya Kitaoka 120a6e
    rectIn.x0 = tfloor(rectTile.x0);
Shinya Kitaoka 120a6e
    rectIn.y0 = tfloor(rectTile.y0);
Shinya Kitaoka 120a6e
    rectIn.x1 = tceil(rectTile.x1);
Shinya Kitaoka 120a6e
    rectIn.y1 = tceil(rectTile.y1);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    int rasInLx = rectIn.getLx();
Shinya Kitaoka 120a6e
    int rasInLy = rectIn.getLy();
Shinya Kitaoka 120a6e
    TTile tileIn;
Shinya Kitaoka 120a6e
    m_input->allocateAndCompute(tileIn, convert(rectIn.getP00()),
Shinya Kitaoka 120a6e
                                TDimension(rasInLx, rasInLy), tile.getRaster(),
Shinya Kitaoka 120a6e
                                frame, ri);
Shinya Kitaoka 120a6e
    TRasterP rasIn = tileIn.getRaster();
Shinya Kitaoka 120a6e
    TRect rectOut =
Shinya Kitaoka 120a6e
        rasIn->getBounds() + (rectIn.getP00() - convert(tile.m_pos));
Shinya Kitaoka 120a6e
    TRasterP rasOut = tile.getRaster()->extract(rectOut);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TPoint p;
Shinya Kitaoka 120a6e
    if (rectIn.x0 < tile.m_pos.x) p.x = rectIn.x0 - tile.m_pos.x;
Shinya Kitaoka 120a6e
    if (rectIn.y0 < tile.m_pos.y) p.y = rectIn.y0 - tile.m_pos.y;
Shinya Kitaoka 120a6e
    if ((TRaster32P)rasIn && (TRaster32P)rasOut)
Shinya Kitaoka 120a6e
      directionalBlur<tpixel32>(rasOut, rasIn, blur, p,</tpixel32>
Shinya Kitaoka 120a6e
                                m_bidirectional->getValue());
Shinya Kitaoka 120a6e
    else if ((TRaster64P)rasIn && (TRaster64P)rasOut)
Shinya Kitaoka 120a6e
      directionalBlur<tpixel64>(rasOut, rasIn, blur, p,</tpixel64>
Shinya Kitaoka 120a6e
                                m_bidirectional->getValue());
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int DirectionalBlurBaseFx::getMemoryRequirement(const TRectD &rect,
Shinya Kitaoka 120a6e
                                                double frame,
Shinya Kitaoka 120a6e
                                                const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  TPointD blurVector;
Shinya Kitaoka 120a6e
  if (m_isMotionBlur) {
Shinya Kitaoka 120a6e
    blurVector = getBlurVector(frame);
Shinya Kitaoka 120a6e
  } else {
Shinya Kitaoka 120a6e
    double angle = m_angle->getValue(frame) * M_PI_180;
Shinya Kitaoka 120a6e
    blurVector.x = m_intensity->getValue(frame) * cos(angle);
Shinya Kitaoka 120a6e
    blurVector.y = m_intensity->getValue(frame) * sin(angle);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  const double limit = 900.0;
Shinya Kitaoka 120a6e
  blurVector.x       = tcrop(blurVector.x, -limit, limit);
Shinya Kitaoka 120a6e
  blurVector.y       = tcrop(blurVector.y, -limit, limit);
Shinya Kitaoka 120a6e
  double shrink      = (info.m_shrinkX + info.m_shrinkY) / 2.0;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TAffine aff = info.m_affine;
Shinya Kitaoka 120a6e
  aff.a13 = aff.a23 = 0;
Shinya Kitaoka 120a6e
  TPointD blur      = (1.0 / shrink) * (aff * blurVector);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return TRasterFx::memorySize(rect.enlarge(blur.x, blur.y), info.m_bpp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(DirectionalBlurFx, "directionalBlurFx");
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(MotionBlurFx, "motionBlurFx");