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
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
class BacklitFx : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(BacklitFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  // TDoubleParamP m_value;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  BacklitFx()
Shinya Kitaoka 120a6e
  //    , m_value (args, "Blur")
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    // m_value->setDefaultValue(100);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  ~BacklitFx(){};
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  bool doGetBBox(double frame, TRectD &bBox);
Shinya Kitaoka 120a6e
  void doCompute(TTile &tile, double frame, const TRenderSettings &ri);
Shinya Kitaoka 120a6e
  int getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 120a6e
                           const TRenderSettings &info);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRect getInvalidRect(const TRect &max);
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
bool BacklitFx::doGetBBox(double frame, TRectD &bBox) {
Shinya Kitaoka 120a6e
  if (m_input.isConnected()) {
Shinya Kitaoka 120a6e
    bool ret = m_input->doGetBBox(frame, bBox);
Shinya Kitaoka 120a6e
    // devo scurire bgColor
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
namespace {
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <class t=""></class>
Shinya Kitaoka 120a6e
void computeBacklit(TRasterPT<t> dst, TRasterPT<t> src, const TPoint &srcPos,</t></t>
luz paz 6454c4
                    // coordinate di src[0,0] rispetto a dst;
Shinya Kitaoka 120a6e
                    // in genere srcPos.x,.y <= 0
Shinya Kitaoka 120a6e
                    const TPoint ¢er
Shinya Kitaoka 120a6e
                    // coordinate della sorgente di luce rispetto a dst;
Shinya Kitaoka 120a6e
                    ) {
Shinya Kitaoka 120a6e
  // TRect srcRect = src->getBounds() + srcPos;
Shinya Kitaoka 120a6e
  // assert(srcRect.contains(dst->getBounds()));
Shinya Kitaoka 120a6e
  // assert(srcRect.contains(center));
Shinya Kitaoka 120a6e
  UINT max = T::maxChannelValue;
Shinya Kitaoka 120a6e
  dst->lock();
Shinya Kitaoka 120a6e
  for (int x = 0; x < dst->getLx(); x++)
Shinya Kitaoka 120a6e
    for (int y = 0; y < dst->getLy(); y++) {
Shinya Kitaoka 120a6e
      int dx   = x - center.x;
Shinya Kitaoka 120a6e
      int dy   = y - center.y;
Shinya Kitaoka 120a6e
      int dist = (int)sqrt((float)(dx * dx + dy * dy));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      int d        = tmin(100, dist) + 1;
Shinya Kitaoka 120a6e
      double value = 0.0;
Shinya Kitaoka 120a6e
      for (int i = 0; i <= d; i++) {
Shinya Kitaoka 120a6e
        int xx = center.x + (x - center.x) * i / d - srcPos.x;
Shinya Kitaoka 120a6e
        int yy = center.y + (y - center.y) * i / d - srcPos.y;
Shinya Kitaoka 120a6e
        assert(src->getBounds().contains(TPoint(xx, yy)));
Shinya Kitaoka 120a6e
        value += (max - src->pixels(yy)[xx].m) * 2;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      value = value / dist;
Shinya Kitaoka 120a6e
      //    double value = sin(dist)<0?0:255;
Shinya Kitaoka 120a6e
      int v = tcrop((int)value, 0, (int)max);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      int srcX = -srcPos.x + x;
Shinya Kitaoka 120a6e
      int srcY = -srcPos.y + y;
Shinya Kitaoka 120a6e
      T srcPix(0, 0, 0, 0);
Shinya Kitaoka 120a6e
      if (src->getBounds().contains(TPoint(srcX, srcY)))
Shinya Kitaoka 120a6e
        srcPix = src->pixels(srcY)[srcX];
Shinya Kitaoka 120a6e
      srcPix.r = srcPix.r * (max - v) / max;
Shinya Kitaoka 120a6e
      srcPix.g = srcPix.g * (max - v) / max;
Shinya Kitaoka 120a6e
      srcPix.b = srcPix.b * (max - v) / max;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      dst->pixels(y)[x] = overPix(srcPix, T(v, v, v, v / 2));
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  dst->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void BacklitFx::doCompute(TTile &tile, double frame,
Shinya Kitaoka 120a6e
                          const TRenderSettings &ri) {
Shinya Kitaoka 120a6e
  if (!m_input.isConnected()) {
Shinya Kitaoka 120a6e
    tile.getRaster()->clear();
Shinya Kitaoka 120a6e
    return;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  TTile srcTile;
Shinya Kitaoka 120a6e
  srcTile.m_pos = tile.m_pos;
Shinya Kitaoka 120a6e
  TPoint srcPos(0, 0);
Shinya Kitaoka 120a6e
  TDimension srcSize = tile.getRaster()->getSize();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  TPoint center(-(int)tile.m_pos.x, -(int)tile.m_pos.y);
Shinya Kitaoka 120a6e
  center += TPoint(10, 10);
Shinya Kitaoka 120a6e
  if (center.x < 0) {
Shinya Kitaoka 120a6e
    srcSize.lx += -center.x;
Shinya Kitaoka 120a6e
    srcPos.x += center.x;
Shinya Kitaoka 120a6e
    srcTile.m_pos.x += center.x;
Shinya Kitaoka 120a6e
  } else if (center.x >= srcSize.lx) {
Shinya Kitaoka 120a6e
    srcSize.lx += center.x - srcSize.lx + 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (center.y < 0) {
Shinya Kitaoka 120a6e
    srcSize.ly += -center.y;
Shinya Kitaoka 120a6e
    srcPos.y += center.y;
Shinya Kitaoka 120a6e
    srcTile.m_pos.y += center.y;
Shinya Kitaoka 120a6e
  } else if (center.y >= srcSize.ly) {
Shinya Kitaoka 120a6e
    srcSize.ly += center.y - srcSize.ly + 1;
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if ((TRaster32P)tile.getRaster()) {
Shinya Kitaoka 120a6e
    TRaster32P src = TRaster32P(srcSize);
Shinya Kitaoka 120a6e
    src->clear();
Shinya Kitaoka 120a6e
    srcTile.getRaster() = src;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_input->compute(srcTile, frame, ri);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRaster32P dst = tile.getRaster();
Shinya Kitaoka 120a6e
    assert(dst);
Shinya Kitaoka 120a6e
    computeBacklit<tpixel32>(dst, src, srcPos, center);</tpixel32>
Shinya Kitaoka 120a6e
  } else if ((TRaster64P)tile.getRaster()) {
Shinya Kitaoka 120a6e
    TRaster64P src = TRaster64P(srcSize);
Shinya Kitaoka 120a6e
    src->clear();
Shinya Kitaoka 120a6e
    srcTile.getRaster() = src;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    m_input->compute(srcTile, frame, ri);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    TRaster64P dst = tile.getRaster();
Shinya Kitaoka 120a6e
    assert(dst);
Shinya Kitaoka 120a6e
    computeBacklit<tpixel64>(dst, src, srcPos, center);</tpixel64>
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // dst->copy(src,srcPos);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int BacklitFx::getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 120a6e
                                    const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  return TRasterFx::memorySize(rect, frame, info);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//---------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
TRect BacklitFx::getInvalidRect(const TRect &max) {
Shinya Kitaoka 120a6e
  if (!m_input.isConnected()) return TRect();
Shinya Kitaoka 120a6e
  TRect rect = m_input->getInvalidRect(max);
Shinya Kitaoka 120a6e
  TRect ris  = max;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (rect.x0 >= 0)
Shinya Kitaoka 120a6e
    ris.x0 = tmin(rect.x0, max.x1);
Shinya Kitaoka 120a6e
  else if (rect.x1 <= 0)
Shinya Kitaoka 120a6e
    ris.x1 = tmax(rect.x1, max.x0);
Shinya Kitaoka 120a6e
  if (rect.y0 >= 0)
Shinya Kitaoka 120a6e
    ris.y0 = tmin(rect.y0, max.y1);
Shinya Kitaoka 120a6e
  else if (rect.y1 <= 0)
Shinya Kitaoka 120a6e
    ris.y1 = tmax(rect.y1, max.y0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  return ris;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
// FX_PLUGIN_IDENTIFIER(BacklitFx        , "backlitFx")