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
//===================================================================
Toshihiro Shimizu 890ddd
Shinya Kitaoka d1f6c4
class EmbossFx final : public TStandardRasterFx {
Shinya Kitaoka 120a6e
  FX_PLUGIN_DECLARATION(EmbossFx)
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  TRasterFxPort m_input;
Shinya Kitaoka 120a6e
  TDoubleParamP m_intensity;
Shinya Kitaoka 120a6e
  TDoubleParamP m_elevation;
Shinya Kitaoka 120a6e
  TDoubleParamP m_direction;
Shinya Kitaoka 120a6e
  TDoubleParamP m_radius;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Shinya Kitaoka 120a6e
  EmbossFx()
Shinya Kitaoka 120a6e
      : m_intensity(0.5), m_elevation(45.0), m_direction(90.0), m_radius(1.0) {
Shinya Kitaoka 120a6e
    m_radius->setMeasureName("fxLength");
Shinya Kitaoka 120a6e
    bindParam(this, "intensity", m_intensity);
Shinya Kitaoka 120a6e
    bindParam(this, "elevation", m_elevation);
Shinya Kitaoka 120a6e
    bindParam(this, "direction", m_direction);
Shinya Kitaoka 120a6e
    bindParam(this, "radius", m_radius);
Shinya Kitaoka 120a6e
    addInputPort("Source", m_input);
Shinya Kitaoka 120a6e
    m_intensity->setValueRange(0.0, 1.0, 0.1);
Shinya Kitaoka 120a6e
    m_elevation->setValueRange(0.0, 360.0);
Shinya Kitaoka 120a6e
    m_direction->setValueRange(0.0, 360.0);
Shinya Kitaoka 120a6e
    m_radius->setValueRange(0.0, 10.0);
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  ~EmbossFx(){};
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
      bool ret = m_input->doGetBBox(frame, bBox, info);
Shinya Kitaoka 120a6e
      return ret;
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 120a6e
  void transform(double frame, int port, const TRectD &rectOnOutput,
Shinya Kitaoka 120a6e
                 const TRenderSettings &infoOnOutput, TRectD &rectOnInput,
Shinya Kitaoka 473e70
                 TRenderSettings &infoOnInput) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  void doCompute(TTile &tile, double frame, const TRenderSettings &ri) override;
Shinya Kitaoka 120a6e
  int getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 473e70
                           const TRenderSettings &info) override;
Shinya Kitaoka 120a6e
Shinya Kitaoka 473e70
  bool canHandle(const TRenderSettings &info, double frame) override {
Shinya Kitaoka 120a6e
    return (isAlmostIsotropic(info.m_affine));
Shinya Kitaoka 120a6e
  }
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
template <typename channel_type="" pixel,="" pixelgray,="" typename=""></typename>
Shinya Kitaoka 120a6e
void doEmboss(TRasterPT<pixel> ras, TRasterPT<pixel> srcraster, double azimuth,</pixel></pixel>
Shinya Kitaoka 120a6e
              double elevation, double intensity, double radius) {
Shinya Kitaoka 120a6e
  double Lx  = cos(azimuth) * cos(elevation) * PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
  double Ly  = sin(azimuth) * cos(elevation) * PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
  double Lz  = sin(elevation) * PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
  double Nz  = (6 * PIXEL::maxChannelValue) * (1 - intensity);
Shinya Kitaoka 120a6e
  double Nz2 = Nz * Nz;
Shinya Kitaoka 120a6e
  int j, m, n;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int border            = (int)radius + 1;
Shinya Kitaoka 120a6e
  double borderFracMult = radius - (int)radius;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  int wrap = srcraster->getWrap();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double nsbuffer, ewbuffer;
Shinya Kitaoka 120a6e
  double nsFracbuffer, ewFracbuffer;
Shinya Kitaoka 120a6e
  double NdotL;
Shinya Kitaoka 120a6e
  double background = Lz;
Shinya Kitaoka 120a6e
  srcraster->lock();
Shinya Kitaoka 120a6e
  ras->lock();
Shinya Kitaoka 120a6e
  for (j = border; j < srcraster->getLy() - border; j++) {
Shinya Kitaoka 120a6e
    PIXEL *pixout    = ras->pixels(j - border);
Shinya Kitaoka 120a6e
    PIXEL *pix       = srcraster->pixels(j) + border;
Shinya Kitaoka 120a6e
    PIXEL *endPixout = pixout + ras->getLx();
Shinya Kitaoka 120a6e
    while (pixout < endPixout) {
Shinya Kitaoka 120a6e
      double val;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Explanation: The fx is performed by calculating kind of a normal vector
Shinya Kitaoka 120a6e
      // N for
Shinya Kitaoka 120a6e
      // the discrete surface generated by the {(x,y,v)} of the raster, where v
Shinya Kitaoka 120a6e
      // is the
Shinya Kitaoka 120a6e
      // grey tone of the pixel at (x,y).
Shinya Kitaoka 120a6e
      // This vector is then dot-product-matched with the input light ray
Shinya Kitaoka 120a6e
      // vector, and a pixel
Shinya Kitaoka 120a6e
      // value according with the product is then returned.
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Here, we build up the sums for the X and Y components of N.
Shinya Kitaoka 120a6e
      nsbuffer = 0;
Shinya Kitaoka 120a6e
      ewbuffer = 0;
Shinya Kitaoka 120a6e
      for (m = 1; m < border; m++)
Shinya Kitaoka 120a6e
        for (n = -m; n <= m; n++) {
Shinya Kitaoka 120a6e
          nsbuffer += PIXELGRAY::from(*(pix + m * wrap + n)).value;
Shinya Kitaoka 120a6e
          nsbuffer -= PIXELGRAY::from(*(pix - m * wrap + n)).value;
Shinya Kitaoka 120a6e
          ewbuffer += PIXELGRAY::from(*(pix + n * wrap + m)).value;
Shinya Kitaoka 120a6e
          ewbuffer -= PIXELGRAY::from(*(pix + n * wrap - m)).value;
Shinya Kitaoka 120a6e
        }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // As the fx radius (aka border) is a double, we make its fractionary part
Shinya Kitaoka 120a6e
      // count less. This ensures continuity of the result against radius
Shinya Kitaoka 120a6e
      // changes.
Shinya Kitaoka 120a6e
      nsFracbuffer = 0;
Shinya Kitaoka 120a6e
      ewFracbuffer = 0;
Shinya Kitaoka 120a6e
      for (n = -m; n <= m; n++) {
Shinya Kitaoka 120a6e
        nsFracbuffer += PIXELGRAY::from(*(pix + m * wrap + n)).value;
Shinya Kitaoka 120a6e
        nsFracbuffer -= PIXELGRAY::from(*(pix - m * wrap + n)).value;
Shinya Kitaoka 120a6e
        ewFracbuffer += PIXELGRAY::from(*(pix + n * wrap + m)).value;
Shinya Kitaoka 120a6e
        ewFracbuffer -= PIXELGRAY::from(*(pix + n * wrap - m)).value;
Shinya Kitaoka 120a6e
      }
Shinya Kitaoka 120a6e
      nsbuffer += nsFracbuffer * borderFracMult;
Shinya Kitaoka 120a6e
      ewbuffer += ewFracbuffer * borderFracMult;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
      // Here the dot-product is performed and the result is finally returned.
Shinya Kitaoka 120a6e
      double Nx = ewbuffer;
Shinya Kitaoka 120a6e
      double Ny = nsbuffer;
Shinya Kitaoka 120a6e
      Nx        = Nx / radius;
Shinya Kitaoka 120a6e
      Ny        = Ny / radius;
Shinya Kitaoka 120a6e
      // val= 127+sinsin*nordsud(pix, wrap)+coscos*eastwest(pix, wrap);
Shinya Kitaoka 120a6e
      if (Nx == 0 && Ny == 0)
Shinya Kitaoka 120a6e
        val = background;
Shinya Kitaoka 120a6e
      else if ((NdotL = Nx * Lx + Ny * Ly + Nz * Lz) < 0)
Shinya Kitaoka 120a6e
        val = 0;
Shinya Kitaoka 120a6e
      else
Shinya Kitaoka 120a6e
        val       = NdotL / sqrt(Nx * Nx + Ny * Ny + Nz2);
Shinya Kitaoka 120a6e
      (pixout)->r = (val < PIXEL::maxChannelValue)
Shinya Kitaoka 120a6e
                        ? (val > 0 ? (CHANNEL_TYPE)val : 0)
Shinya Kitaoka 120a6e
                        : PIXEL::maxChannelValue;
Shinya Kitaoka 120a6e
      (pixout)->g = pixout->r;
Shinya Kitaoka 120a6e
      (pixout)->b = pixout->r;
Shinya Kitaoka 120a6e
      (pixout)->m = (pix)->m;
Shinya Kitaoka 120a6e
      *pixout     = premultiply(*pixout);
Rozhuk Ivan 823a31
      pix++;
Rozhuk Ivan 823a31
      pixout++;
Shinya Kitaoka 120a6e
    }
Shinya Kitaoka 120a6e
  }
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  srcraster->unlock();
Shinya Kitaoka 120a6e
  ras->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void EmbossFx::doCompute(TTile &tile, double frame, const TRenderSettings &ri) {
Shinya Kitaoka 120a6e
  if (!m_input.isConnected()) return;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double min, max, step;
Shinya Kitaoka 120a6e
  m_radius->getValueRange(min, max, step);
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  double scale     = sqrt(fabs(ri.m_affine.det()));
Shinya Kitaoka 120a6e
  double radius    = tcrop(m_radius->getValue(frame), min, max) * scale;
Shinya Kitaoka 120a6e
  double direction = (m_direction->getValue(frame));
Shinya Kitaoka 120a6e
  double elevation = (m_elevation->getValue(frame)) * M_PI_180;
Shinya Kitaoka 120a6e
  double intensity = m_intensity->getValue(frame);
Shinya Kitaoka 120a6e
  double azimuth   = direction * M_PI_180;
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  // NOTE: This enlargement is perhaps needed in the calculation of the fx - but
Shinya Kitaoka 120a6e
  // no output will
Shinya Kitaoka 120a6e
  // be generated for it - so there is no trace of it in the doGetBBox
Shinya Kitaoka 120a6e
  // function...
Shinya Kitaoka 120a6e
  int border = radius + 1;
Shinya Kitaoka 120a6e
  TRasterP srcRas =
Shinya Kitaoka 120a6e
      tile.getRaster()->create(tile.getRaster()->getLx() + border * 2,
Shinya Kitaoka 120a6e
                               tile.getRaster()->getLy() + border * 2);
Shinya Kitaoka 120a6e
  TTile srcTile(srcRas, tile.m_pos - TPointD(border, border));
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  m_input->compute(srcTile, frame, ri);
Shinya Kitaoka 120a6e
  TRaster32P raster32    = tile.getRaster();
Shinya Kitaoka 120a6e
  TRaster32P srcraster32 = srcTile.getRaster();
Shinya Kitaoka 120a6e
Shinya Kitaoka 120a6e
  if (raster32)
Shinya Kitaoka 120a6e
    doEmboss<tpixel32, tpixelgr8,="" uchar="">(raster32, srcraster32, azimuth,</tpixel32,>
Shinya Kitaoka 120a6e
                                         elevation, intensity, radius);
Shinya Kitaoka 120a6e
  else {
Shinya Kitaoka 120a6e
    TRaster64P raster64    = tile.getRaster();
Shinya Kitaoka 120a6e
    TRaster64P srcraster64 = srcTile.getRaster();
Shinya Kitaoka 120a6e
    if (raster64)
Shinya Kitaoka 120a6e
      doEmboss<tpixel64, tpixelgr16,="" ushort="">(raster64, srcraster64, azimuth,</tpixel64,>
Shinya Kitaoka 120a6e
                                             elevation, intensity, radius);
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
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
void EmbossFx::transform(double frame, int port, const TRectD &rectOnOutput,
Shinya Kitaoka 120a6e
                         const TRenderSettings &infoOnOutput,
Shinya Kitaoka 120a6e
                         TRectD &rectOnInput, TRenderSettings &infoOnInput) {
Shinya Kitaoka 120a6e
  infoOnInput = infoOnOutput;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double min, max, step;
Shinya Kitaoka 120a6e
  m_radius->getValueRange(min, max, step);
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  double scale  = sqrt(fabs(infoOnOutput.m_affine.det()));
Shinya Kitaoka 120a6e
  double radius = (tcrop(m_radius->getValue(frame), min, max) * scale);
Shinya Kitaoka 120a6e
  int border    = radius + 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  rectOnInput = rectOnOutput.enlarge(border);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
int EmbossFx::getMemoryRequirement(const TRectD &rect, double frame,
Shinya Kitaoka 120a6e
                                   const TRenderSettings &info) {
Shinya Kitaoka 120a6e
  double scale  = sqrt(fabs(info.m_affine.det()));
Shinya Kitaoka 120a6e
  double radius = m_radius->getValue(frame) * scale;
Shinya Kitaoka 120a6e
  int border    = radius + 1;
Toshihiro Shimizu 890ddd
Shinya Kitaoka 120a6e
  return TRasterFx::memorySize(rect.enlarge(border), info.m_bpp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(EmbossFx, "embossFx");