| #include <sstream> /* std::ostringstream */ |
| #include "tfxparam.h" |
| #include "stdfx.h" |
| |
| #include "ino_common.h" |
| |
| class ino_median final : public TStandardRasterFx { |
| FX_PLUGIN_DECLARATION(ino_median) |
| TRasterFxPort m_input; |
| TRasterFxPort m_refer; |
| |
| TDoubleParamP m_radius; |
| TIntEnumParamP m_channel; |
| |
| TIntEnumParamP m_ref_mode; |
| |
| public: |
| ino_median() |
| : m_radius(0.35 * 640. / 12. / 25.4) |
| |
| |
| |
| |
| |
| , m_channel(new TIntEnumParam()) |
| , m_ref_mode(new TIntEnumParam()) { |
| this->m_radius->setMeasureName("fxLength"); |
| |
| addInputPort("Source", this->m_input); |
| addInputPort("Reference", this->m_refer); |
| |
| bindParam(this, "radius", this->m_radius); |
| bindParam(this, "channel", this->m_channel); |
| bindParam(this, "reference", this->m_ref_mode); |
| |
| this->m_radius->setValueRange(0, 100.0 * 640. / 12. / 25.4); |
| |
| |
| this->m_channel->addItem(0, "Red"); |
| this->m_channel->addItem(1, "Green"); |
| this->m_channel->addItem(2, "Blue"); |
| this->m_channel->addItem(3, "Alpha"); |
| this->m_channel->addItem(4, "All"); |
| this->m_channel->setDefaultValue(4); |
| this->m_channel->setValue(4); |
| |
| this->m_ref_mode->addItem(0, "Red"); |
| 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"); |
| this->m_ref_mode->setDefaultValue(0); |
| this->m_ref_mode->setValue(0); |
| } |
| bool doGetBBox(double frame, TRectD &bBox, |
| const TRenderSettings &info) override { |
| if (this->m_input.isConnected()) { |
| const bool ret = this->m_input->doGetBBox(frame, bBox, info); |
| const double margin = ceil(this->m_radius->getValue(frame)); |
| if (0.0 < margin) { |
| bBox = bBox.enlarge(margin); |
| } |
| return ret; |
| } else { |
| bBox = TRectD(); |
| return false; |
| } |
| } |
| bool canHandle(const TRenderSettings &rend_sets, double frame) override { |
| return true; |
| } |
| int getMemoryRequirement(const TRectD &rect, double frame, |
| const TRenderSettings &info) override { |
| const double radius = this->m_radius->getValue(frame); |
| return TRasterFx::memorySize(rect.enlarge(ceil(radius) + 0.5), info.m_bpp); |
| } |
| void doCompute(TTile &tile, double frame, |
| const TRenderSettings &rend_sets) override; |
| }; |
| FX_PLUGIN_IDENTIFIER(ino_median, "inoMedianFx") |
| |
| #include "igs_median_filter_smooth.h" |
| namespace { |
| void fx_(const TRasterP in_ras |
| , |
| const int margin, TRasterP out_ras |
| |
| , |
| const TRasterP refer_ras, const int refer_mode |
| |
| , |
| const double radius, const int channel) { |
| TRasterGR8P out_gr8(in_ras->getLy(), |
| in_ras->getLx() * ino::channels() * |
| ((TRaster64P)in_ras ? sizeof(unsigned short) |
| : sizeof(unsigned char))); |
| out_gr8->lock(); |
| |
| igs::median_filter_smooth::convert( |
| in_ras->getRawData() |
| , |
| out_gr8->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 |
| |
| , |
| channel, radius, 0 |
| ); |
| |
| ino::arr_to_ras(out_gr8->getRawData(), ino::channels(), out_ras, margin); |
| out_gr8->unlock(); |
| } |
| } |
| |
| void ino_median::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 mm2scale_shrink_pixel = |
| ino::pixel_per_mm() * sqrt(fabs(rend_sets.m_affine.det())) / |
| ((rend_sets.m_shrinkX + rend_sets.m_shrinkY) / 2.0); |
| |
| |
| const double radius = this->m_radius->getValue(frame) * mm2scale_shrink_pixel; |
| |
| const int channel = this->m_channel->getValue(); |
| const int refer_mode = this->m_ref_mode->getValue(); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TRectD enlarge_rect = |
| TRectD(tile.m_pos |
| , |
| TDimensionD( |
| tile.getRaster()->getLx(), tile.getRaster()->getLy())); |
| |
| const int enlarge_pixel = (int)(ceil(radius) + 0.5); |
| enlarge_rect = enlarge_rect.enlarge(enlarge_pixel); |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| TTile enlarge_tile; |
| this->m_input->allocateAndCompute( |
| enlarge_tile, enlarge_rect.getP00(), |
| TDimensionI( |
| (int)(enlarge_rect.getLx() + 0.5), |
| (int)(enlarge_rect.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, enlarge_tile.m_pos, |
| TDimensionI( |
| enlarge_tile.getRaster()->getLx(), |
| enlarge_tile.getRaster()->getLy()), |
| enlarge_tile.getRaster(), frame, rend_sets); |
| } |
| |
| tile.getRaster()->clear(); |
| |
| |
| const bool log_sw = ino::log_enable_sw(); |
| |
| if (log_sw) { |
| std::ostringstream os; |
| os << "params" |
| << " radius " << radius << " channel " << channel << " refer_mode " |
| << refer_mode << " tile w " << tile.getRaster()->getLx() << " h " |
| << tile.getRaster()->getLy() << " pixbits " |
| << ino::pixel_bits(tile.getRaster()) << " frame " << frame |
| << " rand_sets affine_det " << rend_sets.m_affine.det() |
| << " shrink x " << rend_sets.m_shrinkX << " y " << rend_sets.m_shrinkY; |
| if (refer_sw) { |
| os << " refer_tile.m_pos " << refer_tile.m_pos << " refer_tile_getLx " |
| << 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() |
| , |
| enlarge_pixel |
| , |
| tile.getRaster() |
| |
| , |
| refer_tile.getRaster(), refer_mode |
| |
| , |
| radius, channel); |
| 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; |
| } |
| } |