Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_outline_vectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/scriptbinding_level.h"
Toshihiro Shimizu 890ddd
#include "toonz/tcenterlinevectorizer.h"
Toshihiro Shimizu 890ddd
#include "toonz/stage.h"
Toshihiro Shimizu 890ddd
#include "ttoonzimage.h"
Toshihiro Shimizu 890ddd
#include "tpalette.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
namespace TScriptBinding
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
OutlineVectorizer::OutlineVectorizer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters = new NewOutlineConfiguration();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
OutlineVectorizer::~OutlineVectorizer()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	delete m_parameters;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue OutlineVectorizer::toString()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return "Outline Vectorizer";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue OutlineVectorizer::ctor(QScriptContext *context, QScriptEngine *engine)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return create(engine, new OutlineVectorizer());
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue vectorizeImage(QScriptContext *context, QScriptEngine *engine, const TImageP &src, TPalette *palette, NewOutlineConfiguration *parameters)
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
	parameters->m_affine = dpiAff * TTranslation(-center);
Toshihiro Shimizu 890ddd
	parameters->m_thickScale = factor;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	TVectorImageP vi = vc.vectorize(src, *parameters, palette);
Toshihiro Shimizu 890ddd
	vi->setPalette(palette);
Toshihiro Shimizu 890ddd
	return engine->newQObject(new Image(vi), QScriptEngine::AutoOwnership);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QScriptValue OutlineVectorizer::vectorize(QScriptValue arg)
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(context(), engine(), img->getImg(), palette, m_parameters);
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(context(), engine(), srcImg, palette, m_parameters);
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 OutlineVectorizer::getAccuracy() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return (5.0 - m_parameters->m_mergeTol) * 2.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setAccuracy(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_mergeTol = 5.0 - v * 0.5;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int OutlineVectorizer::getDespeckling() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_despeckling;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setDespeckling(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_despeckling = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool OutlineVectorizer::getPreservePaintedAreas() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return !m_parameters->m_leaveUnpainted;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setPreservePaintedAreas(bool v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_leaveUnpainted = !v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double OutlineVectorizer::getCornerAdherence() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_adherenceTol * 100.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setCornerAdherence(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_adherenceTol = 0.01 * v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double OutlineVectorizer::getCornerAngle() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_angleTol * 180.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setCornerAngle(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_angleTol = v / 180.0;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
double OutlineVectorizer::getCornerCurveRadius() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_relativeTol * 100;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setCornerCurveRadius(double v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_relativeTol = 0.01 * v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int OutlineVectorizer::getMaxColors() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_maxColors;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setMaxColors(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_maxColors = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
QString OutlineVectorizer::getTransparentColor() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	TPixel32 c = m_parameters->m_transparentColor;
Toshihiro Shimizu 890ddd
	QColor color(c.r, c.g, c.b, c.m);
Toshihiro Shimizu 890ddd
	return color.name();
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setTransparentColor(const QString &colorName)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	QColor color;
Toshihiro Shimizu 890ddd
	color.setNamedColor(colorName);
Toshihiro Shimizu 890ddd
	if (color.isValid()) {
Toshihiro Shimizu 890ddd
		m_parameters->m_transparentColor = TPixel32(color.red(), color.green(), color.blue(), color.alpha());
Toshihiro Shimizu 890ddd
	} else {
Toshihiro Shimizu 890ddd
		context()->throwError(tr("Invalid color : ").arg(colorName));
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int OutlineVectorizer::getToneThreshold() const
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	return m_parameters->m_toneTol;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void OutlineVectorizer::setToneThreshold(int v)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_parameters->m_toneTol = v;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
} // namespace TScriptBinding