Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "tspectrumparam.h"
shun-iwasawa 8cac36
#include "globalcontrollablefx.h"
Toshihiro Shimizu 890ddd
shun-iwasawa 8cac36
class MultiToneFx final : public GlobalControllableFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(MultiToneFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TSpectrumParamP m_colors;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  MultiToneFx() {
Tact Yoshida 1d7cf9
    std::vector<tspectrum::colorkey> colors = {</tspectrum::colorkey>
Tact Yoshida 1d7cf9
        TSpectrum::ColorKey(0, TPixel32::White),
Tact Yoshida 1d7cf9
        TSpectrum::ColorKey(0.5, TPixel32::Yellow),
Tact Yoshida 1d7cf9
        TSpectrum::ColorKey(1, TPixel32::Red)};
Tact Yoshida 1d7cf9
    m_colors = TSpectrumParamP(colors);
Shinya Kitaoka 120a6e
    bool ret = m_colors->isKeyframe(0);
Shinya Kitaoka 120a6e
    bindParam(this, "colors", m_colors);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
    ret = getParams()->getParam(0)->isKeyframe(0);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~MultiToneFx(){};
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 &) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 38fd86
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 38fd86
    return true;
Shinya Kitaoka 38fd86
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" pixelgray,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doMultiTone(const TRasterPT<pixel> &ras,</pixel>
Shinya Kitaoka 120a6e
                 const TSpectrumT<pixel> &spectrum) {</pixel>
Shinya Kitaoka 120a6e
  double aux = (double)PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
  int j;
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  for (j = 0; j < ras->getLy(); j++) {
Shinya Kitaoka 120a6e
    PIXEL *pix    = ras->pixels(j);
Shinya Kitaoka 120a6e
    PIXEL *endPix = pix + ras->getLx();
Shinya Kitaoka 120a6e
    while (pix < endPix) {
Shinya Kitaoka 120a6e
      if (pix->m) {
Shinya Kitaoka 120a6e
        CHANNEL_TYPE reference;
Shinya Kitaoka 120a6e
        double s;
Shinya Kitaoka 120a6e
        reference = PIXELGRAY::from(*pix).value;
Shinya Kitaoka 120a6e
        s         = reference / aux;
Shinya Kitaoka 120a6e
        if (pix->m == aux) {
Shinya Kitaoka 120a6e
          *pix = spectrum.getPremultipliedValue(s);
Shinya Kitaoka 120a6e
        } else {
Shinya Kitaoka 120a6e
          PIXEL tmp = spectrum.getPremultipliedValue(s);
Shinya Kitaoka 120a6e
          s         = pix->m / aux;
Shinya Kitaoka 120a6e
          tmp.r     = (CHANNEL_TYPE)(tmp.r * s);
Shinya Kitaoka 120a6e
          tmp.g     = (CHANNEL_TYPE)(tmp.g * s);
Shinya Kitaoka 120a6e
          tmp.b     = (CHANNEL_TYPE)(tmp.b * s);
Shinya Kitaoka 120a6e
          tmp.m     = (CHANNEL_TYPE)(tmp.m * s);
Shinya Kitaoka 120a6e
          *pix      = tmp;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
      }
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 MultiToneFx::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
  m_input->compute(tile, frame, ri);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (TRaster32P raster32 = tile.getRaster())
Shinya Kitaoka 120a6e
    doMultiTone<tpixel32, tpixelgr8,="" uchar="">(raster32,</tpixel32,>
Shinya Kitaoka 120a6e
                                            m_colors->getValue(frame));
Shinya Kitaoka 120a6e
  else if (TRaster64P raster64 = tile.getRaster())
Shinya Kitaoka 120a6e
    doMultiTone<tpixel64, tpixelgr16,="" ushort="">(raster64,</tpixel64,>
Shinya Kitaoka 120a6e
                                              m_colors->getValue64(frame));
Shinya Kitaoka 120a6e
  else
Shinya Kitaoka 120a6e
    throw TException("MultiToneFx: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(MultiToneFx, "multiToneFx")