Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tparamset.h"
Toshihiro Shimizu 890ddd
#include "ttonecurveparam.h"
Toshihiro Shimizu 890ddd
#include "tcurves.h"
shun-iwasawa 8cac36
#include "globalcontrollablefx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
int getCubicYfromX(TCubic c, int x, double &s0, double &s1) {
Shinya Kitaoka 120a6e
  double s  = (s1 + s0) * 0.5;
Shinya Kitaoka 120a6e
  TPointD p = c.getPoint(s);
Shinya Kitaoka 120a6e
  if (areAlmostEqual(double(x), p.x, 0.001)) return tround(p.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (x < p.x)
Shinya Kitaoka 120a6e
    return getCubicYfromX(c, x, s0, s);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return getCubicYfromX(c, x, s, s1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int getLinearYfromX(TSegment t, int x, double &s0, double &s1) {
Shinya Kitaoka 120a6e
  double s  = (s1 + s0) * 0.5;
Shinya Kitaoka 120a6e
  TPointD p = t.getPoint(s);
Shinya Kitaoka 120a6e
  if (areAlmostEqual(double(x), p.x, 0.001)) return tround(p.y);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (x < p.x)
Shinya Kitaoka 120a6e
    return getLinearYfromX(t, x, s0, s);
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    return getLinearYfromX(t, x, s, s1);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
shun-iwasawa 0ed559
void truncateSpeeds(double aFrame, double bFrame, TPointD &aSpeedTrunc,
shun-iwasawa 0ed559
                    TPointD &bSpeedTrunc) {
shun-iwasawa 0ed559
  double deltaX = bFrame - aFrame;
shun-iwasawa 0ed559
  if (aSpeedTrunc.x < 0) aSpeedTrunc.x = 0;
shun-iwasawa 0ed559
  if (bSpeedTrunc.x > 0) bSpeedTrunc.x = 0;
shun-iwasawa 0ed559
shun-iwasawa 0ed559
  if (aFrame + aSpeedTrunc.x > bFrame) {
shun-iwasawa 0ed559
    if (aSpeedTrunc.x != 0) {
shun-iwasawa 0ed559
      aSpeedTrunc = aSpeedTrunc * (deltaX / aSpeedTrunc.x);
shun-iwasawa 0ed559
    }
shun-iwasawa 0ed559
  }
shun-iwasawa 0ed559
shun-iwasawa 0ed559
  if (bFrame + bSpeedTrunc.x < aFrame) {
shun-iwasawa 0ed559
    if (bSpeedTrunc.x != 0) {
shun-iwasawa 0ed559
      bSpeedTrunc = -bSpeedTrunc * (deltaX / bSpeedTrunc.x);
shun-iwasawa 0ed559
    }
shun-iwasawa 0ed559
  }
shun-iwasawa 0ed559
}
shun-iwasawa 0ed559
Toshihiro Shimizu 890ddd
template <typename pixel,="" t="" typename=""></typename>
Shinya Kitaoka 120a6e
void fill_lut(QList<tpointd> points, std::vector<t> &lut, bool isLinear) {</t></tpointd>
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  TPointD p0 = points.at(0);
Shinya Kitaoka 120a6e
  TCubic cubic;
Shinya Kitaoka 120a6e
  TSegment segment;
Shinya Kitaoka 120a6e
  double s0 = 0.0;
Shinya Kitaoka 120a6e
  for (i = 1; i < points.size(); i++) {
Shinya Kitaoka 120a6e
    TPointD p1 = points.at(i);
Shinya Kitaoka 120a6e
    TPointD p2 = points.at(++i);
Shinya Kitaoka 120a6e
    TPointD p3 = points.at(++i);
Shinya Kitaoka 120a6e
    if (!isLinear) {
shun-iwasawa 0ed559
      // truncate speed
shun-iwasawa 0ed559
      TPointD aSpeed(p1 - p0);
shun-iwasawa 0ed559
      TPointD bSpeed(p2 - p3);
shun-iwasawa 0ed559
      truncateSpeeds(p0.x, p3.x, aSpeed, bSpeed);
Shinya Kitaoka 120a6e
      cubic.setP0(p0);
shun-iwasawa 0ed559
      cubic.setP1(p0 + aSpeed);
shun-iwasawa 0ed559
      cubic.setP2(p3 + bSpeed);
Shinya Kitaoka 120a6e
      cubic.setP3(p3);
Shinya Kitaoka 120a6e
      int x = (int)p0.x;
Shinya Kitaoka 120a6e
      while (x < 0) x++;
Shinya Kitaoka 120a6e
      while (x < p3.x && x < PIXEL::maxChannelValue + 1) {
Shinya Kitaoka 120a6e
        double s1 = 1.0;
Shinya Kitaoka 120a6e
        int y     = getCubicYfromX(cubic, x, s0, s1);
shun-iwasawa 0ed559
        if (y > PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
          y = PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
        else if (y < 0)
shun-iwasawa 0ed559
          y = 0;
Shinya Kitaoka 120a6e
        lut[x] = y;
Shinya Kitaoka 120a6e
        x++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      segment.setP0(p0);
Shinya Kitaoka 120a6e
      segment.setP1(p3);
Shinya Kitaoka 120a6e
      int x = (int)p0.x;
Shinya Kitaoka 120a6e
      while (x < 0) x++;
Shinya Kitaoka 120a6e
      while (x < p3.x && x < PIXEL::maxChannelValue + 1) {
Shinya Kitaoka 120a6e
        double s1 = 1.0;
Shinya Kitaoka 120a6e
        int y     = getLinearYfromX(segment, x, s0, s1);
shun-iwasawa 0ed559
        if (y > PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
          y = PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
        else if (y < 0)
shun-iwasawa 0ed559
          y = 0;
Shinya Kitaoka 120a6e
        lut[x] = y;
Shinya Kitaoka 120a6e
        x++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    p0 = p3;
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // Namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
shun-iwasawa 8cac36
class ToneCurveFx final : public GlobalControllableFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(ToneCurveFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TToneCurveParamP m_toneCurve;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ToneCurveFx() : m_toneCurve(new TToneCurveParam()) {
Shinya Kitaoka 120a6e
    bindParam(this, "curve", m_toneCurve);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~ToneCurveFx(){};
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 38fd86
    return true;
Shinya Kitaoka 38fd86
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool doGetBBox(double frame, TRectD &bBox,
Shinya Kitaoka 38fd86
                 const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    if (m_input.isConnected())
Shinya Kitaoka 120a6e
      return m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
    else {
Shinya Kitaoka 120a6e
      bBox = TRectD();
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &ri) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
namespace {
Shinya Kitaoka 120a6e
void update_param(double ¶m, TRaster32P ras) { return; }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void update_param(double ¶m, TRaster64P ras) {
Shinya Kitaoka 120a6e
  param = param * 257;
Shinya Kitaoka 120a6e
  return;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
QList<tpointd> getParamSetPoints(const TParamSet *paramSet, int frame) {</tpointd>
Shinya Kitaoka 120a6e
  QList<tpointd> points;</tpointd>
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  for (i = 0; i < paramSet->getParamCount(); i++) {
Shinya Kitaoka 120a6e
    TPointParamP pointParam = paramSet->getParam(i);
Shinya Kitaoka 120a6e
    TPointD point           = pointParam->getValue(frame);
Shinya Kitaoka 120a6e
    int x                   = (int)point.x;
Shinya Kitaoka 120a6e
    int y                   = (int)point.y;
Shinya Kitaoka 120a6e
    points.push_back(TPointD(x, y));
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  return points;
Toshihiro Shimizu 890ddd
}
shun-iwasawa 0ed559
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doToneCurveFx(TRasterPT<pixel> ras, double frame,</pixel>
Shinya Kitaoka 120a6e
                   const TToneCurveParam *toneCurveParam) {
Shinya Kitaoka 120a6e
  QList<qlist<tpointd>> pointsList;</qlist<tpointd>
Shinya Kitaoka 120a6e
  int e;
Shinya Kitaoka 120a6e
  for (e = 0; e < 6; e++) {
Shinya Kitaoka 120a6e
    TParamSet *paramSet =
Shinya Kitaoka 120a6e
        toneCurveParam->getParamSet(TToneCurveParam::ToneChannel(e))
Shinya Kitaoka 120a6e
            .getPointer();
Shinya Kitaoka 120a6e
    QList<tpointd> points = getParamSetPoints(paramSet, frame);</tpointd>
Shinya Kitaoka 120a6e
    pointsList.push_back(points);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  bool isLinear = toneCurveParam->isLinear();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int i, t;
Shinya Kitaoka 120a6e
  for (i = 0; i < pointsList.size(); i++) {
Shinya Kitaoka 120a6e
    QList<tpointd> &points = pointsList[i];</tpointd>
Shinya Kitaoka 120a6e
    for (t = 0; t < points.size(); t++) {
Shinya Kitaoka 120a6e
      TPointD &p = points[t];
Shinya Kitaoka 120a6e
      double &x  = p.x;
Shinya Kitaoka 120a6e
      double &y  = p.y;
Shinya Kitaoka 120a6e
      update_param(x, ras);
Shinya Kitaoka 120a6e
      update_param(y, ras);
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::vector<channel_type> rgbaLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
  std::vector<channel_type> rgbLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
  std::vector<channel_type> rLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
  std::vector<channel_type> gLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
  std::vector<channel_type> bLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
  std::vector<channel_type> aLut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  fill_lut<pixel, channel_type="">(pointsList[0], rgbaLut, isLinear);</pixel,>
Shinya Kitaoka 120a6e
  fill_lut<pixel, channel_type="">(pointsList[1], rgbLut, isLinear);</pixel,>
Shinya Kitaoka 120a6e
  fill_lut<pixel, channel_type="">(pointsList[2], rLut, isLinear);</pixel,>
Shinya Kitaoka 120a6e
  fill_lut<pixel, channel_type="">(pointsList[3], gLut, isLinear);</pixel,>
Shinya Kitaoka 120a6e
  fill_lut<pixel, channel_type="">(pointsList[4], bLut, isLinear);</pixel,>
Shinya Kitaoka 120a6e
  fill_lut<pixel, channel_type="">(pointsList[5], aLut, isLinear);</pixel,>
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int lx = ras->getLx();
Shinya Kitaoka 120a6e
  int ly = ras->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int j;
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  for (j = 0; j < ly; j++) {
Shinya Kitaoka 120a6e
    PIXEL *pix    = ras->pixels(j);
Shinya Kitaoka 120a6e
    PIXEL *endPix = pix + lx;
Shinya Kitaoka 120a6e
    while (pix < endPix) {
Shinya Kitaoka 120a6e
      if (pix->m != 0 && pix->m != PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
        *pix = depremultiply(*pix);
Shinya Kitaoka 120a6e
      pix->r = rLut[(int)(pix->r)];
Shinya Kitaoka 120a6e
      pix->g = gLut[(int)(pix->g)];
Shinya Kitaoka 120a6e
      pix->b = bLut[(int)(pix->b)];
Shinya Kitaoka 120a6e
      pix->m = aLut[(int)(pix->m)];
Shinya Kitaoka 120a6e
      pix->r = rgbLut[(int)(pix->r)];
Shinya Kitaoka 120a6e
      pix->g = rgbLut[(int)(pix->g)];
Shinya Kitaoka 120a6e
      pix->b = rgbLut[(int)(pix->b)];
Shinya Kitaoka 120a6e
      pix->r = rgbaLut[(int)(pix->r)];
Shinya Kitaoka 120a6e
      pix->g = rgbaLut[(int)(pix->g)];
Shinya Kitaoka 120a6e
      pix->b = rgbaLut[(int)(pix->b)];
Shinya Kitaoka 120a6e
      pix->m = rgbaLut[(int)(pix->m)];
Shinya Kitaoka 120a6e
      if (pix->m != 0 && pix->m != PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
        *pix = premultiply(*pix);
Shinya Kitaoka 120a6e
      pix++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ToneCurveFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                            const TRenderSettings &ri) {
Shinya Kitaoka 120a6e
  if (!m_input.isConnected()) return;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  m_input->compute(tile, frame, ri);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRaster32P raster32 = tile.getRaster();
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doToneCurveFx<tpixel32, uchar="">(raster32, frame, m_toneCurve.getPointer());</tpixel32,>
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64 = tile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doToneCurveFx<tpixel64, ushort="">(raster64, frame,</tpixel64,>
Shinya Kitaoka 120a6e
                                      m_toneCurve.getPointer());
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      throw TException("Brightness&Contrast: unsupported Pixel Type");
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(ToneCurveFx, "toneCurveFx");