| #include <sstream> /* std::ostringstream */ |
| #include "tfxparam.h" |
| #include "stdfx.h" |
| #include "tfxattributes.h" |
| |
| #include "ino_common.h" |
| #include "igs_gaussian_blur.h" |
| |
| class ino_blur final : public TStandardRasterFx { |
| FX_PLUGIN_DECLARATION(ino_blur) |
| TRasterFxPort m_input; |
| TRasterFxPort m_refer; |
| |
| TDoubleParamP m_radius; |
| TIntEnumParamP m_ref_mode; |
| |
| public: |
| ino_blur() : m_radius(1.0), m_ref_mode(new TIntEnumParam(0, "Red")) { |
| addInputPort("Source", this->m_input); |
| addInputPort("Reference", this->m_refer); |
| |
| bindParam(this, "radius", this->m_radius); |
| bindParam(this, "reference", this->m_ref_mode); |
| |
| this->m_radius->setMeasureName("fxLength"); |
| this->m_radius->setValueRange(0.0, 1000.0); |
| |
| this->m_ref_mode->addItem(1, "Green"); |
| this->m_ref_mode->addItem(2, "Blue"); |
| this->m_ref_mode->addItem(3, "Alpha"); |
| this->m_ref_mode->addItem(4, "Luminance"); |
| this->m_ref_mode->addItem(-1, "Nothing"); |
| } |
| |
| double get_render_real_radius(const double frame, const TAffine affine) { |
| |
| TPointD rend_vect; |
| rend_vect.x = this->m_radius->getValue(frame); |
| rend_vect.y = 0.0; |
| |
| rend_vect = rend_vect * ino::pixel_per_mm(); |
| |
| |
| |
| |
| |
| |
| TAffine aff(affine); |
| aff.a13 = aff.a23 = 0; |
| rend_vect = aff * rend_vect; |
| |
| return sqrt(rend_vect.x * rend_vect.x + rend_vect.y * rend_vect.y); |
| } |
| void get_render_enlarge(const double frame, const TAffine affine, |
| TRectD &bBox) { |
| const int margin = igs::gaussian_blur_hv::int_radius( |
| this->get_render_real_radius(frame, affine)); |
| if (0 < margin) { |
| bBox = bBox.enlarge(static_cast<double>(margin)); |
| } |
| } |
| |
| bool doGetBBox(double frame, TRectD &bBox, |
| const TRenderSettings &info) override { |
| if (false == this->m_input.isConnected()) { |
| bBox = TRectD(); |
| return false; |
| } |
| const bool ret = this->m_input->doGetBBox(frame, bBox, info); |
| this->get_render_enlarge(frame, info.m_affine, bBox); |
| return ret; |
| } |
| int getMemoryRequirement(const TRectD &rect, double frame, |
| const TRenderSettings &info) override { |
| TRectD bBox(rect); |
| this->get_render_enlarge(frame, info.m_affine, bBox); |
| return TRasterFx::memorySize(bBox, info.m_bpp); |
| } |
| void transform(double frame, int port, const TRectD &rectOnOutput, |
| const TRenderSettings &infoOnOutput, TRectD &rectOnInput, |
| TRenderSettings &infoOnInput) override { |
| rectOnInput = rectOnOutput; |
| infoOnInput = infoOnOutput; |
| this->get_render_enlarge(frame, infoOnOutput.m_affine, rectOnInput); |
| } |
| bool canHandle(const TRenderSettings &info, double frame) override { |
| if (0 == this->m_radius->getValue(frame)) { |
| return true; |
| } else { |
| return isAlmostIsotropic(info.m_affine); |
| } |
| } |
| void doCompute(TTile &tile, double frame, |
| const TRenderSettings &rend_sets) override; |
| }; |
| FX_PLUGIN_IDENTIFIER(ino_blur, "inoBlurFx"); |
| |
| namespace { |
| void fx_(const TRasterP in_ras |
| , |
| TRasterP out_ras |
| , |
| const TRasterP refer_ras, const int refer_mode, const int int_radius, |
| const double real_radius) { |
| TRasterGR8P out_buffer(out_ras->getLy(), |
| out_ras->getLx() * ino::channels() * |
| ((TRaster64P)in_ras ? sizeof(unsigned short) |
| : sizeof(unsigned char))); |
| const int buffer_bytes = igs::gaussian_blur_hv::buffer_bytes( |
| in_ras->getLy(), in_ras->getLx(), int_radius); |
| TRasterGR8P cvt_buffer(buffer_bytes, 1); |
| out_buffer->lock(); |
| cvt_buffer->lock(); |
| igs::gaussian_blur_hv::convert( |
| in_ras->getRawData() |
| , |
| out_buffer->getRawData() |
| |
| , |
| in_ras->getLy() |
| , |
| in_ras->getLx() |
| , |
| ino::channels() |
| , |
| ino::bits(in_ras) |
| |
| , |
| (((refer_ras != nullptr) && (0 <= refer_mode)) |
| ? refer_ras->getRawData() |
| : nullptr) |
| , |
| (((refer_ras != nullptr) && (0 <= refer_mode)) ? ino::bits(refer_ras) |
| : 0) |
| , |
| refer_mode |
| |
| , |
| cvt_buffer->getRawData() |
| , |
| buffer_bytes |
| |
| , |
| int_radius |
| , |
| real_radius |
| ); |
| ino::arr_to_ras(out_buffer->getRawData(), ino::channels(), out_ras, 0); |
| cvt_buffer->unlock(); |
| out_buffer->unlock(); |
| } |
| } |
| |
| void ino_blur::doCompute(TTile &tile, double frame, |
| const TRenderSettings &rend_sets) { |
| |
| if (!this->m_input.isConnected()) { |
| tile.getRaster()->clear(); |
| return; |
| } |
| |
| if (!((TRaster32P)tile.getRaster()) && !((TRaster64P)tile.getRaster())) { |
| throw TRopException("unsupported input pixel type"); |
| } |
| |
| const double real_radius = |
| this->get_render_real_radius(frame, rend_sets.m_affine); |
| const int int_radius = igs::gaussian_blur_hv::int_radius(real_radius); |
| |
| if (0 == int_radius) { |
| this->m_input->compute(tile, frame, rend_sets); |
| return; |
| } |
| |
| const int refer_mode = this->m_ref_mode->getValue(); |
| |
| |
| TRectD bBox = |
| TRectD(tile.m_pos |
| , |
| TDimensionD( |
| tile.getRaster()->getLx(), tile.getRaster()->getLy())); |
| |
| if (0 < int_radius) { |
| bBox = bBox.enlarge(static_cast<double>(int_radius)); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TTile enlarge_tile; |
| this->m_input->allocateAndCompute( |
| enlarge_tile, bBox.getP00(), |
| TDimensionI( |
| static_cast<int>(bBox.getLx() + 0.5), |
| static_cast<int>(bBox.getLy() + 0.5)), |
| tile.getRaster(), frame, rend_sets); |
| |
| |
| TTile refer_tile; |
| bool refer_sw = false; |
| if (this->m_refer.isConnected()) { |
| refer_sw = true; |
| this->m_refer->allocateAndCompute( |
| refer_tile, tile.m_pos |
| , |
| tile.getRaster()->getSize() |
| , |
| tile.getRaster(), frame, rend_sets); |
| } |
| |
| |
| tile.getRaster()->clear(); |
| |
| |
| const bool log_sw = ino::log_enable_sw(); |
| |
| if (log_sw) { |
| std::ostringstream os; |
| os << "params" |
| << " usr_radius " << this->m_radius->getValue(frame) << " real_radius " |
| << real_radius << " int_radius " << int_radius << " refer_mode " |
| << refer_mode << " tile" |
| << " pos " << tile.m_pos << " w " << tile.getRaster()->getLx() << " h " |
| << tile.getRaster()->getLy() << " enl_tile" |
| << " w " << enlarge_tile.getRaster()->getLx() << " h " |
| << enlarge_tile.getRaster()->getLy() << " pixbits " |
| << ino::pixel_bits(tile.getRaster()) << " frame " << frame |
| << " affine a11 " << rend_sets.m_affine.a11 << " a12 " |
| << rend_sets.m_affine.a12 << " a21 " << rend_sets.m_affine.a21 |
| << " a22 " << rend_sets.m_affine.a22; |
| if (refer_sw) { |
| os << " refer_tile" |
| << " pos " << refer_tile.m_pos << " x " |
| << refer_tile.getRaster()->getLx() << " y " |
| << refer_tile.getRaster()->getLy(); |
| } |
| } |
| |
| try { |
| tile.getRaster()->lock(); |
| enlarge_tile.getRaster()->lock(); |
| if (refer_tile.getRaster() != nullptr) { |
| refer_tile.getRaster()->lock(); |
| } |
| fx_(enlarge_tile.getRaster() |
| , |
| tile.getRaster() |
| |
| , |
| refer_tile.getRaster(), refer_mode |
| |
| , |
| int_radius |
| , |
| real_radius); |
| if (refer_tile.getRaster() != nullptr) { |
| refer_tile.getRaster()->unlock(); |
| } |
| enlarge_tile.getRaster()->unlock(); |
| tile.getRaster()->unlock(); |
| } |
| |
| catch (std::bad_alloc &e) { |
| if (refer_tile.getRaster() != nullptr) { |
| refer_tile.getRaster()->unlock(); |
| } |
| enlarge_tile.getRaster()->unlock(); |
| tile.getRaster()->unlock(); |
| if (log_sw) { |
| std::string str("std::bad_alloc <"); |
| str += e.what(); |
| str += '>'; |
| } |
| throw; |
| } catch (std::exception &e) { |
| if (refer_tile.getRaster() != nullptr) { |
| refer_tile.getRaster()->unlock(); |
| } |
| enlarge_tile.getRaster()->unlock(); |
| tile.getRaster()->unlock(); |
| if (log_sw) { |
| std::string str("exception <"); |
| str += e.what(); |
| str += '>'; |
| } |
| throw; |
| } catch (...) { |
| if (refer_tile.getRaster() != nullptr) { |
| refer_tile.getRaster()->unlock(); |
| } |
| enlarge_tile.getRaster()->unlock(); |
| tile.getRaster()->unlock(); |
| if (log_sw) { |
| std::string str("other exception"); |
| } |
| throw; |
| } |
| } |