Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "tpixelutils.h"
shun-iwasawa 8cac36
#include "globalcontrollablefx.h"
Toshihiro Shimizu 890ddd
shun-iwasawa 8cac36
class Bright_ContFx final : public GlobalControllableFx {
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);
shun-iwasawa 481b59
shun-iwasawa 481b59
    enableComputeInFloat(true);
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
shun-iwasawa 8cac36
        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
shun-iwasawa 8cac36
        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
shun-iwasawa 481b59
void my_compute_lut_float(double contrast, double brightness,
shun-iwasawa 481b59
                          std::vector<float> &lut, float &d0, float &d1) {</float>
shun-iwasawa 481b59
  int i;
shun-iwasawa 481b59
  float value, nvalue, power;
shun-iwasawa 481b59
shun-iwasawa 481b59
  int half_maxChannelValue = tfloor(TPixel64::maxChannelValue / 2.0);
shun-iwasawa 481b59
shun-iwasawa 481b59
  int lutSize = TPixel64::maxChannelValue + 1;
shun-iwasawa 481b59
  for (i = 0; i < lutSize; i++) {
shun-iwasawa 481b59
    value = i / float(TPixel64::maxChannelValue);
shun-iwasawa 481b59
    /*brightness*/
shun-iwasawa 481b59
    if (brightness < 0.0)
shun-iwasawa 481b59
      value = value * (1.f + brightness);
shun-iwasawa 481b59
    else
shun-iwasawa 481b59
      value = value + ((1.f - value) * brightness);
shun-iwasawa 481b59
    /*contrast*/
shun-iwasawa 481b59
    if (contrast < 0.f) {
shun-iwasawa 481b59
      if (value > 0.5f)
shun-iwasawa 481b59
        nvalue = 1.f - value;
shun-iwasawa 481b59
      else
shun-iwasawa 481b59
        nvalue = value;
shun-iwasawa 481b59
      if (nvalue < 0.f) nvalue = 0.f;
shun-iwasawa 481b59
      nvalue = 0.5f * pow(nvalue * 2.f, (double)(1.f + contrast));
shun-iwasawa 481b59
      if (value > 0.5f)
shun-iwasawa 481b59
        value = 1.0f - nvalue;
shun-iwasawa 481b59
      else
shun-iwasawa 481b59
        value = nvalue;
shun-iwasawa 481b59
    } else {
shun-iwasawa 481b59
      if (value > 0.5f)
shun-iwasawa 481b59
        nvalue = 1.f - value;
shun-iwasawa 481b59
      else
shun-iwasawa 481b59
        nvalue = value;
shun-iwasawa 481b59
      if (nvalue < 0.f) nvalue = 0.f;
shun-iwasawa 481b59
      power =
shun-iwasawa 481b59
          (contrast == 1.0f) ? half_maxChannelValue : 1.f / (1.f - contrast);
shun-iwasawa 481b59
      nvalue = 0.5f * pow(2.f * nvalue, power);
shun-iwasawa 481b59
      if (value > 0.5f)
shun-iwasawa 481b59
        value = 1.f - nvalue;
shun-iwasawa 481b59
      else
shun-iwasawa 481b59
        value = nvalue;
shun-iwasawa 481b59
    }
shun-iwasawa 481b59
    lut[i] = value;
shun-iwasawa 481b59
  }
shun-iwasawa 481b59
shun-iwasawa 481b59
  d0 = (lut[1] - lut[0]) * float(TPixel64::maxChannelValue);
shun-iwasawa 481b59
  d1 = (lut[TPixel64::maxChannelValue] - lut[TPixel64::maxChannelValue - 1]) *
shun-iwasawa 481b59
       float(TPixel64::maxChannelValue);
shun-iwasawa 481b59
}
shun-iwasawa 481b59
shun-iwasawa 481b59
void doBrightnessContrastFloat(TRasterFP ras, double contrast,
shun-iwasawa 481b59
                               double brightness) {
shun-iwasawa 481b59
  int lx = ras->getLx();
shun-iwasawa 481b59
  int ly = ras->getLy();
shun-iwasawa 481b59
shun-iwasawa 481b59
  // create lut with 65536 levels
shun-iwasawa 481b59
  std::vector<float> lut(TPixel64::maxChannelValue + 1);</float>
shun-iwasawa 481b59
  // values less than 0.0 and more than 1.0 will be linear
shun-iwasawa 481b59
  float d0, d1;
shun-iwasawa 481b59
  my_compute_lut_float(contrast, brightness, lut, d0, d1);
shun-iwasawa 481b59
shun-iwasawa 481b59
  auto getLutValue = [&](float val) {
shun-iwasawa 481b59
    if (val < 0.f)
shun-iwasawa 481b59
      return lut[0] + d0 * val;
shun-iwasawa 481b59
    else if (val >= 1.f)
shun-iwasawa 481b59
      return lut[TPixel64::maxChannelValue] + d1 * (val - 1.f);
shun-iwasawa 481b59
    float v     = val * float(TPixel64::maxChannelValue);
shun-iwasawa 481b59
    int id      = (int)tfloor(v);
shun-iwasawa 481b59
    float ratio = v - float(id);
shun-iwasawa 481b59
    return lut[id] * (1.f - ratio) + lut[id + 1] * ratio;
shun-iwasawa 481b59
  };
shun-iwasawa 481b59
shun-iwasawa 481b59
  int j;
shun-iwasawa 481b59
  ras->lock();
shun-iwasawa 481b59
  for (j = 0; j < ly; j++) {
shun-iwasawa 481b59
    TPixelF *pix    = ras->pixels(j);
shun-iwasawa 481b59
    TPixelF *endPix = pix + lx;
shun-iwasawa 481b59
    while (pix < endPix) {
shun-iwasawa 481b59
      if (pix->m) {
shun-iwasawa 481b59
        *pix   = depremultiply(*pix);
shun-iwasawa 481b59
        pix->r = getLutValue(pix->r);
shun-iwasawa 481b59
        pix->g = getLutValue(pix->g);
shun-iwasawa 481b59
        pix->b = getLutValue(pix->b);
shun-iwasawa 481b59
        pix->m = pix->m;
shun-iwasawa 481b59
        *pix   = premultiply(*pix);
shun-iwasawa 481b59
      }
shun-iwasawa 481b59
      pix++;
shun-iwasawa 481b59
    }
shun-iwasawa 481b59
  }
shun-iwasawa 481b59
  ras->unlock();
shun-iwasawa 481b59
}
shun-iwasawa 481b59
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
shun-iwasawa 8cac36
  double brightness = m_bright->getValue(frame) / 127.0;
shun-iwasawa 8cac36
  double contrast   = m_contrast->getValue(frame) / 127.0;
shun-iwasawa 8cac36
  if (contrast > 1) contrast = 1;
Shinya Kitaoka 120a6e
  if (contrast < -1) contrast = -1;
shun-iwasawa 8cac36
  TRaster32P raster32 = tile.getRaster();
shun-iwasawa 481b59
  TRaster64P raster64 = tile.getRaster();
shun-iwasawa 481b59
  TRasterFP rasterF   = tile.getRaster();
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doBrightnessContrast<tpixel32, uchar="">(raster32, contrast, brightness);</tpixel32,>
shun-iwasawa 481b59
  else if (raster64)
shun-iwasawa 481b59
    doBrightnessContrast<tpixel64, ushort="">(raster64, contrast, brightness);</tpixel64,>
shun-iwasawa 481b59
  else if (rasterF)
shun-iwasawa 481b59
    doBrightnessContrastFloat(rasterF, contrast, brightness);
shun-iwasawa 481b59
  else
shun-iwasawa 481b59
    throw TException("Brightness&Contrast: unsupported Pixel Type");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(Bright_ContFx, "brightContFx")