tahoma2d/toonz/sources/stdfx/ino_density.cpp

181 lines
5.8 KiB
C++
Raw Normal View History

2016-03-19 06:57:51 +13:00
#include <sstream> /* std::ostringstream */
#include "tfxparam.h"
#include "stdfx.h"
#include "ino_common.h"
//------------------------------------------------------------
class ino_density final : public TStandardRasterFx {
2016-06-15 18:43:10 +12:00
FX_PLUGIN_DECLARATION(ino_density)
TRasterFxPort m_input;
TRasterFxPort m_refer;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TDoubleParamP m_density;
2016-03-19 06:57:51 +13:00
2016-06-15 18:43:10 +12:00
TIntEnumParamP m_ref_mode;
2016-03-19 06:57:51 +13:00
public:
2016-06-15 18:43:10 +12:00
ino_density()
: m_density(1.0 * ino::param_range())
, m_ref_mode(new TIntEnumParam(0, "Red")) {
addInputPort("Source", this->m_input);
addInputPort("Reference", this->m_refer);
bindParam(this, "density", this->m_density);
bindParam(this, "reference", this->m_ref_mode);
this->m_density->setValueRange(0.0 * ino::param_range(),
10.0 * ino::param_range());
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");
}
2016-06-20 14:23:05 +12:00
bool doGetBBox(double frame, TRectD &bBox,
const TRenderSettings &info) override {
2016-06-15 18:43:10 +12:00
if (this->m_input.isConnected()) {
return this->m_input->doGetBBox(frame, bBox, info);
} else {
bBox = TRectD();
return false;
}
}
2016-06-19 20:06:29 +12:00
bool canHandle(const TRenderSettings &rend_sets, double frame) override {
2016-06-15 18:43:10 +12:00
return true;
}
2016-06-20 14:23:05 +12:00
void doCompute(TTile &tile, double frame,
const TRenderSettings &rend_sets) override;
2016-03-19 06:57:51 +13:00
};
FX_PLUGIN_IDENTIFIER(ino_density, "inoDensityFx");
//------------------------------------------------------------
#include "igs_density.h"
2016-06-15 18:43:10 +12:00
namespace {
void fx_(TRasterP in_ras, TRasterP refer_ras, const int refer_mode,
2016-06-15 18:43:10 +12:00
const double density) {
TRasterGR8P in_gr8(in_ras->getLy(),
in_ras->getLx() * ino::channels() *
((TRaster64P)in_ras ? sizeof(unsigned short)
: sizeof(unsigned char)));
in_gr8->lock();
ino::ras_to_arr(in_ras, ino::channels(), in_gr8->getRawData());
igs::density::change(
in_gr8->getRawData() // BGRA
,
in_ras->getLy(), in_ras->getLx() // Not use in_ras->getWrap()
,
ino::channels(), ino::bits(in_ras)
,
(((refer_ras != nullptr) && (0 <= refer_mode)) ? refer_ras->getRawData()
: nullptr) // BGRA
2016-06-15 18:43:10 +12:00
,
(((refer_ras != nullptr) && (0 <= refer_mode)) ? ino::bits(refer_ras)
: 0),
refer_mode
2016-06-15 18:43:10 +12:00
,
density);
ino::arr_to_ras(in_gr8->getRawData(), ino::channels(), in_ras, 0);
in_gr8->unlock();
2016-03-19 06:57:51 +13:00
}
}
//------------------------------------------------------------
2016-06-15 18:43:10 +12:00
void ino_density::doCompute(TTile &tile, double frame,
const TRenderSettings &rend_sets) {
/* ------ 接続していなければ処理しない -------------------- */
if (!this->m_input.isConnected()) {
tile.getRaster()->clear(); /* 塗りつぶしクリア */
return;
}
/* ------ サポートしていないPixelタイプはエラーを投げる --- */
if (!((TRaster32P)tile.getRaster()) && !((TRaster64P)tile.getRaster())) {
throw TRopException("unsupported input pixel type");
}
/* ------ 動作パラメータを得る ---------------------------- */
const double density = this->m_density->getValue(frame) / ino::param_range();
const int refer_mode = this->m_ref_mode->getValue();
2016-06-15 18:43:10 +12:00
/* ------ 画像生成 ---------------------------------------- */
this->m_input->compute(tile, frame, rend_sets);
/*------ 参照画像生成 --------------------------------------*/
TTile refer_tile;
bool refer_sw = false;
2016-06-15 18:43:10 +12:00
if (this->m_refer.isConnected()) {
refer_sw = true;
2016-06-15 18:43:10 +12:00
this->m_refer->allocateAndCompute(
refer_tile, tile.m_pos,
2016-06-15 18:43:10 +12:00
TDimensionI(/* Pixel単位 */
tile.getRaster()->getLx(), tile.getRaster()->getLy()),
tile.getRaster(), frame, rend_sets);
}
/* ------ (app_begin)log記憶 ------------------------------ */
const bool log_sw = ino::log_enable_sw();
if (log_sw) {
std::ostringstream os;
os << "params"
<< " den " << density << " refer_mode " << refer_mode << " tile w "
2016-06-15 18:43:10 +12:00
<< tile.getRaster()->getLx() << " h " << tile.getRaster()->getLy()
<< " pixbits " << ino::pixel_bits(tile.getRaster()) << " frame "
<< frame;
if (refer_sw) {
os << " refer_tile.m_pos " << refer_tile.m_pos << " refer_tile_getLx "
<< refer_tile.getRaster()->getLx() << " y "
<< refer_tile.getRaster()->getLy();
2016-06-15 18:43:10 +12:00
}
}
/* ------ fx処理 ------------------------------------------ */
try {
tile.getRaster()->lock();
if (refer_tile.getRaster() != nullptr) {
refer_tile.getRaster()->lock();
}
fx_(tile.getRaster(), refer_tile.getRaster(), refer_mode, density);
if (refer_tile.getRaster() != nullptr) {
refer_tile.getRaster()->unlock();
}
2016-06-15 18:43:10 +12:00
tile.getRaster()->unlock();
}
/* ------ error処理 --------------------------------------- */
catch (std::bad_alloc &e) {
if (refer_tile.getRaster() != nullptr) {
refer_tile.getRaster()->unlock();
}
2016-06-15 18:43:10 +12:00
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();
}
2016-06-15 18:43:10 +12:00
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();
}
2016-06-15 18:43:10 +12:00
tile.getRaster()->unlock();
if (log_sw) {
std::string str("other exception");
}
throw;
}
2016-03-19 06:57:51 +13:00
}