Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
//#include "trop.h"
Toshihiro Shimizu 890ddd
#include <math.h></math.h>
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
shun-iwasawa 8cac36
#include "globalcontrollablefx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//==================================================================
Toshihiro Shimizu 890ddd
shun-iwasawa 8cac36
class ChannelMixerFx final : public GlobalControllableFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(ChannelMixerFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_r_r;
Shinya Kitaoka 120a6e
  TDoubleParamP m_g_r;
Shinya Kitaoka 120a6e
  TDoubleParamP m_b_r;
Shinya Kitaoka 120a6e
  TDoubleParamP m_m_r;
Shinya Kitaoka 120a6e
  TDoubleParamP m_r_g;
Shinya Kitaoka 120a6e
  TDoubleParamP m_g_g;
Shinya Kitaoka 120a6e
  TDoubleParamP m_b_g;
Shinya Kitaoka 120a6e
  TDoubleParamP m_m_g;
Shinya Kitaoka 120a6e
  TDoubleParamP m_r_b;
Shinya Kitaoka 120a6e
  TDoubleParamP m_g_b;
Shinya Kitaoka 120a6e
  TDoubleParamP m_b_b;
Shinya Kitaoka 120a6e
  TDoubleParamP m_m_b;
Shinya Kitaoka 120a6e
  TDoubleParamP m_r_m;
Shinya Kitaoka 120a6e
  TDoubleParamP m_g_m;
Shinya Kitaoka 120a6e
  TDoubleParamP m_b_m;
Shinya Kitaoka 120a6e
  TDoubleParamP m_m_m;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  ChannelMixerFx()
Shinya Kitaoka 120a6e
      : m_r_r(1.0)
Shinya Kitaoka 120a6e
      , m_g_r(0.0)
Shinya Kitaoka 120a6e
      , m_b_r(0.0)
Shinya Kitaoka 120a6e
      , m_m_r(0.0)
Shinya Kitaoka 120a6e
      , m_r_g(0.0)
Shinya Kitaoka 120a6e
      , m_g_g(1.0)
Shinya Kitaoka 120a6e
      , m_b_g(0.0)
Shinya Kitaoka 120a6e
      , m_m_g(0.0)
Shinya Kitaoka 120a6e
      , m_r_b(0.0)
Shinya Kitaoka 120a6e
      , m_g_b(0.0)
Shinya Kitaoka 120a6e
      , m_b_b(1.0)
Shinya Kitaoka 120a6e
      , m_m_b(0.0)
Shinya Kitaoka 120a6e
      , m_r_m(0.0)
Shinya Kitaoka 120a6e
      , m_g_m(0.0)
Shinya Kitaoka 120a6e
      , m_b_m(0.0)
Shinya Kitaoka 120a6e
      , m_m_m(1.0)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  {
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
    bindParam(this, "red_to_red", m_r_r);
Shinya Kitaoka 120a6e
    bindParam(this, "green_to_red", m_g_r);
Shinya Kitaoka 120a6e
    bindParam(this, "blue_to_red", m_b_r);
Shinya Kitaoka 120a6e
    bindParam(this, "matte_to_red", m_m_r);
Shinya Kitaoka 120a6e
    bindParam(this, "red_to_green", m_r_g);
Shinya Kitaoka 120a6e
    bindParam(this, "green_to_green", m_g_g);
Shinya Kitaoka 120a6e
    bindParam(this, "blue_to_green", m_b_g);
Shinya Kitaoka 120a6e
    bindParam(this, "matte_to_green", m_m_g);
Shinya Kitaoka 120a6e
    bindParam(this, "red_to_blue", m_r_b);
Shinya Kitaoka 120a6e
    bindParam(this, "green_to_blue", m_g_b);
Shinya Kitaoka 120a6e
    bindParam(this, "blue_to_blue", m_b_b);
Shinya Kitaoka 120a6e
    bindParam(this, "matte_to_blue", m_m_b);
Shinya Kitaoka 120a6e
    bindParam(this, "red_to_matte", m_r_m);
Shinya Kitaoka 120a6e
    bindParam(this, "green_to_matte", m_g_m);
Shinya Kitaoka 120a6e
    bindParam(this, "blue_to_matte", m_b_m);
Shinya Kitaoka 120a6e
    bindParam(this, "matte_to_matte", m_m_m);
Shinya Kitaoka 120a6e
    m_r_r->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_r_g->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_r_b->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_r_m->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_g_r->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_g_g->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_g_b->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_g_m->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_b_r->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_b_g->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_b_b->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_b_m->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_m_r->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_m_g->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_m_b->setValueRange(0, 1);
Shinya Kitaoka 120a6e
    m_m_m->setValueRange(0, 1);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ~ChannelMixerFx(){};
Toshihiro Shimizu 890ddd
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 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &ri) override;
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
Shinya Kitaoka 120a6e
namespace {
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Shinya Kitaoka 120a6e
void depremult(PIXEL *pix) {
Shinya Kitaoka 120a6e
  if (!pix->m) return;
Shinya Kitaoka 120a6e
  double depremult = (double)PIXEL::maxChannelValue / pix->m;
Shinya Kitaoka 120a6e
  pix->r           = (CHANNEL_TYPE)(pix->r * depremult);
Shinya Kitaoka 120a6e
  pix->g           = (CHANNEL_TYPE)(pix->g * depremult);
Shinya Kitaoka 120a6e
  pix->b           = (CHANNEL_TYPE)(pix->b * depremult);
Toshihiro Shimizu 890ddd
}
shun-iwasawa 8cac36
}  // namespace
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doChannelMixer(TRasterPT<pixel> ras, double r_r, double r_g, double r_b,</pixel>
Shinya Kitaoka 120a6e
                    double r_m, double g_r, double g_g, double g_b, double g_m,
Shinya Kitaoka 120a6e
                    double b_r, double b_g, double b_b, double b_m, double m_r,
Shinya Kitaoka 120a6e
                    double m_g, double m_b, double m_m) {
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
      depremult<pixel, channel_type="">(pix);  // if removed, a black line appears</pixel,>
Shinya Kitaoka 120a6e
                                            // in the edge of a level (default
Shinya Kitaoka 120a6e
                                            // values, not black lines)
Shinya Kitaoka 120a6e
      double red   = pix->r * r_r + pix->g * g_r + pix->b * b_r + pix->m * m_r;
Shinya Kitaoka 120a6e
      double green = pix->r * r_g + pix->g * g_g + pix->b * b_g + pix->m * m_g;
Shinya Kitaoka 120a6e
      double blue  = pix->r * r_b + pix->g * g_b + pix->b * b_b + pix->m * m_b;
Shinya Kitaoka 120a6e
      double matte = pix->r * r_m + pix->g * g_m + pix->b * b_m + pix->m * m_m;
Shinya Kitaoka 120a6e
      red          = tcrop(red, 0.0, aux);
Shinya Kitaoka 120a6e
      green        = tcrop(green, 0.0, aux);
Shinya Kitaoka 120a6e
      blue         = tcrop(blue, 0.0, aux);
Shinya Kitaoka 120a6e
      matte        = tcrop(matte, 0.0, aux);
Shinya Kitaoka 120a6e
      pix->r       = (CHANNEL_TYPE)red;
Shinya Kitaoka 120a6e
      pix->g       = (CHANNEL_TYPE)green;
Shinya Kitaoka 120a6e
      pix->b       = (CHANNEL_TYPE)blue;
Shinya Kitaoka 120a6e
      pix->m       = (CHANNEL_TYPE)matte;
Shinya Kitaoka 38fd86
      *pix = premultiply(*pix);  // if removed, a white edged line appears in
Shinya Kitaoka 120a6e
                                 // the edge of a level (if m>r, g, b)
Shinya Kitaoka 120a6e
      pix++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void ChannelMixerFx::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
  double r_r = m_r_r->getValue(frame);
Shinya Kitaoka 120a6e
  double r_g = m_r_g->getValue(frame);
Shinya Kitaoka 120a6e
  double r_b = m_r_b->getValue(frame);
Shinya Kitaoka 120a6e
  double r_m = m_r_m->getValue(frame);
Shinya Kitaoka 120a6e
  double g_r = m_g_r->getValue(frame);
Shinya Kitaoka 120a6e
  double g_g = m_g_g->getValue(frame);
Shinya Kitaoka 120a6e
  double g_b = m_g_b->getValue(frame);
Shinya Kitaoka 120a6e
  double g_m = m_g_m->getValue(frame);
Shinya Kitaoka 120a6e
  double b_r = m_b_r->getValue(frame);
Shinya Kitaoka 120a6e
  double b_g = m_b_g->getValue(frame);
Shinya Kitaoka 120a6e
  double b_b = m_b_b->getValue(frame);
Shinya Kitaoka 120a6e
  double b_m = m_b_m->getValue(frame);
Shinya Kitaoka 120a6e
  double m_r = m_m_r->getValue(frame);
Shinya Kitaoka 120a6e
  double m_g = m_m_g->getValue(frame);
Shinya Kitaoka 120a6e
  double m_b = m_m_b->getValue(frame);
Shinya Kitaoka 120a6e
  double m_m = m_m_m->getValue(frame);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRaster32P raster32 = tile.getRaster();
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doChannelMixer<tpixel32, uchar="">(raster32, r_r, r_g, r_b, r_m, g_r, g_g, g_b,</tpixel32,>
Shinya Kitaoka 120a6e
                                    g_m, b_r, b_g, b_b, b_m, m_r, m_g, m_b,
Shinya Kitaoka 120a6e
                                    m_m);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64 = tile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doChannelMixer<tpixel64, ushort="">(raster64, r_r, r_g, r_b, r_m, g_r, g_g,</tpixel64,>
Shinya Kitaoka 120a6e
                                       g_b, g_m, b_r, b_g, b_b, b_m, m_r, m_g,
Shinya Kitaoka 120a6e
                                       m_b, m_m);
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(ChannelMixerFx, "channelMixerFx")