Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "texception.h"
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
Toshihiro Shimizu 890ddd
#include "tbasefx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "trop.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "tparamset.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static void backlit(TRaster32P lighted, TRaster32P light, TRaster32P out,
shun-iwasawa 27b0cf
                    double blur, const TPixel &color, double fade,
shun-iwasawa 27b0cf
                    double scale) {
Shinya Kitaoka 120a6e
  assert(light && lighted && out);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(lighted->getSize() == light->getSize());
Shinya Kitaoka 120a6e
  assert(lighted->getSize() == out->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int j;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fade > 0.0) {
Shinya Kitaoka 120a6e
    assert(fade <= 1.0);
Shinya Kitaoka 120a6e
    light->lock();
Shinya Kitaoka 120a6e
    for (j = 0; j < light->getLy(); j++) {
Shinya Kitaoka 120a6e
      TPixel *pix    = light->pixels(j);
Shinya Kitaoka 120a6e
      TPixel *endPix = pix + light->getLx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      while (pix < endPix) {
Shinya Kitaoka 120a6e
        if (pix->m > 0) {
Shinya Kitaoka 120a6e
          if (pix->m == 255) {
Shinya Kitaoka 120a6e
            pix->r = troundp(pix->r + fade * (color.r - pix->r));
Shinya Kitaoka 120a6e
            pix->g = troundp(pix->g + fade * (color.g - pix->g));
Shinya Kitaoka 120a6e
            pix->b = troundp(pix->b + fade * (color.b - pix->b));
Shinya Kitaoka 120a6e
            pix->m = troundp(pix->m + fade * (color.m - pix->m));
Shinya Kitaoka 120a6e
          } else {
Shinya Kitaoka 120a6e
            int val;
Shinya Kitaoka 120a6e
            double factor = pix->m / 255.0;
Shinya Kitaoka 120a6e
            val    = troundp(pix->r + fade * (color.r * factor - pix->r));
Shinya Kitaoka 120a6e
            pix->r = (val > 255) ? 255 : val;
Shinya Kitaoka 120a6e
            val    = troundp(pix->g + fade * (color.g * factor - pix->g));
Shinya Kitaoka 120a6e
            pix->g = (val > 255) ? 255 : val;
Shinya Kitaoka 120a6e
            val    = troundp(pix->b + fade * (color.b * factor - pix->b));
Shinya Kitaoka 120a6e
            pix->b = (val > 255) ? 255 : val;
Shinya Kitaoka 120a6e
            val    = troundp(pix->m + fade * (color.m * factor - pix->m));
Shinya Kitaoka 120a6e
            pix->m = (val > 255) ? 255 : val;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    light->unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRop::ropout(light, lighted, light);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (blur > 0) {
Shinya Kitaoka 120a6e
    double _blur = blur * scale;
Shinya Kitaoka 120a6e
    int bi       = tceil(_blur);
Shinya Kitaoka 120a6e
    TRaster32P ras(light->getLx() + bi, light->getLy() + bi);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRect rect(light->getBounds() + TPoint(bi, bi));
Shinya Kitaoka 120a6e
    TRop::copy(ras->extract(rect), light);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRop::blur(out, ras, _blur, -bi, -bi, true);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double ampl = 1.0 + blur / 15.0;
Shinya Kitaoka 120a6e
    out->lock();
Shinya Kitaoka 120a6e
    for (j = 0; j < out->getLy(); j++) {
Shinya Kitaoka 120a6e
      TPixel *pix    = out->pixels(j);
Shinya Kitaoka 120a6e
      TPixel *endPix = pix + out->getLx();
Shinya Kitaoka 120a6e
      while (pix < endPix) {
Shinya Kitaoka 120a6e
        if (pix->m != 0) {
Shinya Kitaoka 120a6e
          int val = troundp(pix->r * ampl);
Shinya Kitaoka 120a6e
          pix->r  = (val > 204) ? 204 : val;
Shinya Kitaoka 120a6e
          val     = troundp(pix->g * ampl);
Shinya Kitaoka 120a6e
          pix->g  = (val > 204) ? 204 : val;
Shinya Kitaoka 120a6e
          val     = troundp(pix->b * ampl);
Shinya Kitaoka 120a6e
          pix->b  = (val > 204) ? 204 : val;
Shinya Kitaoka 120a6e
          val     = troundp(pix->m * ampl);
Shinya Kitaoka 120a6e
          pix->m  = (val > 204) ? 204 : val;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        pix++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    out->unlock();
Shinya Kitaoka 120a6e
    TRop::over(out, lighted, out);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    TRop::over(out, lighted, light);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
shun-iwasawa 27b0cf
static void backlit(TRaster64P lighted, TRaster64P light, TRaster64P out,
shun-iwasawa 27b0cf
                    double blur, const TPixel &color32, double fade,
shun-iwasawa 27b0cf
                    double scale) {
Shinya Kitaoka 120a6e
  assert(light && lighted && out);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  assert(lighted->getSize() == light->getSize());
Shinya Kitaoka 120a6e
  assert(lighted->getSize() == out->getSize());
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int j;
Shinya Kitaoka 120a6e
  TPixel64 color = toPixel64(color32);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (fade > 0.0) {
Shinya Kitaoka 120a6e
    assert(fade <= 1.0);
Shinya Kitaoka 120a6e
    light->lock();
Shinya Kitaoka 120a6e
    for (j = 0; j < light->getLy(); j++) {
Shinya Kitaoka 120a6e
      TPixel64 *pix    = light->pixels(j);
Shinya Kitaoka 120a6e
      TPixel64 *endPix = pix + light->getLx();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      while (pix < endPix) {
Shinya Kitaoka 120a6e
        if (pix->m > 0) {
Shinya Kitaoka 120a6e
          if (pix->m == 65535) {
Shinya Kitaoka 120a6e
            pix->r = troundp(pix->r + fade * (color.r - pix->r));
Shinya Kitaoka 120a6e
            pix->g = troundp(pix->g + fade * (color.g - pix->g));
Shinya Kitaoka 120a6e
            pix->b = troundp(pix->b + fade * (color.b - pix->b));
Shinya Kitaoka 120a6e
            pix->m = troundp(pix->m + fade * (color.m - pix->m));
Shinya Kitaoka 120a6e
          } else {
Shinya Kitaoka 120a6e
            int val;
Shinya Kitaoka 120a6e
            double factor = pix->m / 65535.0;
Shinya Kitaoka 120a6e
            val    = troundp(pix->r + fade * (color.r * factor - pix->r));
Shinya Kitaoka 120a6e
            pix->r = (val > 65535) ? 65535 : val;
Shinya Kitaoka 120a6e
            val    = troundp(pix->g + fade * (color.g * factor - pix->g));
Shinya Kitaoka 120a6e
            pix->g = (val > 65535) ? 65535 : val;
Shinya Kitaoka 120a6e
            val    = troundp(pix->b + fade * (color.b * factor - pix->b));
Shinya Kitaoka 120a6e
            pix->b = (val > 65535) ? 65535 : val;
Shinya Kitaoka 120a6e
            val    = troundp(pix->m + fade * (color.m * factor - pix->m));
Shinya Kitaoka 120a6e
            pix->m = (val > 65535) ? 65535 : val;
Shinya Kitaoka 120a6e
          }
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        ++pix;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    light->unlock();
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TRop::ropout(light, lighted, light);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (blur > 0) {
Shinya Kitaoka 120a6e
    double _blur = blur * scale;
Shinya Kitaoka 120a6e
    int bi       = tceil(_blur);
Shinya Kitaoka 120a6e
    TRaster64P ras(light->getLx() + bi, light->getLy() + bi);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRect rect(light->getBounds() + TPoint(bi, bi));
Shinya Kitaoka 120a6e
    TRop::copy(ras->extract(rect), light);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRop::blur(out, ras, _blur, -bi, -bi, false);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double ampl = 1.0 + blur / 15.0;
Shinya Kitaoka 120a6e
    out->lock();
Shinya Kitaoka 120a6e
    int fac = (204 << 8) | 204;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    for (j = 0; j < out->getLy(); j++) {
Shinya Kitaoka 120a6e
      TPixel64 *pix    = out->pixels(j);
Shinya Kitaoka 120a6e
      TPixel64 *endPix = pix + out->getLx();
Shinya Kitaoka 120a6e
      while (pix < endPix) {
Shinya Kitaoka 120a6e
        if (pix->m != 0) {
Shinya Kitaoka 120a6e
          int val = troundp(pix->r * ampl);
Shinya Kitaoka 120a6e
          pix->r  = (val > fac) ? fac : val;
Shinya Kitaoka 120a6e
          val     = troundp(pix->g * ampl);
Shinya Kitaoka 120a6e
          pix->g  = (val > fac) ? fac : val;
Shinya Kitaoka 120a6e
          val     = troundp(pix->b * ampl);
Shinya Kitaoka 120a6e
          pix->b  = (val > fac) ? fac : val;
Shinya Kitaoka 120a6e
          val     = troundp(pix->m * ampl);
Shinya Kitaoka 120a6e
          pix->m  = (val > fac) ? fac : val;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
        pix++;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    out->unlock();
Shinya Kitaoka 120a6e
    TRop::over(out, lighted, out);
Shinya Kitaoka 120a6e
  } else
Shinya Kitaoka 120a6e
    TRop::over(out, lighted, light);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class BacklitFx final : public TBaseRasterFx {
Shinya Kitaoka 120a6e
  FX_DECLARATION(BacklitFx)
Shinya Kitaoka 120a6e
  TRasterFxPort m_lighted, m_light;
Shinya Kitaoka 120a6e
  TDoubleParamP m_value;
Shinya Kitaoka 120a6e
  TDoubleParamP m_fade;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TPixelParamP m_color;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  BacklitFx() : m_value(0.0), m_color(TPixel::White), m_fade(0.0) {
Shinya Kitaoka 120a6e
    m_color->enableMatte(true);
Shinya Kitaoka 120a6e
    m_value->setValueRange(0, (std::numeric_limits<double>::max)());</double>
Shinya Kitaoka 120a6e
    m_fade->setValueRange(0.0, 100.0);
Shinya Kitaoka 120a6e
    bindParam(this, "value", m_value);
Shinya Kitaoka 120a6e
    bindParam(this, "color", m_color);
Shinya Kitaoka 120a6e
    bindParam(this, "fade", m_fade);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    addInputPort("Light", m_light);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_lighted);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ~BacklitFx() {}
Shinya Kitaoka 38fd86
  bool doGetBBox(double frame, TRectD &bbox,
Shinya Kitaoka 38fd86
                 const TRenderSettings &info) override {
Rozhuk Ivan 823a31
    if (getActiveTimeRegion().contains(frame)) {
Shinya Kitaoka 120a6e
      if (m_light.isConnected()) {
Shinya Kitaoka 120a6e
        if (m_lighted.isConnected()) {
Shinya Kitaoka 120a6e
          TRectD b0, b1;
Shinya Kitaoka 120a6e
          bool ret = m_light->doGetBBox(frame, b0, info);
Shinya Kitaoka 120a6e
          ret      = ret && m_lighted->doGetBBox(frame, b1, info);
Shinya Kitaoka 120a6e
          bbox     = b0.enlarge(tceil(m_value->getValue(frame))) + b1;
Shinya Kitaoka 120a6e
          return ret;
Rozhuk Ivan 823a31
        } else {
Shinya Kitaoka 120a6e
          return m_light->doGetBBox(frame, bbox, info);
Rozhuk Ivan 823a31
	}
Rozhuk Ivan 823a31
      } else if (m_lighted.isConnected()) {
Shinya Kitaoka 120a6e
        return m_lighted->doGetBBox(frame, bbox, info);
Rozhuk Ivan 823a31
      }
Rozhuk Ivan 823a31
    }
Shinya Kitaoka 120a6e
    return false;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  void doDryCompute(TRectD &rect, double frame,
Shinya Kitaoka 38fd86
                    const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    if (!m_light.isConnected()) return;
Shinya Kitaoka 120a6e
    if (!m_lighted.isConnected()) {
Shinya Kitaoka 120a6e
      m_light->dryCompute(rect, frame, info);
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double value = m_value->getValue(frame);
Shinya Kitaoka 120a6e
    double scale = sqrt(fabs(info.m_affine.det()));
Shinya Kitaoka 120a6e
    int brad     = tceil(value * scale);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRectD inRect = rect.enlarge(brad);
Shinya Kitaoka 120a6e
    inRect = TRectD(tfloor(inRect.x0), tfloor(inRect.y0), tceil(inRect.x1),
Shinya Kitaoka 120a6e
                    tceil(inRect.y1));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_lighted->dryCompute(inRect, frame, info);
Shinya Kitaoka 120a6e
    m_light->dryCompute(inRect, frame, info);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  void doCompute(TTile &tile, double frame,
Shinya Kitaoka 38fd86
                 const TRenderSettings &ri) override {
Shinya Kitaoka 120a6e
    if (!m_light.isConnected()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    if (!m_lighted.isConnected()) {
Shinya Kitaoka 120a6e
      m_light->compute(tile, frame, ri);
Shinya Kitaoka 120a6e
      return;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    double value = m_value->getValue(frame);
Shinya Kitaoka 120a6e
    double scale = sqrt(fabs(ri.m_affine.det()));
Shinya Kitaoka 120a6e
    int brad     = tceil(value * scale);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TDimension tileSize(tile.getRaster()->getSize());
Shinya Kitaoka 120a6e
    TRectD rect(tile.m_pos, TDimensionD(tileSize.lx, tileSize.ly));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    // TRectD
Shinya Kitaoka 120a6e
    // rect(tile.m_pos,convert(tile.getRaster()->getBounds().getP11())+tile.m_pos);
Shinya Kitaoka 120a6e
    rect = rect.enlarge(brad);
Shinya Kitaoka 120a6e
    TRect rectI(tfloor(rect.x0), tfloor(rect.y0), tceil(rect.x1) - 1,
Shinya Kitaoka 120a6e
                tceil(rect.y1) - 1);
Shinya Kitaoka 120a6e
    // m_light->compute(tile, frame, ri);
Shinya Kitaoka 120a6e
    TTile srcTile;
Shinya Kitaoka 120a6e
    m_lighted->allocateAndCompute(srcTile, rect.getP00(), rectI.getSize(),
Shinya Kitaoka 120a6e
                                  tile.getRaster(), frame, ri);
Shinya Kitaoka 120a6e
    TTile ctrTile;
Shinya Kitaoka 120a6e
    m_light->allocateAndCompute(ctrTile, rect.getP00(), rectI.getSize(),
Shinya Kitaoka 120a6e
                                tile.getRaster(), frame, ri);
Shinya Kitaoka 120a6e
    if ((TRaster32P)srcTile.getRaster() && (TRaster32P)tile.getRaster())
Shinya Kitaoka 120a6e
      backlit((TRaster32P)srcTile.getRaster(), (TRaster32P)ctrTile.getRaster(),
Shinya Kitaoka 120a6e
              (TRaster32P)ctrTile.getRaster(), value, m_color->getValue(frame),
Shinya Kitaoka 120a6e
              m_fade->getValue(frame) / 100.0, scale);
Shinya Kitaoka 120a6e
    else if ((TRaster64P)srcTile.getRaster() && (TRaster64P)tile.getRaster())
Shinya Kitaoka 120a6e
      backlit((TRaster64P)srcTile.getRaster(), (TRaster64P)ctrTile.getRaster(),
Shinya Kitaoka 120a6e
              (TRaster64P)ctrTile.getRaster(), value, m_color->getValue(frame),
Shinya Kitaoka 120a6e
              m_fade->getValue(frame) / 100.0, scale);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      throw TRopException("TRop::max invalid raster combination");
Shinya Kitaoka 120a6e
    tile.getRaster()->copy(ctrTile.getRaster(), TPoint(-brad, -brad));
Shinya Kitaoka 120a6e
  }
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 120a6e
  int getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 473e70
                           const TRenderSettings &info) override {
Shinya Kitaoka 120a6e
    double value = m_value->getValue(frame);
Shinya Kitaoka 120a6e
    double scale = sqrt(fabs(info.m_affine.det()));
Shinya Kitaoka 120a6e
    int brad     = tceil(value * scale);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    return TRasterFx::memorySize(rect.enlarge(brad), info.m_bpp);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(BacklitFx, "backlitFx")
Toshihiro Shimizu 890ddd
//==================================================================