Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_centerline_vectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include <qscriptengine></qscriptengine>
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "toonz/Naa2TlvConverter.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CenterlineVectorizer::CenterlineVectorizer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters = new CenterlineConfiguration();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
CenterlineVectorizer::~CenterlineVectorizer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_parameters;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue CenterlineVectorizer::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return "Centerline Vectorizer";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue CenterlineVectorizer::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine, new CenterlineVectorizer());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue CenterlineVectorizer::vectorizeImage(const TImageP &src, TPalette *palette)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	VectorizerCore vc;
Toshihiro Shimizu 890ddd
	TAffine dpiAff;
Toshihiro Shimizu 890ddd
	double factor = Stage::inch;
Toshihiro Shimizu 890ddd
	double dpix = factor / 72, dpiy = factor / 72;
Toshihiro Shimizu 890ddd
	TPointD center;
Toshihiro Shimizu 890ddd
	if (TRasterImageP ri = src) {
Toshihiro Shimizu 890ddd
		ri->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
		center = ri->getRaster()->getCenterD();
Toshihiro Shimizu 890ddd
	} else if (TToonzImageP ti = src) {
Toshihiro Shimizu 890ddd
		ti->getDpi(dpix, dpiy);
Toshihiro Shimizu 890ddd
		center = ti->getRaster()->getCenterD();
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return context()->throwError(QObject::tr("Vectorization failed"));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (dpix != 0.0 && dpiy != 0.0)
Toshihiro Shimizu 890ddd
		dpiAff = TScale(factor / dpix, factor / dpiy);
Toshihiro Shimizu 890ddd
	factor = norm(dpiAff * TPointD(1, 0));
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	m_parameters->m_affine = dpiAff * TTranslation(-center);
Toshihiro Shimizu 890ddd
	m_parameters->m_thickScale = factor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	palette->addRef(); // if there are no other references the vectorize() method below can destroy the palette
Toshihiro Shimizu 890ddd
					   // BEFORE assigning it to the vector image
Toshihiro Shimizu 890ddd
	TVectorImageP vi = vc.vectorize(src, *m_parameters, palette);
Toshihiro Shimizu 890ddd
	vi->setPalette(palette);
Toshihiro Shimizu 890ddd
	palette->release();
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return engine()->newQObject(new Image(vi), QScriptEngine::AutoOwnership);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue CenterlineVectorizer::vectorize(QScriptValue arg)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	Level *level = qscriptvalue_cast<level *="">(arg);</level>
Toshihiro Shimizu 890ddd
	Image *img = qscriptvalue_cast(arg);
Toshihiro Shimizu 890ddd
	QString type;
Toshihiro Shimizu 890ddd
	TPalette *palette = 0;
Toshihiro Shimizu 890ddd
	if (level) {
Toshihiro Shimizu 890ddd
		type = level->getType();
Toshihiro Shimizu 890ddd
		if (type != "Raster" && type != "ToonzRaster")
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("Can't vectorize a %1 level").arg(type));
Toshihiro Shimizu 890ddd
		if (level->getFrameCount() <= 0)
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("Can't vectorize a level with no frames"));
Toshihiro Shimizu 890ddd
		palette = level->getSimpleLevel()->getPalette();
Toshihiro Shimizu 890ddd
	} else if (img) {
Toshihiro Shimizu 890ddd
		type = img->getType();
Toshihiro Shimizu 890ddd
		if (type != "Raster" && type != "ToonzRaster")
Toshihiro Shimizu 890ddd
			return context()->throwError(tr("Can't vectorize a %1 image").arg(type));
Toshihiro Shimizu 890ddd
		if (TToonzImageP ti = img->getImg()) {
Toshihiro Shimizu 890ddd
			palette = ti->getPalette();
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		return context()->throwError(tr("Bad argument (%1): should be an Image or a Level").arg(arg.toString()));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (palette == 0)
Toshihiro Shimizu 890ddd
		palette = new TPalette();
Toshihiro Shimizu 890ddd
	if (img) {
Toshihiro Shimizu 890ddd
		return vectorizeImage(img->getImg(), palette);
Toshihiro Shimizu 890ddd
	} else if (level) {
Toshihiro Shimizu 890ddd
		QScriptValue newLevel = create(engine(), new Level());
Toshihiro Shimizu 890ddd
		QList<tframeid> fids;</tframeid>
Toshihiro Shimizu 890ddd
		level->getFrameIds(fids);
Toshihiro Shimizu 890ddd
		foreach (TFrameId fid, fids) {
Toshihiro Shimizu 890ddd
			TImageP srcImg = level->getImg(fid);
Toshihiro Shimizu 890ddd
			if (srcImg && (srcImg->getType() == TImage::RASTER || srcImg->getType() == TImage::TOONZ_RASTER)) {
Toshihiro Shimizu 890ddd
				QScriptValue newFrame = vectorizeImage(srcImg, palette);
Toshihiro Shimizu 890ddd
				if (newFrame.isError()) {
Toshihiro Shimizu 890ddd
					return newFrame;
Toshihiro Shimizu 890ddd
				}
Toshihiro Shimizu 890ddd
				QScriptValueList args;
Toshihiro Shimizu 890ddd
				args << QString::fromStdString(fid.expand()) << newFrame;
Toshihiro Shimizu 890ddd
				newLevel.property("setFrame").call(newLevel, args);
Toshihiro Shimizu 890ddd
			}
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
		return newLevel;
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		// should never happen
Toshihiro Shimizu 890ddd
		return QScriptValue();
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CenterlineVectorizer::getThreshold() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_threshold / 25;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setThreshold(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_threshold = v * 25;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CenterlineVectorizer::getAccuracy() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return 10 - m_parameters->m_penalty;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setAccuracy(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_penalty = 10 - v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int CenterlineVectorizer::getDespeckling() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_despeckling / 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setDespeckling(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_despeckling = v * 2;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double CenterlineVectorizer::getMaxThickness() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_maxThickness * 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setMaxThickness(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_maxThickness = v / 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double CenterlineVectorizer::getThicknessCalibration() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_thicknessRatio;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setThicknessCalibration(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_thicknessRatio = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CenterlineVectorizer::getPreservePaintedAreas() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return !m_parameters->m_leaveUnpainted;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setPreservePaintedAreas(bool v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_leaveUnpainted = !v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CenterlineVectorizer::getAddBorder() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_makeFrame;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setAddBorder(bool v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_makeFrame = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool CenterlineVectorizer::getEir() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_naaSource;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void CenterlineVectorizer::setEir(bool v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_naaSource = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding