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
Shinya Kitaoka d1f6c4
class Bright_ContFx final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(Bright_ContFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_bright;
Shinya Kitaoka 120a6e
  TDoubleParamP m_contrast;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  Bright_ContFx() : m_bright(0.0), m_contrast(0.0) {
Shinya Kitaoka 120a6e
    bindParam(this, "brightness", m_bright);
Shinya Kitaoka 120a6e
    bindParam(this, "contrast", m_contrast);
Shinya Kitaoka 120a6e
    m_bright->setValueRange(-127, 127);
Shinya Kitaoka 120a6e
    m_contrast->setValueRange(-127, 127);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~Bright_ContFx(){};
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
      bBox = TRectD();
Shinya Kitaoka 120a6e
      return false;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    return m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
  };
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &) override;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//===================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename pixel,="" t="" typename=""></typename>
Shinya Kitaoka 120a6e
void my_compute_lut(double contrast, double brightness, std::vector<t> &lut) {</t>
Shinya Kitaoka 120a6e
  int i;
Shinya Kitaoka 120a6e
  double value, nvalue, power;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int half_maxChannelValue = tfloor(PIXEL::maxChannelValue / 2.0);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int lutSize = PIXEL::maxChannelValue + 1;
Shinya Kitaoka 120a6e
  for (i = 0; i < lutSize; i++) {
Shinya Kitaoka 120a6e
    value = i / double(PIXEL::maxChannelValue);
Shinya Kitaoka 120a6e
    /*brightness*/
Shinya Kitaoka 120a6e
    if (brightness < 0.0)
Shinya Kitaoka 120a6e
      value = value * (1.0 + brightness);
Shinya Kitaoka 120a6e
    else
Shinya Kitaoka 120a6e
      value = value + ((1.0 - value) * brightness);
Shinya Kitaoka 120a6e
    /*contrast*/
Shinya Kitaoka 120a6e
    if (contrast < 0.0) {
Shinya Kitaoka 120a6e
      if (value > 0.5)
Shinya Kitaoka 120a6e
        nvalue = 1.0 - value;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        nvalue                 = value;
Shinya Kitaoka 120a6e
      if (nvalue < 0.0) nvalue = 0.0;
Shinya Kitaoka 120a6e
      nvalue = 0.5 * pow(nvalue * 2.0, (double)(1.0 + contrast));
Shinya Kitaoka 120a6e
      if (value > 0.5)
Shinya Kitaoka 120a6e
        value = 1.0 - nvalue;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        value = nvalue;
Shinya Kitaoka 120a6e
    } else {
Shinya Kitaoka 120a6e
      if (value > 0.5)
Shinya Kitaoka 120a6e
        nvalue = 1.0 - value;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        nvalue                 = value;
Shinya Kitaoka 120a6e
      if (nvalue < 0.0) nvalue = 0.0;
Shinya Kitaoka 120a6e
      power = (contrast == 1.0) ? half_maxChannelValue : 1.0 / (1.0 - contrast);
Shinya Kitaoka 120a6e
      nvalue = 0.5 * pow(2.0 * nvalue, power);
Shinya Kitaoka 120a6e
      if (value > 0.5)
Shinya Kitaoka 120a6e
        value = 1.0 - nvalue;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        value = nvalue;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
    lut[i] = (int)(value * PIXEL::maxChannelValue);
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doBrightnessContrast(TRasterPT<pixel> ras, double contrast,</pixel>
Shinya Kitaoka 120a6e
                          double brightness) {
Shinya Kitaoka 120a6e
  int lx = ras->getLx();
Shinya Kitaoka 120a6e
  int ly = ras->getLy();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  std::vector<channel_type> lut(PIXEL::maxChannelValue + 1);</channel_type>
Shinya Kitaoka 120a6e
  my_compute_lut<pixel, channel_type="">(contrast, brightness, lut);</pixel,>
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) {
Shinya Kitaoka 120a6e
        *pix   = depremultiply(*pix);
Shinya Kitaoka 120a6e
        pix->r = lut[pix->r];
Shinya Kitaoka 120a6e
        pix->g = lut[pix->g];
Shinya Kitaoka 120a6e
        pix->b = lut[pix->b];
Shinya Kitaoka 120a6e
        pix->m = pix->m;
Shinya Kitaoka 120a6e
        *pix   = premultiply(*pix);
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      pix++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void Bright_ContFx::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
  double brightness           = m_bright->getValue(frame) / 127.0;
Shinya Kitaoka 120a6e
  double contrast             = m_contrast->getValue(frame) / 127.0;
Shinya Kitaoka 120a6e
  if (contrast > 1) contrast  = 1;
Shinya Kitaoka 120a6e
  if (contrast < -1) contrast = -1;
Shinya Kitaoka 120a6e
  TRaster32P raster32         = tile.getRaster();
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doBrightnessContrast<tpixel32, uchar="">(raster32, contrast, brightness);</tpixel32,>
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64 = tile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doBrightnessContrast<tpixel64, ushort="">(raster64, contrast, brightness);</tpixel64,>
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(Bright_ContFx, "brightContFx")