Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "stdfx.h"
Toshihiro Shimizu 890ddd
#include "tfxparam.h"
Toshihiro Shimizu 890ddd
#include "ttzpimagefx.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "timage_io.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//=============================================================================
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TileFx : public TStandardRasterFx
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	FX_PLUGIN_DECLARATION(TileFx)
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	enum tilingMode {
Toshihiro Shimizu 890ddd
		eTile = 1,
Toshihiro Shimizu 890ddd
		eTileHorizontally = 2,
Toshihiro Shimizu 890ddd
		eTileVertically = 3
Toshihiro Shimizu 890ddd
	};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRasterFxPort m_input;
Toshihiro Shimizu 890ddd
	TIntEnumParamP m_mode;
Toshihiro Shimizu 890ddd
	TBoolParamP m_xMirror;
Toshihiro Shimizu 890ddd
	TBoolParamP m_yMirror;
Toshihiro Shimizu 890ddd
	TDoubleParamP m_margin;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TileFx();
Toshihiro Shimizu 890ddd
	~TileFx();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info);
Toshihiro Shimizu 890ddd
	bool canHandle(const TRenderSettings &info, double frame);
Toshihiro Shimizu 890ddd
	void doCompute(TTile &tile, double frame, const TRenderSettings &ri);
Toshihiro Shimizu 890ddd
	int getMemoryRequirement(const TRectD &rect, double frame, const TRenderSettings &info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	void transform(
Toshihiro Shimizu 890ddd
		double frame,
Toshihiro Shimizu 890ddd
		int port,
Toshihiro Shimizu 890ddd
		const TRectD &rectOnOutput,
Toshihiro Shimizu 890ddd
		const TRenderSettings &infoOnOutput,
Toshihiro Shimizu 890ddd
		TRectD &rectOnInput,
Toshihiro Shimizu 890ddd
		TRenderSettings &infoOnInput);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
private:
Toshihiro Shimizu 890ddd
	void makeTile(const TTile &inputTile, const TTile &tile) const;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TileFx::TileFx()
Toshihiro Shimizu 890ddd
	: m_mode(new TIntEnumParam(eTile, "Tile")), m_xMirror(false), m_yMirror(false), m_margin(-1.0)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_margin->setMeasureName("fxLength");
Toshihiro Shimizu 890ddd
	addInputPort("Source", m_input);
Toshihiro Shimizu 890ddd
	bindParam(this, "mode", m_mode);
Toshihiro Shimizu 890ddd
	bindParam(this, "xMirror", m_xMirror);
Toshihiro Shimizu 890ddd
	bindParam(this, "yMirror", m_yMirror);
Toshihiro Shimizu 890ddd
	bindParam(this, "margin", m_margin);
Toshihiro Shimizu 890ddd
	m_mode->addItem(eTileHorizontally, "Tile Horizontally");
Toshihiro Shimizu 890ddd
	m_mode->addItem(eTileVertically, "Tile Vertically");
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TileFx::~TileFx() {}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TileFx::canHandle(const TRenderSettings &info, double frame)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Currently, only affines which transform the X and Y axis into themselves may
Toshihiro Shimizu 890ddd
	//be handled by this fx...
Toshihiro Shimizu 890ddd
	return (fabs(info.m_affine.a12) < 0.0001 &&
Toshihiro Shimizu 890ddd
			fabs(info.m_affine.a21) < 0.0001) ||
Toshihiro Shimizu 890ddd
		   (fabs(info.m_affine.a11) < 0.0001 &&
Toshihiro Shimizu 890ddd
			fabs(info.m_affine.a22) < 0.0001);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TileFx::doGetBBox(double frame, TRectD &bBox, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_input.isConnected()) {
Toshihiro Shimizu 890ddd
		bBox = TConsts::infiniteRectD;
Toshihiro Shimizu 890ddd
		return true;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		bBox = TRectD();
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TileFx::transform(
Toshihiro Shimizu 890ddd
	double frame,
Toshihiro Shimizu 890ddd
	int port,
Toshihiro Shimizu 890ddd
	const TRectD &rectOnOutput,
Toshihiro Shimizu 890ddd
	const TRenderSettings &infoOnOutput,
Toshihiro Shimizu 890ddd
	TRectD &rectOnInput,
Toshihiro Shimizu 890ddd
	TRenderSettings &infoOnInput)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	infoOnInput = infoOnOutput;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD inputBox;
Toshihiro Shimizu 890ddd
	m_input->getBBox(frame, inputBox, infoOnOutput);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double scale = sqrt(fabs(infoOnOutput.m_affine.det()));
Toshihiro Shimizu 890ddd
	int margin = m_margin->getValue(frame) * scale;
Toshihiro Shimizu 890ddd
	inputBox = inputBox.enlarge(margin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (inputBox.isEmpty()) {
Toshihiro Shimizu 890ddd
		rectOnInput.empty();
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (inputBox == TConsts::infiniteRectD) {
Toshihiro Shimizu 890ddd
		infoOnInput = infoOnOutput;
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDimensionD size(0, 0);
Toshihiro Shimizu 890ddd
	size.lx = tceil(inputBox.getLx());
Toshihiro Shimizu 890ddd
	size.ly = tceil(inputBox.getLy());
Toshihiro Shimizu 890ddd
	rectOnInput = TRectD(inputBox.getP00(), size);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TileFx::getMemoryRequirement(const TRectD &rect, double frame, const TRenderSettings &info)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TRectD inputBox;
Toshihiro Shimizu 890ddd
	m_input->getBBox(frame, inputBox, info);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double scale = sqrt(fabs(info.m_affine.det()));
Toshihiro Shimizu 890ddd
	int margin = m_margin->getValue(frame) * scale;
Toshihiro Shimizu 890ddd
	inputBox = inputBox.enlarge(margin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return TRasterFx::memorySize(inputBox, info.m_bpp);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TileFx::doCompute(TTile &tile, double frame, const TRenderSettings &ri)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (!m_input.isConnected())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TRectD inputBox;
Toshihiro Shimizu 890ddd
	m_input->getBBox(frame, inputBox, ri);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	double scale = sqrt(fabs(ri.m_affine.det()));
Toshihiro Shimizu 890ddd
	int margin = m_margin->getValue(frame) * scale;
Toshihiro Shimizu 890ddd
	inputBox = inputBox.enlarge(margin);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (inputBox.isEmpty())
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	if (inputBox == TConsts::infiniteRectD) {
Toshihiro Shimizu 890ddd
		m_input->compute(tile, frame, ri);
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TDimension size(0, 0);
Toshihiro Shimizu 890ddd
	size.lx = tceil(inputBox.getLx());
Toshihiro Shimizu 890ddd
	size.ly = tceil(inputBox.getLy());
Toshihiro Shimizu 890ddd
	TTile inputTile;
Toshihiro Shimizu 890ddd
	m_input->allocateAndCompute(inputTile, inputBox.getP00(), size, tile.getRaster(), frame, ri);
Toshihiro Shimizu 890ddd
	makeTile(inputTile, tile);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
//! Make the tile of the image contained in \b inputTile in \b tile
Toshihiro Shimizu 890ddd
/*
Toshihiro Shimizu 890ddd
*/
Toshihiro Shimizu 890ddd
void TileFx::makeTile(const TTile &inputTile, const TTile &tile) const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	//Build the mirroring pattern. It obviously repeats itself out of 2x2 tile blocks.
Toshihiro Shimizu 890ddd
	map<pair<bool, bool="">, TRasterP> mirrorRaster;</pair<bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(false, false)] = inputTile.getRaster();</bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(false, true)] = inputTile.getRaster()->clone();</bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(false, true)]->yMirror();</bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(true, false)] = inputTile.getRaster()->clone();</bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(true, false)]->xMirror();</bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(true, true)] = mirrorRaster[pair<bool, bool="">(true, false)]->clone();</bool,></bool,>
Toshihiro Shimizu 890ddd
	mirrorRaster[pair<bool, bool="">(true, true)]->yMirror();</bool,>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TPoint animatedPos = convert(inputTile.m_pos - tile.m_pos);
Toshihiro Shimizu 890ddd
	TDimension inSize = inputTile.getRaster()->getSize();
Toshihiro Shimizu 890ddd
	TDimension outSize = tile.getRaster()->getSize();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	bool mirrorX = false, mirrorY = false;
Toshihiro Shimizu 890ddd
	int mode = m_mode->getValue();
Toshihiro Shimizu 890ddd
	bool tileOrizontally = mode == 1 || mode == 2;
Toshihiro Shimizu 890ddd
	bool tileVertically = mode == 1 || mode == 3;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Reach the lower left tiling position
Toshihiro Shimizu 890ddd
	while (animatedPos.x > 0 && tileOrizontally) {
Toshihiro Shimizu 890ddd
		animatedPos.x -= inSize.lx;
Toshihiro Shimizu 890ddd
		mirrorX = !mirrorX;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	while (animatedPos.x + inSize.lx < 0 && tileOrizontally) {
Toshihiro Shimizu 890ddd
		animatedPos.x += inSize.lx;
Toshihiro Shimizu 890ddd
		mirrorX = !mirrorX;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	while (animatedPos.y > 0 && tileVertically) {
Toshihiro Shimizu 890ddd
		animatedPos.y -= inSize.ly;
Toshihiro Shimizu 890ddd
		mirrorY = !mirrorY;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	while (animatedPos.y + inSize.ly < 0 && tileVertically) {
Toshihiro Shimizu 890ddd
		animatedPos.y += inSize.ly;
Toshihiro Shimizu 890ddd
		mirrorY = !mirrorY;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	bool doMirroX = mirrorX, doMirroY = mirrorY;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	//Write the tiling blocks.
Toshihiro Shimizu 890ddd
	tile.getRaster()->lock();
Toshihiro Shimizu 890ddd
	inputTile.getRaster()->lock();
Toshihiro Shimizu 890ddd
	TPoint startTilingPos = animatedPos;
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		do {
Toshihiro Shimizu 890ddd
			pair<bool, bool=""> doMirror(doMirroX && m_xMirror->getValue(), doMirroY && m_yMirror->getValue());</bool,>
Toshihiro Shimizu 890ddd
			tile.getRaster()->copy(mirrorRaster[doMirror], startTilingPos);
Toshihiro Shimizu 890ddd
			startTilingPos.x += inSize.lx;
Toshihiro Shimizu 890ddd
			doMirroX = !doMirroX;
Toshihiro Shimizu 890ddd
		} while (startTilingPos.x < outSize.lx && tileOrizontally);
Toshihiro Shimizu 890ddd
		startTilingPos.y += inSize.ly;
Toshihiro Shimizu 890ddd
		startTilingPos.x = animatedPos.x;
Toshihiro Shimizu 890ddd
		doMirroY = !doMirroY;
Toshihiro Shimizu 890ddd
		doMirroX = mirrorX;
Toshihiro Shimizu 890ddd
	} while (startTilingPos.y < outSize.ly && tileVertically);
Toshihiro Shimizu 890ddd
	inputTile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
	tile.getRaster()->unlock();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//------------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
FX_PLUGIN_IDENTIFIER(TileFx, "tileFx");